diff --git a/src/app.ts b/src/app.ts index 0c0365f2..880c3fc6 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,7 +4,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts"; import * as colors from "@std/fmt/colors"; import { type MaybeLazyMiddleware, - type MiddlewareFn, + type Middleware, runMiddlewares, } from "./middlewares/mod.ts"; import { Context } from "./context.ts"; @@ -209,16 +209,16 @@ export class App { } /** - * 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 | MiddlewareFn): this { + notFound(routeOrMiddleware: Route | Middleware): this { this.#commands.push(newNotFoundCmd(routeOrMiddleware)); return this; } onError( path: string, - routeOrMiddleware: Route | MiddlewareFn, + routeOrMiddleware: Route | Middleware, ): this { this.#commands.push(newErrorCmd(path, routeOrMiddleware, true)); return this; @@ -250,42 +250,42 @@ export class App { /** * Add middlewares for GET requests at the specified path. */ - get(path: string, ...middlewares: MaybeLazy>[]): this { + get(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("GET", path, middlewares, false)); return this; } /** * Add middlewares for POST requests at the specified path. */ - post(path: string, ...middlewares: MaybeLazy>[]): this { + post(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("POST", path, middlewares, false)); return this; } /** * Add middlewares for PATCH requests at the specified path. */ - patch(path: string, ...middlewares: MaybeLazy>[]): this { + patch(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("PATCH", path, middlewares, false)); return this; } /** * Add middlewares for PUT requests at the specified path. */ - put(path: string, ...middlewares: MaybeLazy>[]): this { + put(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("PUT", path, middlewares, false)); return this; } /** * Add middlewares for DELETE requests at the specified path. */ - delete(path: string, ...middlewares: MaybeLazy>[]): this { + delete(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("DELETE", path, middlewares, false)); return this; } /** * Add middlewares for HEAD requests at the specified path. */ - head(path: string, ...middlewares: MaybeLazy>[]): this { + head(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("HEAD", path, middlewares, false)); return this; } @@ -293,7 +293,7 @@ export class App { /** * Add middlewares for all HTTP verbs at the specified path. */ - all(path: string, ...middlewares: MaybeLazy>[]): this { + all(path: string, ...middlewares: MaybeLazy>[]): this { this.#commands.push(newHandlerCmd("ALL", path, middlewares, false)); return this; } diff --git a/src/commands.ts b/src/commands.ts index 0b245ba8..ff7e2a2d 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,6 +1,6 @@ import { HttpError } from "./error.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 { getOrCreateSegment, @@ -55,7 +55,7 @@ export interface ErrorCmd { } export function newErrorCmd( pattern: string, - routeOrMiddleware: Route | MiddlewareFn, + routeOrMiddleware: Route | Middleware, includeLastSegment: boolean, ): ErrorCmd { const route = typeof routeOrMiddleware === "function" @@ -114,10 +114,10 @@ export function newMiddlewareCmd( export interface NotFoundCmd { type: CommandType.NotFound; - fn: MiddlewareFn; + fn: Middleware; } export function newNotFoundCmd( - routeOrMiddleware: Route | MiddlewareFn, + routeOrMiddleware: Route | Middleware, ): NotFoundCmd { const route = typeof routeOrMiddleware === "function" ? { handler: routeOrMiddleware } @@ -165,13 +165,13 @@ export interface HandlerCommand { type: CommandType.Handler; pattern: string; method: Method | "ALL"; - fns: MaybeLazy>[]; + fns: MaybeLazy>[]; includeLastSegment: boolean; } export function newHandlerCmd( method: Method | "ALL", pattern: string, - fns: MaybeLazy>[], + fns: MaybeLazy>[], includeLastSegment: boolean, ): HandlerCommand { return { diff --git a/src/dev/middlewares/automatic_workspace_folders.ts b/src/dev/middlewares/automatic_workspace_folders.ts index 2a282041..6138ce5b 100644 --- a/src/dev/middlewares/automatic_workspace_folders.ts +++ b/src/dev/middlewares/automatic_workspace_folders.ts @@ -1,7 +1,7 @@ import { eTag, ifNoneMatch } from "@std/http/etag"; import { generate } from "@std/uuid/v5"; 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. @@ -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 */ -export function automaticWorkspaceFolders(root: string): MiddlewareFn { +export function automaticWorkspaceFolders(root: string): Middleware { let uuid: string | undefined; let etag: string | undefined; let content: string | undefined; diff --git a/src/dev/middlewares/error_overlay/middleware.tsx b/src/dev/middlewares/error_overlay/middleware.tsx index b4aa91ab..d461ed56 100644 --- a/src/dev/middlewares/error_overlay/middleware.tsx +++ b/src/dev/middlewares/error_overlay/middleware.tsx @@ -1,10 +1,10 @@ import { DEV_ERROR_OVERLAY_URL } from "../../../constants.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 { ErrorOverlay } from "./overlay.tsx"; -export function devErrorOverlay(): MiddlewareFn { +export function devErrorOverlay(): Middleware { return async (ctx) => { const { config, url } = ctx; if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) { diff --git a/src/dev/middlewares/live_reload.ts b/src/dev/middlewares/live_reload.ts index 8e182e42..7f67794c 100644 --- a/src/dev/middlewares/live_reload.ts +++ b/src/dev/middlewares/live_reload.ts @@ -1,8 +1,8 @@ -import type { MiddlewareFn } from "../../middlewares/mod.ts"; +import type { Middleware } from "../../middlewares/mod.ts"; import { ALIVE_URL } from "../../constants.ts"; // Live reload: Send updates to browser -export function liveReload(): MiddlewareFn { +export function liveReload(): Middleware { const revision = Date.now(); return (ctx) => { diff --git a/src/fs_routes.ts b/src/fs_routes.ts index e166886d..5a22374b 100644 --- a/src/fs_routes.ts +++ b/src/fs_routes.ts @@ -1,7 +1,7 @@ import type { AnyComponent } from "preact"; import type { MaybeLazy, Route, RouteConfig } from "./types.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 HandlerFn, isHandlerByMethod } from "./handlers.ts"; import type { PageProps } from "./render.ts"; @@ -73,8 +73,8 @@ export function fsItemsToCommands( const { handlers, mod } = validateFsMod(filePath, rawMod); let middlewares = (handlers ?? mod.default) as unknown as - | MiddlewareFn - | MiddlewareFn[] + | Middleware + | Middleware[] | HandlerByMethod ?? null; if (middlewares === null) continue; diff --git a/src/middlewares/cors.ts b/src/middlewares/cors.ts index ce00651a..7de8c730 100644 --- a/src/middlewares/cors.ts +++ b/src/middlewares/cors.ts @@ -1,5 +1,5 @@ import type { Context } from "../context.ts"; -import type { MiddlewareFn } from "./mod.ts"; +import type { Middleware } from "./mod.ts"; export type CORSOptions = { /** @@ -61,7 +61,7 @@ export type CORSOptions = { * // ]; * ``` */ -export function cors(options?: CORSOptions): MiddlewareFn { +export function cors(options?: CORSOptions): Middleware { const opts: CORSOptions = { origin: "*", allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"], diff --git a/src/middlewares/mod.ts b/src/middlewares/mod.ts index fcbf3ded..c523d081 100644 --- a/src/middlewares/mod.ts +++ b/src/middlewares/mod.ts @@ -85,7 +85,7 @@ export type MiddlewareFn = Middleware; */ export type MaybeLazyMiddleware = ( ctx: Context, -) => Response | Promise>; +) => Response | Promise>; export function runMiddlewares( middlewares: MaybeLazyMiddleware[], diff --git a/src/middlewares/mod_test.ts b/src/middlewares/mod_test.ts index 6bf86cb4..77c44f1f 100644 --- a/src/middlewares/mod_test.ts +++ b/src/middlewares/mod_test.ts @@ -1,11 +1,11 @@ import { runMiddlewares } from "./mod.ts"; import { expect } from "@std/expect"; 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"; Deno.test("runMiddleware", async () => { - const middlewares: MiddlewareFn<{ text: string }>[] = [ + const middlewares: Middleware<{ text: string }>[] = [ (ctx) => { ctx.state.text = "A"; return ctx.next(); @@ -33,7 +33,7 @@ Deno.test("runMiddleware", 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) { ctx.state.count = 0; } else { @@ -55,19 +55,19 @@ Deno.test("runMiddleware - middlewares should only be called once", async () => Deno.test("runMiddleware - runs multiple stacks", async () => { type State = { text: string }; - const A: MiddlewareFn = (ctx) => { + const A: Middleware = (ctx) => { ctx.state.text += "A"; return ctx.next(); }; - const B: MiddlewareFn = (ctx) => { + const B: Middleware = (ctx) => { ctx.state.text += "B"; return ctx.next(); }; - const C: MiddlewareFn = (ctx) => { + const C: Middleware = (ctx) => { ctx.state.text += "C"; return ctx.next(); }; - const D: MiddlewareFn = (ctx) => { + const D: Middleware = (ctx) => { ctx.state.text += "D"; return ctx.next(); }; @@ -95,7 +95,7 @@ Deno.test("runMiddleware - throws errors", async () => { let thrownB: unknown = null; let thrownC: unknown = null; - const middlewares: MiddlewareFn<{ text: string }>[] = [ + const middlewares: Middleware<{ text: string }>[] = [ async (ctx) => { try { return await ctx.next(); @@ -144,7 +144,7 @@ Deno.test("runMiddleware - lazy middlewares", async () => { let called = 0; // deno-lint-ignore require-await - const lazy: Lazy> = async () => { + const lazy: Lazy> = async () => { called++; return (ctx) => { ctx.state.text += "_lazy"; @@ -152,7 +152,7 @@ Deno.test("runMiddleware - lazy middlewares", async () => { }; }; - const middlewares: MaybeLazy>[] = [ + const middlewares: MaybeLazy>[] = [ async (ctx) => { ctx.state.text = "A"; return await ctx.next(); diff --git a/src/middlewares/static_files.ts b/src/middlewares/static_files.ts index 87974c33..8fe90990 100644 --- a/src/middlewares/static_files.ts +++ b/src/middlewares/static_files.ts @@ -1,6 +1,6 @@ import * as path from "@std/path"; 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 { BUILD_ID } from "../runtime/build_id.ts"; import { tracer } from "../otel.ts"; @@ -13,7 +13,7 @@ import { getBuildCache } from "../context.ts"; * app.use(staticFiles()); * ``` */ -export function staticFiles(): MiddlewareFn { +export function staticFiles(): Middleware { return async function freshServeStaticFiles(ctx) { const { req, url, config } = ctx; diff --git a/src/middlewares/trailing_slashes.ts b/src/middlewares/trailing_slashes.ts index 98a004f1..2ea39928 100644 --- a/src/middlewares/trailing_slashes.ts +++ b/src/middlewares/trailing_slashes.ts @@ -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. @@ -12,7 +12,7 @@ import type { MiddlewareFn } from "./mod.ts"; */ export function trailingSlashes( mode: "always" | "never", -): MiddlewareFn { +): Middleware { return function trailingSlashesMiddleware(ctx) { const url = ctx.url; if (url.pathname !== "/") { diff --git a/src/segments.ts b/src/segments.ts index 46c7673f..63c31448 100644 --- a/src/segments.ts +++ b/src/segments.ts @@ -1,5 +1,5 @@ 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 { LayoutConfig, Route } from "./types.ts"; import { type Context, getInternals } from "./context.ts"; @@ -24,7 +24,7 @@ export interface Segment { config: LayoutConfig | null; } | null; errorRoute: Route | null; - notFound: MiddlewareFn | null; + notFound: Middleware | null; app: RouteComponent | null; children: Map>; parent: Segment | null;