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