alepha
Version:
Easy-to-use modern TypeScript framework for building many kind of applications.
105 lines • 3.52 kB
TypeScript
//#region ../../src/router/providers/RouterProvider.d.ts
declare abstract class RouterProvider<T extends Route = Route> {
protected routePathRegex: RegExp;
protected tree: Tree<T>;
protected cache: Map<string, RouteMatch<T>>;
protected maxCacheSize: number;
match(path: string): RouteMatch<T>;
protected test(path: string): void;
protected push(route: T): void;
protected createRouteMatch(path: string): RouteMatch<T>;
protected mapParams(match: RouteMatch<T>): RouteMatch<T>;
protected createParts(path: string): string[];
}
interface RouteMatch<T extends Route> {
route?: T;
params?: Record<string, string>;
}
interface Route {
path: string;
/**
* Rename a param in the route.
* This is automatically filled when you have scenarios like:
* `/customers/:id` and `/customers/:userId/payments`
*
* In this case, `:id` will be renamed to `:userId` in the second route.
*/
mapParams?: Record<string, string>;
}
interface Tree<T extends Route> {
route?: T;
children: {
[key: string]: Tree<T>;
};
param?: {
route?: T;
name: string;
children: {
[key: string]: Tree<T>;
};
};
wildcard?: {
route: T;
};
}
//#endregion
//#region ../../src/router/TemplatedPathParser.d.ts
/**
* Parses and manipulates templated paths with `{param}` placeholders.
*
* Used by both RouterProvider (HTTP routes) and TopicProvider (pub/sub topics)
* to handle parameterized path templates in a unified way.
*
* @example
* ```ts
* const parser = new TemplatedPathParser("/users/{userId}/posts/{postId}");
* parser.interpolate({ userId: "7", postId: "42" }); // "/users/7/posts/42"
* parser.extract("/users/7/posts/42"); // { userId: "7", postId: "42" }
* parser.wildcardize("+"); // "/users/+/posts/+"
* ```
*
* @example
* ```ts
* // Redis-style colon-separated keys
* const parser = new TemplatedPathParser("cache:{namespace}:{key}", ":");
* parser.interpolate({ namespace: "users", key: "42" }); // "cache:users:42"
* parser.wildcardize("*"); // "cache:*:*"
* ```
*/
declare class TemplatedPathParser {
protected static readonly PARAM_REGEX: RegExp;
readonly template: string;
readonly separator: string;
readonly paramNames: readonly string[];
readonly hasParams: boolean;
protected readonly extractRegex: RegExp | null;
constructor(template: string, separator?: string);
/**
* Replaces each `{param}` in the template with the corresponding value
* from the provided params record.
*/
interpolate(params: Record<string, string>): string;
/**
* Extracts parameter values from a concrete path by matching it against
* the template structure.
*
* Returns `null` when the path does not match the template.
* Returns `{}` when the template has no parameters and the path matches.
*/
extract(path: string): Record<string, string> | null;
/**
* Replaces each `{param}` placeholder in the template with the given
* wildcard string. Defaults to `"+"` (MQTT-style).
*/
wildcardize(wildcard?: string): string;
/**
* Normalises a path by collapsing repeated separators and stripping a
* trailing separator (unless the path is just the separator itself).
*/
normalize(path: string): string;
protected buildExtractRegex(): RegExp;
protected escapeRegex(s: string): string;
}
//#endregion
export { Route, RouteMatch, RouterProvider, TemplatedPathParser, Tree };
//# sourceMappingURL=index.d.ts.map