import { type SpecialNextParam, convertToMiddleware } from '../middlewares/shared.js'; import { MiddlewareCompositeAsync } from '../middlewares/composite-async.js'; import { MiddlewareRouteAsync } from './route-async.js'; import { each } from '../each.js'; import type { RouterOptions, Routes } from './router.js'; import type { Routable, RouteBuilderArguments } from './route.js'; import type { MiddlewareAsync } from '../middlewares/shared.js' import type { UriTemplate } from '../uri-template/index.js'; export type RouteBuilderAsync = (...args: RouteBuilderArguments) => MiddlewareRouteAsync; /** * Dynamically applies asynchronous routes to a middleware composite. * * This function recursively processes route definitions and attaches them to the parent middleware chain. * * @template T - The context type containing the routable request and parameters. * @template TSpecialNextParam - The type for special next parameters (defaults to {@link SpecialNextParam}) * @param {Routes, TSpecialNextParam>} routes - Route definitions mapping URI templates to handler functions or nested route objects * @param {MiddlewareCompositeAsync & { route: RouteBuilderAsync }} [parent] - Parent middleware container to attach routes to * @returns {MiddlewareCompositeAsync} Configured middleware composite with attached routes */ export function useRoutesAsync( routes: Routes, TSpecialNextParam>, parent?: MiddlewareCompositeAsync & { route: RouteBuilderAsync } ): MiddlewareCompositeAsync { if (!parent) parent = Object.assign(new MiddlewareCompositeAsync('byroutes'), { route(...args: RouteBuilderArguments) { return new MiddlewareRouteAsync(...args) } }); each(routes, (route: ((...args: T) => Promise) | Routes, TSpecialNextParam>, match) => { if (typeof match == 'number') return; const routed = new MiddlewareRouteAsync(match as string); if (typeof (route) == 'object') { useRoutesAsync(route, routed); } else routed.useMiddleware(convertToMiddleware(route)); parent.useMiddleware(routed); }); return parent; } /** * Base class for defining asynchronous routing middleware chains. * * Extends the middleware composite to provide route configuration capabilities for asynchronous handlers. * * @template T - The context type containing the routable request and parameters * @template TSpecialNextParam - The type for special next parameters (defaults to {@link SpecialNextParam}) * @extends {MiddlewareCompositeAsync} * @implements {MiddlewareAsync} */ export class RouterAsync< T extends [{ path: string; params?: Record }, ...unknown[]], TSpecialNextParam extends SpecialNextParam = SpecialNextParam > extends MiddlewareCompositeAsync implements MiddlewareAsync { /** * Creates a new RouterAsync instance with optional configuration. * * @param {RouterOptions} [options] - Configuration options including middleware priority and name */ constructor(options?: RouterOptions) { super(options?.name); } /** * Creates a new route configuration chain for defining route handlers. * * @function route * @param {...RouteBuilderArguments} args - Route configuration parameters (path template, HTTP method, etc.) * @returns {MiddlewareRouteAsync} Newly created route configuration instance */ public route(...args: RouteBuilderArguments): MiddlewareRouteAsync { return new MiddlewareRouteAsync(...args); } /** * Attaches a collection of route definitions to the router. * * @param {Routes, TSpecialNextParam>} routes - Route definitions to be applied * @returns {this} Current router instance for method chaining */ public useRoutes(routes: Routes, TSpecialNextParam>): this { useRoutesAsync(routes, this); return this; } /** * Adds middleware to the router's chain either globally or for a specific route. * * @function useMiddleware * @param {string | UriTemplate | MiddlewareAsync} routeOrMiddleware - * Route definition (string/URI template) or middleware function * @param {...MiddlewareAsync} middlewares - Additional middleware functions * @returns {this} Current router instance for method chaining */ public useMiddleware(...middlewares: MiddlewareAsync[]): this; public useMiddleware( routeOrMiddleware: string | UriTemplate | MiddlewareAsync, ...middlewares: MiddlewareAsync[] ): this; public useMiddleware( routeOrMiddleware: string | UriTemplate | MiddlewareAsync, ...middlewares: MiddlewareAsync[] ): this { if (typeof routeOrMiddleware === 'string' || Array.isArray(routeOrMiddleware)) { const routed = new MiddlewareRouteAsync(routeOrMiddleware); routed.useMiddleware(...middlewares); super.useMiddleware(routed); } else super.useMiddleware(routeOrMiddleware, ...middlewares); return this; } /** * Registers route handlers or middleware functions. * * @function use * @param {(string | UriTemplate | ((...args: T) => Promise))} routeOrHandler - * Route definition (string/URI template) or handler function * @param {...((...args: T) => Promise)} handlers - Additional handler functions * @returns {this} Current router instance for method chaining */ public use(routeOrHandler: string | UriTemplate, ...handlers: ((...args: T) => Promise)[]): this; public use(...handlers: ((...args: T) => Promise)[]): this; public use(routeOrHandler: string | UriTemplate | ((...args: T) => Promise), ...handlers: ((...args: T) => Promise)[]): this { if (typeof routeOrHandler === 'string' || Array.isArray(routeOrHandler)) { const routed = new MiddlewareRouteAsync(routeOrHandler); routed.use(...handlers); super.useMiddleware(routed); return this; } else return super.use(routeOrHandler, ...handlers); } } // // create Router#VERB functions // methods.concat('all').forEach(function (method) // { // Router.prototype[method] = function (path) // { // var route = this.route(path) // route[method].apply(route, slice.call(arguments, 1)) // return this // } // })