@kitbag/router 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,7 +11,7 @@ Type safe router for Vue.js
11
11
 
12
12
  ## Getting Started
13
13
 
14
- Get Started with our [documentation](https://kitbag-router.netlify.app/)
14
+ Get Started with our [documentation](https://kitbag-router.netlify.app/) or our [intro video](https://kitbag-router.netlify.app/)
15
15
 
16
16
  ## Installation
17
17
 
@@ -75,26 +75,27 @@ declare module '@kitbag/router' {
75
75
  To navigate to another route, you can use `router.push`. This method will update the URL for the browser and also add the URL into the history so when a user uses the back button on their browser it will behave as expected.
76
76
 
77
77
  ```ts
78
+ import { defineAsyncComponent } from 'vue'
78
79
  import { createRoute, useRouter } from '@kitbag/router'
79
80
 
80
81
  const user = createRoute({
81
82
  name: 'user',
82
83
  path: '/user',
83
- component: ...,
84
+ component: defineAsyncComponent(() => import('./UserPage.vue')),
84
85
  })
85
86
 
86
87
  const profile = createRoute({
87
88
  parent: user,
88
89
  name: 'profile',
89
90
  path: '/profile',
90
- component: ...,
91
+ component: defineAsyncComponent(() => import('./ProfilePage.vue')),
91
92
  })
92
93
 
93
94
  const settings = createRoute({
94
95
  parent: user,
95
96
  name: 'settings',
96
97
  path: '/settings',
97
- component: ...,
98
+ component: defineAsyncComponent(() => import('./SettingsPage.vue')),
98
99
  })
99
100
 
100
101
  const router = useRouter([user, profile, settings])
@@ -109,14 +110,14 @@ router.push('/user/settings')
109
110
  router.push('https://github.com/kitbagjs/router')
110
111
  ```
111
112
 
112
- This `source` argument is type safe, expecting either a Url or a valid route "key". Url is any string that starts with "http", "https", or a forward slash "/". Route key is a string of route names joined by a period `.` that lead to a non-disabled route. Additionally if using the route key, push will require params be passed in if there are any.
113
+ This `source` argument is type safe, expecting either a Url or a valid route "key". Url is any string that starts with "http", "https", or a forward slash "/". Route key is a string of route names joined by a period `.`. Additionally if using the route key, push will require params be passed in if there are any.
113
114
 
114
115
  ## Update
115
116
 
116
117
  If you only wish to change the params on the current route you can use `router.route.update`.
117
118
 
118
119
  ```ts
119
- router.route.update('myParam': 123)
120
+ router.route.update('myParam', 123)
120
121
  ```
121
122
 
122
123
  or for setting multiple params at once
@@ -162,6 +163,6 @@ This component gives the router the power to change the URL without reloading th
162
163
  [netlify-badge]: https://api.netlify.com/api/v1/badges/c12f79b8-49f9-4529-bc23-f8ffca8919a3/deploy-status
163
164
  [netlify-url]: https://app.netlify.com/sites/kitbag-router/deploys
164
165
  [discord-badge]: https://img.shields.io/discord/1079625926024900739?logo=discord&label=Discord
165
- [discord-url]: https://discord.gg/UT7JrAxU
166
+ [discord-url]: https://discord.gg/zw7dpcc5HV
166
167
  [stackblitz-badge]: https://developer.stackblitz.com/img/open_in_stackblitz_small.svg
167
168
  [stackblitz-url]: https://stackblitz.com/~/github.com/kitbagjs/router-preview
@@ -88,6 +88,8 @@ export declare type AfterRouteHookResponse<TRoutes extends Routes> = RouteHookSu
88
88
 
89
89
  declare type AllPropertiesAreOptional<T> = Record<string, unknown> extends T ? true : IsEmptyObject<OnlyRequiredProperties<T>>;
90
90
 
91
+ declare type AsNamedRoute<T extends Route> = IsRouteUnnamed<T> extends true ? never : T;
92
+
91
93
  /**
92
94
  * Represents a function called before a route change, potentially altering the routing operation.
93
95
  * @param to - {@link ResolvedRoute} The resolved route the router is navigating to.
@@ -121,6 +123,8 @@ declare type BuiltInRejectionType = typeof builtInRejections[number];
121
123
 
122
124
  declare type CombineKey<TParentKey extends string | undefined, TChildKey extends string | undefined> = StringHasValue<TParentKey> extends true ? StringHasValue<TChildKey> extends true ? `${TParentKey}.${TChildKey}` : TParentKey : StringHasValue<TChildKey> extends true ? TChildKey : '';
123
125
 
126
+ declare type CombineMeta<TParent extends Record<string, unknown>, TChild extends Record<string, unknown>> = TParent & TChild;
127
+
124
128
  declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TParent> extends {
125
129
  path: infer TParentPath extends string;
126
130
  params: infer TParentParams extends Record<string, unknown>;
@@ -139,6 +143,8 @@ declare type CombineQuery<TParent extends Query, TChild extends Query> = ToQuery
139
143
 
140
144
  declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
141
145
 
146
+ declare type CombineState<TParent extends Record<string, Param>, TChild extends Record<string, Param>> = TParent & TChild;
147
+
142
148
  /**
143
149
  * Creates a component wrapper which has no props itself but mounts another component within while binding its props
144
150
  *
@@ -157,13 +163,17 @@ declare type CombineQueryString<TParent extends string | undefined, TChild exten
157
163
  * })
158
164
  * ```
159
165
  */
160
- export declare function component<TComponent extends Component>(component: TComponent, props: PropsGetter<TComponent>): Component;
166
+ export declare function component<TComponent extends Component>(component: TComponent, props: ComponentPropsGetter<TComponent>): Component;
167
+
168
+ export declare type ComponentProps<TComponent extends Component> = TComponent extends Constructor ? InstanceType<TComponent>['$props'] : TComponent extends AsyncComponentLoader<infer T extends Component> ? ComponentProps<T> : TComponent extends FunctionalComponent<infer T> ? T : never;
169
+
170
+ declare type ComponentPropsGetter<TComponent extends Component> = () => MaybePromise<ComponentProps<TComponent>>;
161
171
 
162
172
  declare type Constructor = new (...args: any) => any;
163
173
 
164
- export declare function createExternalRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const THost extends string | Host | undefined = undefined>(options: CreateRouteOptionsWithoutParent<TName, TPath, TQuery, THost>): Route<ToKey<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>>;
174
+ export declare function createExternalRoute<const THost extends string | Host, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptions<TName, TPath, TQuery> & WithHost<THost> & WithoutParent): Route<ToKey<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>>;
165
175
 
166
- export declare function createExternalRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptionsWithParent<TParent, TName, TPath, TQuery, Host<'', {}>>): Route<CombineKey<TParent['key'], ToKey<TName>>, ToHost<Host<'', {}>>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
176
+ export declare function createExternalRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptions<TName, TPath, TQuery> & WithoutHost & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
167
177
 
168
178
  export declare function createParam<TParam extends ParamWithDefault>(param: TParam): TParam;
169
179
 
@@ -171,19 +181,23 @@ export declare function createParam<TParam extends Param>(param: TParam): ParamG
171
181
 
172
182
  export declare function createParam<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
173
183
 
174
- export declare function createRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptionsWithoutParent<TName, TPath, TQuery>): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
184
+ export declare function createRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithoutParent & (WithState<TState> | WithoutState)): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, TMeta, TState>;
175
185
 
