kdu-router 4.0.16 → 4.1.6

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.
@@ -1,1349 +0,0 @@
1
- import { AllowedComponentProps } from 'kdu';
2
- import { App } from 'kdu';
3
- import { Component } from 'kdu';
4
- import { ComponentCustomProps } from 'kdu';
5
- import { ComponentPublicInstance } from 'kdu';
6
- import { ComputedRef } from 'kdu';
7
- import { InjectionKey } from 'kdu';
8
- import { KNode } from 'kdu';
9
- import { KNodeProps } from 'kdu';
10
- import { Ref } from 'kdu';
11
- import { UnwrapRef } from 'kdu';
12
-
13
- declare type Awaitable<T> = T | Promise<T>;
14
-
15
- /**
16
- * Creates a in-memory based history. The main purpose of this history is to handle SSR. It starts in a special location that is nowhere.
17
- * It's up to the user to replace that location with the starter location by either calling `router.push` or `router.replace`.
18
- *
19
- * @param base - Base applied to all urls, defaults to '/'
20
- * @returns a history object that can be passed to the router constructor
21
- */
22
- export declare function createMemoryHistory(base?: string): RouterHistory;
23
-
24
- /**
25
- * Creates a Router instance that can be used by a Kdu app.
26
- *
27
- * @param options - {@link RouterOptions}
28
- */
29
- export declare function createRouter(options: RouterOptions): Router;
30
-
31
- /**
32
- * Creates a Router Matcher.
33
- *
34
- * @internal
35
- * @param routes - array of initial routes
36
- * @param globalOptions - global route options
37
- */
38
- export declare function createRouterMatcher(routes: RouteRecordRaw[], globalOptions: PathParserOptions): RouterMatcher;
39
-
40
- /**
41
- * Creates a hash history. Useful for web applications with no host (e.g.
42
- * `file://`) or when configuring a server to handle any URL is not possible.
43
- *
44
- * @param base - optional base to provide. Defaults to `location.pathname +
45
- * location.search` If there is a `<base>` tag in the `head`, its value will be
46
- * ignored in favor of this parameter **but note it affects all the
47
- * history.pushState() calls**, meaning that if you use a `<base>` tag, it's
48
- * `href` value **has to match this parameter** (ignoring anything after the
49
- * `#`).
50
- *
51
- * @example
52
- * ```js
53
- * // at https://example.com/folder
54
- * createWebHashHistory() // gives a url of `https://example.com/folder#`
55
- * createWebHashHistory('/folder/') // gives a url of `https://example.com/folder/#`
56
- * // if the `#` is provided in the base, it won't be added by `createWebHashHistory`
57
- * createWebHashHistory('/folder/#/app/') // gives a url of `https://example.com/folder/#/app/`
58
- * // you should avoid doing this because it changes the original url and breaks copying urls
59
- * createWebHashHistory('/other-folder/') // gives a url of `https://example.com/other-folder/#`
60
- *
61
- * // at file:///usr/etc/folder/index.html
62
- * // for locations with no `host`, the base is ignored
63
- * createWebHashHistory('/iAmIgnored') // gives a url of `file:///usr/etc/folder/index.html#`
64
- * ```
65
- */
66
- export declare function createWebHashHistory(base?: string): RouterHistory;
67
-
68
- /**
69
- * Creates an HTML5 history. Most common history for single page applications.
70
- *
71
- * @param base -
72
- */
73
- export declare function createWebHistory(base?: string): RouterHistory;
74
-
75
- /**
76
- * Internal type to define an ErrorHandler
77
- *
78
- * @param error - error thrown
79
- * @param to - location we were navigating to when the error happened
80
- * @param from - location we were navigating from when the error happened
81
- * @internal
82
- */
83
- declare type _ErrorHandler = (error: any, to: RouteLocationNormalized, from: RouteLocationNormalizedLoaded) => any;
84
-
85
- /**
86
- * Flags so we can combine them when checking for multiple errors
87
- */
88
- declare const enum ErrorTypes {
89
- MATCHER_NOT_FOUND = 1,
90
- NAVIGATION_GUARD_REDIRECT = 2,
91
- NAVIGATION_ABORTED = 4,
92
- NAVIGATION_CANCELLED = 8,
93
- NAVIGATION_DUPLICATED = 16
94
- }
95
-
96
- declare type HistoryLocation = string;
97
-
98
- /**
99
- * Allowed HTML history.state
100
- */
101
- export declare interface HistoryState {
102
- [x: number]: HistoryStateValue;
103
- [x: string]: HistoryStateValue;
104
- }
105
-
106
- declare interface HistoryStateArray extends Array<HistoryStateValue> {
107
- }
108
-
109
- /**
110
- * Allowed variables in HTML5 history state
111
- */
112
- declare type HistoryStateValue = string | number | boolean | null | undefined | HistoryState | HistoryStateArray;
113
-
114
- /**
115
- * Check if an object is a {@link NavigationFailure}.
116
- *
117
- * @example
118
- * ```js
119
- * import { isNavigationFailure, NavigationFailureType } from 'kdu-router'
120
- *
121
- * router.afterEach((to, from, failure) => {
122
- * // Any kind of navigation failure
123
- * if (isNavigationFailure(failure)) {
124
- * // ...
125
- * }
126
- * // Only duplicated navigations
127
- * if (isNavigationFailure(failure, NavigationFailureType.duplicated)) {
128
- * // ...
129
- * }
130
- * // Aborted or canceled navigations
131
- * if (isNavigationFailure(failure, NavigationFailureType.aborted | NavigationFailureType.canceled)) {
132
- * // ...
133
- * }
134
- * })
135
- * ```
136
- * @param error - possible {@link NavigationFailure}
137
- * @param type - optional types to check for
138
- */
139
- export declare function isNavigationFailure(error: any, type?: ErrorTypes.NAVIGATION_GUARD_REDIRECT): error is NavigationRedirectError;
140
-
141
- export declare function isNavigationFailure(error: any, type?: ErrorTypes | NavigationFailureType): error is NavigationFailure;
142
-
143
- /**
144
- * Type to transform a static object into one that allows passing Refs as
145
- * values.
146
- * @internal
147
- */
148
- declare type KduUseOptions<T> = {
149
- [k in keyof T]: Ref<T[k]> | T[k];
150
- };
151
-
152
- declare type Lazy<T> = () => Promise<T>;
153
-
154
- declare interface LocationAsName {
155
- name: RouteRecordName;
156
- params?: RouteParams;
157
- }
158
-
159
- /**
160
- * @internal
161
- */
162
- export declare interface LocationAsPath {
163
- path: string;
164
- }
165
-
166
- declare interface LocationAsRelative {
167
- params?: RouteParams;
168
- }
169
-
170
- /**
171
- * @internal
172
- */
173
- export declare interface LocationAsRelativeRaw {
174
- name?: RouteRecordName;
175
- params?: RouteParamsRaw;
176
- }
177
-
178
- /**
179
- * Normalized query object that appears in {@link RouteLocationNormalized}
180
- *
181
- * @public
182
- */
183
- export declare type LocationQuery = Record<string, LocationQueryValue | LocationQueryValue[]>;
184
-
185
- /**
186
- * Loose {@link LocationQuery} object that can be passed to functions like
187
- * {@link Router.push} and {@link Router.replace} or anywhere when creating a
188
- * {@link RouteLocationRaw}
189
- *
190
- * @public
191
- */
192
- export declare type LocationQueryRaw = Record<string | number, LocationQueryValueRaw | LocationQueryValueRaw[]>;
193
-
194
- /**
195
- * Possible values in normalized {@link LocationQuery}. `null` renders the query
196
- * param but without an `=`.
197
- *
198
- * @example
199
- * ```
200
- * ?isNull&isEmpty=&other=other
201
- * gives
202
- * `{ isNull: null, isEmpty: '', other: 'other' }`.
203
- * ```
204
- *
205
- * @internal
206
- */
207
- export declare type LocationQueryValue = string | null;
208
-
209
- /**
210
- * Possible values when defining a query.
211
- *
212
- * @internal
213
- */
214
- export declare type LocationQueryValueRaw = LocationQueryValue | number | undefined;
215
-
216
- /**
217
- * RouteRecord being rendered by the closest ancestor Router View. Used for
218
- * `onBeforeRouteUpdate` and `onBeforeRouteLeave`. rvlm stands for Router View
219
- * Location Matched
220
- *
221
- * @internal
222
- */
223
- export declare const matchedRouteKey: InjectionKey<ComputedRef<RouteRecordNormalized | undefined>>;
224
-
225
- declare interface MatcherLocation extends Pick<RouteLocation, 'name' | 'path' | 'params' | 'matched' | 'meta'> {
226
- }
227
-
228
- declare type MatcherLocationRaw = LocationAsPath | LocationAsName | LocationAsRelative;
229
-
230
- declare interface NavigationCallback {
231
- (to: HistoryLocation, from: HistoryLocation, information: NavigationInformation): void;
232
- }
233
-
234
- declare enum NavigationDirection {
235
- back = "back",
236
- forward = "forward",
237
- unknown = ""
238
- }
239
-
240
- /**
241
- * Extended Error that contains extra information regarding a failed navigation.
242
- */
243
- export declare interface NavigationFailure extends Error {
244
- /**
245
- * Type of the navigation. One of {@link NavigationFailureType}
246
- */
247
- type: ErrorTypes.NAVIGATION_CANCELLED | ErrorTypes.NAVIGATION_ABORTED | ErrorTypes.NAVIGATION_DUPLICATED;
248
- /**
249
- * Route location we were navigating from
250
- */
251
- from: RouteLocationNormalized;
252
- /**
253
- * Route location we were navigating to
254
- */
255
- to: RouteLocationNormalized;
256
- }
257
-
258
- /**
259
- * Enumeration with all possible types for navigation failures. Can be passed to
260
- * {@link isNavigationFailure} to check for specific failures.
261
- */
262
- export declare enum NavigationFailureType {
263
- /**
264
- * An aborted navigation is a navigation that failed because a navigation
265
- * guard returned `false` or called `next(false)`
266
- */
267
- aborted = 4,
268
- /**
269
- * A cancelled navigation is a navigation that failed because a more recent
270
- * navigation finished started (not necessarily finished).
271
- */
272
- cancelled = 8,
273
- /**
274
- * A duplicated navigation is a navigation that failed because it was
275
- * initiated while already being at the exact same location.
276
- */
277
- duplicated = 16
278
- }
279
-
280
- /**
281
- * Navigation guard. See [Navigation
282
- * Guards](/guide/advanced/navigation-guards.md).
283
- */
284
- export declare interface NavigationGuard {
285
- (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn>;
286
- }
287
-
288
- export declare interface NavigationGuardNext {
289
- (): void;
290
- (error: Error): void;
291
- (location: RouteLocationRaw): void;
292
- (valid: boolean | undefined): void;
293
- (cb: NavigationGuardNextCallback): void;
294
- }
295
-
296
- declare type NavigationGuardNextCallback = (vm: ComponentPublicInstance) => any;
297
-
298
- declare type NavigationGuardReturn = void | Error | RouteLocationRaw | boolean | NavigationGuardNextCallback;
299
-
300
- /**
301
- * {@inheritDoc NavigationGuard}
302
- */
303
- export declare interface NavigationGuardWithThis<T> {
304
- (this: T, to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): NavigationGuardReturn | Promise<NavigationGuardReturn>;
305
- }
306
-
307
- export declare interface NavigationHookAfter {
308
- (to: RouteLocationNormalized, from: RouteLocationNormalized, failure?: NavigationFailure | void): any;
309
- }
310
-
311
- declare interface NavigationInformation {
312
- type: NavigationType_2;
313
- direction: NavigationDirection;
314
- delta: number;
315
- }
316
-
317
- declare interface NavigationRedirectError extends Omit<NavigationFailure, 'to' | 'type'> {
318
- type: ErrorTypes.NAVIGATION_GUARD_REDIRECT;
319
- to: RouteLocationRaw;
320
- }
321
-
322
- declare enum NavigationType_2 {
323
- pop = "pop",
324
- push = "push"
325
- }
326
-
327
- /**
328
- * Add a navigation guard that triggers whenever the component for the current
329
- * location is about to be left. Similar to {@link beforeRouteLeave} but can be
330
- * used in any component. The guard is removed when the component is unmounted.
331
- *
332
- * @param leaveGuard - {@link NavigationGuard}
333
- */
334
- export declare function onBeforeRouteLeave(leaveGuard: NavigationGuard): void;
335
-
336
- /**
337
- * Add a navigation guard that triggers whenever the current location is about
338
- * to be updated. Similar to {@link beforeRouteUpdate} but can be used in any
339
- * component. The guard is removed when the component is unmounted.
340
- *
341
- * @param updateGuard - {@link NavigationGuard}
342
- */
343
- export declare function onBeforeRouteUpdate(updateGuard: NavigationGuard): void;
344
-
345
- /**
346
- * Transforms a queryString into a {@link LocationQuery} object. Accept both, a
347
- * version with the leading `?` and without Should work as URLSearchParams
348
-
349
- * @internal
350
- *
351
- * @param search - search string to parse
352
- * @returns a query object
353
- */
354
- export declare function parseQuery(search: string): LocationQuery;
355
-
356
- declare type PathParams = Record<string, string | string[]>;
357
-
358
- declare interface PathParser {
359
- /**
360
- * The regexp used to match a url
361
- */
362
- re: RegExp;
363
- /**
364
- * The score of the parser
365
- */
366
- score: Array<number[]>;
367
- /**
368
- * Keys that appeared in the path
369
- */
370
- keys: PathParserParamKey[];
371
- /**
372
- * Parses a url and returns the matched params or nul if it doesn't match. An
373
- * optional param that isn't preset will be an empty string. A repeatable
374
- * param will be an array if there is at least one value.
375
- *
376
- * @param path - url to parse
377
- * @returns a Params object, empty if there are no params. `null` if there is
378
- * no match
379
- */
380
- parse(path: string): PathParams | null;
381
- /**
382
- * Creates a string version of the url
383
- *
384
- * @param params - object of params
385
- * @returns a url
386
- */
387
- stringify(params: PathParams): string;
388
- }
389
-
390
- export declare type PathParserOptions = Pick<_PathParserOptions, 'end' | 'sensitive' | 'strict'>;
391
-
392
- /**
393
- * @internal
394
- */
395
- export declare interface _PathParserOptions {
396
- /**
397
- * Makes the RegExp case sensitive. Defaults to false
398
- */
399
- sensitive?: boolean;
400
- /**
401
- * Should we disallow a trailing slash. Defaults to false
402
- */
403
- strict?: boolean;
404
- /**
405
- * Should the RegExp match from the beginning by prepending a `^` to it. Defaults to true
406
- * @internal
407
- */
408
- start?: boolean;
409
- /**
410
- * Should the RegExp match until the end by appending a `$` to it. Defaults to true
411
- */
412
- end?: boolean;
413
- }
414
-
415
- /**
416
- * A param in a url like `/users/:id`
417
- */
418
- declare interface PathParserParamKey {
419
- name: string;
420
- repeatable: boolean;
421
- optional: boolean;
422
- }
423
-
424
- /**
425
- * Allowed Component definitions in route records provided by the user
426
- */
427
- declare type RawRouteComponent = RouteComponent | Lazy<RouteComponent>;
428
-
429
- /**
430
- * Allowed Component in {@link RouteLocationMatched}
431
- */
432
- export declare type RouteComponent = Component;
433
-
434
- /**
435
- * {@link RouteLocationRaw} resolved using the matcher
436
- */
437
- export declare interface RouteLocation extends _RouteLocationBase {
438
- /**
439
- * Array of {@link RouteRecord} containing components as they were
440
- * passed when adding records. It can also contain redirect records. This
441
- * can't be used directly
442
- */
443
- matched: RouteRecord[];
444
- }
445
-
446
- /**
447
- * Base properties for a normalized route location.
448
- *
449
- * @internal
450
- */
451
- export declare interface _RouteLocationBase {
452
- /**
453
- * Percentage encoded pathname section of the URL.
454
- */
455
- path: string;
456
- /**
457
- * The whole location including the `search` and `hash`. This string is
458
- * percentage encoded.
459
- */
460
- fullPath: string;
461
- /**
462
- * Object representation of the `search` property of the current location.
463
- */
464
- query: LocationQuery;
465
- /**
466
- * Hash of the current location. If present, starts with a `#`.
467
- */
468
- hash: string;
469
- /**
470
- * Name of the matched record
471
- */
472
- name: RouteRecordName | null | undefined;
473
- /**
474
- * Object of decoded params extracted from the `path`.
475
- */
476
- params: RouteParams;
477
- /**
478
- * Contains the location we were initially trying to access before ending up
479
- * on the current location.
480
- */
481
- redirectedFrom: RouteLocation | undefined;
482
- /**
483
- * Merged `meta` properties from all of the matched route records.
484
- */
485
- meta: RouteMeta;
486
- }
487
-
488
- /**
489
- * Allows overriding the current route returned by `useRoute` in tests. rl
490
- * stands for route location
491
- *
492
- * @internal
493
- */
494
- export declare const routeLocationKey: InjectionKey<RouteLocationNormalizedLoaded>;
495
-
496
- export declare interface RouteLocationMatched extends RouteRecordNormalized {
497
- components: Record<string, RouteComponent>;
498
- }
499
-
500
- /**
501
- * Similar to {@link RouteLocation} but its
502
- * {@link RouteLocationNormalized.matched} cannot contain redirect records
503
- */
504
- export declare interface RouteLocationNormalized extends _RouteLocationBase {
505
- /**
506
- * Array of {@link RouteRecordNormalized}
507
- */
508
- matched: RouteRecordNormalized[];
509
- }
510
-
511
- /**
512
- * {@link RouteLocationRaw} with
513
- */
514
- export declare interface RouteLocationNormalizedLoaded extends _RouteLocationBase {
515
- /**
516
- * Array of {@link RouteLocationMatched} containing only plain components (any
517
- * lazy-loaded components have been loaded and were replaced inside of the
518
- * `components` object) so it can be directly used to display routes. It
519
- * cannot contain redirect records either
520
- */
521
- matched: RouteLocationMatched[];
522
- }
523
-
524
- export declare interface RouteLocationOptions {
525
- /**
526
- * Replace the entry in the history instead of pushing a new entry
527
- */
528
- replace?: boolean;
529
- /**
530
- * Triggers the navigation even if the location is the same as the current one
531
- */
532
- force?: boolean;
533
- /**
534
- * State to save using the History API. This cannot contain any reactive
535
- * values and some primitives like Symbols are forbidden. More info at
536
- * https://developer.mozilla.org/en-US/docs/Web/API/History/state
537
- */
538
- state?: HistoryState;
539
- }
540
-
541
- /**
542
- * User-level route location
543
- */
544
- export declare type RouteLocationRaw = string | (RouteQueryAndHash & LocationAsPath & RouteLocationOptions) | (RouteQueryAndHash & LocationAsRelativeRaw & RouteLocationOptions);
545
-
546
- /**
547
- * Interface to type `meta` fields in route records.
548
- *
549
- * @example
550
- *
551
- * ```ts
552
- * // typings.d.ts or router.ts
553
- * import 'kdu-router';
554
- *
555
- * declare module 'kdu-router' {
556
- * interface RouteMeta {
557
- * requiresAuth?: boolean
558
- * }
559
- * }
560
- * ```
561
- */
562
- export declare interface RouteMeta extends Record<string | number | symbol, unknown> {
563
- }
564
-
565
- export declare type RouteParams = Record<string, RouteParamValue | RouteParamValue[]>;
566
-
567
- export declare type RouteParamsRaw = Record<string, RouteParamValueRaw | Exclude<RouteParamValueRaw, null | undefined>[]>;
568
-
569
- /**
570
- * @internal
571
- */
572
- export declare type RouteParamValue = string;
573
-
574
- /**
575
- * @internal
576
- */
577
- export declare type RouteParamValueRaw = RouteParamValue | number | null | undefined;
578
-
579
- /**
580
- * @internal
581
- */
582
- export declare interface RouteQueryAndHash {
583
- query?: LocationQueryRaw;
584
- hash?: string;
585
- }
586
-
587
- /**
588
- * Router instance
589
- */
590
- export declare interface Router {
591
- /**
592
- * @internal
593
- */
594
- /**
595
- * Current {@link RouteLocationNormalized}
596
- */
597
- readonly currentRoute: Ref<RouteLocationNormalizedLoaded>;
598
- /**
599
- * Original options object passed to create the Router
600
- */
601
- readonly options: RouterOptions;
602
- /**
603
- * Add a new {@link RouteRecordRaw route record} as the child of an existing route.
604
- *
605
- * @param parentName - Parent Route Record where `route` should be appended at
606
- * @param route - Route Record to add
607
- */
608
- addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void;
609
- /**
610
- * Add a new {@link RouteRecordRaw route record} to the router.
611
- *
612
- * @param route - Route Record to add
613
- */
614
- addRoute(route: RouteRecordRaw): () => void;
615
- /**
616
- * Remove an existing route by its name.
617
- *
618
- * @param name - Name of the route to remove
619
- */
620
- removeRoute(name: RouteRecordName): void;
621
- /**
622
- * Checks if a route with a given name exists
623
- *
624
- * @param name - Name of the route to check
625
- */
626
- hasRoute(name: RouteRecordName): boolean;
627
- /**
628
- * Get a full list of all the {@link RouteRecord route records}.
629
- */
630
- getRoutes(): RouteRecord[];
631
- /**
632
- * Returns the {@link RouteLocation normalized version} of a
633
- * {@link RouteLocationRaw route location}. Also includes an `href` property
634
- * that includes any existing `base`. By default the `currentLocation` used is
635
- * `route.currentRoute` and should only be overriden in advanced use cases.
636
- *
637
- * @param to - Raw route location to resolve
638
- * @param currentLocation - Optional current location to resolve against
639
- */
640
- resolve(to: RouteLocationRaw, currentLocation?: RouteLocationNormalizedLoaded): RouteLocation & {
641
- href: string;
642
- };
643
- /**
644
- * Programmatically navigate to a new URL by pushing an entry in the history
645
- * stack.
646
- *
647
- * @param to - Route location to navigate to
648
- */
649
- push(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>;
650
- /**
651
- * Programmatically navigate to a new URL by replacing the current entry in
652
- * the history stack.
653
- *
654
- * @param to - Route location to navigate to
655
- */
656
- replace(to: RouteLocationRaw): Promise<NavigationFailure | void | undefined>;
657
- /**
658
- * Go back in history if possible by calling `history.back()`. Equivalent to
659
- * `router.go(-1)`.
660
- */
661
- back(): ReturnType<Router['go']>;
662
- /**
663
- * Go forward in history if possible by calling `history.forward()`.
664
- * Equivalent to `router.go(1)`.
665
- */
666
- forward(): ReturnType<Router['go']>;
667
- /**
668
- * Allows you to move forward or backward through the history. Calls
669
- * `history.go()`.
670
- *
671
- * @param delta - The position in the history to which you want to move,
672
- * relative to the current page
673
- */
674
- go(delta: number): void;
675
- /**
676
- * Add a navigation guard that executes before any navigation. Returns a
677
- * function that removes the registered guard.
678
- *
679
- * @param guard - navigation guard to add
680
- */
681
- beforeEach(guard: NavigationGuardWithThis<undefined>): () => void;
682
- /**
683
- * Add a navigation guard that executes before navigation is about to be
684
- * resolved. At this state all component have been fetched and other
685
- * navigation guards have been successful. Returns a function that removes the
686
- * registered guard.
687
- *
688
- * @example
689
- * ```js
690
- * router.beforeResolve(to => {
691
- * if (to.meta.requiresAuth && !isAuthenticated) return false
692
- * })
693
- * ```
694
- *
695
- * @param guard - navigation guard to add
696
- */
697
- beforeResolve(guard: NavigationGuardWithThis<undefined>): () => void;
698
- /**
699
- * Add a navigation hook that is executed after every navigation. Returns a
700
- * function that removes the registered hook.
701
- *
702
- * @example
703
- * ```js
704
- * router.afterEach((to, from, failure) => {
705
- * if (isNavigationFailure(failure)) {
706
- * console.log('failed navigation', failure)
707
- * }
708
- * })
709
- * ```
710
- *
711
- * @param guard - navigation hook to add
712
- */
713
- afterEach(guard: NavigationHookAfter): () => void;
714
- /**
715
- * Adds an error handler that is called every time a non caught error happens
716
- * during navigation. This includes errors thrown synchronously and
717
- * asynchronously, errors returned or passed to `next` in any navigation
718
- * guard, and errors occurred when trying to resolve an async component that
719
- * is required to render a route.
720
- *
721
- * @param handler - error handler to register
722
- */
723
- onError(handler: _ErrorHandler): () => void;
724
- /**
725
- * Returns a Promise that resolves when the router has completed the initial
726
- * navigation, which means it has resolved all async enter hooks and async
727
- * components that are associated with the initial route. If the initial
728
- * navigation already happened, the promise resolves immediately.
729
- *
730
- * This is useful in server-side rendering to ensure consistent output on both
731
- * the server and the client. Note that on server side, you need to manually
732
- * push the initial location while on client side, the router automatically
733
- * picks it up from the URL.
734
- */
735
- isReady(): Promise<void>;
736
- /**
737
- * Called automatically by `app.use(router)`. Should not be called manually by
738
- * the user.
739
- *
740
- * @internal
741
- * @param app - Application that uses the router
742
- */
743
- install(app: App): void;
744
- }
745
-
746
- /**
747
- * {@inheritDoc RouteRecordNormalized}
748
- */
749
- export declare type RouteRecord = RouteRecordNormalized;
750
-
751
- /**
752
- * Common properties among all kind of {@link RouteRecordRaw}
753
- * @internal
754
- */
755
- export declare interface _RouteRecordBase extends PathParserOptions {
756
- /**
757
- * Path of the record. Should start with `/` unless the record is the child of
758
- * another record.
759
- *
760
- * @example `/users/:id` matches `/users/1` as well as `/users/posva`.
761
- */
762
- path: string;
763
- /**
764
- * Where to redirect if the route is directly matched. The redirection happens
765
- * before any navigation guard and triggers a new navigation with the new
766
- * target location.
767
- */
768
- redirect?: RouteRecordRedirectOption;
769
- /**
770
- * Array of nested routes.
771
- */
772
- children?: RouteRecordRaw[];
773
- /**
774
- * Aliases for the record. Allows defining extra paths that will behave like a
775
- * copy of the record. Allows having paths shorthands like `/users/:id` and
776
- * `/u/:id`. All `alias` and `path` values must share the same params.
777
- */
778
- alias?: string | string[];
779
- /**
780
- * Name for the route record.
781
- */
782
- name?: RouteRecordName;
783
- /**
784
- * Before Enter guard specific to this record. Note `beforeEnter` has no
785
- * effect if the record has a `redirect` property.
786
- */
787
- beforeEnter?: NavigationGuardWithThis<undefined> | NavigationGuardWithThis<undefined>[];
788
- /**
789
- * Arbitrary data attached to the record.
790
- */
791
- meta?: RouteMeta;
792
- }
793
-
794
- declare interface RouteRecordMatcher extends PathParser {
795
- record: RouteRecord;
796
- parent: RouteRecordMatcher | undefined;
797
- children: RouteRecordMatcher[];
798
- alias: RouteRecordMatcher[];
799
- }
800
-
801
- /**
802
- * Route Record defining multiple named components with the `components` option.
803
- */
804
- declare interface RouteRecordMultipleViews extends _RouteRecordBase {
805
- /**
806
- * Components to display when the URL matches this route. Allow using named views.
807
- */
808
- components: Record<string, RawRouteComponent>;
809
- component?: never;
810
- /**
811
- * Allow passing down params as props to the component rendered by
812
- * `router-view`. Should be an object with the same keys as `components` or a
813
- * boolean to be applied to every component.
814
- */
815
- props?: Record<string, _RouteRecordProps> | boolean;
816
- }
817
-
818
- /**
819
- * Possible values for a user-defined route record's name
820
- */
821
- export declare type RouteRecordName = string | symbol;
822
-
823
- /**
824
- * Normalized version of a {@link RouteRecord route record}
825
- */
826
- export declare interface RouteRecordNormalized {
827
- /**
828
- * {@inheritDoc _RouteRecordBase.path}
829
- */
830
- path: _RouteRecordBase['path'];
831
- /**
832
- * {@inheritDoc _RouteRecordBase.redirect}
833
- */
834
- redirect: _RouteRecordBase['redirect'] | undefined;
835
- /**
836
- * {@inheritDoc _RouteRecordBase.name}
837
- */
838
- name: _RouteRecordBase['name'];
839
- /**
840
- * {@inheritDoc RouteRecordMultipleViews.components}
841
- */
842
- components: RouteRecordMultipleViews['components'];
843
- /**
844
- * {@inheritDoc _RouteRecordBase.components}
845
- */
846
- children: Exclude<_RouteRecordBase['children'], void>;
847
- /**
848
- * {@inheritDoc _RouteRecordBase.meta}
849
- */
850
- meta: Exclude<_RouteRecordBase['meta'], void>;
851
- /**
852
- * {@inheritDoc RouteRecordMultipleViews.props}
853
- */
854
- props: Record<string, _RouteRecordProps>;
855
- /**
856
- * Registered beforeEnter guards
857
- */
858
- beforeEnter: _RouteRecordBase['beforeEnter'];
859
- /**
860
- * Registered leave guards
861
- *
862
- * @internal
863
- */
864
- leaveGuards: Set<NavigationGuard>;
865
- /**
866
- * Registered update guards
867
- *
868
- * @internal
869
- */
870
- updateGuards: Set<NavigationGuard>;
871
- /**
872
- * Registered beforeRouteEnter callbacks passed to `next` or returned in guards
873
- *
874
- * @internal
875
- */
876
- enterCallbacks: Record<string, NavigationGuardNextCallback[]>;
877
- /**
878
- * Mounted route component instances
879
- * Having the instances on the record mean beforeRouteUpdate and
880
- * beforeRouteLeave guards can only be invoked with the latest mounted app
881
- * instance if there are multiple application instances rendering the same
882
- * view, basically duplicating the content on the page, which shouldn't happen
883
- * in practice. It will work if multiple apps are rendering different named
884
- * views.
885
- */
886
- instances: Record<string, ComponentPublicInstance | undefined | null>;
887
- /**
888
- * Defines if this record is the alias of another one. This property is
889
- * `undefined` if the record is the original one.
890
- */
891
- aliasOf: RouteRecordNormalized | undefined;
892
- }
893
-
894
- /**
895
- * @internal
896
- */
897
- declare type _RouteRecordProps = boolean | Record<string, any> | ((to: RouteLocationNormalized) => Record<string, any>);
898
-
899
- export declare type RouteRecordRaw = RouteRecordSingleView | RouteRecordMultipleViews | RouteRecordRedirect;
900
-
901
- /**
902
- * Route Record that defines a redirect. Cannot have `component` or `components`
903
- * as it is never rendered.
904
- */
905
- declare interface RouteRecordRedirect extends _RouteRecordBase {
906
- redirect: RouteRecordRedirectOption;
907
- component?: never;
908
- components?: never;
909
- }
910
-
911
- /**
912
- * @internal
913
- */
914
- export declare type RouteRecordRedirectOption = RouteLocationRaw | ((to: RouteLocation) => RouteLocationRaw);
915
-
916
- /**
917
- * Route Record defining one single component with the `component` option.
918
- */
919
- declare interface RouteRecordSingleView extends _RouteRecordBase {
920
- /**
921
- * Component to display when the URL matches this route.
922
- */
923
- component: RawRouteComponent;
924
- components?: never;
925
- /**
926
- * Allow passing down params as props to the component rendered by `router-view`.
927
- */
928
- props?: _RouteRecordProps;
929
- }
930
-
931
- /**
932
- * Interface implemented by History implementations that can be passed to the
933
- * router as {@link Router.history}
934
- *
935
- * @alpha
936
- */
937
- export declare interface RouterHistory {
938
- /**
939
- * Base path that is prepended to every url. This allows hosting an SPA at a
940
- * subfolder of a domain like `example.com/subfolder` by having a `base` of
941
- * `/subfolder`
942
- */
943
- readonly base: string;
944
- /**
945
- * Current History location
946
- */
947
- readonly location: HistoryLocation;
948
- /**
949
- * Current History state
950
- */
951
- readonly state: HistoryState;
952
- /**
953
- * Navigates to a location. In the case of an HTML5 History implementation,
954
- * this will call `history.pushState` to effectively change the URL.
955
- *
956
- * @param to - location to push
957
- * @param data - optional {@link HistoryState} to be associated with the
958
- * navigation entry
959
- */
960
- push(to: HistoryLocation, data?: HistoryState): void;
961
- /**
962
- * Same as {@link RouterHistory.push} but performs a `history.replaceState`
963
- * instead of `history.pushState`
964
- *
965
- * @param to - location to set
966
- * @param data - optional {@link HistoryState} to be associated with the
967
- * navigation entry
968
- */
969
- replace(to: HistoryLocation, data?: HistoryState): void;
970
- /**
971
- * Traverses history in a given direction.
972
- *
973
- * @example
974
- * ```js
975
- * myHistory.go(-1) // equivalent to window.history.back()
976
- * myHistory.go(1) // equivalent to window.history.forward()
977
- * ```
978
- *
979
- * @param delta - distance to travel. If delta is \< 0, it will go back,
980
- * if it's \> 0, it will go forward by that amount of entries.
981
- * @param triggerListeners - whether this should trigger listeners attached to
982
- * the history
983
- */
984
- go(delta: number, triggerListeners?: boolean): void;
985
- /**
986
- * Attach a listener to the History implementation that is triggered when the
987
- * navigation is triggered from outside (like the Browser back and forward
988
- * buttons) or when passing `true` to {@link RouterHistory.back} and
989
- * {@link RouterHistory.forward}
990
- *
991
- * @param callback - listener to attach
992
- * @returns a callback to remove the listener
993
- */
994
- listen(callback: NavigationCallback): () => void;
995
- /**
996
- * Generates the corresponding href to be used in an anchor tag.
997
- *
998
- * @param location - history location that should create an href
999
- */
1000
- createHref(location: HistoryLocation): string;
1001
- /**
1002
- * Clears any event listener attached by the history implementation.
1003
- */
1004
- destroy(): void;
1005
- }
1006
-
1007
- /**
1008
- * Allows overriding the router instance returned by `useRouter` in tests. r
1009
- * stands for router
1010
- *
1011
- * @internal
1012
- */
1013
- export declare const routerKey: InjectionKey<Router>;
1014
-
1015
- /**
1016
- * Component to render a link that triggers a navigation on click.
1017
- */
1018
- export declare const RouterLink: {
1019
- new (): {
1020
- $props: AllowedComponentProps & ComponentCustomProps & KNodeProps & RouterLinkProps;
1021
- $slots: {
1022
- default: (arg: UnwrapRef<ReturnType<typeof useLink>>) => KNode[];
1023
- };
1024
- };
1025
- /**
1026
- * Access to `useLink()` without depending on using kdu-router
1027
- *
1028
- * @internal
1029
- */
1030
- useLink: typeof useLink;
1031
- };
1032
-
1033
- declare interface RouterLinkOptions {
1034
- /**
1035
- * Route Location the link should navigate to when clicked on.
1036
- */
1037
- to: RouteLocationRaw;
1038
- /**
1039
- * Calls `router.replace` instead of `router.push`.
1040
- */
1041
- replace?: boolean;
1042
- }
1043
-
1044
- export declare interface RouterLinkProps extends RouterLinkOptions {
1045
- /**
1046
- * Whether RouterLink should not wrap its content in an `a` tag. Useful when
1047
- * using `k-slot` to create a custom RouterLink
1048
- */
1049
- custom?: boolean;
1050
- /**
1051
- * Class to apply when the link is active
1052
- */
1053
- activeClass?: string;
1054
- /**
1055
- * Class to apply when the link is exact active
1056
- */
1057
- exactActiveClass?: string;
1058
- /**
1059
- * Value passed to the attribute `aria-current` when the link is exact active. Defaults to "page"
1060
- */
1061
- ariaCurrentValue?: 'page' | 'step' | 'location' | 'date' | 'time' | 'true' | 'false';
1062
- }
1063
-
1064
- /**
1065
- * Internal RouterMatcher
1066
- *
1067
- * @internal
1068
- */
1069
- export declare interface RouterMatcher {
1070
- addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void;
1071
- removeRoute: {
1072
- (matcher: RouteRecordMatcher): void;
1073
- (name: RouteRecordName): void;
1074
- };
1075
- getRoutes: () => RouteRecordMatcher[];
1076
- getRecordMatcher: (name: RouteRecordName) => RouteRecordMatcher | undefined;
1077
- /**
1078
- * Resolves a location. Gives access to the route record that corresponds to the actual path as well as filling the corresponding params objects
1079
- *
1080
- * @param location - MatcherLocationRaw to resolve to a url
1081
- * @param currentLocation - MatcherLocation of the current location
1082
- */
1083
- resolve: (location: MatcherLocationRaw, currentLocation: MatcherLocation) => MatcherLocation;
1084
- }
1085
-
1086
- /**
1087
- * Options to initialize a {@link Router} instance.
1088
- */
1089
- export declare interface RouterOptions extends PathParserOptions {
1090
- /**
1091
- * History implementation used by the router. Most web applications should use
1092
- * `createWebHistory` but it requires the server to be properly configured.
1093
- * You can also use a _hash_ based history with `createWebHashHistory` that
1094
- * does not require any configuration on the server but isn't handled at all
1095
- * by search engines and does poorly on SEO.
1096
- *
1097
- * @example
1098
- * ```js
1099
- * createRouter({
1100
- * history: createWebHistory(),
1101
- * // other options...
1102
- * })
1103
- * ```
1104
- */
1105
- history: RouterHistory;
1106
- /**
1107
- * Initial list of routes that should be added to the router.
1108
- */
1109
- routes: RouteRecordRaw[];
1110
- /**
1111
- * Function to control scrolling when navigating between pages. Can return a
1112
- * Promise to delay scrolling. Check {@link ScrollBehavior}.
1113
- *
1114
- * @example
1115
- * ```js
1116
- * function scrollBehavior(to, from, savedPosition) {
1117
- * // `to` and `from` are both route locations
1118
- * // `savedPosition` can be null if there isn't one
1119
- * }
1120
- * ```
1121
- */
1122
- scrollBehavior?: RouterScrollBehavior;
1123
- /**
1124
- * Custom implementation to parse a query. See its counterpart,
1125
- * {@link RouterOptions.stringifyQuery}.
1126
- *
1127
- * @example
1128
- * Let's say you want to use the package {@link https://github.com/ljharb/qs | qs}
1129
- * to parse queries, you can provide both `parseQuery` and `stringifyQuery`:
1130
- * ```js
1131
- * import qs from 'qs'
1132
- *
1133
- * createRouter({
1134
- * // other options...
1135
- * parseQuery: qs.parse,
1136
- * stringifyQuery: qs.stringify,
1137
- * })
1138
- * ```
1139
- */
1140
- parseQuery?: typeof parseQuery;
1141
- /**
1142
- * Custom implementation to stringify a query object. Should not prepend a leading `?`.
1143
- * {@link RouterOptions.parseQuery | parseQuery} counterpart to handle query parsing.
1144
- */
1145
- stringifyQuery?: typeof stringifyQuery;
1146
- /**
1147
- * Default class applied to active {@link RouterLink}. If none is provided,
1148
- * `router-link-active` will be applied.
1149
- */
1150
- linkActiveClass?: string;
1151
- /**
1152
- * Default class applied to exact active {@link RouterLink}. If none is provided,
1153
- * `router-link-exact-active` will be applied.
1154
- */
1155
- linkExactActiveClass?: string;
1156
- }
1157
-
1158
- /**
1159
- * Type of the `scrollBehavior` option that can be passed to `createRouter`.
1160
- */
1161
- export declare interface RouterScrollBehavior {
1162
- /**
1163
- * @param to - Route location where we are navigating to
1164
- * @param from - Route location where we are navigating from
1165
- * @param savedPosition - saved position if it exists, `null` otherwise
1166
- */
1167
- (to: RouteLocationNormalized, from: RouteLocationNormalizedLoaded, savedPosition: _ScrollPositionNormalized | null): Awaitable<ScrollPosition | false | void>;
1168
- }
1169
-
1170
- /**
1171
- * Component to display the current route the user is at.
1172
- */
1173
- export declare const RouterView: new () => {
1174
- $props: AllowedComponentProps & ComponentCustomProps & KNodeProps & RouterViewProps;
1175
- $slots: {
1176
- default: (arg: {
1177
- Component: KNode;
1178
- route: RouteLocationNormalizedLoaded;
1179
- }) => KNode[];
1180
- };
1181
- };
1182
-
1183
- /**
1184
- * Allows overriding the current route used by router-view. Internally this is
1185
- * used when the `route` prop is passed.
1186
- *
1187
- * @internal
1188
- */
1189
- export declare const routerViewLocationKey: InjectionKey<Ref<RouteLocationNormalizedLoaded>>;
1190
-
1191
- export declare interface RouterViewProps {
1192
- name?: string;
1193
- route?: RouteLocationNormalized;
1194
- }
1195
-
1196
- declare type ScrollPosition = ScrollPositionCoordinates | ScrollPositionElement;
1197
-
1198
- /**
1199
- * Scroll position similar to
1200
- * {@link https://developer.mozilla.org/en-US/docs/Web/API/ScrollToOptions | `ScrollToOptions`}.
1201
- * Note that not all browsers support `behavior`.
1202
- */
1203
- declare type ScrollPositionCoordinates = {
1204
- behavior?: ScrollOptions['behavior'];
1205
- left?: number;
1206
- top?: number;
1207
- };
1208
-
1209
- declare interface ScrollPositionElement extends ScrollToOptions {
1210
- /**
1211
- * A valid CSS selector. Note some characters must be escaped in id selectors (https://mathiasbynens.be/notes/css-escapes).
1212
- * @example
1213
- * Here are a few examples:
1214
- *
1215
- * - `.title`
1216
- * - `.content:first-child`
1217
- * - `#marker`
1218
- * - `#marker\~with\~symbols`
1219
- * - `#marker.with.dot`: selects `class="with dot" id="marker"`, not `id="marker.with.dot"`
1220
- *
1221
- */
1222
- el: string | Element;
1223
- }
1224
-
1225
- /**
1226
- * Internal normalized version of {@link ScrollPositionCoordinates} that always
1227
- * has `left` and `top` coordinates.
1228
- *
1229
- * @internal
1230
- */
1231
- declare type _ScrollPositionNormalized = {
1232
- behavior?: ScrollOptions['behavior'];
1233
- left: number;
1234
- top: number;
1235
- };
1236
-
1237
- /**
1238
- * Initial route location where the router is. Can be used in navigation guards
1239
- * to differentiate the initial navigation.
1240
- *
1241
- * @example
1242
- * ```js
1243
- * import { START_LOCATION } from 'kdu-router'
1244
- *
1245
- * router.beforeEach((to, from) => {
1246
- * if (from === START_LOCATION) {
1247
- * // initial navigation
1248
- * }
1249
- * })
1250
- * ```
1251
- */
1252
- export declare const START_LOCATION: RouteLocationNormalizedLoaded;
1253
-
1254
- /**
1255
- * Stringifies a {@link LocationQueryRaw} object. Like `URLSearchParams`, it
1256
- * doesn't prepend a `?`
1257
- *
1258
- * @internal
1259
- *
1260
- * @param query - query object to stringify
1261
- * @returns string version of the query without the leading `?`
1262
- */
1263
- export declare function stringifyQuery(query: LocationQueryRaw): string;
1264
-
1265
- export declare function useLink(props: UseLinkOptions): {
1266
- route: ComputedRef<RouteLocation & {
1267
- href: string;
1268
- }>;
1269
- href: ComputedRef<string>;
1270
- isActive: ComputedRef<boolean>;
1271
- isExactActive: ComputedRef<boolean>;
1272
- navigate: (e?: MouseEvent) => Promise<void | NavigationFailure>;
1273
- };
1274
-
1275
- export declare type UseLinkOptions = KduUseOptions<RouterLinkOptions>;
1276
-
1277
- /**
1278
- * Returns the current route location. Equivalent to using `$route` inside
1279
- * templates.
1280
- */
1281
- export declare function useRoute(): RouteLocationNormalizedLoaded;
1282
-
1283
- /**
1284
- * Returns the router instance. Equivalent to using `$router` inside
1285
- * templates.
1286
- */
1287
- export declare function useRouter(): Router;
1288
-
1289
- /**
1290
- * Allows overriding the router view depth to control which component in
1291
- * `matched` is rendered. rvd stands for Router View Depth
1292
- *
1293
- * @internal
1294
- */
1295
- export declare const viewDepthKey: InjectionKey<number>;
1296
-
1297
- export { }
1298
-
1299
- declare module '@kdujs/runtime-core' {
1300
- export interface ComponentCustomOptions {
1301
- /**
1302
- * Guard called when the router is navigating to the route that is rendering
1303
- * this component from a different route. Differently from `beforeRouteUpdate`
1304
- * and `beforeRouteLeave`, `beforeRouteEnter` does not have access to the
1305
- * component instance through `this` because it triggers before the component
1306
- * is even mounted.
1307
- *
1308
- * @param to - RouteLocationRaw we are navigating to
1309
- * @param from - RouteLocationRaw we are navigating from
1310
- * @param next - function to validate, cancel or modify (by redirecting) the
1311
- * navigation
1312
- */
1313
- beforeRouteEnter?: NavigationGuardWithThis<undefined>
1314
-
1315
- /**
1316
- * Guard called whenever the route that renders this component has changed but
1317
- * it is reused for the new route. This allows you to guard for changes in
1318
- * params, the query or the hash.
1319
- *
1320
- * @param to - RouteLocationRaw we are navigating to
1321
- * @param from - RouteLocationRaw we are navigating from
1322
- * @param next - function to validate, cancel or modify (by redirecting) the
1323
- * navigation
1324
- */
1325
- beforeRouteUpdate?: NavigationGuard
1326
-
1327
- /**
1328
- * Guard called when the router is navigating away from the current route that
1329
- * is rendering this component.
1330
- *
1331
- * @param to - RouteLocationRaw we are navigating to
1332
- * @param from - RouteLocationRaw we are navigating from
1333
- * @param next - function to validate, cancel or modify (by redirecting) the
1334
- * navigation
1335
- */
1336
- beforeRouteLeave?: NavigationGuard
1337
- }
1338
-
1339
- export interface ComponentCustomProperties {
1340
- /**
1341
- * Normalized current location. See {@link RouteLocationNormalizedLoaded}.
1342
- */
1343
- $route: RouteLocationNormalizedLoaded
1344
- /**
1345
- * {@link Router} instance used by the application.
1346
- */
1347
- $router: Router
1348
- }
1349
- }