@kitbag/router 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -109,7 +109,7 @@ router.push('/user/settings')
109
109
  router.push('https://github.com/kitbagjs/router')
110
110
  ```
111
111
 
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.
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 `.`. Additionally if using the route key, push will require params be passed in if there are any.
113
113
 
114
114
  ## Update
115
115
 
@@ -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.
@@ -157,13 +159,17 @@ declare type CombineQueryString<TParent extends string | undefined, TChild exten
157
159
  * })
158
160
  * ```
159
161
  */
160
- export declare function component<TComponent extends Component>(component: TComponent, props: PropsGetter<TComponent>): Component;
162
+ export declare function component<TComponent extends Component>(component: TComponent, props: ComponentPropsGetter<TComponent>): Component;
163
+
164
+ 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;
165
+
166
+ declare type ComponentPropsGetter<TComponent extends Component> = () => MaybePromise<ComponentProps<TComponent>>;
161
167
 
162
168
  declare type Constructor = new (...args: any) => any;
163
169
 
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>>;
170
+ 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
171
 
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>>>;
172
+ 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
173
 
168
174
  export declare function createParam<TParam extends ParamWithDefault>(param: TParam): TParam;
169
175
 
@@ -171,19 +177,23 @@ export declare function createParam<TParam extends Param>(param: TParam): ParamG
171
177
 
172
178
  export declare function createParam<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
173
179
 
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>>;
180
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
181
+
182
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
175
183
 
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>>>;
184
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery>> & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
177
185
 
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 & {
186
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
187
+
188
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery>> & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
189
+
190
+ 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>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
191
+
192
+ 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
193
  /**
180
194
  * Name for route, used to create route keys and in navigation.
181
195
  */
182
196
  name?: TName;
183
- /**
184
- * Host part of URL.
185
- */
186
- host?: THost;
187
197
  /**
188
198
  * Path part of URL.
189
199
  */
@@ -195,25 +205,16 @@ export declare type CreateRouteOptions<TName extends string | undefined = string
195
205
  /**
196
206
  * Represents additional metadata associated with a route, customizable via declaration merging.
197
207
  */
198
- meta?: RouteMeta;
199
- disabled?: boolean;
208
+ meta?: TMeta;
200
209
  };
201
210
 
202
211
  /**
203
212
  * The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
204
213
  */
205
- export declare type CreateRouteOptionsWithMeta = CreateRouteOptions & {
214
+ export declare type CreateRouteOptionsMatched = CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithComponent | WithComponents | WithoutComponents) & (WithParent | WithoutParent) & {
206
215
  meta: RouteMeta;
207
216
  };
208
217
 
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
218
  /**
218
219
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
219
220
  *
@@ -376,10 +377,6 @@ export declare function isRoute<TRouteKey extends RegisteredRoutesKey>(route: un
376
377
 
377
378
  export declare function isRoute(route: unknown, routeKey?: string, options?: IsRouteOptions): boolean;
378
379
 
379
- declare type IsRouteDisabled<T extends Route> = T extends {
380
- disabled: true;
381
- } ? true : false;
382
-
383
380
  declare type IsRouteOptions = {
384
381
  exact?: boolean;
385
382
  };
@@ -471,6 +468,10 @@ declare const paramStart = "[";
471
468
 
472
469
  export declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
473
470
 
471
+ declare type ParentPath<TParent extends Route | undefined> = TParent extends Route ? TParent['path'] : Path<'', {}>;
472
+
473
+ declare type ParentQuery<TParent extends Route | undefined> = TParent extends Route ? TParent['query'] : Query<'', {}>;
474
+
474
475
  declare type Path<TPath extends string = string, TParams extends PathParamsWithParamNameExtracted<TPath> = Record<string, Param | undefined>> = {
475
476
  path: TPath;
476
477
  params: string extends TPath ? Record<string, Param> : Identity<ExtractParamsFromPathString<TPath, TParams>>;
@@ -504,10 +505,6 @@ declare type PathParamsWithParamNameExtracted<TPath extends string> = {
504
505
  [K in keyof ExtractParamsFromPathString<TPath> as ExtractParamName<K>]?: Param;
505
506
  };
506
507
 
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
508
  declare type Query<TQuery extends string = string, TQueryParams extends QueryParamsWithParamNameExtracted<TQuery> = Record<string, Param | undefined>> = {
512
509
  query: TQuery;
513
510
  params: string extends TQuery ? Record<string, Param> : Identity<ExtractQueryParamsFromQueryString<TQuery, TQueryParams>>;
@@ -542,7 +539,7 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
542
539
  };
543
540
 
544
541
  /**
545
- * Represents the state of currently registered router, and rejections. Used to provide correct type context for
542
+ * Represents the state of currently registered router, rejections, and route meta. Used to provide correct type context for
546
543
  * components like `RouterLink`, as well as for composables like `useRouter`, `useRoute`, and hooks.
547
544
  *
548
545
  * @example
@@ -550,6 +547,8 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
550
547
  * declare module '@kitbag/router' {
551
548
  * interface Register {
552
549
  * router: typeof router
550
+ * rejections: ["NotAuthorized"],
551
+ * routeMeta: { public?: boolean }
553
552
  * }
554
553
  * }
555
554
  * ```
@@ -591,19 +590,12 @@ export declare type RegisteredRouterReplace = RouterReplace<RegisteredRoutes>;
591
590
  */
