import { isPromiseLike } from "../teardown-manager.js"; /* A middleware promise is the opposite of a promise which will fail when there is a result * and which will succeed as long as there is no middleware that handled the context */ export type MiddlewarePromise = Promise>; export type MiddlewareResult = Error | T | OptionsResponse; /** * Defines a middleware interface that handles context and returns a result. * @template T - The context parameters the middleware handles. * @template TSpecialNextParam - The special next parameter type. */ export interface Middleware { /** * Handles the provided context and returns a middleware result. * @param ...context - The context parameters to process. * @returns The result of the middleware processing. */ handle(...context: T): MiddlewareResult; } /** * Defines an error middleware interface to handle errors in the context. * @template T - The context parameters the middleware handles. * @template U - The special next parameter type. */ export interface ErrorMiddleware { /** * Handles an error and the provided context. * @param error - The error or options response to handle. * @param ...context - The context parameters associated with the error. * @returns The result of error handling. */ handleError(error: MiddlewareResult, ...context: T): MiddlewareResult; } export interface MiddlewareAsync { handle(...context: T): Promise>; } export interface ErrorMiddlewareAsync { handleError(error: MiddlewareResult, ...context: T): Promise>; } export type AnyAsyncMiddleware = MiddlewareAsync | ErrorMiddlewareAsync ; export type AnySyncMiddleware = Middleware | ErrorMiddleware ; export type AnyMiddleware = AnyAsyncMiddleware | AnySyncMiddleware ; export type OptionsResponse = { allow: string[] }; export type MiddlewareError = { error: T }; export type MiddlewareSuccess = { success: T | Promise }; export type SpecialNextParam = 'break' | undefined; export const NotHandled = Promise.resolve(undefined); /** * Converts a function into a middleware handler. * @template T - The function's argument types. * @template U - The special next parameter type. * @param fn - The function to convert into a middleware. * @returns A middleware handler function. */ export function toMiddleware(fn: (...args: T) => unknown): Middleware['handle'] export function toMiddleware(fn: (...args: T) => Promise): MiddlewareAsync['handle'] export function toMiddleware(fn: (...args: T) => unknown): Middleware['handle'] | MiddlewareAsync['handle'] { return function (...args: T) { let result: unknown; try { result = fn.apply(this, args); } catch (e) { return e; } if (isPromiseLike(result)) return result.then(x => { throw x }, x => x); else throw result } } /** * Converts a function into a middleware object. * @template T - The function's argument types. * @template TSpecialNextParam - The special next parameter type. * @param fn - The function to convert into a middleware. * @returns A middleware object with appropriate handling methods. */ export function convertToMiddleware(fn: (...args: T) => Promise): MiddlewareAsync export function convertToMiddleware(fn: (...args: T) => TReturnType): Middleware export function convertToMiddleware(fn: (...args: T) => TReturnType): TReturnType extends Promise ? MiddlewareAsync : Middleware export function convertToMiddleware(fn: (...args: T) => unknown): MiddlewareAsync | Middleware { return { handle: toMiddleware(fn) } } /** * Converts a function into an error middleware object. * @template T - The context argument types. * @template TSpecialNextParam - The special next parameter type. * @param fn - The function to convert into an error middleware. * @returns An error middleware object with error handling capabilities. */ export function convertToErrorMiddleware(fn: (error: Exclude, TSpecialNextParam>, ...args: T) => Promise): ErrorMiddlewareAsync export function convertToErrorMiddleware(fn: (error: Exclude, TSpecialNextParam>, ...args: T) => unknown): ErrorMiddleware export function convertToErrorMiddleware(fn: (error: Exclude, TSpecialNextParam>, ...args: T) => unknown): ErrorMiddleware | ErrorMiddlewareAsync { return { handleError: toMiddleware<[Exclude, TSpecialNextParam>, ...T], TSpecialNextParam>(fn) } } /** * Checks if a value represents a middleware error. * @template TSpecialNextParam - The special next parameter type. * @param x - The value to check. * @returns True if the value is a middleware error, false otherwise. */ export function isMiddlewareError(x: MiddlewareError> | MiddlewareSuccess): x is MiddlewareError> { return typeof x['error'] != 'undefined'; } /** * Checks if a middleware is an error middleware. * @template T - The context argument types. * @template TSpecialNextParam - The special next parameter type. * @param middleware - The middleware to check. * @returns True if the middleware handles errors, false otherwise. */ export function isErrorMiddleware(middleware: unknown): middleware is ErrorMiddleware | ErrorMiddlewareAsync { return typeof middleware?.['handleError'] == 'function'; } /** * Checks if a middleware is a standard (non-error) middleware. * @template T - The context argument types. * @template TSpecialNextParam - The special next parameter type. * @param middleware - The middleware to check. * @returns True if the middleware is standard, false otherwise. */ export function isStandardMiddleware(middleware: unknown): middleware is Middleware | MiddlewareAsync { return typeof middleware?.['handle'] == 'function'; } /** * Processes a middleware with the provided context. * @template X - The expected result type. * @template T - The context parameters. * @template TSpecialNextParam - The special next parameter type. * @param middleware - The middleware to process. * @param ...req - The context parameters for processing. * @returns The processed result. */ export function process(middleware: Middleware | MiddlewareAsync, ...req: T): X { let error: MiddlewareResult | Promise>; try { error = middleware.handle(...req) } catch (result) { return result as X; } throw error; }