@riogz/router
Version:
A simple, lightweight, powerful, view-agnostic, modular and extensible router
97 lines (96 loc) • 3.39 kB
TypeScript
import { Path, URLParamsEncodingType } from '../path-parser';
import { IOptions as QueryParamsOptions } from '../search-params';
export interface RouteDefinition {
name: string;
path: string;
[key: string]: any;
}
export type Route = RouteNode | RouteDefinition;
export type Callback = (...args: any[]) => void;
export type TrailingSlashMode = 'default' | 'never' | 'always';
export type QueryParamsMode = 'default' | 'strict' | 'loose';
export interface BuildOptions {
trailingSlashMode?: TrailingSlashMode;
queryParamsMode?: QueryParamsMode;
queryParams?: QueryParamsOptions;
urlParamsEncoding?: URLParamsEncodingType;
}
export interface MatchOptions {
caseSensitive?: boolean;
trailingSlashMode?: TrailingSlashMode;
queryParamsMode?: QueryParamsMode;
queryParams?: QueryParamsOptions;
strictTrailingSlash?: boolean;
strongMatching?: boolean;
urlParamsEncoding?: URLParamsEncodingType;
}
export type { QueryParamsOptions };
export interface MatchResponse {
segments: RouteNode[];
params: Record<string, any>;
}
export interface RouteNodeStateMeta {
[routeName: string]: {
[routeParams: string]: 'query' | 'url';
};
}
export interface RouteNodeState {
name: string;
params: Record<string, any>;
meta: RouteNodeStateMeta;
}
export interface RouteNodeOptions {
finalSort?: boolean;
onAdd?: Callback;
parent?: RouteNode;
sort?: boolean;
}
export declare class RouteNode {
name: string;
absolute: boolean;
path: string;
parser: Path | null;
children: RouteNode[];
parent?: RouteNode;
constructor(name?: string, path?: string, childRoutes?: Route[], options?: RouteNodeOptions);
getParentSegments(segments?: RouteNode[]): RouteNode[];
setParent(parent: RouteNode): void;
setPath(path?: string): void;
add(route: Route | Route[], cb?: Callback, sort?: boolean): this;
addNode(name: string, path: string): this;
/**
* Removes a direct child RouteNode by its name.
* If the name is a composite (e.g., 'parent.child'), it will attempt to remove 'parent' from this node's children.
*
* @param name The name of the child node to remove.
* @returns True if the child node was found and removed, false otherwise.
*/
removeNode(name: string): boolean;
getPath(routeName: string): string | null;
getNonAbsoluteChildren(): RouteNode[];
sortChildren(): void;
sortDescendants(): void;
/**
* Creates a deep clone of this RouteNode and all its children.
* The cloned node will have the same structure but be completely independent.
*
* @returns A new RouteNode instance that is a deep copy of this node
*/
clone(): RouteNode;
/**
* Helper method to recursively convert a RouteNode to a RouteDefinition
* @private
*/
private convertNodeToDefinition;
buildPath(routeName: string, params?: Record<string, any>, options?: BuildOptions): string;
buildState(name: string, params?: Record<string, any>): RouteNodeState | null;
matchPath(path: string, options?: MatchOptions): RouteNodeState | null;
private _updateWith;
private addRouteNode;
private checkParents;
private hasParentsParams;
private findAbsoluteChildren;
private findSlashChild;
private getSegmentsByName;
private getSegmentsMatchingPath;
}