UNPKG

@stacksjs/router

Version:
38 lines (37 loc) 1.15 kB
import type { EnhancedRequest } from '@stacksjs/bun-router'; export declare interface MiddlewareConfig { name: string priority?: number handle: (request: EnhancedRequest) => void | Promise<void> } /** * Middleware class for defining route middleware * * Provides a simple, structured way to define middleware handlers * that can be attached to routes and route groups. * * The request object is an EnhancedRequest with helper methods like * `bearerToken()`, `get()`, `input()`, `has()`, etc. * * @example * ```ts * import { Middleware } from '@stacksjs/router' * * export default new Middleware({ * name: 'Auth', * priority: 1, * async handle(request) { * const token = request.bearerToken() * if (!token) throw new HttpError(401, 'Unauthorized') * }, * }) * ``` */ export type Request = EnhancedRequest; export declare class Middleware { readonly name: string; readonly priority: number; readonly handle: (request: EnhancedRequest) => void | Promise<void>; constructor(config: MiddlewareConfig); toRouterHandler(): (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>; }