592
591
  export declare type RegisteredRouterRoute = RegisteredRouter['route'];
593
592
 
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
593
  /**
602
594
  * Represents the Router routes property within {@link Register}
603
595
  */
604
596
  export declare type RegisteredRoutes = Register extends {
605
597
  router: Router<infer TRoutes extends Routes>;
606
- } ? TRoutes : Route<string, Host, Path, Query, false>[];
598
+ } ? TRoutes : Route[];
607
599
 
608
600
  /**
609
601
  * Represents the union of all possible RouteKeys registered within {@link Register}
@@ -654,18 +646,19 @@ declare type ResolvedRouteQuery = {
654
646
  * @template TKey - Represents the unique key identifying the route, typically a string.
655
647
  * @template TPath - The type or structure of the route's path.
656
648
  * @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
649
  */
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> = {
650
+ export declare type Route<TKey extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, TMeta extends RouteMeta = RouteMeta> = {
660
651
  /**
661
652
  * The specific route properties that were matched in the current route.
662
653
  */
663
- matched: CreateRouteOptionsWithMeta;
654
+ matched: CreateRouteOptionsMatched & {
655
+ meta: TMeta;
656
+ };
664
657
  /**
665
658
  * The specific route properties that were matched in the current route, including any ancestors.
666
659
  * Order of routes will be from greatest ancestor to narrowest matched.
667
660
  */
668
- matches: CreateRouteOptionsWithMeta[];
661
+ matches: CreateRouteOptionsMatched[];
669
662
  /**
670
663
  * Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
671
664
  */
@@ -683,10 +676,6 @@ export declare type Route<TKey extends string = string, THost extends Host = Hos
683
676
  */
684
677
  query: TQuery;
685
678
  depth: number;
686
- /**
687
- * Indicates if the route is disabled.
688
- */
689
- disabled: TDisabled;
690
679
  };
691
680
 
692
681
  declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesKey<TRoutes>> = RoutesMap<TRoutes>[TKey];
@@ -758,21 +747,14 @@ declare type RouteHookSuccessResponse = {
758
747
  status: 'SUCCESS';
759
748
  };
760
749
 
761
- declare type RouteIsNamedAndNotDisabled<T extends Route> = IsRouteDisabled<T> extends true ? never : IsRouteUnnamed<T> extends true ? never : T;
762
-
763
750
  /**
764
751
  * 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
752
  */
774
- export declare interface RouteMeta extends Record<string, unknown> {
775
- }
753
+ export declare type RouteMeta = Register extends {
754
+ routeMeta: infer RouteMeta extends Record<string, unknown>;
755
+ } ? RouteMeta : Record<string, unknown>;
756
+
757
+ 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
758
 
777
759
  declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
778
760
 
@@ -976,8 +958,10 @@ name?: string | undefined;
976
958
  default?: ((props: {
977
959
  route: Readonly<{
978
960
  key: string;
979
- matched: CreateRouteOptionsWithMeta;
980
- matches: CreateRouteOptionsWithMeta[];
961
+ matched: CreateRouteOptionsMatched & {
962
+ meta: Record<string, unknown>;
963
+ };
964
+ matches: CreateRouteOptionsMatched[];
981
965
  query: ResolvedRouteQuery;
982
966
  params: Writable< {
983
967
  [x: string]: any;
@@ -1003,8 +987,10 @@ name?: string | undefined;
1003
987
  default?: ((props: {
1004
988
  route: Readonly<{
1005
989
  key: string;
1006
- matched: CreateRouteOptionsWithMeta;
1007
- matches: CreateRouteOptionsWithMeta[];
990
+ matched: CreateRouteOptionsMatched & {
991
+ meta: Record<string, unknown>;
992
+ };
993
+ matches: CreateRouteOptionsMatched[];
1008
994
  query: ResolvedRouteQuery;
1009
995
  params: Writable< {
1010
996
  [x: string]: any;
@@ -1036,7 +1022,7 @@ export declare type Routes = Readonly<Route[]>;
1036
1022
  declare type RoutesKey<TRoutes extends Routes> = string & keyof RoutesMap<TRoutes>;
1037
1023
 
1038
1024
  declare type RoutesMap<TRoutes extends Routes = []> = {
1039
- [K in TRoutes[number] as RouteIsNamedAndNotDisabled<K>['key']]: RouteIsNamedAndNotDisabled<K>;
1025
+ [K in TRoutes[number] as AsNamedRoute<K>['key']]: AsNamedRoute<K>;
1040
1026
  };
1041
1027
 
1042
1028
  declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = ResolvedRoute extends TRoute ? {
@@ -1163,13 +1149,22 @@ export declare class UseRouteInvalidError extends Error {
1163
1149
  */
1164
1150
  export declare function useRouter(): RegisteredRouter;
1165
1151
 
1166
- declare type WithComponent = Partial<WithSingleComponent | WithComponentRecord>;
1152
+ declare type WithComponent<TComponent extends Component = Component, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1153
+ /**
1154
+ * A Vue component, which can be either synchronous or asynchronous components.
1155
+ */
1156
+ component: TComponent;
1157
+ props?: (params: TParams) => TComponent extends Component ? MaybePromise<ComponentProps<TComponent>> : {};
1158
+ };
1167
1159
 
1168
- declare type WithComponentRecord = {
1160
+ declare type WithComponents<TComponents extends Record<string, Component> = Record<string, Component>, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1169
1161
  /**
1170
1162
  * Multiple components for named views, which can be either synchronous or asynchronous components.
1171
1163
  */
1172
- components: Record<string, Component>;
1164
+ components: TComponents;
1165
+ props?: {
1166
+ [TKey in keyof TComponents]?: (params: TParams) => TComponents[TKey] extends Component ? MaybePromise<ComponentProps<TComponents[TKey]>> : {};
1167
+ };
1173
1168
  };
1174
1169
 
1175
1170
  export declare function withDefault<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
@@ -1186,15 +1181,33 @@ declare type WithHooks = {
1186
1181
  onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
1187
1182
  };
1188
1183
 
1184
+ declare type WithHost<THost extends string | Host = string | Host> = {
1185
+ /**
1186
+ * Host part of URL.
1187
+ */
1188
+ host: THost;
1189
+ };
1190
+
1189
1191
  declare type WithOptionalProperties<T> = {
1190
1192
  [P in keyof T]-?: undefined extends T[P] ? P : never;
1191
1193
  }[keyof T];
1192
1194
 
1193
- declare type WithSingleComponent = {
1194
- /**
1195
- * A Vue component, which can be either synchronous or asynchronous components.
1196
- */
1197
- component: Component;
1195
+ declare type WithoutComponents = {
1196
+ component?: never;
1197
+ components?: never;
1198
+ props?: never;
1199
+ };
1200
+
1201
+ declare type WithoutHost = {
1202
+ host?: never;
1203
+ };
1204
+
1205
+ declare type WithoutParent = {
1206
+ parent?: never;
1207
+ };
1208
+
1209
+ declare type WithParent<TParent extends Route = Route> = {
1210
+ parent: TParent;
1198
1211
  };
1199
1212
 
1200
1213
  declare type Writable<T> = {