176
- export declare function createRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptionsWithParent<TParent, TName, TPath, TQuery>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
186
+ export declare function createRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>>;
177
187
 
178
- export declare type CreateRouteOptions<TName extends string | undefined = string, TPath extends string | Path | undefined = string | Path | undefined, TQuery extends string | Query | undefined = string | Query | undefined, THost extends string | Host | undefined = string | Host | undefined, _TParent extends Route | undefined = undefined> = WithComponent & WithHooks & {
188
+ export declare function createRoute<TComponent extends Component, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery>> & WithoutParent & (WithState<TState> | WithoutState)): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, TMeta, TState>;
189
+
190
+ export declare function createRoute<TComponent extends Component, const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>>;
191
+
192
+ export declare function createRoute<TComponents extends Record<string, Component>, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery>> & WithoutParent & (WithState<TState> | WithoutState)): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, TMeta, TState>;
193
+
194
+ export declare function createRoute<TComponents extends Record<string, Component>, const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>>;
195
+
196
+ export declare type CreateRouteOptions<TName extends string | undefined = string | undefined, TPath extends string | Path | undefined = string | Path | undefined, TQuery extends string | Query | undefined = string | Query | undefined, TMeta extends RouteMeta = RouteMeta> = {
179
197
  /**
180
198
  * Name for route, used to create route keys and in navigation.
181
199
  */
182
200
  name?: TName;
183
- /**
184
- * Host part of URL.
185
- */
186
- host?: THost;
187
201
  /**
188
202
  * Path part of URL.
189
203
  */
@@ -195,25 +209,16 @@ export declare type CreateRouteOptions<TName extends string | undefined = string
195
209
  /**
196
210
  * Represents additional metadata associated with a route, customizable via declaration merging.
197
211
  */
198
- meta?: RouteMeta;
199
- disabled?: boolean;
212
+ meta?: TMeta;
200
213
  };
