rvx
Version:
A signal based rendering library
99 lines (98 loc) • 2.97 kB
TypeScript
import { Expression } from "../core/signals.js";
import { Component } from "../core/types.js";
import { View } from "../core/view.js";
export interface RouteMatchResult {
/**
* The normalized matched path.
*/
path: string;
/**
* The parameters extracted from the matched path.
*/
params?: unknown;
}
export interface RouteMatchFn {
/**
* Check if this route matches the specified path.
*
* @param path The path to match against.
* @returns The match result or undefined if the path doesn't match.
*/
(path: string): RouteMatchResult | undefined;
}
export interface Route {
/**
* The paths this route matches.
*/
match?: string | RegExp | RouteMatchFn;
}
export interface ParentRouteMatch<T extends Route> {
/**
* The route that has been matched.
*/
route: T;
/**
* The normalized matched path.
*/
path: string;
/**
* The parameters extracted from the matched path.
*/
params?: unknown;
}
export interface RouteMatch<T extends Route> extends ParentRouteMatch<T> {
/**
* The normalized remaining rest path.
*/
rest: string;
}
/**
* Find the first matching route.
*
* @param path The {@link normalize normalized} path to match against. Non normalized paths result in undefined behavior.
* @param routes The routes to test in order.
* @returns A match or undefined if none of the routes matched.
*/
export declare function matchRoute<T extends Route>(path: string, routes: Iterable<T>): RouteMatch<T> | undefined;
export interface WatchedRoutes<T extends Route> {
match: () => ParentRouteMatch<T> | undefined;
rest: () => string;
}
/**
* Watch and match routes against an expression.
*
* @param path The normalized path.
* @param routes The routes to watch.
* @returns An object with individually watchable route match and the unmatched rest path.
*/
export declare function watchRoutes<T extends Route>(path: Expression<string>, routes: Expression<Iterable<T>>): WatchedRoutes<T>;
/**
* Props passed to the root component of a {@link ComponentRoute}.
*/
export interface RouteProps<P = unknown> {
/** Matched route parameters. */
params: P;
}
/**
* A route where the content is a component to render.
*/
export interface ComponentRoute<P = unknown> extends Route {
content: Component<RouteProps<P>>;
}
/**
* Match and render routes in the current context.
*
* A {@link ChildRouter} is injected as a replacement for the current router when rendering route content.
*/
export declare function routes(routes: Expression<Iterable<ComponentRoute<any>>>): View;
/**
* Match and render routes in the current context.
*
* A {@link ChildRouter} is injected as a replacement for the current router when rendering route content.
*/
export declare function Routes(props: {
/**
* The routes to match.
*/
routes: Expression<Iterable<ComponentRoute<any>>>;
}): View;