@kitbag/router 0.15.2 → 0.16.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.
@@ -207,10 +207,20 @@ declare type CombineQueryString<TParent extends string | undefined, TChild exten
207
207
 
208
208
  declare type CombineState<TParent extends Record<string, Param>, TChild extends Record<string, Param>> = TParent & TChild;
209
209
 
210
- 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;
210
+ 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 : {};
211
+
212
+ declare type ComponentPropsAreOptional<TComponent extends Component> = Partial<ComponentProps<TComponent>> extends ComponentProps<TComponent> ? true : false;
211
213
 
212
214
  declare type Constructor = new (...args: any) => any;
213
215
 
216
+ /**
217
+ * The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
218
+ */
219
+ export declare type CreatedRouteOptions = Omit<CreateRouteOptions, 'props'> & WithHooks & {
220
+ id: string;
221
+ props?: unknown;
222
+ };
223
+
214
224
  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, const THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery> & WithHost<THost> & WithoutParent): Route<ToName<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta>;
215
225
 
216
226
  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, const THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery> & WithoutHost & WithParent<TParent>): Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>>;
@@ -221,19 +231,9 @@ export declare function createParam<TParam extends Param>(param: TParam): ParamG
221
231
 
222
232
  export declare function createParam<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
223
233
 
224
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithoutComponents & WithoutParent & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState, TName>;
225
-
226
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithoutComponents & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TName | TParent['matches'][number]['name']>;
227
-
228
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithComponent<TComponent, Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState, TName>> & WithoutParent & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState, TName>;
234
+ export declare function createRoute<const TOptions extends CreateRouteOptions, const TProps extends CreateRouteProps<TOptions>>(options: TOptions, ...args: CreateRouteWithProps<TOptions, TProps>): ToRoute<TOptions, CreateRouteProps<TOptions> extends TProps ? undefined : TProps>;
229
235
 
