-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathtypes.ts
More file actions
183 lines (154 loc) · 4.13 KB
/
types.ts
File metadata and controls
183 lines (154 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { Component, JSX } from "solid-js";
export type Params = Record<string, string>;
export type SetParams = Record<string, string | number | boolean | null | undefined>;
export interface Path {
pathname: string;
search: string;
hash: string;
}
export interface Location<S = unknown> extends Path {
query: Params;
state: Readonly<Partial<S>> | null;
key: string;
}
export interface NavigateOptions<S = unknown> {
resolve: boolean;
replace: boolean;
scroll: boolean;
state: S;
}
export interface Navigator {
(to: string, options?: Partial<NavigateOptions>): void;
(delta: number): void;
}
export type NavigatorFactory = (route?: RouteContext) => Navigator;
export interface LocationChange<S = unknown> {
value: string;
replace?: boolean;
scroll?: boolean;
state?: S;
}
export type LocationChangeSignal = [() => LocationChange, (next: LocationChange) => void];
export interface RouterIntegration {
signal: LocationChangeSignal;
utils?: Partial<RouterUtils>;
}
export interface RouteDataFuncArgs<T = unknown> {
data: T extends RouteDataFunc<infer _, infer R> ? R : T;
params: Params;
location: Location;
navigate: Navigator;
}
export type RouteDataFunc<T = unknown, R = unknown> = (args: RouteDataFuncArgs<T>) => R;
export type RouteDefinition<S extends string | string[] = any> = {
path: S;
matchFilters?: MatchFilters<S>;
data?: RouteDataFunc;
children?: RouteDefinition | RouteDefinition[];
} & (
| {
element?: never;
component: Component;
}
| {
component?: never;
element?: JSX.Element;
preload?: () => void;
}
);
export type MatchFilter = string[] | RegExp | ((s: string) => boolean);
export type PathParams<P extends string | readonly string[]> =
P extends `${infer Head}/${infer Tail}`
? [...PathParams<Head>, ...PathParams<Tail>]
: P extends `:${infer S}?`
? [S]
: P extends `:${infer S}`
? [S]
: P extends `*${infer S}`
? [S]
: [];
export type MatchFilters<P extends string | readonly string[] = any> = P extends string
? { [K in PathParams<P>[number]]?: MatchFilter }
: Record<string, MatchFilter>;
export interface PathMatch {
params: Params;
path: string;
}
export interface RouteMatch extends PathMatch {
route: Route;
}
export interface OutputMatch {
originalPath: string;
pattern: string;
path: string;
params: Params;
}
export interface Route {
key: unknown;
originalPath: string;
pattern: string;
element: () => JSX.Element;
preload?: () => void;
data?: RouteDataFunc;
matcher: (location: string) => PathMatch | null;
matchFilters?: MatchFilters;
}
export interface Branch {
routes: Route[];
score: number;
matcher: (location: string) => RouteMatch[] | null;
}
export interface RouteContext {
parent?: RouteContext;
child?: RouteContext;
data?: unknown;
pattern: string;
params: Params;
path: () => string;
outlet: () => JSX.Element;
resolvePath(to: string): string | undefined;
}
export interface RouterUtils {
/** This produces the `href` attribute shown in the browser. */
renderPath(path: string): string;
parsePath(str: string): string;
go(delta: number): void;
beforeLeave: BeforeLeaveLifecycle;
}
export interface OutputMatch {
originalPath: string;
pattern: string;
path: string;
params: Params;
}
export interface RouterOutput {
url?: string;
matches: OutputMatch[][];
}
export interface RouterContext {
base: RouteContext;
out?: RouterOutput;
location: Location;
navigatorFactory: NavigatorFactory;
isRouting: () => boolean;
renderPath(path: string): string;
parsePath(str: string): string;
beforeLeave: BeforeLeaveLifecycle;
}
export interface BeforeLeaveEventArgs {
from: Location;
to: string | number;
options?: Partial<NavigateOptions>;
readonly defaultPrevented: boolean;
preventDefault(): void;
retry(force?: boolean): void;
}
export interface BeforeLeaveListener {
listener(e: BeforeLeaveEventArgs): void;
location: Location;
navigate: Navigator;
}
export interface BeforeLeaveLifecycle {
subscribe(listener: BeforeLeaveListener): () => void;
confirm(to: string | number, options?: Partial<NavigateOptions>): boolean;
}