@thi.ng/router
Version:
Generic trie-based router with support for wildcards, route param validation/coercion, auth
46 lines • 2.2 kB
TypeScript
import type { Event, INotify, IObjectOf, Listener, Maybe, SomeRequired } from "@thi.ng/api";
import { type AugmentedRoute, type Route, type RouteMatch, type RouteParamValidator, type RouterEventType, type RouterOpts } from "./api.js";
import { Trie } from "./trie.js";
export declare class Router<T = any> implements INotify<RouterEventType> {
opts: RouterOpts<T>;
current: Maybe<RouteMatch>;
protected index: Record<string, AugmentedRoute>;
protected routes: Trie<AugmentedRoute>;
constructor(config: RouterOpts<T>);
addListener(id: RouterEventType, fn: Listener<RouterEventType>, scope?: any): boolean;
removeListener(id: RouterEventType, fn: Listener<RouterEventType>, scope?: any): boolean;
notify(event: Event<RouterEventType>): boolean;
start(): void;
addRoutes(routes: Route[]): void;
/**
* Main router function. Attempts to match given input string against all
* configured routes. Before returning, triggers {@link EVENT_ROUTE_CHANGED}
* with return value as well. If none of the routes matches, emits
* {@link EVENT_ROUTE_FAILED} and then falls back to configured default
* route.
*
* @remarks
* See {@link RouteAuthenticator} for details about `ctx` handling.
*
* @param src - route path to match
* @param ctx - arbitrary user context
*/
route(src: string, ctx?: T): Maybe<RouteMatch>;
/**
* Returns a formatted version of given {@link RouteMatch}, incl. any
* params, or alternatively a registered route ID (and optional route
* params). Throws an error if an invalid route `id` is provided.
*
* @param id -
* @param params -
* @param rest -
*/
format(id: string, params?: any, rest?: string[]): string;
format(match: SomeRequired<RouteMatch, "id">): string;
routeForID(id: string): Maybe<AugmentedRoute>;
protected augmentRoute(route: Route): AugmentedRoute;
protected matchRoutes(src: string, ctx?: T): Maybe<RouteMatch>;
protected validateRouteParams(params: any, validators: IObjectOf<Partial<RouteParamValidator>>): boolean;
protected handleRouteFailure(): boolean;
}
//# sourceMappingURL=router.d.ts.map