@kitbag/router 0.0.2 → 0.1.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
@@ -2,9 +2,8 @@
2
2
 
3
3
  A simple and versatile mapping utility for Typescript.
4
4
 
5
- [![Npm Version](https://img.shields.io/npm/v/@kitbag/router.svg)](https://www.npmjs.org/package/kitbag/router)
6
- [![Zip Size](https://img.badgesize.io/https:/unpkg.com/@kitbag/router/dist/kitbag-router?compression=gzip)](https:/unpkg.com/@kitbag/router/dist/kitbag-router)
7
- [![Netlify Status](https://api.netlify.com/api/v1/badges/d6033c76-88c3-4963-b24f-7a0bda20f271/deploy-status)](https://app.netlify.com/sites/kitbag-router/deploys)
5
+ [![Npm Version](https://img.shields.io/npm/v/@kitbag/router.svg)](https://www.npmjs.org/package/@kitbag/router)
6
+ [![Netlify Status](https://api.netlify.com/api/v1/badges/c12f79b8-49f9-4529-bc23-f8ffca8919a3/deploy-status)](https://app.netlify.com/sites/kitbag-router/deploys)
8
7
 
9
8
  Get started with the [documentation](https://kitbag-router.netlify.app/)
10
9
 
@@ -138,7 +137,7 @@ Give your route components a place to be mounted
138
137
  </div>
139
138
  ```
140
139
 
141
- This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested `RouterView` which would be responsible for rendering any children that route may have. See more about [nested routes](https://kitbag-router.netlify.app/core-concepts/defining-routes#nested-routes).
140
+ This component can be mounted anywhere you want route components to be mounted. Nested routes can also have a nested `RouterView` which would be responsible for rendering any children that route may have. Read more about [nested routes](https://kitbag-router.netlify.app/core-concepts/defining-routes#nested-routes).
142
141
 
143
142
  ## RouterLink
144
143
 
@@ -74,15 +74,23 @@ export declare type AfterRouteHookResponse<TRoutes extends Routes> = RouteHookSu
74
74
  declare type AllPropertiesAreOptional<T> = Record<string, unknown> extends T ? true : IsEmptyObject<OnlyRequiredProperties<T>>;
75
75
 
76
76
  declare type BaseResolvedRoute = Route & {
77
- pathParams: Record<string, unknown>;
78
- queryParams: Record<string, unknown>;
77
+ path: {
78
+ params: Record<string, unknown>;
79
+ };
80
+ query: {
81
+ params: Record<string, unknown>;
82
+ };
79
83
  };
80
84
 
81
85
  declare type BaseRoute = {
82
86
  key: string;
83
87
  disabled: false;
84
- pathParams: Record<string, unknown>;
85
- queryParams: Record<string, unknown>;
88
+ path: {
89
+ params: Record<string, unknown>;
90
+ };
91
+ query: {
92
+ params: Record<string, unknown>;
93
+ };
86
94
  };
87
95
 
88
96
  /**
@@ -119,12 +127,30 @@ declare type BuiltInRejectionType = typeof builtInRejections[number];
119
127
  /**
120
128
  * Represents properties for child routes, including required component, name, and path.
121
129
  */
122
- declare type ChildRouteProps = WithHooks & {
130
+ export declare type ChildRouteProps = WithHooks & {
131
+ /**
132
+ * Name for route, used to create route keys and in navigation.
133
+ */
123
134
  name: string;
135
+ /**
136
+ * Children routes, expected type comes from `createRoutes()`
137
+ */
124
138
  disabled?: boolean;
139
+ /**
140
+ * Path part of URL.
141
+ */
125
142
  path: string | Path;
143
+ /**
144
+ * Query (aka search) part of URL.
145
+ */
126
146
  query?: string | Query;
147
+ /**
148
+ * Type alias for Vue components, which can be either synchronous or asynchronous components.
149
+ */
127
150
  component: RouteComponent;
151
+ /**
152
+ * Represents additional metadata associated with a route, customizable via declaration merging.
153
+ */
128
154
  meta?: RouteMeta;
129
155
  };
130
156
 
@@ -144,7 +170,9 @@ declare type CombineQuery<TParent extends Query | undefined, TChild extends Quer
144
170
  } ? ToQuery<TChild> extends {
145
171
  query: infer TChildQuery extends string;
146
172
  params: infer TChildParams extends Record<string, unknown>;
147
- } ? MergeParams<TParentParams, TChildParams> extends QueryParams<`${TParentQuery}${TChildQuery}`> ? Query<`${TParentQuery}${TChildQuery}`, MergeParams<TParentParams, TChildParams>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
173
+ } ? MergeParams<TParentParams, TChildParams> extends QueryParams<CombineQueryString<TParentQuery, TChildQuery>> ? Query<CombineQueryString<TParentQuery, TChildQuery>, MergeParams<TParentParams, TChildParams>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
174
+
175
+ declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
148
176
 
149
177
  /**
150
178
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
@@ -172,7 +200,7 @@ declare type CombineQuery<TParent extends Query | undefined, TChild extends Quer
172
200
  export declare function createRouter<const T extends Routes>(routes: T, options?: RouterOptions): Router<T>;
173
201
 
174
202
  /**
175
- * Creates an array of Route instances from a defined set of route properties, handling hierarchical route combinations.
203
+ * Creates an array of routes from a defined set of route properties, handling hierarchical route combinations.
176
204
  * This function also validates for duplicate parameter keys across the combined routes.
177
205
  *
178
206
  * @param routesProps - An array of route properties used to configure and create routes.
@@ -244,11 +272,19 @@ declare type ExtractRouteChildren<TRoute extends RouteProps> = TRoute extends Pa
244
272
  * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
245
273
  */
246
274
  export declare type ExtractRouteParamTypes<TRoute extends {
247
- pathParams: Record<string, unknown>;
248
- queryParams: Record<string, unknown>;
275
+ path: {
276
+ params: Record<string, unknown>;
277
+ };
278
+ query: {
279
+ params: Record<string, unknown>;
280
+ };
249
281
  }> = TRoute extends {
250
- pathParams: infer PathParams extends Record<string, Param | undefined>;
251
- queryParams: infer QueryParams extends Record<string, Param | undefined>;
282
+ path: {
283
+ params: infer PathParams extends Record<string, Param | undefined>;
284
+ };
285
+ query: {
286
+ params: infer QueryParams extends Record<string, Param | undefined>;
287
+ };
252
288
  } ? ExtractParamTypes<MergeParams<PathParams, QueryParams>> : Record<string, unknown>;
253
289
 
254
290
  declare type Flatten<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends unknown[] ? Flatten<[...First, ...Flatten<Rest>]> : [First, ...Flatten<Rest>] : [];
@@ -286,6 +322,13 @@ export declare function isParamGetSet(value: Param): value is ParamGetSet;
286
322
  */
287
323
  export declare function isParamGetter(value: Param): value is ParamGetter;
288
324
 
325
+ /**
326
+ * Type guard function to determine if a given route configuration is a parent route, based on the presence of children.
327
+ * @param value - The route configuration to check.
328
+ * @returns True if the route configuration has children, indicating it is a parent route.
329
+ */
330
+ export declare function isParentRoute(value: RouteProps): value is ParentRouteProps;
331
+
289
332
  declare type MakeOptional<T> = {
290
333
  [P in WithOptionalProperties<T>]?: T[P];
291
334
  } & {
@@ -357,33 +400,54 @@ declare type OnlyRequiredProperties<T> = {
357
400
  [K in keyof T as Extract<T[K], undefined> extends never ? K : never]: T[K];
358
401
  };
359
402
 
360
- declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor;
403
+ export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor;
361
404
 
362
405
  declare type ParamEnd = '/';
363
406
 
364
- declare type ParamExtras = {
407
+ export declare type ParamExtras = {
365
408
  invalid: (message?: string) => never;
366
409
  };
367
410
 
368
- declare type ParamGetSet<T = any> = {
411
+ export declare type ParamGetSet<T = any> = {
369
412
  get: ParamGetter<T>;
370
413
  set: ParamSetter<T>;
371
414
  };
372
415
 
373
- declare type ParamGetter<T = any> = (value: string, extras: ParamExtras) => T;
416
+ export declare type ParamGetter<T = any> = (value: string, extras: ParamExtras) => T;
374
417
 
375
- declare type ParamSetter<T = any> = (value: T, extras: ParamExtras) => string;
418
+ export declare type ParamSetter<T = any> = (value: T, extras: ParamExtras) => string;
376
419
 
377
420
  /**
378
421
  * Represents properties common to parent routes in a route configuration, including hooks, path, and optional query parameters.
379
422
  */
380
- declare type ParentRouteProps = WithHooks & {
423
+ export declare type ParentRouteProps = WithHooks & {
424
+ /**
425
+ * Name for route, used to create route keys and in navigation.
426
+ */
381
427
  name?: string;
428
+ /**
429
+ * Path part of URL.
430
+ */
382
431
  path: string | Path;
432
+ /**
433
+ * Query (aka search) part of URL.
434
+ */
383
435
  query?: string | Query;
436
+ /**
437
+ * Disabled routes will not ever match but can still provide physical structure, nested children behave normally.
438
+ */
384
439
  disabled?: boolean;
440
+ /**
441
+ * Children routes, expected type comes from `createRoutes()`
442
+ */
385
443
  children: Routes;
444
+ /**
445
+ * Type alias for Vue components, which can be either synchronous or asynchronous components.
446
+ */
386
447
  component?: RouteComponent;
448
+ /**
449
+ * Represents additional metadata associated with a route, customizable via declaration merging.
450
+ */
387
451
  meta?: RouteMeta;
388
452
  };
389
453
 
@@ -521,11 +585,31 @@ export declare type RegisteredRoutes = Register extends {
521
585
  */
522
586
  export declare type RegisteredRoutesKey = RoutesKey<RegisteredRoutes>;
523
587
 
588
+ /**
589
+ * Represents a route that the router has matched to current browser location.
590
+ * @template TRoute - Underlying Route that has been resolved.
591
+ */
524
592
  declare type ResolvedRoute<TRoute extends Route = BaseResolvedRoute> = DeepReadonly<{
593
+ /**
594
+ * The specific route properties that were matched in the current route.
595
+ */
525
596
  matched: TRoute['matched'];
597
+ /**
598
+ * The specific route properties that were matched in the current route, including any ancestors.
599
+ * Order of routes will be from greatest ancestor to narrowest matched.
600
+ */
526
601
  matches: RouteProps[];
602
+ /**
603
+ * Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
604
+ */
527
605
  key: TRoute['key'];
606
+ /**
607
+ * Accessor for query string values from user in the current browser location.
608
+ */
528
609
  query: ResolvedRouteQuery;
610
+ /**
611
+ * Key value pair for route params, values will be the user provided value from current browser location.
612
+ */
529
613
  params: ExtractRouteParamTypes<TRoute>;
530
614
  }>;
531
615
 
@@ -535,7 +619,7 @@ declare type ResolvedRouteQuery = {
535
619
  };
536
620
 
537
621
  /**
538
- * Represents the structure of a route within the application.
622
+ * Represents the structure of a route within the application. Return value of `createRoutes`
539
623
  * @template TKey - Represents the unique key identifying the route, typically a string.
540
624
  * @template TPath - The type or structure of the route's path.
541
625
  * @template TQuery - The type or structure of the query parameters associated with the route.
@@ -563,17 +647,6 @@ export declare type Route<TKey extends string | undefined = any, TPath extends s
563
647
  * Represents the structured query of the route, including query params.
564
648
  */
565
649
  query: ToQuery<TQuery>;
566
- /**
567
- * Extracted path params based on the route's path type.
568
- */
569
- pathParams: ToPath<TPath>['params'];
570
- /**
571
- * Extracted query params based on the route's query type.
572
- */
573
- queryParams: ToQuery<TQuery>['params'];
574
- /**
575
- * Represents the depth of the route in the route hierarchy.
576
- */
577
650
  depth: number;
578
651
  /**
579
652
  * Indicates if the route is disabled.
@@ -584,7 +657,7 @@ export declare type Route<TKey extends string | undefined = any, TPath extends s
584
657
  /**
585
658
  * Type alias for Vue components, which can be either synchronous or asynchronous components.
586
659
  */
587
- declare type RouteComponent = Component | DefineComponent | AsyncComponentLoader;
660
+ export declare type RouteComponent = Component | DefineComponent | AsyncComponentLoader;
588
661
 
589
662
  declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesKey<TRoutes>> = RoutesMap<TRoutes>[TKey];
590
663
 
@@ -656,7 +729,7 @@ declare type RouteHookSuccessResponse = {
656
729
  };
657
730
 
658
731
  /**
659
- * Represents additional metadata associated with a route, customizable via extensions.
732
+ * Represents additional metadata associated with a route, customizable via declaration merging.
660
733
  * @example
661
734
  * ```ts
662
735
  * declare module '@kitbag/router' {
@@ -666,7 +739,7 @@ declare type RouteHookSuccessResponse = {
666
739
  * }
667
740
  * ```
668
741
  */
669
- declare interface RouteMeta extends Record<string, unknown> {
742
+ export declare interface RouteMeta extends Record<string, unknown> {
670
743
  }
671
744
 
672
745
  declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypes<RouteGetByKey<TRoutes, TKey>>;
@@ -674,7 +747,7 @@ declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = Ext
674
747
  /**
675
748
  * Unifies the properties of both parent and child routes, ensuring type safety and consistency across route configurations.
676
749
  */
677
- declare type RouteProps = Readonly<ParentRouteProps | ChildRouteProps>;
750
+ export declare type RouteProps = Readonly<ParentRouteProps | ChildRouteProps>;
678
751
 
679
752
  /**
680
753
  * The Route properties originally provided to `createRoutes`. The only change is normalizing meta to always default to an empty object.
@@ -786,7 +859,7 @@ to: Url | ToCallback;
786
859
  }>;
787
860
 
788
861
  /**
789
- * Represents an error thrown when an attempt is made to use routing functionality before the router has been installed.
862
+ * An error thrown when an attempt is made to use routing functionality before the router has been installed.
790
863
  */
791
864
  export declare class RouterNotInstalledError extends Error {
792
865
  constructor();
@@ -890,6 +963,8 @@ declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = {
890
963
  (params: Partial<TRoute['params']>, options?: RouterPushOptions): Promise<void>;
891
964
  };
892
965
 
966
+ declare type StringHasValue<T extends string | undefined> = T extends undefined ? false : '' extends T ? false : true;
967
+
893
968
  declare type ToCallback = (resolve: RegisteredRouter['resolve']) => string;
894
969
 
895
970
  declare type ToPath<T extends string | Path> = T extends string ? Path<T, {}> : T;
@@ -927,7 +1002,7 @@ export declare function useRoute<TRouteKey extends string & keyof RegisteredRout
927
1002
  export declare function useRoute(): RouterRoute;
928
1003
 
929
1004
  /**
930
- * Represents an error thrown when there is a mismatch between an expected route and the one actually used.
1005
+ * An error thrown when there is a mismatch between an expected route and the one actually used.
931
1006
  */
932
1007
  export declare class UseRouteInvalidError extends Error {
933
1008
  /**