chore: switch MiddlewareFn -> Middleware (#3137)
This commit is contained in:
+11
-11
@@ -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<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));
|
||||
return this;
|
||||
}
|
||||
|
||||
onError(
|
||||
path: string,
|
||||
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
|
||||
routeOrMiddleware: Route<State> | Middleware<State>,
|
||||
): this {
|
||||
this.#commands.push(newErrorCmd(path, routeOrMiddleware, true));
|
||||
return this;
|
||||
@@ -250,42 +250,42 @@ export class App<State> {
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
@@ -293,7 +293,7 @@ export class App<State> {
|
||||
/**
|
||||
* 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));
|
||||
return this;
|
||||
}
|
||||
|
||||
+6
-6
@@ -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<State> {
|
||||
}
|
||||
export function newErrorCmd<State>(
|
||||
pattern: string,
|
||||
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
|
||||
routeOrMiddleware: Route<State> | Middleware<State>,
|
||||
includeLastSegment: boolean,
|
||||
): ErrorCmd<State> {
|
||||
const route = typeof routeOrMiddleware === "function"
|
||||
@@ -114,10 +114,10 @@ export function newMiddlewareCmd<State>(
|
||||
|
||||
export interface NotFoundCmd<State> {
|
||||
type: CommandType.NotFound;
|
||||
fn: MiddlewareFn<State>;
|
||||
fn: Middleware<State>;
|
||||
}
|
||||
export function newNotFoundCmd<State>(
|
||||
routeOrMiddleware: Route<State> | MiddlewareFn<State>,
|
||||
routeOrMiddleware: Route<State> | Middleware<State>,
|
||||
): NotFoundCmd<State> {
|
||||
const route = typeof routeOrMiddleware === "function"
|
||||
? { handler: routeOrMiddleware }
|
||||
@@ -165,13 +165,13 @@ export interface HandlerCommand<State> {
|
||||
type: CommandType.Handler;
|
||||
pattern: string;
|
||||
method: Method | "ALL";
|
||||
fns: MaybeLazy<MiddlewareFn<State>>[];
|
||||
fns: MaybeLazy<Middleware<State>>[];
|
||||
includeLastSegment: boolean;
|
||||
}
|
||||
export function newHandlerCmd<State>(
|
||||
method: Method | "ALL",
|
||||
pattern: string,
|
||||
fns: MaybeLazy<MiddlewareFn<State>>[],
|
||||
fns: MaybeLazy<Middleware<State>>[],
|
||||
includeLastSegment: boolean,
|
||||
): HandlerCommand<State> {
|
||||
return {
|
||||
|
||||
@@ -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<T>(root: string): MiddlewareFn<T> {
|
||||
export function automaticWorkspaceFolders<T>(root: string): Middleware<T> {
|
||||
let uuid: string | undefined;
|
||||
let etag: string | undefined;
|
||||
let content: string | undefined;
|
||||
|
||||
@@ -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<T>(): MiddlewareFn<T> {
|
||||
export function devErrorOverlay<T>(): Middleware<T> {
|
||||
return async (ctx) => {
|
||||
const { config, url } = ctx;
|
||||
if (url.pathname === config.basePath + DEV_ERROR_OVERLAY_URL) {
|
||||
|
||||
@@ -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<T>(): MiddlewareFn<T> {
|
||||
export function liveReload<T>(): Middleware<T> {
|
||||
const revision = Date.now();
|
||||
|
||||
return (ctx) => {
|
||||
|
||||
+3
-3
@@ -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<State>(
|
||||
const { handlers, mod } = validateFsMod(filePath, rawMod);
|
||||
|
||||
let middlewares = (handlers ?? mod.default) as unknown as
|
||||
| MiddlewareFn<State>
|
||||
| MiddlewareFn<State>[]
|
||||
| Middleware<State>
|
||||
| Middleware<State>[]
|
||||
| HandlerByMethod<unknown, State> ??
|
||||
null;
|
||||
if (middlewares === null) continue;
|
||||
|
||||
@@ -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<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> = {
|
||||
origin: "*",
|
||||
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"],
|
||||
|
||||
@@ -85,7 +85,7 @@ export type MiddlewareFn<State> = Middleware<State>;
|
||||
*/
|
||||
export type MaybeLazyMiddleware<State> = (
|
||||
ctx: Context<State>,
|
||||
) => Response | Promise<Response | MiddlewareFn<State>>;
|
||||
) => Response | Promise<Response | Middleware<State>>;
|
||||
|
||||
export function runMiddlewares<State>(
|
||||
middlewares: MaybeLazyMiddleware<State>[],
|
||||
|
||||
+10
-10
@@ -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<State> = (ctx) => {
|
||||
const A: Middleware<State> = (ctx) => {
|
||||
ctx.state.text += "A";
|
||||
return ctx.next();
|
||||
};
|
||||
const B: MiddlewareFn<State> = (ctx) => {
|
||||
const B: Middleware<State> = (ctx) => {
|
||||
ctx.state.text += "B";
|
||||
return ctx.next();
|
||||
};
|
||||
const C: MiddlewareFn<State> = (ctx) => {
|
||||
const C: Middleware<State> = (ctx) => {
|
||||
ctx.state.text += "C";
|
||||
return ctx.next();
|
||||
};
|
||||
const D: MiddlewareFn<State> = (ctx) => {
|
||||
const D: Middleware<State> = (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<MiddlewareFn<State>> = async () => {
|
||||
const lazy: Lazy<Middleware<State>> = async () => {
|
||||
called++;
|
||||
return (ctx) => {
|
||||
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) => {
|
||||
ctx.state.text = "A";
|
||||
return await ctx.next();
|
||||
|
||||
@@ -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<T>(): MiddlewareFn<T> {
|
||||
export function staticFiles<T>(): Middleware<T> {
|
||||
return async function freshServeStaticFiles(ctx) {
|
||||
const { req, url, config } = ctx;
|
||||
|
||||
|
||||
@@ -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<State>(
|
||||
mode: "always" | "never",
|
||||
): MiddlewareFn<State> {
|
||||
): Middleware<State> {
|
||||
return function trailingSlashesMiddleware(ctx) {
|
||||
const url = ctx.url;
|
||||
if (url.pathname !== "/") {
|
||||
|
||||
+2
-2
@@ -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<State> {
|
||||
config: LayoutConfig | null;
|
||||
} | null;
|
||||
errorRoute: Route<State> | null;
|
||||
notFound: MiddlewareFn<State> | null;
|
||||
notFound: Middleware<State> | null;
|
||||
app: RouteComponent<State> | null;
|
||||
children: Map<string, Segment<State>>;
|
||||
parent: Segment<State> | null;
|
||||
|
||||
Reference in New Issue
Block a user