201
214
 
202
215
  /**
203
216
  * The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
204
217
  */
205
- export declare type CreateRouteOptionsWithMeta = CreateRouteOptions & {
218
+ declare type CreateRouteOptionsMatched = CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithComponent | WithComponents | WithoutComponents) & (WithParent | WithoutParent) & (WithState | WithoutState) & {
206
219
  meta: RouteMeta;
207
220
  };
208
221
 
209
- declare type CreateRouteOptionsWithoutParent<TName extends string | undefined = undefined, TPath extends string | Path | undefined = undefined, TQuery extends string | Query | undefined = undefined, THost extends string | Host | undefined = undefined> = CreateRouteOptions<TName, TPath, TQuery, THost> & {
210
- parent?: never;
211
- };
212
-
213
- declare type CreateRouteOptionsWithParent<TParent extends Route, TName extends string | undefined = undefined, TPath extends string | Path | undefined = undefined, TQuery extends string | Query | undefined = undefined, THost extends string | Host | undefined = undefined> = CreateRouteOptions<TName, TPath, TQuery, THost, TParent> & {
214
- parent: TParent;
215
- };
216
-
217
222
  /**
218
223
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
219
224
  *
@@ -334,6 +339,14 @@ declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extend
334
339
  };
335
340
  } ? ExtractParamTypesWithoutLosingOptional<HostParams & PathParams & QueryParams> : Record<string, unknown>;
336
341
 
342
+ declare type ExtractRouteStateParamsAsOptional<T extends Record<string, Param>> = ExtractParamTypes<{
343
+ [K in keyof T as K extends string ? `?${K}` : never]: T[K];
344
+ }>;
345
+
346
+ declare type ExtractStateParams<TRoute> = TRoute extends {
347
+ state: infer TState extends Record<string, Param>;
348
+ } ? ExtractParamTypes<TState> : Record<string, unknown>;
349
+
337
350
  declare type Host<THost extends string = string, TParams extends HostParamsWithParamNameExtracted<THost> = Record<string, Param | undefined>> = {
338
351
  host: THost;
339
352
  params: string extends THost ? Record<string, Param> : Identity<ExtractParamsFromHostString<THost, TParams>>;
@@ -376,10 +389,6 @@ export declare function isRoute<TRouteKey extends RegisteredRoutesKey>(route: un
376
389
 
377
390
  export declare function isRoute(route: unknown, routeKey?: string, options?: IsRouteOptions): boolean;
378
391
 
379
- declare type IsRouteDisabled<T extends Route> = T extends {
380
- disabled: true;
381
- } ? true : false;
382
-
383
392
  declare type IsRouteOptions = {
384
393
  exact?: boolean;
385
394
  };
@@ -471,6 +480,10 @@ declare const paramStart = "[";
471
480
 
472
481
  export declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
473
482
 
483
+ declare type ParentPath<TParent extends Route | undefined> = TParent extends Route ? TParent['path'] : Path<'', {}>;
484
+
485
+ declare type ParentQuery<TParent extends Route | undefined> = TParent extends Route ? TParent['query'] : Query<'', {}>;
486
+
474
487
  declare type Path<TPath extends string = string, TParams extends PathParamsWithParamNameExtracted<TPath> = Record<string, Param | undefined>> = {
475
488
  path: TPath;
476
489
  params: string extends TPath ? Record<string, Param> : Identity<ExtractParamsFromPathString<TPath, TParams>>;
@@ -504,10 +517,6 @@ declare type PathParamsWithParamNameExtracted<TPath extends string> = {
504
517
  [K in keyof ExtractParamsFromPathString<TPath> as ExtractParamName<K>]?: Param;
505
518
  };
506
519
 
507
- declare type Props<TComponent extends Component> = TComponent extends Constructor ? InstanceType<TComponent>['$props'] : TComponent extends AsyncComponentLoader<infer T extends Component> ? Props<T> : TComponent extends FunctionalComponent<infer T> ? T : never;
508
-
509
- declare type PropsGetter<TComponent extends Component> = () => MaybePromise<Props<TComponent>>;
510
-
511
520
  declare type Query<TQuery extends string = string, TQueryParams extends QueryParamsWithParamNameExtracted<TQuery> = Record<string, Param | undefined>> = {
512
521
  query: TQuery;
513
522
  params: string extends TQuery ? Record<string, Param> : Identity<ExtractQueryParamsFromQueryString<TQuery, TQueryParams>>;
@@ -542,7 +551,7 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
542
551
  };
543
552
 
544
553
  /**
545
- * Represents the state of currently registered router, and rejections. Used to provide correct type context for
554
+ * Represents the state of currently registered router, rejections, and route meta. Used to provide correct type context for
546
555
  * components like `RouterLink`, as well as for composables like `useRouter`, `useRoute`, and hooks.
547
556
  *
548
557
  * @example
@@ -550,6 +559,8 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
550
559
  * declare module '@kitbag/router' {
551
560
  * interface Register {
552
561
  * router: typeof router
562
+ * rejections: ["NotAuthorized"],
563
+ * routeMeta: { public?: boolean }
553
564
  * }
554
565
  * }
555
566
  * ```
@@ -591,19 +602,12 @@ export declare type RegisteredRouterReplace = RouterReplace<RegisteredRoutes>;
591
602
  */
