1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | import { Path, MatchFunction, ParseOptions, TokensToRegexpOptions, RegexpToFunctionOptions } from 'path-to-regexp';
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export interface RouterContext {
|
15 | [propName: string]: any;
|
16 | }
|
17 | export interface ResolveContext extends RouterContext {
|
18 | |
19 |
|
20 |
|
21 | pathname: string;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 | export interface RouteParams {
|
27 | [paramName: string]: string | string[];
|
28 | }
|
29 | export type RouteResult<T> = T | null | undefined | Promise<T | null | undefined>;
|
30 | export interface RouteContext<R = any, C extends RouterContext = RouterContext> extends ResolveContext {
|
31 | |
32 |
|
33 |
|
34 | router: UniversalRouter<R, C>;
|
35 | |
36 |
|
37 |
|
38 | route: Route<R, C>;
|
39 | |
40 |
|
41 |
|
42 | baseUrl: string;
|
43 | |
44 |
|
45 |
|
46 | path: string;
|
47 | |
48 |
|
49 |
|
50 | params: RouteParams;
|
51 | |
52 |
|
53 |
|
54 | next: (resume?: boolean) => Promise<R>;
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | export interface Route<R = any, C extends RouterContext = RouterContext> {
|
64 | |
65 |
|
66 |
|
67 | path?: Path;
|
68 | |
69 |
|
70 |
|
71 | name?: string;
|
72 | |
73 |
|
74 |
|
75 | parent?: Route<R, C> | null;
|
76 | |
77 |
|
78 |
|
79 | children?: Routes<R, C> | null;
|
80 | |
81 |
|
82 |
|
83 |
|
84 | action?: (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
|
85 | |
86 |
|
87 |
|
88 | match?: MatchFunction<RouteParams>;
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | export type Routes<R = any, C extends RouterContext = RouterContext> = Array<Route<R, C>>;
|
97 | export type ResolveRoute<R = any, C extends RouterContext = RouterContext> = (context: RouteContext<R, C>, params: RouteParams) => RouteResult<R>;
|
98 | export type RouteError = Error & {
|
99 | status?: number;
|
100 | };
|
101 | export type ErrorHandler<R = any> = (error: RouteError, context: ResolveContext) => RouteResult<R>;
|
102 | export 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 | }
|
108 | export interface RouteMatch<R = any, C extends RouterContext = RouterContext> {
|
109 | route: Route<R, C>;
|
110 | baseUrl: string;
|
111 | path: string;
|
112 | params: RouteParams;
|
113 | }
|
114 | declare 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 | }
|
126 | export default UniversalRouter;
|