kdu-router 3.5.4 → 4.0.0

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