UNPKG

4.66 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 * In addition to a URL path string, any arbitrary data can be passed to
12 * the `router.resolve()` method, that becomes available inside action functions.
13 */
14export interface RouterContext {
15 [propName: string]: any;
16}
17export interface ResolveContext extends RouterContext {
18 /**
19 * URL which was transmitted to `router.resolve()`.
20 */
21 pathname: string;
22}
23/**
24 * Params is a key/value object that represents extracted URL parameters.
25 */
26export interface RouteParams {
27 [paramName: string]: string | string[];
28}
29export type RouteResult<T> = T | null | undefined | Promise<T | null | undefined>;
30export interface RouteContext<R = any, C extends RouterContext = RouterContext> extends ResolveContext {
31 /**
32 * Current router instance.
33 */
34 router: UniversalRouter<R, C>;
35 /**
36 * Matched route object.
37 */
38 route: Route<R, C>;
39 /**
40 * Base URL path relative to the path of the current route.
41 */
42 baseUrl: string;
43 /**
44 * Matched path.
45 */
46 path: string;
47 /**
48 * Matched path params.
49 */
50 params: RouteParams;
51 /**
52 * Middleware style function which can continue resolving.
53 */
54 next: (resume?: boolean) => Promise<R>;
55}
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 type Routes<R = any, C extends RouterContext = RouterContext> = Array<Route<R, C>>;
97export type ResolveRoute<R = any, C extends RouterContext = RouterContext> = (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
98export type RouteError = Error & {
99 status?: number;
100};
101export 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): Promise<RouteResult<R>>;
125}
126export default UniversalRouter;