Skip to content

Commit ae960ad

Browse files
chore: switch MiddlewareFn -> Middleware (#3137)
1 parent a988dc3 commit ae960ad

File tree

12 files changed

+45
-45
lines changed

12 files changed

+45
-45
lines changed

src/app.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
44
import * as colors from "@std/fmt/colors";
55
import {
66
type MaybeLazyMiddleware,
7-
type MiddlewareFn,
7+
type Middleware,
88
runMiddlewares,
99
} from "./middlewares/mod.ts";
1010
import { Context } from "./context.ts";
@@ -209,16 +209,16 @@ export class App<State> {
209209
}
210210

211211
/**
212-
* Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode MiddlewareFn}.
212+
* Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode Middleware}.
213213
*/
214-
notFound(routeOrMiddleware: Route<State> | MiddlewareFn<State>): this {
214+
notFound(routeOrMiddleware: Route<State> | Middleware<State>): this {
215215
this.#commands.push(newNotFoundCmd(routeOrMiddleware));
216216
return this;
217217
}
218218

219219
onError(
220220
path: string,
221-
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
221+
routeOrMiddleware: Route<State> | Middleware<State>,
222222
): this {
223223
this.#commands.push(newErrorCmd(path, routeOrMiddleware, true));
224224
return this;
@@ -250,50 +250,50 @@ export class App<State> {
250250
/**
251251
* Add middlewares for GET requests at the specified path.
252252
*/
253-
get(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
253+
get(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
254254
this.#commands.push(newHandlerCmd("GET", path, middlewares, false));
255255
return this;
256256
}
257257
/**
258258
* Add middlewares for POST requests at the specified path.
259259
*/
260-
post(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
260+
post(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
261261
this.#commands.push(newHandlerCmd("POST", path, middlewares, false));
262262
return this;
263263
}
264264
/**
265265
* Add middlewares for PATCH requests at the specified path.
266266
*/
267-
patch(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
267+
patch(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
268268
this.#commands.push(newHandlerCmd("PATCH", path, middlewares, false));
269269
return this;
270270
}
271271
/**
272272
* Add middlewares for PUT requests at the specified path.
273273
*/
274-
put(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
274+
put(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
275275
this.#commands.push(newHandlerCmd("PUT", path, middlewares, false));
276276
return this;
277277
}
278278
/**
279279
* Add middlewares for DELETE requests at the specified path.
280280
*/
281-
delete(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
281+
delete(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
282282
this.#commands.push(newHandlerCmd("DELETE", path, middlewares, false));
283283
return this;
284284
}
285285
/**
286286
* Add middlewares for HEAD requests at the specified path.
287287
*/
288-
head(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
288+
head(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
289289
this.#commands.push(newHandlerCmd("HEAD", path, middlewares, false));
290290
return this;
291291
}
292292

293293
/**
294294
* Add middlewares for all HTTP verbs at the specified path.
295295
*/
296-
all(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this {
296+
all(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
297297
this.#commands.push(newHandlerCmd("ALL", path, middlewares, false));
298298
return this;
299299
}

src/commands.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpError } from "./error.ts";
22
import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
3-
import type { MaybeLazyMiddleware, MiddlewareFn } from "./middlewares/mod.ts";
3+
import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts";
44
import { mergePath, type Method, type Router } from "./router.ts";
55
import {
66
getOrCreateSegment,
@@ -55,7 +55,7 @@ export interface ErrorCmd<State> {
5555
}
5656
export function newErrorCmd<State>(
5757
pattern: string,
58-
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
58+
routeOrMiddleware: Route<State> | Middleware<State>,
5959
includeLastSegment: boolean,
6060
): ErrorCmd<State> {
6161
const route = typeof routeOrMiddleware === "function"
@@ -114,10 +114,10 @@ export function newMiddlewareCmd<State>(
114114

115115
export interface NotFoundCmd<State> {
116116
type: CommandType.NotFound;
117-
fn: MiddlewareFn<State>;
117+
fn: Middleware<State>;
118118
}
119119
export function newNotFoundCmd<State>(
120-
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
120+
routeOrMiddleware: Route<State> | Middleware<State>,
121121
): NotFoundCmd<State> {
122122
const route = typeof routeOrMiddleware === "function"
123123
? { handler: routeOrMiddleware }
@@ -165,13 +165,13 @@ export interface HandlerCommand<State> {
165165
type: CommandType.Handler;
166166
pattern: string;
167167
method: Method | "ALL";
168-
fns: MaybeLazy<MiddlewareFn<State>>[];
168+
fns: MaybeLazy<Middleware<State>>[];
169169
includeLastSegment: boolean;
170170
}
171171
export function newHandlerCmd<State>(
172172
method: Method | "ALL",
173173
pattern: string,
174-
fns: MaybeLazy<MiddlewareFn<State>>[],
174+
fns: MaybeLazy<Middleware<State>>[],
175175
includeLastSegment: boolean,
176176
): HandlerCommand<State> {
177177
return {

src/dev/middlewares/automatic_workspace_folders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { eTag, ifNoneMatch } from "@std/http/etag";
22
import { generate } from "@std/uuid/v5";
33
import { NAMESPACE_URL } from "@std/uuid/constants";
4-
import type { MiddlewareFn } from "../../middlewares/mod.ts";
4+
import type { Middleware } from "../../middlewares/mod.ts";
55

66
/**
77
* Automatically detects workspace folders for DevTools in Chromium browsers.
@@ -19,7 +19,7 @@ import type { MiddlewareFn } from "../../middlewares/mod.ts";
1919
*
2020
* @see https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md
2121
*/
22-
export function automaticWorkspaceFolders<T>(root: string): MiddlewareFn<T> {
22+
export function automaticWorkspaceFolders<T>(root: string): Middleware<T> {
2323
let uuid: string | undefined;
2424
let etag: string | undefined;
2525
let content: string | undefined;

src/dev/middlewares/error_overlay/middleware.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { DEV_ERROR_OVERLAY_URL } from "../../../constants.ts";
22
import { HttpError } from "../../../error.ts";
3-
import type { MiddlewareFn } from "../../../middlewares/mod.ts";
3+
import type { Middleware } from "../../../middlewares/mod.ts";
44
import { FreshScripts } from "../../../runtime/server/preact_hooks.tsx";
55
import { ErrorOverlay } from "./overlay.tsx";
66

7-
export function devErrorOverlay<T>(): MiddlewareFn<T> {
7+
export function devErrorOverlay<T>(): Middleware<T> {
88
return async (ctx) => {
99
const { config, url } = ctx;
1010
if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) {

src/dev/middlewares/live_reload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { MiddlewareFn } from "../../middlewares/mod.ts";
1+
import type { Middleware } from "../../middlewares/mod.ts";
22
import { ALIVE_URL } from "../../constants.ts";
33

44
// Live reload: Send updates to browser
5-
export function liveReload<T>(): MiddlewareFn<T> {
5+
export function liveReload<T>(): Middleware<T> {
66
const revision = Date.now();
77

88
return (ctx) => {

src/fs_routes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AnyComponent } from "preact";
22
import type { MaybeLazy, Route, RouteConfig } from "./types.ts";
33
import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
4-
import type { MiddlewareFn } from "./middlewares/mod.ts";
4+
import type { Middleware } from "./middlewares/mod.ts";
55
import type { AsyncAnyComponent } from "./render.ts";
66
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
77
import type { PageProps } from "./render.ts";
@@ -73,8 +73,8 @@ export function fsItemsToCommands<State>(
7373
const { handlers, mod } = validateFsMod(filePath, rawMod);
7474

7575
let middlewares = (handlers ?? mod.default) as unknown as
76-
| MiddlewareFn<State>
77-
| MiddlewareFn<State>[]
76+
| Middleware<State>
77+
| Middleware<State>[]
7878
| HandlerByMethod<unknown, State> ??
7979
null;
8080
if (middlewares === null) continue;

src/middlewares/cors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Context } from "../context.ts";
2-
import type { MiddlewareFn } from "./mod.ts";
2+
import type { Middleware } from "./mod.ts";
33

44
export type CORSOptions<State> = {
55
/**
@@ -61,7 +61,7 @@ export type CORSOptions<State> = {
6161
* // ];
6262
* ```
6363
*/
64-
export function cors<State>(options?: CORSOptions<State>): MiddlewareFn<State> {
64+
export function cors<State>(options?: CORSOptions<State>): Middleware<State> {
6565
const opts: CORSOptions<State> = {
6666
origin: "*",
6767
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],

src/middlewares/mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export type MiddlewareFn<State> = Middleware<State>;
8585
*/
8686
export type MaybeLazyMiddleware<State> = (
8787
ctx: Context<State>,
88-
) => Response | Promise<Response | MiddlewareFn<State>>;
88+
) => Response | Promise<Response | Middleware<State>>;
8989

9090
export function runMiddlewares<State>(
9191
middlewares: MaybeLazyMiddleware<State>[],

src/middlewares/mod_test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { runMiddlewares } from "./mod.ts";
22
import { expect } from "@std/expect";
33
import { serveMiddleware } from "../test_utils.ts";
4-
import type { MiddlewareFn } from "./mod.ts";
4+
import type { Middleware } from "./mod.ts";
55
import type { Lazy, MaybeLazy } from "../types.ts";
66

77
Deno.test("runMiddleware", async () => {
8-
const middlewares: MiddlewareFn<{ text: string }>[] = [
8+
const middlewares: Middleware<{ text: string }>[] = [
99
(ctx) => {
1010
ctx.state.text = "A";
1111
return ctx.next();
@@ -33,7 +33,7 @@ Deno.test("runMiddleware", async () => {
3333
});
3434

3535
Deno.test("runMiddleware - middlewares should only be called once", async () => {
36-
const A: MiddlewareFn<{ count: number }> = (ctx) => {
36+
const A: Middleware<{ count: number }> = (ctx) => {
3737
if (ctx.state.count === undefined) {
3838
ctx.state.count = 0;
3939
} else {
@@ -55,19 +55,19 @@ Deno.test("runMiddleware - middlewares should only be called once", async () =>
5555

5656
Deno.test("runMiddleware - runs multiple stacks", async () => {
5757
type State = { text: string };
58-
const A: MiddlewareFn<State> = (ctx) => {
58+
const A: Middleware<State> = (ctx) => {
5959
ctx.state.text += "A";
6060
return ctx.next();
6161
};
62-
const B: MiddlewareFn<State> = (ctx) => {
62+
const B: Middleware<State> = (ctx) => {
6363
ctx.state.text += "B";
6464
return ctx.next();
6565
};
66-
const C: MiddlewareFn<State> = (ctx) => {
66+
const C: Middleware<State> = (ctx) => {
6767
ctx.state.text += "C";
6868
return ctx.next();
6969
};
70-
const D: MiddlewareFn<State> = (ctx) => {
70+
const D: Middleware<State> = (ctx) => {
7171
ctx.state.text += "D";
7272
return ctx.next();
7373
};
@@ -95,7 +95,7 @@ Deno.test("runMiddleware - throws errors", async () => {
9595
let thrownB: unknown = null;
9696
let thrownC: unknown = null;
9797

98-
const middlewares: MiddlewareFn<{ text: string }>[] = [
98+
const middlewares: Middleware<{ text: string }>[] = [
9999
async (ctx) => {
100100
try {
101101
return await ctx.next();
@@ -144,15 +144,15 @@ Deno.test("runMiddleware - lazy middlewares", async () => {
144144

145145
let called = 0;
146146
// deno-lint-ignore require-await
147-
const lazy: Lazy<MiddlewareFn<State>> = async () => {
147+
const lazy: Lazy<Middleware<State>> = async () => {
148148
called++;
149149
return (ctx) => {
150150
ctx.state.text += "_lazy";
151151
return ctx.next();
152152
};
153153
};
154154

155-
const middlewares: MaybeLazy<MiddlewareFn<State>>[] = [
155+
const middlewares: MaybeLazy<Middleware<State>>[] = [
156156
async (ctx) => {
157157
ctx.state.text = "A";
158158
return await ctx.next();

src/middlewares/static_files.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "@std/path";
22
import { contentType as getContentType } from "@std/media-types/content-type";
3-
import type { MiddlewareFn } from "./mod.ts";
3+
import type { Middleware } from "./mod.ts";
44
import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
55
import { BUILD_ID } from "../runtime/build_id.ts";
66
import { tracer } from "../otel.ts";
@@ -13,7 +13,7 @@ import { getBuildCache } from "../context.ts";
1313
* app.use(staticFiles());
1414
* ```
1515
*/
16-
export function staticFiles<T>(): MiddlewareFn<T> {
16+
export function staticFiles<T>(): Middleware<T> {
1717
return async function freshServeStaticFiles(ctx) {
1818
const { req, url, config } = ctx;
1919

0 commit comments

Comments
 (0)