rou3
Version:
Lightweight and fast router for JavaScript.
59 lines • 2.18 kB
text/typescript
//#region src/types.d.ts
interface RouterContext<T = unknown> {
root: Node<T>;
static: Record<string, Node<T> | undefined>;
}
type ParamsIndexMap = Array<[Index: number, name: string | RegExp, optional: boolean]>;
type MethodData<T = unknown> = {
data: T;
paramsMap?: ParamsIndexMap;
};
interface Node<T = unknown> {
key: string;
static?: Record<string, Node<T>>;
param?: Node<T>;
wildcard?: Node<T>;
methods?: Record<string, MethodData<T>[] | undefined>;
}
type MatchedRoute<T = unknown> = {
data: T;
params?: Record<string, string>;
};
//#endregion
//#region src/compiler.d.ts
interface RouterCompilerOptions {
matchAll?: boolean;
}
/**
* Compiles the router instance into a faster route-matching function.
*
* **IMPORTANT:** `compileRouter` requires eval support with `new Function()` in the runtime for JIT compilation.
*
* @example
* import { createRouter, addRoute } from "rou3";
* import { compileRouter } from "rou3/compiler";
* const router = createRouter();
* // [add some routes]
* const findRoute = compileRouter(router);
* const matchAll = compileRouter(router, { matchAll: true });
* findRoute("GET", "/path/foo/bar");
*
* @param router - The router context to compile.
*/
declare function compileRouter<T, O extends RouterCompilerOptions = RouterCompilerOptions>(router: RouterContext<T>, opts?: O): (method: string, path: string) => O["matchAll"] extends true ? MatchedRoute<T>[] : MatchedRoute<T> | undefined;
/**
* Compile the router instance into a compact runnable code.
*
* **IMPORTANT:** Route data must be serializable to JSON (i.e., no functions or classes) or implement the `toJSON()` method to render custom code.
*
* @example
* import { createRouter, addRoute } from "rou3";
* import { compileRouterToString } from "rou3/compiler";
* const router = createRouter();
* // [add some routes with serializable data]
* const compilerCode = compileRouterToString(router, "findRoute");
* // "const findRoute=(m, p) => {}"
*/
declare function compileRouterToString(router: RouterContext, functionName?: string, opts?: RouterCompilerOptions): string;
//#endregion
export { RouterCompilerOptions, compileRouter, compileRouterToString };