UNPKG

route-path-match

Version:

compile route path template, match route path params

62 lines (52 loc) 1.53 kB
export interface RouteHandler { (req: any, res: any, ...args: any[]): void | Promise<void | RouteHandler[]>; } export type StaticResponse = { code?: Number; redirect?: string; headers?: { [key: string]: string; }; body?: string | any; }; export type RouteParams = { [key: string]: any; }; export type RouteOptions = { path?: "*" | string; method?: "*" | string; response?: string | StaticResponse; // handler会继承父路由指定的handler列表 handler?: RouteHandler | RouteHandler[]; params?: RouteParams; children?: RouteOptions[]; }; export type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "HEAD"; export type Route = { path: "*" | string | RegExp; method: "*" | HttpMethod[]; handler: RouteHandler[]; params?: RouteParams; }; export declare class RouteMatcher { routes: Route[]; constructor(routes: RouteOptions[], defaultRoute?: RouteOptions); matchRoute(path: string, method: string): Route | void; } export type HookHandler = ( req: any, res: any, ...args: any[] ) => void | Promise<void>; export type HookTasks = { onRoute(route: Route, req: any, res: any): void | Promise<void>; onStart: HookHandler; onEnd: HookHandler; onFinish: HookHandler; onError(err: any, req: any, res: any): void | Promise<void>; }; export declare class ExpressRouter extends RouteMatcher { toResponseHandler(StaticResponse): RouteHandler; isHeadersSent(req: any, res: any): boolean; handle(hookTasks: HookTasks, req: any, res: any): Promise<void>; }