import { Method, Request, Response } from "./types.js"; /** * A handler for a request */ export type Handler = (ctx: RouteContext, req: Request, res: Response) => Promise; export default class Router { private readonly trees; /** * register adds a new handler to the router for the given method and path * * @throws {Error} If a handler is already registered for the given method and path * @throws {Error} If a dynamic segment is registered without a name (i.e. `/users/:/comments` would result in an * error) */ register(method: Method, path: string, handler: Handler): void; private _register; /** * Find returns the handler for the given method and path or null if not found */ find(method: Method, path: string): RouteContext | null; /** * clean path ensures there is no leading slash */ private cleanPath; /** * debug returns a string representation of the router tree, * this is mainly used for unit test snapshots as a simplified * way to verify the tree is being built correctly. */ debug(): string; } export interface RouteContext { handler: Handler; path: string; paramNames: string[]; params: string[]; /** * If set then there was no exact match for the path, but we found a route that * matched if we where to either add or remove a trailing slash. This field would * then be set to the true path to use. * * i.e. if the user requested `/users` but the route is registered as `/users/`, then * we would return the `/users/` handler and set this field to `/users/`. * * The opposite is also true, if the user requested `/users/` but the route was reigstered * as `/users`, then we would return the `/users` handler and set this field to `/users`. */ tsfSuggestion?: string; } /** * createParamsObj returns an object with the param names as keys and the values extracted from the route request */ export declare function createParamsObj(ctx: RouteContext): Record; export declare function reconstructRegisteredURL(ctx: RouteContext): string;