230
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithComponent<TComponent, Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TName | TParent['matches'][number]['name']>> & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TName | TParent['matches'][number]['name']>;
231
-
232
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithComponents<TComponents, Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState, TName>> & WithoutParent & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState, TName>;
233
-
234
- 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 THash extends string | Hash | undefined = undefined, const TMeta extends RouteMeta = RouteMeta, const TState extends Record<string, Param> = Record<string, Param>>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHooks & WithComponents<TComponents, Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TName | TParent['matches'][number]['name']>> & WithParent<TParent> & (WithState<TState> | WithoutState)): Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TName | TParent['matches'][number]['name']>;
235
-
236
- 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, THash extends string | Hash | undefined = string | Hash | undefined, TMeta extends RouteMeta = RouteMeta> = {
236
+ 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, THash extends string | Hash | undefined = string | Hash | undefined, TMeta extends RouteMeta = RouteMeta> = WithHooks & {
237
237
  /**
238
238
  * Name for route, used to create route keys and in navigation.
239
239
  */
@@ -258,15 +258,33 @@ export declare type CreateRouteOptions<TName extends string | undefined = string
258
258
  * Determines what assets are prefetched when router-link is rendered for this route. Overrides router level prefetch.
259
259
  */
260
260
  prefetch?: PrefetchConfig;
261
+ /**
262
+ * Type params for additional data intended to be stored in history state, all keys will be optional unless a default is provided.
263
+ */
264
+ state?: Record<string, Param>;
265
+ /**
266
+ * An optional parent route to nest this route under.
267
+ */
268
+ parent?: Route;
269
+ /**
270
+ * An optional component to render when this route is matched.
271
+ *
272
+ * @default RouterView
273
+ */
274
+ component?: Component;
275
+ /**
276
+ * An object of named components to render using named views
277
+ */
278
+ components?: Record<string, Component>;
279
+ /**
280
+ * Props have been moved to the second argument of `createRoute`. This property can no longer be used.
281
+ *
282
+ * @deprecated
283
+ */
284
+ props?: never;
261
285
  };
262
286
 
263
- /**
264
- * The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
265
- */
266
- declare type CreateRouteOptionsMatched<TName extends string | undefined = string | undefined> = CreateRouteOptions<TName> & WithHooks & (WithHost | WithoutHost) & (WithComponent | WithComponents | WithoutComponents) & (WithParent | WithoutParent) & (WithState | WithoutState) & {
267
- id: string;
268
- meta: RouteMeta;
269
- };
287
+ declare type CreateRouteProps<TOptions extends CreateRouteOptions = CreateRouteOptions> = TOptions['component'] extends Component ? PropsGetter<TOptions, TOptions['component']> : TOptions['components'] extends Record<string, Component> ? RoutePropsRecord<TOptions, TOptions['components']> : PropsGetter<TOptions, typeof RouterView>;
270
288
 
271
289
  /**
272
290
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
@@ -297,6 +315,8 @@ export declare function createRouter<const TRoutes extends Routes, const TOption
297
315
 
298
316
  export declare function createRouterPlugin<TRoutes extends Routes = [], TRejections extends Record<string, Component> = {}>(plugin: Partial<RouterPlugin<TRoutes, TRejections>>): RouterPlugin<TRoutes, TRejections>;
299
317
 
318
+ declare type CreateRouteWithProps<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions>> = CreateRouteProps<TOptions> extends PropsGetter<TOptions> ? Partial<ReturnType<CreateRouteProps<TOptions>>> extends ReturnType<CreateRouteProps<TOptions>> ? [props?: TProps] : [props: TProps] : Partial<CreateRouteProps<TOptions>> extends CreateRouteProps<TOptions> ? [props?: TProps] : [props: TProps];
319
+
300
320
  /**
301
321
  * An error thrown when duplicate parameters are detected in a route.
302
322
  * Param names must be unique. This includes params defined in a path
@@ -497,6 +517,8 @@ export declare function isUrl(value: unknown): value is Url;
497
517
  */
498
518
  declare type KeysOfUnion<T> = T extends T ? keyof T : never;
499
519
 
520
+ declare type LastInArray<T, TFallback = never> = T extends [...any[], infer Last] ? Last : TFallback;
521
+
500
522
  declare type MakeOptional<T> = {
501
523
  [P in WithOptionalProperties<T>]?: T[P];
502
524
  } & {
@@ -516,16 +538,6 @@ export declare class MetaPropertyConflict extends Error {
516
538
  constructor(property?: string);
517
539
  }
518
540
 
519
- /**
520
- * Registers a hook that is called after a route has been entered. Must be called during setup.
521
- * This allows performing actions right after the component becomes active, such as fetching data or setting up event listeners.
522
- *
523
- * @param AfterRouteHook - The hook callback function
524
- * @returns {RouteHookRemove} A function that removes the added hook.
525
- * @group Hooks
526
- */
527
- export declare const onAfterRouteEnter: AddAfterRouteHook;
528
-
529
541
  /**
530
542
  * Registers a hook that is called after a route has been left. Must be called during setup.
531
543
  * This can be used for cleanup actions after the component is no longer active, ensuring proper resource management.
@@ -662,6 +674,8 @@ export declare type PropsCallbackContext = {
662
674
  reject: CallbackContext['reject'];
663
675
  };
664
676
 
677
+ declare type PropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponent extends Component = Component> = (route: ResolvedRoute<ToRoute<TOptions, undefined>>, context: PropsCallbackContext) => MaybePromise<ComponentProps<TComponent>>;
678
+
665
679
  declare type Query<TQuery extends string = string, TQueryParams extends QueryParamsWithParamNameExtracted<TQuery> = Record<string, Param | undefined>> = {
666
680
  value: TQuery;
667
681
  params: string extends TQuery ? Record<string, Param> : Identity<ExtractQueryParamsFromQueryString<TQuery, TQueryParams>>;
@@ -820,7 +834,7 @@ export declare type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
820
834
  * @template TPath - The type or structure of the route's path.
821
835
  * @template TQuery - The type or structure of the query parameters associated with the route.
822
836
  */
823
- export declare type Route<TName extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, THash extends Hash = Hash, TMeta extends RouteMeta = RouteMeta, TState extends Record<string, Param> = Record<string, Param>, TMatchNames extends string | undefined = string | undefined> = {
837
+ export declare type Route<TName extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, THash extends Hash = Hash, TMeta extends RouteMeta = RouteMeta, TState extends Record<string, Param> = Record<string, Param>, TMatches extends CreatedRouteOptions[] = CreatedRouteOptions[]> = {
824
838
  /**
825
839
  * Unique identifier for the route, generated by router.
826
840
  */
@@ -828,12 +842,12 @@ export declare type Route<TName extends string = string, THost extends Host = Ho
828
842
  /**
829
843
  * The specific route properties that were matched in the current route.
830
844
  */
831
- matched: CreateRouteOptionsMatched<TName>;
845
+ matched: LastInArray<TMatches, CreatedRouteOptions>;
832
846
  /**
833
847
  * The specific route properties that were matched in the current route, including any ancestors.
834
848
  * Order of routes will be from greatest ancestor to narrowest matched.
835
849
  */
836
- matches: CreateRouteOptionsMatched<TMatchNames>[];
850
+ matches: TMatches;
837
851
  /**
838
852
  * Identifier for the route as defined by user. Name must be unique among named routes. Name is used for routing and for matching.
839
853
  */
@@ -926,6 +940,12 @@ export declare type RouteMeta<T = Register> = T extends {
926
940
 
927
941
  declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
928
942
 
943
+ declare type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
944
+ [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
945
+ } & {
946
+ [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends false ? K : never]: PropsGetter<TOptions, TComponents[K]>;
947
+ };
948
+
929
949
  export declare type Router<TRoutes extends Routes = any, TOptions extends RouterOptions = any, TPlugin extends RouterPlugin = any> = {
930
950
  /**
931
951
  * Installs the router into a Vue application instance.
@@ -1230,12 +1250,29 @@ declare type ToHash<T extends string | Hash | undefined> = T extends string ? Ha
1230
1250
 
1231
1251
  declare type ToHost<T extends string | Host | undefined> = T extends string ? Host<T, {}> : T extends undefined ? Host<'', {}> : unknown extends T ? Host<'', {}> : T;
1232
1252
 
1253
+ declare type ToMatch<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = Omit<TOptions, 'props'> & {
1254
+ id: string;
1255
+ props: TProps;
1256
+ };
1257
+
1258
+ declare type ToMatches<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = TOptions extends {
1259
+ parent: infer TParent extends Route;
1260
+ } ? [...TParent['matches'], ToMatch<TOptions, TProps>] : [ToMatch<TOptions, TProps>];
1261
+
1262
+ declare type ToMeta<TMeta extends RouteMeta | undefined> = TMeta extends undefined ? {} : unknown extends TMeta ? {} : TMeta;
1263
+
1233
1264
  declare type ToName<T extends string | undefined> = T extends string ? T : '';
1234
1265
 
1235
1266
  declare type ToPath<T extends string | Path | undefined> = T extends string ? Path<T, {}> : T extends undefined ? Path<'', {}> : unknown extends T ? Path<'', {}> : T;
1236
1267
 
1237
1268
  declare type ToQuery<T extends string | Query | undefined> = T extends string ? Query<T, {}> : T extends undefined ? Query<'', {}> : unknown extends T ? Query<'', {}> : T;
1238
1269
 
1270
+ declare type ToRoute<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = CreateRouteOptions extends TOptions ? Route : TOptions extends {
1271
+ parent: infer TParent extends Route;
1272
+ } ? Route<ToName<TOptions['name']>, Host<'', {}>, CombinePath<ToPath<TParent['path']>, ToPath<TOptions['path']>>, CombineQuery<ToQuery<TParent['query']>, ToQuery<TOptions['query']>>, CombineHash<ToHash<TParent['hash']>, ToHash<TOptions['hash']>>, CombineMeta<ToMeta<TParent['meta']>, ToMeta<TOptions['meta']>>, CombineState<ToState<TParent['state']>, ToState<TOptions['state']>>, ToMatches<TOptions, TProps>> : Route<ToName<TOptions['name']>, Host<'', {}>, ToPath<TOptions['path']>, ToQuery<TOptions['query']>, ToHash<TOptions['hash']>, ToMeta<TOptions['meta']>, ToState<TOptions['state']>, ToMatches<TOptions, TProps>>;
1273
+
1274
+ declare type ToState<TState extends Record<string, Param> | undefined> = TState extends undefined ? Record<string, Param> : unknown extends TState ? {} : TState;
1275
+
1239
1276
  export declare type Url = `http://${string}` | `https://${string}` | `/${string}`;
1240
1277
 
1241
1278
  export declare type UrlParts = {
@@ -1358,24 +1395,6 @@ export declare class UseRouteInvalidError extends Error {
1358
1395
  */
1359
1396
  export declare function useRouter(): RegisteredRouter;
1360
1397
 
1361
- declare type WithComponent<TComponent extends Component = Component, TRoute extends Route = Route> = {
1362
- /**
1363
- * A Vue component, which can be either synchronous or asynchronous components.
1364
- */
1365
- component: TComponent;
1366
- props?: (route: ResolvedRoute<TRoute>, context: PropsCallbackContext) => TComponent extends Component ? MaybePromise<ComponentProps<TComponent>> : {};
1367
- };
1368
-
1369
- declare type WithComponents<TComponents extends Record<string, Component> = Record<string, Component>, TRoute extends Route = Route> = {
1370
- /**
1371
- * Multiple components for named views, which can be either synchronous or asynchronous components.
1372
- */
1373
- components: TComponents;
1374
- props?: {
1375
- [TKey in keyof TComponents]?: (route: ResolvedRoute<TRoute>, context: PropsCallbackContext) => TComponents[TKey] extends Component ? MaybePromise<ComponentProps<TComponents[TKey]>> : {};
1376
- };
1377
- };
1378
-
1379
1398
  export declare function withDefault<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
1380
1399
 
1381
1400
  /**
@@ -1401,12 +1420,6 @@ declare type WithOptionalProperties<T> = {
1401
1420
  [P in keyof T]-?: undefined extends T[P] ? P : never;
1402
1421
  }[keyof T];
1403
1422
 
1404
- declare type WithoutComponents = {
1405
- component?: never;
1406
- components?: never;
1407
- props?: never;
1408
- };
1409
-
1410
1423
  declare type WithoutHost = {
1411
1424
  host?: never;
1412
1425
  };
@@ -1415,21 +1428,10 @@ declare type WithoutParent = {
1415
1428
  parent?: never;
1416
1429
  };
1417
1430
 
1418
- declare type WithoutState = {
1419
- state?: never;
1420
- };
1421
-
1422
1431
  declare type WithParent<TParent extends Route = Route> = {
1423
1432
  parent: TParent;
1424
1433
  };
1425
1434
 
1426
- declare type WithState<TState extends Record<string, Param> = Record<string, Param>> = {
1427
- /**
1428
- * Type params for additional data intended to be stored in history state, all keys will be optional unless a default is provided.
1429
- */
1430
- state: TState;
1431
- };
1432
-
1433
1435
  export { }
1434
1436
 
1435
1437