@kitbag/router 0.14.2 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { App } from 'vue';
1
2
  import { AsyncComponentLoader } from 'vue';
2
3
  import { Component } from 'vue';
3
4
  import { ComponentOptionsMixin } from 'vue';
@@ -6,7 +7,6 @@ import { ComputedRef } from 'vue';
6
7
  import { DefineComponent } from 'vue';
7
8
  import { FunctionalComponent } from 'vue';
8
9
  import { MaybeRefOrGetter } from 'vue';
9
- import { Plugin as Plugin_2 } from 'vue';
10
10
  import { PublicProps } from 'vue';
11
11
  import { Ref } from 'vue';
12
12
  import { UnwrapRef } from 'vue';
@@ -38,6 +38,17 @@ export declare type AddAfterRouteHook = (hook: AfterRouteHook) => RouteHookRemov
38
38
  */
39
39
  export declare type AddBeforeRouteHook = (hook: BeforeRouteHook) => RouteHookRemove;
40
40
 
41
+ export declare type AddComponentAfterRouteHook = (hook: AfterRouteHookRegistration) => RouteHookRemove;
42
+
43
+ export declare type AddComponentBeforeRouteHook = (hook: BeforeRouteHookRegistration) => RouteHookRemove;
44
+
45
+ export declare type AddGlobalRouteHooks = (hooks: RouteHooks) => void;
46
+
47
+ export declare type AfterHookContext = {
48
+ to: ResolvedRoute;
49
+ from: ResolvedRoute | null;
50
+ };
51
+
41
52
  /**
42
53
  * Represents a function called after a route change has occurred.
43
54
  * @param to - {@link ResolvedRoute} The resolved route the router has navigated to.
@@ -56,6 +67,12 @@ export declare type AfterRouteHookContext = RouteHookContext;
56
67
  */
57
68
  export declare type AfterRouteHookLifecycle = 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'onAfterRouteLeave';
58
69
 
70
+ export declare type AfterRouteHookRegistration = {
71
+ lifecycle: 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'onAfterRouteLeave';
72
+ hook: AfterRouteHook;
73
+ depth: number;
74
+ };
75
+
59
76
  /**
60
77
  * Type for responses from an after route hook, which may indicate different outcomes such as success, push, or reject.
61
78
  * @template TRoutes - The type of the routes configuration.
@@ -66,6 +83,13 @@ declare type AllPropertiesAreOptional<T> = Record<string, unknown> extends T ? t
66
83
 
67
84
  declare type AsNamedRoute<T extends Route> = IsRouteUnnamed<T> extends true ? never : T;
68
85
 
86
+ /**
87
+ * Converts a type to a string if it is a string, otherwise returns never.
88
+ * Specifically useful when using keyof T to produce a union of strings
89
+ * rather than string | number | symbol.
90
+ */
91
+ declare type AsString<T> = T extends string ? T : never;
92
+
69
93
  /**
70
94
  * Converts a string to a valid URL.
71
95
  * @param value - The string to convert.
@@ -73,6 +97,11 @@ declare type AsNamedRoute<T extends Route> = IsRouteUnnamed<T> extends true ? ne
73
97
  */
74
98
  export declare function asUrl(value: string): Url;
75
99
 
100
+ export declare type BeforeHookContext = {
101
+ to: ResolvedRoute;
102
+ from: ResolvedRoute | null;
103
+ };
104
+
76
105
  /**
77
106
  * Represents a function called before a route change, potentially altering the routing operation.
78
107
  * @param to - {@link ResolvedRoute} The resolved route the router is navigating to.
@@ -94,15 +123,19 @@ export declare type BeforeRouteHookContext = RouteHookContext & {
94
123
  */
95
124
  export declare type BeforeRouteHookLifecycle = 'onBeforeRouteEnter' | 'onBeforeRouteUpdate' | 'onBeforeRouteLeave';
96
125
 
126
+ export declare type BeforeRouteHookRegistration = {
127
+ lifecycle: 'onBeforeRouteEnter' | 'onBeforeRouteUpdate' | 'onBeforeRouteLeave';
128
+ hook: BeforeRouteHook;
129
+ depth: number;
130
+ };
131
+
97
132
  /**
98
133
  * Type for responses from a before route hook, which may indicate different outcomes such as success, push, reject, or abort.
99
134
  * @template TRoutes - The type of the routes configuration.
100
135
  */
101
136
  export declare type BeforeRouteHookResponse = CallbackSuccessResponse | CallbackPushResponse | CallbackRejectResponse | CallbackAbortResponse;
