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 declare const NotHandled: Promise; /** * 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 declare function toMiddleware(fn: (...args: T) => unknown): Middleware['handle']; export declare function toMiddleware(fn: (...args: T) => Promise): MiddlewareAsync['handle']; /** * 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 declare function convertToMiddleware(fn: (...args: T) => Promise): MiddlewareAsync; export declare function convertToMiddleware(fn: (...args: T) => TReturnType): Middleware; export declare function convertToMiddleware(fn: (...args: T) => TReturnType): TReturnType extends Promise ? MiddlewareAsync : Middleware; /** * 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 declare function convertToErrorMiddleware(fn: (error: Exclude, TSpecialNextParam>, ...args: T) => Promise): ErrorMiddlewareAsync; export declare function convertToErrorMiddleware(fn: (error: Exclude, TSpecialNextParam>, ...args: T) => unknown): ErrorMiddleware; /** * 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 declare function isMiddlewareError(x: MiddlewareError> | MiddlewareSuccess): x is MiddlewareError>; /** * 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 declare function isErrorMiddleware(middleware: unknown): middleware is ErrorMiddleware | ErrorMiddlewareAsync; /** * 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 declare function isStandardMiddleware(middleware: unknown): middleware is Middleware | MiddlewareAsync; /** * 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 declare function process(middleware: Middleware | MiddlewareAsync, ...req: T): X;