592
603
  export declare type RegisteredRouterRoute = RegisteredRouter['route'];
593
604
 
594
- /**
595
- * Represents the State property registered within {@link Register}
596
- */
597
- export declare type RegisteredRouterState = Register extends {
598
- state: infer TState;
599
- } ? TState : {};
600
-
601
605
  /**
602
606
  * Represents the Router routes property within {@link Register}
603
607
  */
604
608
  export declare type RegisteredRoutes = Register extends {
605
609
  router: Router<infer TRoutes extends Routes>;
606
- } ? TRoutes : Route<string, Host, Path, Query, false>[];
610
+ } ? TRoutes : Route[];
607
611
 
608
612
  /**
609
613
  * Represents the union of all possible RouteKeys registered within {@link Register}
@@ -642,6 +646,10 @@ declare type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
642
646
  * Key value pair for route params, values will be the user provided value from current browser location.
643
647
  */
644
648
  params: ExtractRouteParamTypes<TRoute>;
649
+ /**
650
+ * Type for additional data intended to be stored in history state.
651
+ */
652
+ state: ExtractRouteStateParamsAsOptional<TRoute['state']>;
645
653
  }>;
646
654
 
647
655
  declare type ResolvedRouteQuery = {
@@ -654,18 +662,17 @@ declare type ResolvedRouteQuery = {
654
662
  * @template TKey - Represents the unique key identifying the route, typically a string.
655
663
  * @template TPath - The type or structure of the route's path.
656
664
  * @template TQuery - The type or structure of the query parameters associated with the route.
657
- * @template TDisabled - Indicates whether the route is disabled, which could affect routing logic.
658
665
  */
659
- export declare type Route<TKey extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, TDisabled extends boolean = boolean> = {
666
+ export declare type Route<TKey extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, TMeta extends RouteMeta = RouteMeta, TState extends Record<string, Param> = Record<string, Param>> = {
660
667
  /**
661
668
  * The specific route properties that were matched in the current route.
662
669
  */
663
- matched: CreateRouteOptionsWithMeta;
670
+ matched: CreateRouteOptionsMatched;
664
671
  /**
665
672
  * The specific route properties that were matched in the current route, including any ancestors.
666
673
  * Order of routes will be from greatest ancestor to narrowest matched.
667
674
  */
668
- matches: CreateRouteOptionsWithMeta[];
675
+ matches: CreateRouteOptionsMatched[];
669
676
  /**
670
677
  * Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
671
678
  */
@@ -682,11 +689,15 @@ export declare type Route<TKey extends string = string, THost extends Host = Hos
682
689
  * Represents the structured query of the route, including query params.
683
690
  */
684
691
  query: TQuery;
685
- depth: number;
686
692
  /**
687
- * Indicates if the route is disabled.
693
+ * Represents additional metadata associated with a route, combined with any parents.
688
694
  */
689
- disabled: TDisabled;
695
+ meta: TMeta;
696
+ /**
697
+ * Represents the schema of the route state, combined with any parents.
698
+ */
699
+ state: TState;
700
+ depth: number;
690
701
  };
691
702
 
692
703
  declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesKey<TRoutes>> = RoutesMap<TRoutes>[TKey];
@@ -758,21 +769,14 @@ declare type RouteHookSuccessResponse = {
758
769
  status: 'SUCCESS';
759
770
  };
760
771
 
761
- declare type RouteIsNamedAndNotDisabled<T extends Route> = IsRouteDisabled<T> extends true ? never : IsRouteUnnamed<T> extends true ? never : T;
762
-
763
772
  /**
764
773
  * Represents additional metadata associated with a route, customizable via declaration merging.
765
- * @example
766
- * ```ts
767
- * declare module '@kitbag/router' {
768
- * interface RouteMeta {
769
- * pageTitle?: string
770
- * }
771
- * }
772
- * ```
773
774
  */
774
- export declare interface RouteMeta extends Record<string, unknown> {
775
- }
775
+ export declare type RouteMeta = Register extends {
776
+ routeMeta: infer RouteMeta extends Record<string, unknown>;
777
+ } ? RouteMeta : Record<string, unknown>;
778
+
779
+ declare type RouteParams<TPath extends string | Path | undefined, TQuery extends string | Query | undefined, TParent extends Route | undefined = undefined> = ExtractParamTypes<Identity<CombinePath<ParentPath<TParent>, ToPath<TPath>>['params'] & CombineQuery<ParentQuery<TParent>, ToQuery<TQuery>>['params']>>;
776
780
 
777
781
  declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
778
782
 
@@ -900,9 +904,16 @@ export declare type RouterOptions = {
900
904
  */
901
905
  initialUrl?: string;
902
906
  /**
903
- * Specifies the history mode for the router, such as 'hash', 'history', or 'abstract'.
907
+ * Specifies the history mode for the router, such as "browser", "memory", or "hash".
908
+ *
909
+ * @default "auto"
904
910
  */
905
911
  historyMode?: RouterHistoryMode;
912
+ /**
913
+ * Base path to be prepended to any URL. Can be used for Vue applications that run in nested folder for domain.
914
+ * For example having `base` of `/foo` would assume all routes should start with `your.domain.com/foo`.
915
+ */
916
+ base?: string;
906
917
  } & RouterRejectionComponents;
907
918
 
908
919
  declare type RouterPush<TRoutes extends Routes = any> = {
@@ -910,11 +921,12 @@ declare type RouterPush<TRoutes extends Routes = any> = {
910
921
  (source: Url, options?: RouterPushOptions): Promise<void>;
911
922
  };
912
923
 
913
- declare type RouterPushArgs<TRoutes extends Routes, TSource extends RoutesKey<TRoutes>, TParams = RouteParamsByKey<TRoutes, TSource>> = AllPropertiesAreOptional<TParams> extends true ? [params?: TParams, options?: RouterPushOptions] : [params: TParams, options?: RouterPushOptions];
924
+ declare type RouterPushArgs<TRoutes extends Routes, TSource extends RoutesKey<TRoutes>> = AllPropertiesAreOptional<RouteParamsByKey<TRoutes, TSource>> extends true ? [params?: RouteParamsByKey<TRoutes, TSource>, options?: RouterPushOptions<RouteStateByKey<TRoutes, TSource>>] : [params: RouteParamsByKey<TRoutes, TSource>, options?: RouterPushOptions<RouteStateByKey<TRoutes, TSource>>];
914
925
 
915
- declare type RouterPushOptions = {
926
+ declare type RouterPushOptions<TState = unknown> = {
916
927
  query?: Record<string, string>;
917
928
  replace?: boolean;
929
+ state?: Partial<TState>;
918
930
  };
919
931
 
920
932
  export declare type RouterReject = (type: RouterRejectionType) => void;
@@ -937,9 +949,12 @@ declare type RouterReplace<TRoutes extends Routes> = {
937
949
  (source: Url, options?: RouterReplaceOptions): Promise<void>;
938
950
  };
939
951
 
940
- declare type RouterReplaceArgs<TRoutes extends Routes, TSource extends RoutesKey<TRoutes>, TParams = RouteParamsByKey<TRoutes, TSource>> = AllPropertiesAreOptional<TParams> extends true ? [params?: TParams, options?: RouterReplaceOptions] : [params: TParams, options?: RouterReplaceOptions];
952
+ declare type RouterReplaceArgs<TRoutes extends Routes, TSource extends RoutesKey<TRoutes>, TParams = RouteParamsByKey<TRoutes, TSource>> = AllPropertiesAreOptional<TParams> extends true ? [params?: TParams, options?: RouterReplaceOptions<RouteStateByKey<TRoutes, TSource>>] : [params: TParams, options?: RouterReplaceOptions<RouteStateByKey<TRoutes, TSource>>];
941
953
 
942
- declare type RouterReplaceOptions = Omit<RouterPushOptions, 'replace'>;
954
+ declare type RouterReplaceOptions<TState = unknown> = {
955
+ query?: Record<string, string>;
956
+ state?: Partial<TState>;
957
+ };
943
958
 
944
959
  declare type RouterResolve<TRoutes extends Routes> = {
945
960
  <TSource extends RoutesKey<TRoutes>>(source: TSource, ...args: RouterResolveArgs<TRoutes, TSource>): string;
@@ -956,6 +971,7 @@ declare type RouterRoute<TRoute extends ResolvedRoute = ResolvedRoute> = Readonl
956
971
  key: TRoute['key'];
957
972
  matched: TRoute['matched'];
958
973
  matches: TRoute['matches'];
974
+ state: TRoute['state'];
959
975
  query: ResolvedRouteQuery;
960
976
  params: Writable<TRoute['params']>;
961
977
  update: RouteUpdate<TRoute>;
@@ -976,8 +992,16 @@ name?: string | undefined;
976
992
  default?: ((props: {
977
993
  route: Readonly<{
978
994
  key: string;
979
- matched: CreateRouteOptionsWithMeta;
980
- matches: CreateRouteOptionsWithMeta[];
995
+ matched: CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithoutComponents | WithComponent | WithComponents) & (WithoutParent | WithParent) & (WithoutState | WithState) & {
996
+ meta: Record<string, unknown>;
997
+ };
998
+ matches: (CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithoutComponents | WithComponent | WithComponents) & (WithoutParent | WithParent) & (WithoutState | WithState) & {
999
+ meta: Record<string, unknown>;
1000
+ })[];
1001
+ state: {
1002
+ [x: string]: any;
1003
+ [x: number]: any;
1004
+ };
981
1005
  query: ResolvedRouteQuery;
982
1006
  params: Writable< {
983
1007
  [x: string]: any;
@@ -1003,8 +1027,16 @@ name?: string | undefined;
1003
1027
  default?: ((props: {
1004
1028
  route: Readonly<{
1005
1029
  key: string;
1006
- matched: CreateRouteOptionsWithMeta;
1007
- matches: CreateRouteOptionsWithMeta[];
1030
+ matched: CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithoutComponents | WithComponent | WithComponents) & (WithoutParent | WithParent) & (WithoutState | WithState) & {
1031
+ meta: Record<string, unknown>;
1032
+ };
1033
+ matches: (CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithoutComponents | WithComponent | WithComponents) & (WithoutParent | WithParent) & (WithoutState | WithState) & {
1034
+ meta: Record<string, unknown>;
1035
+ })[];
1036
+ state: {
1037
+ [x: string]: any;
1038
+ [x: number]: any;
1039
+ };
1008
1040
  query: ResolvedRouteQuery;
1009
1041
  params: Writable< {
1010
1042
  [x: string]: any;
@@ -1036,9 +1068,11 @@ export declare type Routes = Readonly<Route[]>;
1036
1068
  declare type RoutesKey<TRoutes extends Routes> = string & keyof RoutesMap<TRoutes>;
1037
1069
 
1038
1070
  declare type RoutesMap<TRoutes extends Routes = []> = {
1039
- [K in TRoutes[number] as RouteIsNamedAndNotDisabled<K>['key']]: RouteIsNamedAndNotDisabled<K>;
1071
+ [K in TRoutes[number] as AsNamedRoute<K>['key']]: AsNamedRoute<K>;
1040
1072
  };
1041
1073
 
1074
+ declare type RouteStateByKey<TRoutes extends Routes, TKey extends string> = ExtractStateParams<RouteGetByKey<TRoutes, TKey>>;
1075
+
1042
1076
  declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = ResolvedRoute extends TRoute ? {
1043
1077
  (key: string, value: unknown, options?: RouterPushOptions): Promise<void>;
1044
1078
  (params: Partial<TRoute['params']>, options?: RouterPushOptions): Promise<void>;
@@ -1163,13 +1197,22 @@ export declare class UseRouteInvalidError extends Error {
1163
1197
  */
1164
1198
  export declare function useRouter(): RegisteredRouter;
1165
1199
 
1166
- declare type WithComponent = Partial<WithSingleComponent | WithComponentRecord>;
1200
+ declare type WithComponent<TComponent extends Component = Component, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1201
+ /**
1202
+ * A Vue component, which can be either synchronous or asynchronous components.
1203
+ */
1204
+ component: TComponent;
1205
+ props?: (params: TParams) => TComponent extends Component ? MaybePromise<ComponentProps<TComponent>> : {};
1206
+ };
1167
1207
 
1168
- declare type WithComponentRecord = {
1208
+ declare type WithComponents<TComponents extends Record<string, Component> = Record<string, Component>, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1169
1209
  /**
1170
1210
  * Multiple components for named views, which can be either synchronous or asynchronous components.
1171
1211
  */
1172
- components: Record<string, Component>;
1212
+ components: TComponents;
1213
+ props?: {
1214
+ [TKey in keyof TComponents]?: (params: TParams) => TComponents[TKey] extends Component ? MaybePromise<ComponentProps<TComponents[TKey]>> : {};
1215
+ };
1173
1216
  };
1174
1217
 
1175
1218
  export declare function withDefault<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
@@ -1186,15 +1229,44 @@ declare type WithHooks = {
1186
1229
  onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
1187
1230
  };
1188
1231
 
1232
+ declare type WithHost<THost extends string | Host = string | Host> = {
1233
+ /**
1234
+ * Host part of URL.
1235
+ */
1236
+ host: THost;
1237
+ };
1238
+
1189
1239
  declare type WithOptionalProperties<T> = {
1190
1240
  [P in keyof T]-?: undefined extends T[P] ? P : never;
1191
1241
  }[keyof T];
1192
1242
 
1193
- declare type WithSingleComponent = {
1243
+ declare type WithoutComponents = {
1244
+ component?: never;
1245
+ components?: never;
1246
+ props?: never;
1247
+ };
1248
+
1249
+ declare type WithoutHost = {
1250
+ host?: never;
1251
+ };
1252
+
1253
+ declare type WithoutParent = {
1254
+ parent?: never;
1255
+ };
1256
+
1257
+ declare type WithoutState = {
1258
+ state?: never;
1259
+ };
1260
+
1261
+ declare type WithParent<TParent extends Route = Route> = {
1262
+ parent: TParent;
1263
+ };
1264
+
1265
+ declare type WithState<TState extends Record<string, Param> = Record<string, Param>> = {
1194
1266
  /**
1195
- * A Vue component, which can be either synchronous or asynchronous components.
1267
+ * Type params for additional data intended to be stored in history state, all keys will be optional unless a default is provided.
1196
1268
  */
1197
- component: Component;
1269
+ state: TState;
1198
1270
  };
1199
1271
 
1200
1272
  declare type Writable<T> = {
@@ -1204,7 +1276,7 @@ declare type Writable<T> = {
1204
1276
  export { }
1205
1277
 
1206
1278
 
1207
- declare module '@vue/runtime-core' {
1279
+ declare module 'vue' {
1208
1280
  interface GlobalComponents {
1209
1281
  RouterView: typeof RouterView;
1210
1282
  RouterLink: typeof RouterLink;