/opt/canhelp/node_modules/rou3/dist
Edit: /opt/canhelp/node_modules/rou3/dist/compiler.d.mts (2255B)
interface RouterContext
{
root: Node;
static: Record | undefined>;
}
type ParamsIndexMap = Array<[Index: number, name: string | RegExp, optional: boolean]>;
type MethodData = {
data: T;
paramsMap?: ParamsIndexMap;
paramsRegexp: RegExp[];
};
interface Node {
key: string;
static?: Record>;
param?: Node;
wildcard?: Node;
hasRegexParam?: boolean;
methods?: Record[] | undefined>;
}
type MatchedRoute = {
data: T;
params?: Record;
};
interface RouterCompilerOptions {
matchAll?: boolean;
serialize?: (data: T) => string;
}
/**
* 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 = RouterCompilerOptions>(router: RouterContext, opts?: O): (method: string, path: string) => O["matchAll"] extends true ? MatchedRoute[] : MatchedRoute | 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 or you can pass custom `serialize` function in options.
*
* @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;
export { RouterCompilerOptions, compileRouter, compileRouterToString };