UNPKG

file-routing-expressjs

Version:

A dependency-free, flexible, system-based file routing for Express.

94 lines (91 loc) 2.61 kB
import { RequestHandler, ErrorRequestHandler } from 'express'; type ExpressMethod = "get" | "post" | "delete" | "put" | "patch" | "all"; type PartialExpressMethodRecord<T> = Partial<Record<ExpressMethod, T>>; /** * Route configuration (applies to both groups and endpoints) */ type RoutePlugins = Record<string, boolean | { enabled?: boolean; config?: any; }>; type RouteConfig = Partial<{ pattern: RegExp | string | PartialExpressMethodRecord<RegExp | string>; plugins?: PartialExpressMethodRecord<RoutePlugins>; }>; /** * Group-level configuration (applies to nested routes) */ type RouteGroupConfig = Partial<{ pattern: RegExp | string; }>; /** * Base route entry (shared structure) */ type RouteEntryConfig = { route: string; name: string; basename: string; target: string; isParam: boolean; }; /** * Group entry (represents a route group / folder) */ type RouteGroupEntryConfig = RouteEntryConfig & { parent: string; }; /** * Route mapping input */ type RouteMappingOptions = { target: string; route: string; parentGroup?: { route: string; }; }; /** * Express handlers map per method */ type RouteHandlersMap = Record<ExpressMethod, RequestHandler | undefined>; /** * Error handler types */ type RouteErrorHandler = ErrorRequestHandler; type RouteErrorMap = RouteErrorHandler | PartialExpressMethodRecord<RouteErrorHandler | undefined> | undefined; /** * Middleware types */ type RouteGroupMiddleware = RequestHandler | RequestHandler[]; type RouteMiddleware = RouteGroupMiddleware | PartialExpressMethodRecord<RouteGroupMiddleware>; /** * Context for a route group (folder-level) */ type RouteGroupContext = { config: RouteConfig; middlewares: RequestHandler[]; errorHandler?: RouteErrorHandler; }; /** * Context for a route definition (endpoint-level) */ type RouteDefinitionContext = { config: RouteConfig; middlewares: RouteMiddleware; errorHandler?: RouteErrorMap; }; /** * Debug / introspection types */ type RouteEndpoint = { depth: number; name: string; endpoint: string; method: string; middlewares: string[]; errorHandler: string; plugins: string[]; children: RouteEndpoint[]; }; type RouteEndpoints = RouteEndpoint[]; export type { ExpressMethod, PartialExpressMethodRecord, RouteConfig, RouteDefinitionContext, RouteEndpoint, RouteEndpoints, RouteEntryConfig, RouteErrorHandler, RouteErrorMap, RouteGroupConfig, RouteGroupContext, RouteGroupEntryConfig, RouteGroupMiddleware, RouteHandlersMap, RouteMappingOptions, RouteMiddleware, RoutePlugins };