102
137
 
103
- declare const builtInRejections: ['NotFound'];
104
-
105
- declare type BuiltInRejectionType = typeof builtInRejections[number];
138
+ declare type BuiltInRejectionType = 'NotFound';
106
139
 
107
140
  /**
108
141
  * Defines the structure of an aborted callback response.
@@ -258,9 +291,11 @@ declare type CreateRouteOptionsMatched<TName extends string | undefined = string
258
291
  * const router = createRouter(routes)
259
292
  * ```
260
293
  */
261
- export declare function createRouter<const TRoutes extends Routes, const TOptions extends RouterOptions>(routes: TRoutes, options?: TOptions): Router<TRoutes, TOptions>;
294
+ export declare function createRouter<const TRoutes extends Routes, const TOptions extends RouterOptions, const TPlugin extends RouterPlugin = EmptyRouterPlugin>(routes: TRoutes, options?: TOptions, plugins?: TPlugin[]): Router<TRoutes, TOptions, TPlugin>;
295
+
296
+ export declare function createRouter<const TRoutes extends Routes, const TOptions extends RouterOptions, const TPlugin extends RouterPlugin = EmptyRouterPlugin>(routes: TRoutes[], options?: TOptions, plugins?: TPlugin[]): Router<TRoutes, TOptions, TPlugin>;
262
297
 
263
- export declare function createRouter<const TRoutes extends Routes, const TOptions extends RouterOptions>(arrayOfRoutes: TRoutes[], options?: TOptions): Router<TRoutes, TOptions>;
298
+ export declare function createRouterPlugin<TRoutes extends Routes = [], TRejections extends Record<string, Component> = {}>(plugin: Partial<RouterPlugin<TRoutes, TRejections>>): RouterPlugin<TRoutes, TRejections>;
264
299
 
265
300
  /**
266
301
  * An error thrown when duplicate parameters are detected in a route.
@@ -276,6 +311,11 @@ export declare class DuplicateParamsError extends Error {
276
311
  constructor(paramName: string);
277
312
  }
278
313
 
314
+ declare type EmptyRouterPlugin = {
315
+ routes: [];
316
+ rejections: {};
317
+ };
318
+
279
319
  /**
280
320
  * Extracts the parameter name from a string, handling optional parameters denoted by a leading '?'.
281
321
  * @template TParam - The string from which to extract the parameter name.
@@ -452,6 +492,11 @@ declare type IsRouteUnnamed<T extends Route> = StringHasValue<T['name']> extends
452
492
  */
453
493
  export declare function isUrl(value: unknown): value is Url;
454
494
 
