chore: switch MiddlewareFn -> Middleware (#3137)

This commit is contained in:
Marvin Hagemeister
2025-07-25 16:00:49 +02:00
committed by GitHub
parent a988dc37e1
commit ae960adefd
12 changed files with 45 additions and 45 deletions
+11 -11
View File
@@ -4,7 +4,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors"; import * as colors from "@std/fmt/colors";
import { import {
type MaybeLazyMiddleware, type MaybeLazyMiddleware,
type MiddlewareFn, type Middleware,
runMiddlewares, runMiddlewares,
} from "./middlewares/mod.ts"; } from "./middlewares/mod.ts";
import { Context } from "./context.ts"; import { Context } from "./context.ts";
@@ -209,16 +209,16 @@ export class App<State> {
} }
/** /**
* Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode MiddlewareFn}. * Set the app's 404 error handler. Can be a {@linkcode Route} or a {@linkcode Middleware}.
*/ */
notFound(routeOrMiddleware: Route<State> | MiddlewareFn<State>): this { notFound(routeOrMiddleware: Route<State> | Middleware<State>): this {
this.#commands.push(newNotFoundCmd(routeOrMiddleware)); this.#commands.push(newNotFoundCmd(routeOrMiddleware));
return this; return this;
} }
onError( onError(
path: string, path: string,
routeOrMiddleware: Route<State> | MiddlewareFn<State>, routeOrMiddleware: Route<State> | Middleware<State>,
): this { ): this {
this.#commands.push(newErrorCmd(path, routeOrMiddleware, true)); this.#commands.push(newErrorCmd(path, routeOrMiddleware, true));
return this; return this;
@@ -250,42 +250,42 @@ export class App<State> {
/** /**
* Add middlewares for GET requests at the specified path. * Add middlewares for GET requests at the specified path.
*/ */
get(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { get(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("GET", path, middlewares, false)); this.#commands.push(newHandlerCmd("GET", path, middlewares, false));
return this; return this;
} }
/** /**
* Add middlewares for POST requests at the specified path. * Add middlewares for POST requests at the specified path.
*/ */
post(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { post(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("POST", path, middlewares, false)); this.#commands.push(newHandlerCmd("POST", path, middlewares, false));
return this; return this;
} }
/** /**
* Add middlewares for PATCH requests at the specified path. * Add middlewares for PATCH requests at the specified path.
*/ */
patch(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { patch(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("PATCH", path, middlewares, false)); this.#commands.push(newHandlerCmd("PATCH", path, middlewares, false));
return this; return this;
} }
/** /**
* Add middlewares for PUT requests at the specified path. * Add middlewares for PUT requests at the specified path.
*/ */
put(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { put(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("PUT", path, middlewares, false)); this.#commands.push(newHandlerCmd("PUT", path, middlewares, false));
return this; return this;
} }
/** /**
* Add middlewares for DELETE requests at the specified path. * Add middlewares for DELETE requests at the specified path.
*/ */
delete(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { delete(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("DELETE", path, middlewares, false)); this.#commands.push(newHandlerCmd("DELETE", path, middlewares, false));
return this; return this;
} }
/** /**
* Add middlewares for HEAD requests at the specified path. * Add middlewares for HEAD requests at the specified path.
*/ */
head(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { head(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("HEAD", path, middlewares, false)); this.#commands.push(newHandlerCmd("HEAD", path, middlewares, false));
return this; return this;
} }
@@ -293,7 +293,7 @@ export class App<State> {
/** /**
* Add middlewares for all HTTP verbs at the specified path. * Add middlewares for all HTTP verbs at the specified path.
*/ */
all(path: string, ...middlewares: MaybeLazy<MiddlewareFn<State>>[]): this { all(path: string, ...middlewares: MaybeLazy<Middleware<State>>[]): this {
this.#commands.push(newHandlerCmd("ALL", path, middlewares, false)); this.#commands.push(newHandlerCmd("ALL", path, middlewares, false));
return this; return this;
} }
+6 -6
View File
@@ -1,6 +1,6 @@
import { HttpError } from "./error.ts"; import { HttpError } from "./error.ts";
import { isHandlerByMethod, type PageResponse } from "./handlers.ts"; import { isHandlerByMethod, type PageResponse } from "./handlers.ts";
import type { MaybeLazyMiddleware, MiddlewareFn } from "./middlewares/mod.ts"; import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts";
import { mergePath, type Method, type Router } from "./router.ts"; import { mergePath, type Method, type Router } from "./router.ts";
import { import {
getOrCreateSegment, getOrCreateSegment,
@@ -55,7 +55,7 @@ export interface ErrorCmd<State> {
} }
export function newErrorCmd<State>( export function newErrorCmd<State>(
pattern: string, pattern: string,
routeOrMiddleware: Route<State> | MiddlewareFn<State>, routeOrMiddleware: Route<State> | Middleware<State>,
includeLastSegment: boolean, includeLastSegment: boolean,
): ErrorCmd<State> { ): ErrorCmd<State> {
const route = typeof routeOrMiddleware === "function" const route = typeof routeOrMiddleware === "function"
@@ -114,10 +114,10 @@ export function newMiddlewareCmd<State>(
export interface NotFoundCmd<State> { export interface NotFoundCmd<State> {
type: CommandType.NotFound; type: CommandType.NotFound;
fn: MiddlewareFn<State>; fn: Middleware<State>;
} }
export function newNotFoundCmd<State>( export function newNotFoundCmd<State>(
routeOrMiddleware: Route<State> | MiddlewareFn<State>, routeOrMiddleware: Route<State> | Middleware<State>,
): NotFoundCmd<State> { ): NotFoundCmd<State> {
const route = typeof routeOrMiddleware === "function" const route = typeof routeOrMiddleware === "function"
? { handler: routeOrMiddleware } ? { handler: routeOrMiddleware }
@@ -165,13 +165,13 @@ export interface HandlerCommand<State> {
type: CommandType.Handler; type: CommandType.Handler;
pattern: string; pattern: string;
method: Method | "ALL"; method: Method | "ALL";
fns: MaybeLazy<MiddlewareFn<State>>[]; fns: MaybeLazy<Middleware<State>>[];
includeLastSegment: boolean; includeLastSegment: boolean;
} }
export function newHandlerCmd<State>( export function newHandlerCmd<State>(
method: Method | "ALL", method: Method | "ALL",
pattern: string, pattern: string,
fns: MaybeLazy<MiddlewareFn<State>>[], fns: MaybeLazy<Middleware<State>>[],
includeLastSegment: boolean, includeLastSegment: boolean,
): HandlerCommand<State> { ): HandlerCommand<State> {
return { return {
@@ -1,7 +1,7 @@
import { eTag, ifNoneMatch } from "@std/http/etag"; import { eTag, ifNoneMatch } from "@std/http/etag";
import { generate } from "@std/uuid/v5"; import { generate } from "@std/uuid/v5";
import { NAMESPACE_URL } from "@std/uuid/constants"; import { NAMESPACE_URL } from "@std/uuid/constants";
import type { MiddlewareFn } from "../../middlewares/mod.ts"; import type { Middleware } from "../../middlewares/mod.ts";
/** /**
* Automatically detects workspace folders for DevTools in Chromium browsers. * Automatically detects workspace folders for DevTools in Chromium browsers.
@@ -19,7 +19,7 @@ import type { MiddlewareFn } from "../../middlewares/mod.ts";
* *
* @see https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md * @see https://chromium.googlesource.com/devtools/devtools-frontend/+/main/docs/ecosystem/automatic_workspace_folders.md
*/ */
export function automaticWorkspaceFolders<T>(root: string): MiddlewareFn<T> { export function automaticWorkspaceFolders<T>(root: string): Middleware<T> {
let uuid: string | undefined; let uuid: string | undefined;
let etag: string | undefined; let etag: string | undefined;
let content: string | undefined; let content: string | undefined;
@@ -1,10 +1,10 @@
import { DEV_ERROR_OVERLAY_URL } from "../../../constants.ts"; import { DEV_ERROR_OVERLAY_URL } from "../../../constants.ts";
import { HttpError } from "../../../error.ts"; import { HttpError } from "../../../error.ts";
import type { MiddlewareFn } from "../../../middlewares/mod.ts"; import type { Middleware } from "../../../middlewares/mod.ts";
import { FreshScripts } from "../../../runtime/server/preact_hooks.tsx"; import { FreshScripts } from "../../../runtime/server/preact_hooks.tsx";
import { ErrorOverlay } from "./overlay.tsx"; import { ErrorOverlay } from "./overlay.tsx";
export function devErrorOverlay<T>(): MiddlewareFn<T> { export function devErrorOverlay<T>(): Middleware<T> {
return async (ctx) => { return async (ctx) => {
const { config, url } = ctx; const { config, url } = ctx;
if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) { if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) {
+2 -2
View File
@@ -1,8 +1,8 @@
import type { MiddlewareFn } from "../../middlewares/mod.ts"; import type { Middleware } from "../../middlewares/mod.ts";
import { ALIVE_URL } from "../../constants.ts"; import { ALIVE_URL } from "../../constants.ts";
// Live reload: Send updates to browser // Live reload: Send updates to browser
export function liveReload<T>(): MiddlewareFn<T> { export function liveReload<T>(): Middleware<T> {
const revision = Date.now(); const revision = Date.now();
return (ctx) => { return (ctx) => {
+3 -3
View File
@@ -1,7 +1,7 @@
import type { AnyComponent } from "preact"; import type { AnyComponent } from "preact";
import type { MaybeLazy, Route, RouteConfig } from "./types.ts"; import type { MaybeLazy, Route, RouteConfig } from "./types.ts";
import type { HandlerByMethod, RouteHandler } from "./handlers.ts"; import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
import type { MiddlewareFn } from "./middlewares/mod.ts"; import type { Middleware } from "./middlewares/mod.ts";
import type { AsyncAnyComponent } from "./render.ts"; import type { AsyncAnyComponent } from "./render.ts";
import { type HandlerFn, isHandlerByMethod } from "./handlers.ts"; import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
import type { PageProps } from "./render.ts"; import type { PageProps } from "./render.ts";
@@ -73,8 +73,8 @@ export function fsItemsToCommands<State>(
const { handlers, mod } = validateFsMod(filePath, rawMod); const { handlers, mod } = validateFsMod(filePath, rawMod);
let middlewares = (handlers ?? mod.default) as unknown as let middlewares = (handlers ?? mod.default) as unknown as
| MiddlewareFn<State> | Middleware<State>
| MiddlewareFn<State>[] | Middleware<State>[]
| HandlerByMethod<unknown, State> ?? | HandlerByMethod<unknown, State> ??
null; null;
if (middlewares === null) continue; if (middlewares === null) continue;
+2 -2
View File
@@ -1,5 +1,5 @@
import type { Context } from "../context.ts"; import type { Context } from "../context.ts";
import type { MiddlewareFn } from "./mod.ts"; import type { Middleware } from "./mod.ts";
export type CORSOptions<State> = { export type CORSOptions<State> = {
/** /**
@@ -61,7 +61,7 @@ export type CORSOptions<State> = {
* // ]; * // ];
* ``` * ```
*/ */
export function cors<State>(options?: CORSOptions<State>): MiddlewareFn<State> { export function cors<State>(options?: CORSOptions<State>): Middleware<State> {
const opts: CORSOptions<State> = { const opts: CORSOptions<State> = {
origin: "*", origin: "*",
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"], allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
+1 -1
View File
@@ -85,7 +85,7 @@ export type MiddlewareFn<State> = Middleware<State>;
*/ */
export type MaybeLazyMiddleware<State> = ( export type MaybeLazyMiddleware<State> = (
ctx: Context<State>, ctx: Context<State>,
) => Response | Promise<Response | MiddlewareFn<State>>; ) => Response | Promise<Response | Middleware<State>>;
export function runMiddlewares<State>( export function runMiddlewares<State>(
middlewares: MaybeLazyMiddleware<State>[], middlewares: MaybeLazyMiddleware<State>[],
+10 -10
View File
@@ -1,11 +1,11 @@
import { runMiddlewares } from "./mod.ts"; import { runMiddlewares } from "./mod.ts";
import { expect } from "@std/expect"; import { expect } from "@std/expect";
import { serveMiddleware } from "../test_utils.ts"; import { serveMiddleware } from "../test_utils.ts";
import type { MiddlewareFn } from "./mod.ts"; import type { Middleware } from "./mod.ts";
import type { Lazy, MaybeLazy } from "../types.ts"; import type { Lazy, MaybeLazy } from "../types.ts";
Deno.test("runMiddleware", async () => { Deno.test("runMiddleware", async () => {
const middlewares: MiddlewareFn<{ text: string }>[] = [ const middlewares: Middleware<{ text: string }>[] = [
(ctx) => { (ctx) => {
ctx.state.text = "A"; ctx.state.text = "A";
return ctx.next(); return ctx.next();
@@ -33,7 +33,7 @@ Deno.test("runMiddleware", async () => {
}); });
Deno.test("runMiddleware - middlewares should only be called once", async () => { Deno.test("runMiddleware - middlewares should only be called once", async () => {
const A: MiddlewareFn<{ count: number }> = (ctx) => { const A: Middleware<{ count: number }> = (ctx) => {
if (ctx.state.count === undefined) { if (ctx.state.count === undefined) {
ctx.state.count = 0; ctx.state.count = 0;
} else { } else {
@@ -55,19 +55,19 @@ Deno.test("runMiddleware - middlewares should only be called once", async () =>
Deno.test("runMiddleware - runs multiple stacks", async () => { Deno.test("runMiddleware - runs multiple stacks", async () => {
type State = { text: string }; type State = { text: string };
const A: MiddlewareFn<State> = (ctx) => { const A: Middleware<State> = (ctx) => {
ctx.state.text += "A"; ctx.state.text += "A";
return ctx.next(); return ctx.next();
}; };
const B: MiddlewareFn<State> = (ctx) => { const B: Middleware<State> = (ctx) => {
ctx.state.text += "B"; ctx.state.text += "B";
return ctx.next(); return ctx.next();
}; };
const C: MiddlewareFn<State> = (ctx) => { const C: Middleware<State> = (ctx) => {
ctx.state.text += "C"; ctx.state.text += "C";
return ctx.next(); return ctx.next();
}; };
const D: MiddlewareFn<State> = (ctx) => { const D: Middleware<State> = (ctx) => {
ctx.state.text += "D"; ctx.state.text += "D";
return ctx.next(); return ctx.next();
}; };
@@ -95,7 +95,7 @@ Deno.test("runMiddleware - throws errors", async () => {
let thrownB: unknown = null; let thrownB: unknown = null;
let thrownC: unknown = null; let thrownC: unknown = null;
const middlewares: MiddlewareFn<{ text: string }>[] = [ const middlewares: Middleware<{ text: string }>[] = [
async (ctx) => { async (ctx) => {
try { try {
return await ctx.next(); return await ctx.next();
@@ -144,7 +144,7 @@ Deno.test("runMiddleware - lazy middlewares", async () => {
let called = 0; let called = 0;
// deno-lint-ignore require-await // deno-lint-ignore require-await
const lazy: Lazy<MiddlewareFn<State>> = async () => { const lazy: Lazy<Middleware<State>> = async () => {
called++; called++;
return (ctx) => { return (ctx) => {
ctx.state.text += "_lazy"; ctx.state.text += "_lazy";
@@ -152,7 +152,7 @@ Deno.test("runMiddleware - lazy middlewares", async () => {
}; };
}; };
const middlewares: MaybeLazy<MiddlewareFn<State>>[] = [ const middlewares: MaybeLazy<Middleware<State>>[] = [
async (ctx) => { async (ctx) => {
ctx.state.text = "A"; ctx.state.text = "A";
return await ctx.next(); return await ctx.next();
+2 -2
View File
@@ -1,6 +1,6 @@
import * as path from "@std/path"; import * as path from "@std/path";
import { contentType as getContentType } from "@std/media-types/content-type"; import { contentType as getContentType } from "@std/media-types/content-type";
import type { MiddlewareFn } from "./mod.ts"; import type { Middleware } from "./mod.ts";
import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx"; import { ASSET_CACHE_BUST_KEY } from "../runtime/shared_internal.tsx";
import { BUILD_ID } from "../runtime/build_id.ts"; import { BUILD_ID } from "../runtime/build_id.ts";
import { tracer } from "../otel.ts"; import { tracer } from "../otel.ts";
@@ -13,7 +13,7 @@ import { getBuildCache } from "../context.ts";
* app.use(staticFiles()); * app.use(staticFiles());
* ``` * ```
*/ */
export function staticFiles<T>(): MiddlewareFn<T> { export function staticFiles<T>(): Middleware<T> {
return async function freshServeStaticFiles(ctx) { return async function freshServeStaticFiles(ctx) {
const { req, url, config } = ctx; const { req, url, config } = ctx;
+2 -2
View File
@@ -1,4 +1,4 @@
import type { MiddlewareFn } from "./mod.ts"; import type { Middleware } from "./mod.ts";
/** /**
* Fresh middleware to force URLs to end with a slash or never end with one. * Fresh middleware to force URLs to end with a slash or never end with one.
@@ -12,7 +12,7 @@ import type { MiddlewareFn } from "./mod.ts";
*/ */
export function trailingSlashes<State>( export function trailingSlashes<State>(
mode: "always" | "never", mode: "always" | "never",
): MiddlewareFn<State> { ): Middleware<State> {
return function trailingSlashesMiddleware(ctx) { return function trailingSlashesMiddleware(ctx) {
const url = ctx.url; const url = ctx.url;
if (url.pathname !== "/") { if (url.pathname !== "/") {
+2 -2
View File
@@ -1,5 +1,5 @@
import type { AnyComponent } from "preact"; import type { AnyComponent } from "preact";
import type { MaybeLazyMiddleware, MiddlewareFn } from "./middlewares/mod.ts"; import type { MaybeLazyMiddleware, Middleware } from "./middlewares/mod.ts";
import { type Method, patternToSegments } from "./router.ts"; import { type Method, patternToSegments } from "./router.ts";
import type { LayoutConfig, Route } from "./types.ts"; import type { LayoutConfig, Route } from "./types.ts";
import { type Context, getInternals } from "./context.ts"; import { type Context, getInternals } from "./context.ts";
@@ -24,7 +24,7 @@ export interface Segment<State> {
config: LayoutConfig | null; config: LayoutConfig | null;
} | null; } | null;
errorRoute: Route<State> | null; errorRoute: Route<State> | null;
notFound: MiddlewareFn<State> | null; notFound: Middleware<State> | null;
app: RouteComponent<State> | null; app: RouteComponent<State> | null;
children: Map<string, Segment<State>>; children: Map<string, Segment<State>>;
parent: Segment<State> | null; parent: Segment<State> | null;