// Copyright 2018-2023 the oak authors. All rights reserved. MIT license. // deno-lint-ignore-file import type { State } from "./application.ts"; import type { Context } from "./context.ts"; /** A function for chaining middleware. */ export type Next = () => Promise; /** Middleware are functions which are chained together to deal with * requests. */ export interface Middleware< S extends State = Record, T extends Context = Context, > { (context: T, next: Next): Promise | unknown; } /** Middleware objects allow encapsulation of middleware along with the ability * to initialize the middleware upon listen. */ export interface MiddlewareObject< S extends State = Record, T extends Context = Context, > { /** Optional function for delayed initialization which will be called when * the application starts listening. */ init?: () => Promise | unknown; /** The method to be called to handle the request. */ handleRequest(context: T, next: Next): Promise | unknown; } /** Type that represents {@linkcode Middleware} or * {@linkcode MiddlewareObject}. */ export type MiddlewareOrMiddlewareObject< S extends State = Record, T extends Context = Context, > = Middleware | MiddlewareObject; /** A type guard that returns true if the value is * {@linkcode MiddlewareObject}. */ export function isMiddlewareObject< S extends State = Record, T extends Context = Context, >(value: MiddlewareOrMiddlewareObject): value is MiddlewareObject { return value && typeof value === "object" && "handleRequest" in value; } /** Compose multiple middleware functions into a single middleware function. */ export function compose< S extends State = Record, T extends Context = Context, >( middleware: MiddlewareOrMiddlewareObject[], ): (context: T, next?: Next) => Promise { return function composedMiddleware( context: T, next?: Next, ): Promise { let index = -1; async function dispatch(i: number): Promise { if (i <= index) { throw new Error("next() called multiple times."); } index = i; let m: MiddlewareOrMiddlewareObject | undefined = middleware[i]; let fn: Middleware | undefined; if (typeof m === "function") { fn = m; } else if (m && typeof m.handleRequest === "function") { fn = (m as MiddlewareObject).handleRequest.bind(m); } if (i === middleware.length) { fn = next; } if (!fn) { return; } await fn(context, dispatch.bind(null, i + 1)); } return dispatch(0); }; }