vitarx-router 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +14 -0
- package/dist/index.d.ts +2 -0
- package/dist/router/helper.d.ts +92 -0
- package/dist/router/index.d.ts +6 -0
- package/dist/router/memory-router.d.ts +45 -0
- package/dist/router/router.d.ts +414 -0
- package/dist/router/type.d.ts +477 -0
- package/dist/router/update.d.ts +8 -0
- package/dist/router/utils.d.ts +152 -0
- package/dist/router/validate/index.d.ts +2 -0
- package/dist/router/validate/inject-props.d.ts +8 -0
- package/dist/router/validate/validate-widget.d.ts +8 -0
- package/dist/router/web-history-router.d.ts +65 -0
- package/dist/vitarx-router.js +1223 -0
- package/dist/widget/RouterView.d.ts +84 -0
- package/dist/widget/index.d.ts +1 -0
- package/package.json +30 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { WidgetType } from 'vitarx';
|
|
2
|
+
import { LAZY_LOADER_SYMBOL } from './utils.js';
|
|
3
|
+
import { default as Router } from './router.js';
|
|
4
|
+
/**
|
|
5
|
+
* 延迟加载/惰性加载
|
|
6
|
+
*/
|
|
7
|
+
export interface LazyLoad<T> {
|
|
8
|
+
[LAZY_LOADER_SYMBOL]: boolean;
|
|
9
|
+
(): Promise<{
|
|
10
|
+
default: T;
|
|
11
|
+
}>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* hash字符串类型
|
|
15
|
+
*/
|
|
16
|
+
export type HashStr = `#${string}` | '';
|
|
17
|
+
/**
|
|
18
|
+
* 路由参数注入
|
|
19
|
+
*/
|
|
20
|
+
export type InjectProps = boolean | Record<string, any> | ((route: RouteLocation) => Record<string, any>);
|
|
21
|
+
/**
|
|
22
|
+
* 命名的props
|
|
23
|
+
*/
|
|
24
|
+
export type InjectNamedProps<k extends string = string> = Record<k, InjectProps>;
|
|
25
|
+
/**
|
|
26
|
+
* 路由元数据
|
|
27
|
+
*/
|
|
28
|
+
export interface RouteMeta {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 路由视图小部件
|
|
33
|
+
*/
|
|
34
|
+
type RouteWidget = WidgetType | LazyLoad<WidgetType>;
|
|
35
|
+
/**
|
|
36
|
+
* 命名的路由视图小部件
|
|
37
|
+
*/
|
|
38
|
+
type NamedRouteWidget<K extends string = string> = Record<K, RouteWidget>;
|
|
39
|
+
/**
|
|
40
|
+
* 允许的路由小部件联合类型
|
|
41
|
+
*/
|
|
42
|
+
type AllowedRouteWidget = RouteWidget | NamedRouteWidget;
|
|
43
|
+
/**
|
|
44
|
+
* 路由路线配置
|
|
45
|
+
*
|
|
46
|
+
* @template WIDGET 允许的路由小部件类型,用于类型重载
|
|
47
|
+
*/
|
|
48
|
+
export interface Route<WIDGET extends AllowedRouteWidget = AllowedRouteWidget> {
|
|
49
|
+
/**
|
|
50
|
+
* 路由路径
|
|
51
|
+
*
|
|
52
|
+
* 支持动态路由参数
|
|
53
|
+
*
|
|
54
|
+
* example:
|
|
55
|
+
* `/user` // 静态路径
|
|
56
|
+
* `/user/{id}` // 必填参数
|
|
57
|
+
* `/user/{id?}` // 可选参数
|
|
58
|
+
* `/user/{id?}/{name}` // 错误的用例,可选参数后面不能再有其他参数
|
|
59
|
+
*/
|
|
60
|
+
path: RoutePath;
|
|
61
|
+
/**
|
|
62
|
+
* 动态路由参数匹配规则
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* path:`/user/[id]`
|
|
66
|
+
* pattern:{id:'\d+'}
|
|
67
|
+
*/
|
|
68
|
+
pattern?: Record<string, RegExp>;
|
|
69
|
+
/**
|
|
70
|
+
* 路由名称
|
|
71
|
+
*
|
|
72
|
+
* 必须全局保持唯一!
|
|
73
|
+
*/
|
|
74
|
+
name?: string;
|
|
75
|
+
/**
|
|
76
|
+
* 要展示的Widget
|
|
77
|
+
*
|
|
78
|
+
* 支持两种类型:
|
|
79
|
+
* 1. WidgetType: `YourWidget` 可以是函数式小部件,也可以是类小部件
|
|
80
|
+
* 2. LazyLoad: `() => import('./YourWidget')` 代码分块,懒加载,它会自动被LazyWidget包裹。
|
|
81
|
+
* 3. undefined: 自身不展示任何ui,仅做为父路由,使children继承父路由的`path`和`pattern`。
|
|
82
|
+
* 4. Record<string, WidgetType | LazyLoad<WidgetType>>: 命名的小部件,同级的`RouterView`会根据name属性展示对应的小部件。
|
|
83
|
+
*/
|
|
84
|
+
widget?: WIDGET;
|
|
85
|
+
/**
|
|
86
|
+
* 子路由
|
|
87
|
+
*
|
|
88
|
+
* 子路由path不要以父路由path开头,内部会自动拼接。
|
|
89
|
+
*/
|
|
90
|
+
children?: Route[];
|
|
91
|
+
/**
|
|
92
|
+
* 路由元数据
|
|
93
|
+
*
|
|
94
|
+
* 存储一些自定义的数据,不会影响路由匹配
|
|
95
|
+
*/
|
|
96
|
+
meta?: RouteMeta;
|
|
97
|
+
/**
|
|
98
|
+
* 将路由参数注入到小部件实例的props中
|
|
99
|
+
*
|
|
100
|
+
* 如果是命名视图,则必须定义给每个命名视图定义`props`配置
|
|
101
|
+
*
|
|
102
|
+
* @default true
|
|
103
|
+
*/
|
|
104
|
+
injectProps?: (WIDGET extends NamedRouteWidget<infer k> ? InjectNamedProps<k> : InjectProps) | boolean;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* 规范化过后的路由线路配置
|
|
108
|
+
*/
|
|
109
|
+
export interface RouteNormalized extends MakeRequired<Route, 'meta' | 'pattern'> {
|
|
110
|
+
children: RouteNormalized[];
|
|
111
|
+
widget: undefined | Record<string, RouteWidget>;
|
|
112
|
+
injectProps: undefined | InjectNamedProps;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 路由匹配的详情数据
|
|
116
|
+
*
|
|
117
|
+
* 所有和url相关的数据都已`decodeURIComponent`解码
|
|
118
|
+
*/
|
|
119
|
+
export interface RouteLocation {
|
|
120
|
+
/**
|
|
121
|
+
* 路由索引,调用`push`|`replace`时传入的index
|
|
122
|
+
*/
|
|
123
|
+
index: RouteIndex;
|
|
124
|
+
/**
|
|
125
|
+
* 完整的path,包含了query和hash
|
|
126
|
+
*
|
|
127
|
+
* 1. path|memory模式:`${base}${path}${query}${hash}`
|
|
128
|
+
* 2. hash模式:`${base}/#${path}${query}${hash}`
|
|
129
|
+
*/
|
|
130
|
+
fullPath: string;
|
|
131
|
+
/**
|
|
132
|
+
* pathname
|
|
133
|
+
*
|
|
134
|
+
* 如果是`hash`模式,pathname和`window.location.pathname`获取的值是不一致的,因为它是从`window.location.hash`中提取出来的
|
|
135
|
+
*/
|
|
136
|
+
path: `/${string}`;
|
|
137
|
+
/**
|
|
138
|
+
* hash
|
|
139
|
+
*
|
|
140
|
+
* 带有#前缀,空字符串代表没有hash。
|
|
141
|
+
*
|
|
142
|
+
* @default ''
|
|
143
|
+
*/
|
|
144
|
+
hash: HashStr;
|
|
145
|
+
/**
|
|
146
|
+
* 动态路由path匹配的参数,包括调用`push`|`replace`时传入的params
|
|
147
|
+
*
|
|
148
|
+
* > 注意:必须是能够被序列化的参数,否则会导致异常。
|
|
149
|
+
*
|
|
150
|
+
* @default {}
|
|
151
|
+
*/
|
|
152
|
+
params: Record<string, any>;
|
|
153
|
+
/**
|
|
154
|
+
* search参数
|
|
155
|
+
*
|
|
156
|
+
* @default {}
|
|
157
|
+
*/
|
|
158
|
+
query: Record<string, string>;
|
|
159
|
+
/**
|
|
160
|
+
* 匹配的路由对象
|
|
161
|
+
*
|
|
162
|
+
* > 注意:如果数组中存在多个`RouteNormalized`对象,则说明是嵌套路由,第一个则是最顶层的父路由,最后一个是精确匹配到的路由。
|
|
163
|
+
*
|
|
164
|
+
* 未匹配到路由时,它会是空数组。
|
|
165
|
+
*
|
|
166
|
+
* @default []
|
|
167
|
+
*/
|
|
168
|
+
matched: RouteNormalized[];
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* 路由前置钩子返回值
|
|
172
|
+
*
|
|
173
|
+
* 1. 返回false表示阻止路由跳转,true表示继续路由跳转
|
|
174
|
+
* 2. 返回{@link RouteTarget}重定向目标
|
|
175
|
+
* 3. 返回void表示继续路由跳转
|
|
176
|
+
* @note 如果返回promise,则promise resolve的值会被作为返回值
|
|
177
|
+
*/
|
|
178
|
+
export type BeforeEachCallbackResult = boolean | RouteTarget | void | Promise<boolean | RouteTarget | void>;
|
|
179
|
+
/**
|
|
180
|
+
* 全局路由前置钩子
|
|
181
|
+
*
|
|
182
|
+
* 此时路由导航还未正式开始,此钩子常用于鉴权,如果不符合条件,可以返回false阻止路由,亦可以返回重定向目标。
|
|
183
|
+
*
|
|
184
|
+
* @this {Router} - 路由器实例
|
|
185
|
+
* @param {DeepReadonly<RouteLocation>} to - 要跳转的目标路由
|
|
186
|
+
* @param {DeepReadonly<RouteLocation>} from - 从哪个路由跳转过来
|
|
187
|
+
* @returns {boolean | RouteTarget | void} - 返回false表示阻止路由跳转,返回{@link RouteTarget}重定向目标
|
|
188
|
+
*/
|
|
189
|
+
export type BeforeEachCallback = (this: Router, to: DeepReadonly<RouteLocation>, from: DeepReadonly<RouteLocation>) => BeforeEachCallbackResult;
|
|
190
|
+
/**
|
|
191
|
+
* 全局路由后置钩子
|
|
192
|
+
*
|
|
193
|
+
* 此时视图已经渲染完成,可以做一些操作,如:修改页面标题等。
|
|
194
|
+
*
|
|
195
|
+
* @this {Router} - 路由器实例
|
|
196
|
+
* @param {DeepReadonly<RouteLocation>} to - 当前路由数据
|
|
197
|
+
* @param {DeepReadonly<RouteLocation>} from - 从哪个路由跳转过来
|
|
198
|
+
*/
|
|
199
|
+
type AfterEachCallback = (this: Router, to: DeepReadonly<RouteLocation>, from: DeepReadonly<RouteLocation>) => void;
|
|
200
|
+
/**
|
|
201
|
+
* 路由模式
|
|
202
|
+
*/
|
|
203
|
+
export type HistoryMode = 'hash' | 'path' | 'memory';
|
|
204
|
+
/**
|
|
205
|
+
* 滚动行为
|
|
206
|
+
*
|
|
207
|
+
* 1. auto: 默认值,浏览器会自动决定滚动行为
|
|
208
|
+
* 2. instant: 立即滚动到目标位置,不考虑动画效果
|
|
209
|
+
* 3. smooth: 平滑滚动到目标位置,考虑动画效果
|
|
210
|
+
*/
|
|
211
|
+
export type _ScrollBehavior = 'auto' | 'instant' | 'smooth';
|
|
212
|
+
/**
|
|
213
|
+
* 滚动配置
|
|
214
|
+
*
|
|
215
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Window/scrollTo
|
|
216
|
+
*/
|
|
217
|
+
export interface _ScrollToOptions {
|
|
218
|
+
/**
|
|
219
|
+
* 滚动到目标位置的X坐标
|
|
220
|
+
*/
|
|
221
|
+
left?: number;
|
|
222
|
+
/**
|
|
223
|
+
* 滚动到目标位置的Y坐标
|
|
224
|
+
*/
|
|
225
|
+
top?: number;
|
|
226
|
+
/**
|
|
227
|
+
* 滚动行为
|
|
228
|
+
*
|
|
229
|
+
* @see _ScrollBehavior
|
|
230
|
+
*/
|
|
231
|
+
behavior?: _ScrollBehavior;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* 滚动到视图配置
|
|
235
|
+
*
|
|
236
|
+
* @see https://developer.mozilla.org/zh-CN/docs/Web/API/Element/scrollIntoView
|
|
237
|
+
*/
|
|
238
|
+
export interface _ScrollIntoViewOptions extends ScrollIntoViewOptions {
|
|
239
|
+
el: Element | `#${string}` | string;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* 滚动目标
|
|
243
|
+
*/
|
|
244
|
+
export type ScrollTarget = _ScrollToOptions | _ScrollIntoViewOptions;
|
|
245
|
+
/**
|
|
246
|
+
* 滚动结果
|
|
247
|
+
*
|
|
248
|
+
* false表示不滚动,否则内部会根据`ScrollTarget`滚动到指定的位置
|
|
249
|
+
*/
|
|
250
|
+
type ScrollResult = ScrollTarget | false;
|
|
251
|
+
/**
|
|
252
|
+
* 滚动行为处理器
|
|
253
|
+
*
|
|
254
|
+
* 仅浏览器环境有效。
|
|
255
|
+
*
|
|
256
|
+
* @param {RouteLocation} to - 要跳转的目标路由
|
|
257
|
+
* @param {RouteLocation} from - 从哪个路由跳转过来
|
|
258
|
+
* @param {_ScrollToOptions|undefined} savedPosition - 保存滚动位置,仅`history`模式前进或后退时有效
|
|
259
|
+
* @returns {ScrollResult} - 返回滚动结果,由内部程序完成滚动
|
|
260
|
+
*/
|
|
261
|
+
export type ScrollBehaviorHandler = (to: RouteTarget, from: RouteTarget, savedPosition: _ScrollToOptions | undefined) => ScrollResult | Promise<ScrollResult>;
|
|
262
|
+
/**
|
|
263
|
+
* 路由器配置
|
|
264
|
+
*/
|
|
265
|
+
export interface RouterOptions<T_MODE extends HistoryMode = HistoryMode> {
|
|
266
|
+
/**
|
|
267
|
+
* 根路径
|
|
268
|
+
*
|
|
269
|
+
* 假设你的项目在`/sub-path`目录下运行,那么你需要设置该值为`/sub-path`,它与`vite.base`配置值应保持一致。
|
|
270
|
+
*
|
|
271
|
+
* @default '/'
|
|
272
|
+
*/
|
|
273
|
+
base?: `/${string}`;
|
|
274
|
+
/**
|
|
275
|
+
* 历史记录模式
|
|
276
|
+
*
|
|
277
|
+
* 可选值如下:
|
|
278
|
+
* 1. path模式:使用path值作为路由标识,如:`/page1`
|
|
279
|
+
* 2. hash模式:使用hash值作为路由标识,如:`/#/page1`
|
|
280
|
+
* 3. memory模式:内存模式,用于非浏览器端,或不需要使用浏览器回退和前进功能时使用。
|
|
281
|
+
*
|
|
282
|
+
* @note 使用memory模式需在路由器实例化完成后使用replace替换掉初始的伪路由,另外两种模式是浏览器端使用的,会自动完成这个操作!
|
|
283
|
+
* @default 'path' 默认视为是浏览器端
|
|
284
|
+
*/
|
|
285
|
+
mode?: T_MODE;
|
|
286
|
+
/**
|
|
287
|
+
* 是否严格匹配路由
|
|
288
|
+
*
|
|
289
|
+
* 严格匹配指:区分大小写
|
|
290
|
+
*
|
|
291
|
+
* @default false
|
|
292
|
+
*/
|
|
293
|
+
strict?: boolean;
|
|
294
|
+
/**
|
|
295
|
+
* 路由表
|
|
296
|
+
*
|
|
297
|
+
* @note 注意:路由表传入过后,不应该在外部进行修改,如需修改需使用`Router.removeRoute`或`Router.addRoute`方法。
|
|
298
|
+
*/
|
|
299
|
+
routes: Route[];
|
|
300
|
+
/**
|
|
301
|
+
* 全局路由前置钩子
|
|
302
|
+
*
|
|
303
|
+
* @see BeforeEachCallback
|
|
304
|
+
*/
|
|
305
|
+
beforeEach?: BeforeEachCallback;
|
|
306
|
+
/**
|
|
307
|
+
* 全局路由后置钩子
|
|
308
|
+
*
|
|
309
|
+
* @see AfterEachCallback
|
|
310
|
+
*/
|
|
311
|
+
afterEach?: AfterEachCallback;
|
|
312
|
+
/**
|
|
313
|
+
* 滚动行为
|
|
314
|
+
*
|
|
315
|
+
* 可以传入`ScrollBehavior`或`ScrollBehaviorHandler`函数自定义滚动行为。
|
|
316
|
+
*
|
|
317
|
+
* @default 'smooth'
|
|
318
|
+
*/
|
|
319
|
+
scrollBehavior?: _ScrollBehavior | ScrollBehaviorHandler;
|
|
320
|
+
/**
|
|
321
|
+
* 支持的后缀名,如:.html、.md等。
|
|
322
|
+
*
|
|
323
|
+
* 默认支持所有后缀名进行匹配,如:`/page.html`、`/page.md`等。
|
|
324
|
+
*
|
|
325
|
+
* 可选值类型:
|
|
326
|
+
* 1. 通配符:`*`:支持所有后缀名
|
|
327
|
+
* 2. 字符串类型:`html`:支持html后缀名
|
|
328
|
+
* 3. 数组:`['html','md']`:同时支持html和md后缀名
|
|
329
|
+
* 4. false:不做任何处理,硬性匹配。
|
|
330
|
+
* @default false
|
|
331
|
+
*/
|
|
332
|
+
suffix?: '*' | string | string[] | false;
|
|
333
|
+
/**
|
|
334
|
+
* 默认path变量匹配模式
|
|
335
|
+
*
|
|
336
|
+
* @default '/[\w.]+/'
|
|
337
|
+
*/
|
|
338
|
+
pattern?: RegExp;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 已初始化的路由配置
|
|
342
|
+
*/
|
|
343
|
+
export type InitializedRouterOptions = MakeRequired<RouterOptions, Exclude<keyof RouterOptions, 'beforeEach' | 'afterEach'>>;
|
|
344
|
+
/**
|
|
345
|
+
* 路由路径
|
|
346
|
+
*/
|
|
347
|
+
export type RoutePath = `/${string}`;
|
|
348
|
+
/**
|
|
349
|
+
* 命名路由
|
|
350
|
+
*/
|
|
351
|
+
export type RouteName = string;
|
|
352
|
+
/**
|
|
353
|
+
* 路由索引
|
|
354
|
+
*/
|
|
355
|
+
export type RouteIndex = RoutePath | RouteName;
|
|
356
|
+
/**
|
|
357
|
+
* 路由目标
|
|
358
|
+
*/
|
|
359
|
+
export interface RouteTarget {
|
|
360
|
+
/**
|
|
361
|
+
* 索引,/开头为路径,否则为名称
|
|
362
|
+
*/
|
|
363
|
+
index: RouteIndex;
|
|
364
|
+
/**
|
|
365
|
+
* hash
|
|
366
|
+
*/
|
|
367
|
+
hash?: HashStr;
|
|
368
|
+
/**
|
|
369
|
+
* 路由query参数
|
|
370
|
+
*/
|
|
371
|
+
query?: Record<string, string>;
|
|
372
|
+
/**
|
|
373
|
+
* 路由参数
|
|
374
|
+
*/
|
|
375
|
+
params?: Record<string, any>;
|
|
376
|
+
/**
|
|
377
|
+
* 是否替换当前路由
|
|
378
|
+
*/
|
|
379
|
+
isReplace?: boolean;
|
|
380
|
+
}
|
|
381
|
+
/**
|
|
382
|
+
* 动态路由记录
|
|
383
|
+
*/
|
|
384
|
+
export interface DynamicRouteRecord {
|
|
385
|
+
regex: RegExp;
|
|
386
|
+
route: RouteNormalized;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* 导航结果
|
|
390
|
+
*/
|
|
391
|
+
export interface NavigateResult {
|
|
392
|
+
/**
|
|
393
|
+
* 状态
|
|
394
|
+
*
|
|
395
|
+
* @see NavigateStatus
|
|
396
|
+
*/
|
|
397
|
+
status: NavigateStatus;
|
|
398
|
+
/**
|
|
399
|
+
* 状态描述
|
|
400
|
+
*/
|
|
401
|
+
message: string;
|
|
402
|
+
/**
|
|
403
|
+
* 最终的导航数据
|
|
404
|
+
*
|
|
405
|
+
* 它和守卫钩子`to`参数一致。
|
|
406
|
+
*/
|
|
407
|
+
to: Readonly<RouteLocation>;
|
|
408
|
+
/**
|
|
409
|
+
* 导航完成前的路由数据
|
|
410
|
+
*
|
|
411
|
+
* 它和守卫钩子`from`参数一致。
|
|
412
|
+
*/
|
|
413
|
+
from: Readonly<RouteLocation>;
|
|
414
|
+
/**
|
|
415
|
+
* 如果在守卫过程中被重定向,则redirectFrom为最初的路由目标,否则为undefined。
|
|
416
|
+
*/
|
|
417
|
+
redirectFrom: RouteTarget | undefined;
|
|
418
|
+
/**
|
|
419
|
+
* 捕获到的异常
|
|
420
|
+
*/
|
|
421
|
+
error?: unknown;
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* 路由匹配结果
|
|
425
|
+
*/
|
|
426
|
+
export type MatchResult = {
|
|
427
|
+
/**
|
|
428
|
+
* 匹配的路由对象
|
|
429
|
+
*/
|
|
430
|
+
route: RouteNormalized;
|
|
431
|
+
/**
|
|
432
|
+
* path参数
|
|
433
|
+
*
|
|
434
|
+
* 非动态路由path,值固定为undefined
|
|
435
|
+
*/
|
|
436
|
+
params: Record<string, string> | undefined;
|
|
437
|
+
} | undefined;
|
|
438
|
+
/**
|
|
439
|
+
* 导航结果
|
|
440
|
+
*
|
|
441
|
+
* 枚举值:
|
|
442
|
+
* 0. success: 导航成功
|
|
443
|
+
* 1. aborted: 导航被阻止
|
|
444
|
+
* 2. cancelled: 导航被取消
|
|
445
|
+
* 3. duplicated: 重复导航
|
|
446
|
+
* 4. not_matched: 路由未匹配
|
|
447
|
+
* 5. exception: 捕获到异常
|
|
448
|
+
*/
|
|
449
|
+
export declare enum NavigateStatus {
|
|
450
|
+
/**
|
|
451
|
+
* 导航成功
|
|
452
|
+
*/
|
|
453
|
+
success = 0,
|
|
454
|
+
/**
|
|
455
|
+
* 导航被阻止
|
|
456
|
+
*/
|
|
457
|
+
aborted = 1,
|
|
458
|
+
/**
|
|
459
|
+
* 导航被取消
|
|
460
|
+
*
|
|
461
|
+
* 正在等待中间件处理结果时又触发了新的导航请求
|
|
462
|
+
*/
|
|
463
|
+
cancelled = 2,
|
|
464
|
+
/**
|
|
465
|
+
* 重复导航
|
|
466
|
+
*/
|
|
467
|
+
duplicated = 3,
|
|
468
|
+
/**
|
|
469
|
+
* 路由未匹配
|
|
470
|
+
*/
|
|
471
|
+
not_matched = 4,
|
|
472
|
+
/**
|
|
473
|
+
* 捕获到异常
|
|
474
|
+
*/
|
|
475
|
+
exception = 5
|
|
476
|
+
}
|
|
477
|
+
export {};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { LazyLoad, Route, RouteLocation, RouteNormalized, RoutePath, RouteTarget } from './type.js';
|
|
2
|
+
import { WidgetType } from 'vitarx';
|
|
3
|
+
export declare const LAZY_LOADER_SYMBOL: unique symbol;
|
|
4
|
+
export type LAZY_LOADER_SYMBOL = typeof LAZY_LOADER_SYMBOL;
|
|
5
|
+
/**
|
|
6
|
+
* 判断path是否包含变量
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* isVariable('/{id}') // true
|
|
10
|
+
* isVariable('/{id?}') // true
|
|
11
|
+
* isVariable('/home') // false
|
|
12
|
+
* @param path
|
|
13
|
+
*/
|
|
14
|
+
export declare function isVariablePath(path: string): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 判断 path 是否包含可选变量
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* isOptionalVariablePath('/{id?}') // true
|
|
20
|
+
* isOptionalVariablePath('/{id}') // false
|
|
21
|
+
* isOptionalVariablePath('/*') // false
|
|
22
|
+
* @param path
|
|
23
|
+
*/
|
|
24
|
+
export declare function isOptionalVariablePath(path: string): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* 获取路径中的可选变量数量
|
|
27
|
+
*
|
|
28
|
+
* @param path - 路径
|
|
29
|
+
* @return {number} - 可选变量数量
|
|
30
|
+
*/
|
|
31
|
+
export declare function optionalVariableCount(path: string): number;
|
|
32
|
+
/**
|
|
33
|
+
* 判断路由是否具有子路由
|
|
34
|
+
*
|
|
35
|
+
* @param route
|
|
36
|
+
*/
|
|
37
|
+
export declare function isRouteGroup(route: Route): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* 生成动态路由匹配正则
|
|
40
|
+
*
|
|
41
|
+
* @param {string} path - 路径字符串
|
|
42
|
+
* @param {Record<string, any>} pattern - 自定义的正则规则集合
|
|
43
|
+
* @param {boolean} strict - 是否严格匹配
|
|
44
|
+
* @param defaultPattern
|
|
45
|
+
* @return { {regex: RegExp length: number; optional: number} } - 返回动态匹配的正则表达式
|
|
46
|
+
*/
|
|
47
|
+
export declare function createDynamicPattern(path: string, pattern: Record<string, any>, strict: boolean, defaultPattern: RegExp): {
|
|
48
|
+
regex: RegExp;
|
|
49
|
+
length: number;
|
|
50
|
+
optional: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 格式化path
|
|
54
|
+
*
|
|
55
|
+
* @param {string} path - 路径字符串
|
|
56
|
+
* @return {string} - 格式化后的路径字符串
|
|
57
|
+
*/
|
|
58
|
+
export declare function formatPath(path: string): RoutePath;
|
|
59
|
+
/**
|
|
60
|
+
* 合并路径参数
|
|
61
|
+
*
|
|
62
|
+
* @param {string} path - 路径字符串
|
|
63
|
+
* @param {Record<string, string>} params - 路径参数对象
|
|
64
|
+
*/
|
|
65
|
+
export declare function mergePathParams(path: RoutePath, params: Record<string, string>): RoutePath;
|
|
66
|
+
/**
|
|
67
|
+
* ## 判断一个函数是否为延迟加载器
|
|
68
|
+
*
|
|
69
|
+
* @param lazyLoader
|
|
70
|
+
* @return {boolean}
|
|
71
|
+
*/
|
|
72
|
+
export declare function isLazyLoad(lazyLoader: any): lazyLoader is LazyLoad<WidgetType>;
|
|
73
|
+
/**
|
|
74
|
+
* 格式化hash
|
|
75
|
+
*
|
|
76
|
+
* @param {any} hash - hash
|
|
77
|
+
* @param {true} addHashPrefix - 添加#前缀
|
|
78
|
+
* @return {string} - 格式化后的hash带有#
|
|
79
|
+
*/
|
|
80
|
+
export declare function formatHash(hash: any, addHashPrefix: true): `#${string}` | '';
|
|
81
|
+
/**
|
|
82
|
+
* 格式化hash
|
|
83
|
+
*
|
|
84
|
+
* @param {any} hash - hash
|
|
85
|
+
* @param {false} addHashPrefix - 去除#前缀
|
|
86
|
+
* @return {string} - 格式化后的hash,不带#前缀
|
|
87
|
+
*/
|
|
88
|
+
export declare function formatHash(hash: any, addHashPrefix: false): string;
|
|
89
|
+
/**
|
|
90
|
+
* 将 query 字符串转为对象
|
|
91
|
+
*
|
|
92
|
+
* @param {string} queryString - 查询字符串(如 ?key1=value1&key2=value2)
|
|
93
|
+
* @return {Record<string, string>} - 转换后的对象
|
|
94
|
+
*/
|
|
95
|
+
export declare function queryStringToObject(queryString: string): Record<string, string>;
|
|
96
|
+
/**
|
|
97
|
+
* 将对象转换为 query 字符串
|
|
98
|
+
*
|
|
99
|
+
* @param {Record<string, string>} obj - 要转换的对象
|
|
100
|
+
* @return {string} 转换后的查询字符串(如 ?key1=value1&key2=value2)
|
|
101
|
+
*/
|
|
102
|
+
export declare function objectToQueryString(obj: Record<string, string>): `?${string}` | '';
|
|
103
|
+
/**
|
|
104
|
+
* 从 URL 对象中提取 path、hash 和 query
|
|
105
|
+
*
|
|
106
|
+
* @param {URL} url - 当前的 URL 对象
|
|
107
|
+
* @param {string} mode - 路由模式 ('path' 或 'hash')
|
|
108
|
+
* @param {string} base - 路由的根路径
|
|
109
|
+
* @returns {object} - 包含 path、hash 和 query 的对象
|
|
110
|
+
*/
|
|
111
|
+
export declare function urlToRouteTarget(url: URL | Location, mode: 'path' | 'hash', base: `/${string}`): MakeRequired<RouteTarget, 'query' | 'hash'>;
|
|
112
|
+
/**
|
|
113
|
+
* 拆分路径和后缀
|
|
114
|
+
*
|
|
115
|
+
* @param path
|
|
116
|
+
*/
|
|
117
|
+
export declare function splitPathAndSuffix(path: string): {
|
|
118
|
+
path: string;
|
|
119
|
+
suffix: string;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* 获取路径后缀
|
|
123
|
+
*
|
|
124
|
+
* @param path
|
|
125
|
+
* @return {string} 有则返回`.${string}`无则返回空字符串
|
|
126
|
+
*/
|
|
127
|
+
export declare function getPathSuffix(path: string): `.${string}` | '';
|
|
128
|
+
/**
|
|
129
|
+
* 根据路由表生成路由索引
|
|
130
|
+
*
|
|
131
|
+
* 该函数提供给node脚本使用,生成对应的`RoutePath`和`RouteName`类型,优化类型推断
|
|
132
|
+
*
|
|
133
|
+
* @param {Route[]} routes - 路由表
|
|
134
|
+
* @return {{ paths: string[], names: string[] }} - 路由索引对象,包含所有路由路径和名称
|
|
135
|
+
*/
|
|
136
|
+
export declare function generateRouteIndex(routes: Route[]): {
|
|
137
|
+
paths: string[];
|
|
138
|
+
names: string[];
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* 辅助判断是否为路由位置对象
|
|
142
|
+
*
|
|
143
|
+
* @param obj
|
|
144
|
+
*/
|
|
145
|
+
export declare function isRouteLocationTypeObject(obj: any): obj is RouteLocation;
|
|
146
|
+
/**
|
|
147
|
+
* 规范化路由对象
|
|
148
|
+
*
|
|
149
|
+
* @param route
|
|
150
|
+
* @private
|
|
151
|
+
*/
|
|
152
|
+
export declare function normalizeRoute(route: Route): RouteNormalized;
|