495
+ /**
496
+ * Extracts the keys of a union type.
497
+ */
498
+ declare type KeysOfUnion<T> = T extends T ? keyof T : never;
499
+
455
500
  declare type MakeOptional<T> = {
456
501
  [P in WithOptionalProperties<T>]?: T[P];
457
502
  } & {
@@ -586,7 +631,7 @@ declare type PathParamsWithParamNameExtracted<TPath extends string> = {
586
631
  /**
587
632
  * Determines what assets are prefetched. A boolean enables or disables all prefetching.
588
633
  */
589
- declare type PrefetchConfig = boolean | PrefetchStrategy | PrefetchConfigOptions;
634
+ export declare type PrefetchConfig = boolean | PrefetchStrategy | PrefetchConfigOptions;
590
635
 
591
636
  declare type PrefetchConfigOptions = {
592
637
  /**
@@ -606,12 +651,12 @@ declare type PrefetchConfigOptions = {
606
651
  * eager: Fetched immediately
607
652
  * lazy: Fetched when visible
608
653
  */
609
- declare type PrefetchStrategy = 'eager' | 'lazy';
654
+ export declare type PrefetchStrategy = 'eager' | 'lazy' | 'intent';
610
655
 
611
656
  /**
612
657
  * Context provided to props callback functions
613
658
  */
614
- declare type PropsCallbackContext = {
659
+ export declare type PropsCallbackContext = {
615
660
  push: CallbackContext['push'];
616
661
  replace: CallbackContext['replace'];
617
662
  reject: CallbackContext['reject'];
@@ -649,7 +694,7 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
649
694
  [K in keyof ExtractQueryParamsFromQueryString<T> as ExtractParamName<K>]?: Param;
650
695
  };
651
696
 
652
- declare type QuerySource = ConstructorParameters<typeof URLSearchParams>[0];
697
+ export declare type QuerySource = ConstructorParameters<typeof URLSearchParams>[0];
653
698
 
654
699
  /**
655
700
  * Represents the state of currently registered router, and route meta. Used to provide correct type context for
@@ -671,8 +716,8 @@ export declare interface Register {
671
716
  /**
672
717
  * Represents the possible Rejections registered within {@link Register}
673
718
  */
674
- export declare type RegisteredRejectionType = Register extends {
675
- router: Router<infer __TRoutes extends Routes, infer TOptions extends RouterOptions>;
719
+ export declare type RegisteredRejectionType<T = Register> = T extends {
720
+ router: Router<Routes, infer TOptions extends RouterOptions>;
676
721
  } ? keyof TOptions['rejections'] | BuiltInRejectionType : BuiltInRejectionType;
677
722
 
678
723
  /**
@@ -683,7 +728,7 @@ export declare type RegisteredRouteMap = RoutesMap<RegisteredRoutes>;
683
728
  /**
684
729
  * Represents the Router property within {@link Register}
685
730
  */
686
- export declare type RegisteredRouter = Register extends {
731
+ export declare type RegisteredRouter<T = Register> = T extends {
687
732
  router: infer TRouter;
688
733
  } ? TRouter : Router;
689
734
 
@@ -710,7 +755,7 @@ export declare type RegisteredRouterRoute = RegisteredRouter['route'];
710
755
  /**
711
756
  * Represents the Router routes property within {@link Register}
712
757
  */
713
- export declare type RegisteredRoutes = Register extends {
758
+ export declare type RegisteredRoutes<T = Register> = T extends {
714
759
  router: Router<infer TRoutes extends Routes>;
715
760
  } ? TRoutes : Route[];
716
761
 
@@ -831,6 +876,10 @@ declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesName<TRout
831
876
  */
832
877
  export declare type RouteHook = BeforeRouteHook | AfterRouteHook;
833
878
 
879
+ export declare type RouteHookAfterRunner = (context: AfterHookContext) => Promise<AfterRouteHookResponse>;
880
+
881
+ export declare type RouteHookBeforeRunner = (context: BeforeHookContext) => Promise<BeforeRouteHookResponse>;
882
+
834
883
  /**
835
884
  * Context provided to route hooks, containing context of previous route and functions for triggering rejections and push/replace to another route.
836
885
  */
@@ -857,24 +906,40 @@ export declare type RouteHookRemove = () => void;
857
906
  */
858
907
  export declare type RouteHookResponse = BeforeRouteHookResponse | AfterRouteHookResponse;
859
908
 
909
+ declare class RouteHooks {
910
+ onBeforeRouteEnter: Set<BeforeRouteHook>;
911
+ onBeforeRouteUpdate: Set<BeforeRouteHook>;
912
+ onBeforeRouteLeave: Set<BeforeRouteHook>;
913
+ onAfterRouteEnter: Set<AfterRouteHook>;
914
+ onAfterRouteUpdate: Set<AfterRouteHook>;
915
+ onAfterRouteLeave: Set<AfterRouteHook>;
916
+ }
917
+
918
+ export declare type RouteHookTiming = 'global' | 'component';
919
+
860
920
  /**
861
921
  * Represents additional metadata associated with a route, customizable via declaration merging.
862
922
  */
863
- export declare type RouteMeta = Register extends {
923
+ export declare type RouteMeta<T = Register> = T extends {
864
924
  routeMeta: infer RouteMeta extends Record<string, unknown>;
865
925
  } ? RouteMeta : Record<string, unknown>;
866
926
 
867
927
  declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
868
928
 
869
- export declare type Router<TRoutes extends Routes = any, __TOptions extends RouterOptions = any> = Plugin_2 & {
929
+ export declare type Router<TRoutes extends Routes = any, TOptions extends RouterOptions = any, TPlugin extends RouterPlugin = any> = {
930
+ /**
931
+ * Installs the router into a Vue application instance.
932
+ * @param app The Vue application instance to install the router into
933
+ */
934
+ install: (app: App) => void;
870
935
  /**
871
936
  * Manages the current route state.
872
937
  */
873
- route: RouterRoutes<TRoutes>;
938
+ route: RouterRoutes<TRoutes> | RouterRoutes<TPlugin['routes']>;
874
939
  /**
875
940
  * Creates a ResolvedRoute record for a given route name and params.
876
941
  */
877
- resolve: RouterResolve<TRoutes>;
942
+ resolve: RouterResolve<TRoutes | TPlugin['routes']>;
878
943
  /**
879
944
  * Creates a ResolvedRoute record for a given URL.
880
945
  */
@@ -882,15 +947,15 @@ export declare type Router<TRoutes extends Routes = any, __TOptions extends Rout
882
947
  /**
883
948
  * Navigates to a specified path or route object in the history stack, adding a new entry.
884
949
  */
885
- push: RouterPush<TRoutes>;
950
+ push: RouterPush<TRoutes | TPlugin['routes']>;
886
951
  /**
887
952
  * Replaces the current entry in the history stack with a new one.
888
953
  */
889
- replace: RouterReplace<TRoutes>;
954
+ replace: RouterReplace<TRoutes | TPlugin['routes']>;
890
955
  /**
891
956
  * Handles route rejection based on a specified rejection type.
892
957
  */
893
- reject: RouterReject;
958
+ reject: RouterReject<keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
894
959
  /**
895
960
  * Forces the router to re-evaluate the current route.
896
961
  */
@@ -943,13 +1008,14 @@ export declare type Router<TRoutes extends Routes = any, __TOptions extends Rout
943
1008
  * Initializes the router based on the initial route. Automatically called when the router is installed. Calling this more than once has no effect.
944
1009
  */
945
1010
  start: () => Promise<void>;
1011
+ /**
1012
+ * Stops the router and teardown any listeners.
1013
+ */
1014
+ stop: () => void;
946
1015
  };
947
1016
 
948
1017
  declare type RouterHistoryMode = 'auto' | 'browser' | 'memory' | 'hash';
949
1018
 
950
- /**
951
- * @ignore
952
- */
953
1019
  export declare const RouterLink: __VLS_WithTemplateSlots_2<DefineComponent< {
954
1020
  /**
955
1021
  * The url string to navigate to or a callback that returns a url string
@@ -968,9 +1034,9 @@ to: Url | ResolvedRoute | ToCallback;
968
1034
  * Determines what assets are prefetched when router-link is rendered for this route. Overrides route level prefetch.
969
1035
  */
970
1036
  prefetch?: PrefetchConfig;
971
- } & RouterPushOptions & {}>, {
1037
+ } & RouterPushOptions> & Readonly<{}>, {
972
1038
  prefetch: PrefetchConfig;
973
- }, {}, {}, {}, string, ComponentProvideOptions, false, {}>, Readonly<{
1039
+ }, {}, {}, {}, string, ComponentProvideOptions, false, {}, HTMLAnchorElement>, Readonly<{
974
1040
  default?: (props: {
975
1041
  route: ResolvedRoute | undefined;
976
1042
  isMatch: boolean;
@@ -997,7 +1063,7 @@ export declare class RouterNotInstalledError extends Error {
997
1063
  /**
998
1064
  * Options to initialize a {@link Router} instance.
999
1065
  */
1000
- export declare type RouterOptions = {
1066
+ export declare type RouterOptions = WithHooks & {
1001
1067
  /**
1002
1068
  * Initial URL for the router to use. Required if using Node environment. Defaults to window.location when using browser.
1003
1069
  *
@@ -1025,6 +1091,17 @@ export declare type RouterOptions = {
1025
1091
  rejections?: Partial<Record<string, Component>>;
1026
1092
  };
1027
1093
 
1094
+ export declare type RouterPlugin<TRoutes extends Routes = Routes, TRejections extends Record<string, Component> = Record<string, Component>> = {
1095
+ routes: TRoutes;
1096
+ rejections: TRejections;
1097
+ onBeforeRouteEnter?: MaybeArray<BeforeRouteHook>;
1098
+ onAfterRouteEnter?: MaybeArray<AfterRouteHook>;
1099
+ onBeforeRouteUpdate?: MaybeArray<BeforeRouteHook>;
1100
+ onAfterRouteUpdate?: MaybeArray<AfterRouteHook>;
1101
+ onBeforeRouteLeave?: MaybeArray<BeforeRouteHook>;
1102
+ onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
1103
+ };
1104
+
1028
1105
  declare type RouterPush<TRoutes extends Routes = any> = {
1029
1106
  <TSource extends RoutesName<TRoutes>>(name: TSource, ...args: RouterPushArgs<TRoutes, TSource>): Promise<void>;
1030
1107
  (route: ResolvedRoute, options?: RouterPushOptions): Promise<void>;
@@ -1033,17 +1110,29 @@ declare type RouterPush<TRoutes extends Routes = any> = {
1033
1110
 
1034
1111
  declare type RouterPushArgs<TRoutes extends Routes, TSource extends RoutesName<TRoutes>> = AllPropertiesAreOptional<RouteParamsByKey<TRoutes, TSource>> extends true ? [params?: RouteParamsByKey<TRoutes, TSource>, options?: RouterPushOptions<RouteStateByName<TRoutes, TSource>>] : [params: RouteParamsByKey<TRoutes, TSource>, options?: RouterPushOptions<RouteStateByName<TRoutes, TSource>>];
1035
1112
 
1036
- declare type RouterPushOptions<TState = unknown> = {
1113
+ export declare type RouterPushOptions<TState = unknown> = {
1114
+ /**
1115
+ * The query string to add to the url.
1116
+ */
1037
1117
  query?: QuerySource;
1118
+ /**
1119
+ * The hash to append to the url.
1120
+ */
1038
1121
  hash?: string;
1122
+ /**
1123
+ * Whether to replace the current history entry.
1124
+ */
1039
1125
  replace?: boolean;
1126
+ /**
1127
+ * State values to pass to the route.
1128
+ */
1040
1129
  state?: Partial<TState>;
1041
1130
  };
1042
1131
 
1043
- export declare type RouterReject = (type: RegisteredRejectionType) => void;
1132
+ declare type RouterReject<TRejectionType extends PropertyKey> = (type: AsString<TRejectionType> | BuiltInRejectionType) => void;
1044
1133
 
1045
1134
  declare type RouterRejection = Ref<null | {
1046
- type: RegisteredRejectionType;
1135
+ type: string;
1047
1136
  component: Component;
1048
1137
  }>;
1049
1138
 
@@ -1071,7 +1160,7 @@ declare type RouterResolveOptions<TState = unknown> = {
1071
1160
  state?: Partial<TState>;
1072
1161
  };
1073
1162
 
1074
- declare type RouterRoute<TRoute extends ResolvedRoute = ResolvedRoute> = {
1163
+ export declare type RouterRoute<TRoute extends ResolvedRoute = ResolvedRoute> = {
1075
1164
  readonly id: TRoute['id'];
1076
1165
  readonly name: TRoute['name'];
1077
1166
  readonly matched: TRoute['matched'];
@@ -1092,14 +1181,11 @@ export declare type RouterRoutes<TRoutes extends Routes> = {
1092
1181
  [K in keyof TRoutes]: RouterRoute<ResolvedRoute<TRoutes[K]>>;
1093
1182
  }[number];
1094
1183
 
1095
- /**
1096
- * @ignore
1097
- */
1098
1184
  export declare const RouterView: __VLS_WithTemplateSlots<DefineComponent< {
1099
1185
  name?: string;
1100
1186
  }, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<{
1101
1187
  name?: string;
1102
- } & {}>, {}, {}, {}, {}, string, ComponentProvideOptions, false, {}>, Readonly<{
1188
+ }> & Readonly<{}>, {}, {}, {}, {}, string, ComponentProvideOptions, false, {}, any>, Readonly<{
1103
1189
  default?: (props: {
1104
1190
  route: RouterRoute;
1105
1191
  component: Component;
@@ -1138,7 +1224,7 @@ declare type RouteWithMatch<TRoute extends RouterRoute, TRouteName extends TRout
1138
1224
 
1139
1225
  declare type StringHasValue<T> = string extends T ? true : '' extends T ? false : T extends string ? true : false;
1140
1226
 
1141
- declare type ToCallback = (resolve: RegisteredRouter['resolve']) => ResolvedRoute | Url | undefined;
1227
+ export declare type ToCallback = (resolve: RegisteredRouter['resolve']) => ResolvedRoute | Url | undefined;
1142
1228
 
1143
1229
  declare type ToHash<T extends string | Hash | undefined> = T extends string ? Hash<T> : T extends undefined ? Hash<''> : unknown extends T ? Hash<''> : T;
1144
1230
 
@@ -1295,7 +1381,7 @@ export declare function withDefault<TParam extends Param>(param: TParam, default
1295
1381
  /**
1296
1382
  * Defines route hooks that can be applied before entering, updating, or leaving a route, as well as after these events.
1297
1383
  */
1298
- declare type WithHooks = {
1384
+ export declare type WithHooks = {
1299
1385
  onBeforeRouteEnter?: MaybeArray<BeforeRouteHook>;
1300
1386
  onBeforeRouteUpdate?: MaybeArray<BeforeRouteHook>;
1301
1387
  onBeforeRouteLeave?: MaybeArray<BeforeRouteHook>;