/** * FURI - Fast Uniform Resource Identifier. * * The Fast and Furious Node.js Router. * Copyright(c) 2016, 2025 Rajinder Yadav. * * Labs DevMentor.org Corp. * This code is released as-is without warranty under the "GNU GENERAL PUBLIC LICENSE". */ import { IncomingMessage, ServerResponse } from 'node:http'; import { HttpRequest, HttpResponse, HandlerFunction, RouteMap } from './types.js'; import { ApplicationContext } from './application-context.js'; /** * The FuriRouter class is responsible for two things: * * 1. The creation of the route table. * * 2. Routing HTTP requests to middlewares and the appropriate * request handler based on the URI and method. * * In the process it will parse segment names for named routes. */ export declare class FuriRouter { protected readonly httpMethodMap: RouteMap[]; constructor(); /** * Assign a middleware to the provided URI lookup map. * There are two overloaded functions: * 1. Application level middleware registration. * use(...fn: RequestCallback[]): FuriRouter; * 2. Route level middleware registration. * use(uri: string, ...fn: RequestCallback[]): FuriRouter; * * When called without a path, the middleware is added to application level middleware. * When called with a path, the middleware is added to the route level. * * Middlewares without a path will be called in order of registration, * before other all routes, irrespective of their path. Otherwise the * middleware will be called in the order of registration for each route. * * @param uri Optional String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ use(router: FuriRouter): FuriRouter; use(uri: string, router: FuriRouter): FuriRouter; use(...fn: HandlerFunction[]): FuriRouter; use(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign Request handler to all HTTP lookup maps. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ all(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign a HTTP GET handler to the provided URI lookup map. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ get(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign a HTTP PATCH handler to the provided URI lookup map. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ patch(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign a HTTP POST handler to the provided URI lookup map. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ post(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign a HTTP PUT handler to the provided URI lookup map. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ put(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Assign a HTTP DELETE handler to the provided URI lookup map. * * @param uri String value of URI. * @param fn Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ delete(uri: string, ...fn: HandlerFunction[]): FuriRouter; /** * Node requires a handler function for incoming HTTP request. * This handler function is usually passed to createServer(). * * @returns Reference to request handler function. */ protected handler(): (incomingMessage: IncomingMessage, response: ServerResponse) => void; /** * Dispatches incoming HTTP requests to the appropriate handler function. * * @param request HTTP request. * @param response HTTP response. * @returns void. */ dispatch(incomingMessage: IncomingMessage, response: ServerResponse): void; /** * Convert named segments path to a RegEx key and collect segment names. * * URI => /aa/:one/bb/cc/:two/e * KEY => /aa/(\w+)/bb/cc/(\w+)/e * params => ['one', 'two'] * return => { params: ['one', 'two'], key: '/aa/(\w+)/bb/cc/(\w+)/e' } * * @param uri URI with segment names. * @return Object with regex key and array with param names. */ protected createNamedRouteSearchKey(tokens: string[]): { params: string[]; key: string; }; /** * Match URI with named segments and return param object containing * the property of each named segment and its value on the request object. * * @param uri Path URI to be matched. * @param pk Path object with RegEx key and segments. * @return null If URI doesn't match Path Object. * @return param Object containing property and its value for each segment from Path object. */ protected attachPathParamsToRequestIfExists(uri: string, pk: { params: string[]; key: string; }, request: HttpRequest): boolean; /** * Build HTTP Request handler mappings and assign callback function * * @param mapIndex The URI Map used to look up callbacks. * @param uri String value of URI. * @param callbacks Reference to callback functions of type RequestHandlerFunc. * @returns Reference to self, allows method chaining. */ protected buildRequestMap(mapIndex: number, uri: string, callbacks: HandlerFunction[]): FuriRouter; /** * Execute all top level middlewares. * @param applicationContext Application context object. */ protected callTopLevelMiddlewares(applicationContext: ApplicationContext): void; /** * Check if each path token matches its ordinal key values, * named path segments always match and are saved to request.params. * * @param pathNames Array of path segments. * @param keyNames Array of key names. * @param request HttpRequest object. * @returns boolean True if all tokens match, otherwise false. */ protected fastPathMatch(pathNames: string[], keyNames: string[], request: HttpRequest): boolean; /** * This method calls the callbacks for the mapped URL if it exists. * If one does not exist a HTTP status error code is returned. * * @param mapIndex The URI Map used to look up callbacks. * @param request Reference to Node request object (IncomingMessage). * @param response Reference to Node response object (ServerResponse). */ protected processHTTPMethod(mapIndex: number, request: HttpRequest, response: HttpResponse, throwOnNotFound?: boolean): void; /** * Merge given router maps into existing top-level router map. * This will occur when the caller adds a route-less router middleware. * * @param routeMap UriMap[] to merge into the current httpMaps. * @return void */ protected mergeRouterMaps(routeMap: RouteMap[]): void; } //# sourceMappingURL=furi-router.d.ts.map