UNPKG

4.52 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: UniversalRouterSync<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) => R;
54}
55export declare type RouteResultSync<T> = T | 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 */
62export interface Route<R = any, C extends RouterContext = RouterContext> {
63 /**
64 * A string, array of strings, or a regular expression. Defaults to an empty string.
65 */
66 path?: Path;
67 /**
68 * A unique string that can be used to generate the route URL.
69 */
70 name?: string;
71 /**
72 * The link to the parent route is automatically populated by the router. Useful for breadcrumbs.
73 */
74 parent?: Route<R, C> | null;
75 /**
76 * An array of Route objects. Nested routes are perfect to be used in middleware routes.
77 */
78 children?: Routes<R, C> | null;
79 /**
80 * Action method should return anything except `null` or `undefined` to be resolved by router
81 * otherwise router will throw `Page not found` error if all matched routes returned nothing.
82 */
83 action?: (context: RouteContext<R, C>, params: RouteParams) => RouteResultSync<R>;
84 /**
85 * The route path match function. Used for internal caching.
86 */
87 match?: MatchFunction<RouteParams>;
88}
89/**
90 * Routes is an array of type Route.
91 * @template C User context that is made union with RouterContext.
92 * @template R Result that every action function resolves to.
93 */
94export declare type Routes<R = any, C extends RouterContext = RouterContext> = Array<Route<R, C>>;
95export declare type ResolveRoute<R = any, C extends RouterContext = RouterContext> = (context: RouteContext<R, C>, params: RouteParams) => RouteResultSync<R>;
96export declare type RouteError = Error & {
97 status?: number;
98};
99export declare type ErrorHandler<R = any> = (error: RouteError, context: ResolveContext) => RouteResultSync<R>;
100export interface RouterOptions<R = any, C extends RouterContext = RouterContext> extends ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions {
101 context?: C;
102 baseUrl?: string;
103 resolveRoute?: ResolveRoute<R, C>;
104 errorHandler?: ErrorHandler<R>;
105}
106export interface RouteMatch<R = any, C extends RouterContext = RouterContext> {
107 route: Route<R, C>;
108 baseUrl: string;
109 path: string;
110 params: RouteParams;
111}
112declare class UniversalRouterSync<R = any, C extends RouterContext = RouterContext> {
113 root: Route<R, C>;
114 baseUrl: string;
115 options: RouterOptions<R, C>;
116 constructor(routes: Routes<R, C> | Route<R, C>, options?: RouterOptions<R, C>);
117 /**
118 * Traverses the list of routes in the order they are defined until it finds
119 * the first route that matches provided URL path string and whose action function
120 * returns anything other than `null` or `undefined`.
121 */
122 resolve(pathnameOrContext: string | ResolveContext): RouteResultSync<R>;
123}
124export default UniversalRouterSync;