UNPKG

4.69 kBTypeScriptView Raw
1/**
2 * Universal Router (https://www.kriasoft.com/universal-router/)
3 *
4 * Copyright (c) 2015-present Kriasoft.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.txt file in the root directory of this source tree.
8 */
9import { Path, MatchFunction, ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions } from 'path-to-regexp';
10/**
11 * Params is a key/value object that represents extracted URL parameters.
12 */
13export interface RouteParams {
14 [paramName: string]: string | string[];
15}
16/**
17 * In addition to a URL path string, any arbitrary data can be passed to
18 * the `router.resolve()` method, that becomes available inside action functions.
19 */
20export interface RouterContext {
21 [propName: string]: any;
22}
23export interface ResolveContext extends RouterContext {
24 /**
25 * URL which was transmitted to `router.resolve()`.
26 */
27 pathname: string;
28}
29export interface RouteContext<R = any, C extends RouterContext = RouterContext> extends ResolveContext {
30 /**
31 * Current router instance.
32 */
33 router: UniversalRouter<R, C>;
34 /**
35 * Matched route object.
36 */
37 route: Route<R, C>;
38 /**
39 * Base URL path relative to the path of the current route.
40 */
41 baseUrl: string;
42 /**
43 * Matched path.
44 */
45 path: string;
46 /**
47 * Matched path params.
48 */
49 params: RouteParams;
50 /**
51 * Middleware style function which can continue resolving.
52 */
53 next: (resume?: boolean) => Promise<R>;
54}
55export declare type RouteResult<T> = T | Promise<T | null | undefined> | null | undefined;
56/**
57 * A Route is a singular route in your application. It contains a path, an
58 * action function, and optional children which are an array of Route.
59 * @template C User context that is made union with RouterContext.
60 * @template R Result that every action function resolves to.
61 * If the action returns a Promise, R can be the type the Promise resolves to.
62 */
63export interface Route<R = any, C extends RouterContext = RouterContext> {
64 /**
65 * A string, array of strings, or a regular expression. Defaults to an empty string.
66 */
67 path?: Path;
68 /**
69 * A unique string that can be used to generate the route URL.
70 */
71 name?: string;
72 /**
73 * The link to the parent route is automatically populated by the router. Useful for breadcrumbs.
74 */
75 parent?: Route<R, C> | null;
76 /**
77 * An array of Route objects. Nested routes are perfect to be used in middleware routes.
78 */
79 children?: Routes<R, C> | null;
80 /**
81 * Action method should return anything except `null` or `undefined` to be resolved by router
82 * otherwise router will throw `Page not found` error if all matched routes returned nothing.
83 */
84 action?: (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
85 /**
86 * The route path match function. Used for internal caching.
87 */
88 match?: MatchFunction<RouteParams>;
89}
90/**
91 * Routes is an array of type Route.
92 * @template C User context that is made union with RouterContext.
93 * @template R Result that every action function resolves to.
94 * If the action returns a Promise, R can be the type the Promise resolves to.
95 */
96export declare type Routes<R = any, C extends RouterContext = RouterContext> = Array<Route<R, C>>;
97export declare type ResolveRoute<R = any, C extends RouterContext = RouterContext> = (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
98export declare type RouteError = Error & {
99 status?: number;
100};
101export declare type ErrorHandler<R = any> = (error: RouteError, context: ResolveContext) => RouteResult<R>;
102export interface RouterOptions<R = any, C extends RouterContext = RouterContext> extends ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions {
103 context?: C;
104 baseUrl?: string;
105 resolveRoute?: ResolveRoute<R, C>;
106 errorHandler?: ErrorHandler<R>;
107}
108export interface RouteMatch<R = any, C extends RouterContext = RouterContext> {
109 route: Route<R, C>;
110 baseUrl: string;
111 path: string;
112 params: RouteParams;
113}
114declare class UniversalRouter<R = any, C extends RouterContext = RouterContext> {
115 root: Route<R, C>;
116 baseUrl: string;
117 options: RouterOptions<R, C>;
118 constructor(routes: Routes<R, C> | Route<R, C>, options?: RouterOptions<R, C>);
119 /**
120 * Traverses the list of routes in the order they are defined until it finds
121 * the first route that matches provided URL path string and whose action function
122 * returns anything other than `null` or `undefined`.
123 */
124 resolve(pathnameOrContext: string | ResolveContext): RouteResult<R>;
125}
126export default UniversalRouter;