@kitbag/router 0.3.0 → 0.4.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 +3 -1
- package/dist/kitbag-router.d.ts +44 -19
- package/dist/kitbag-router.js +523 -503
- package/dist/kitbag-router.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,6 +7,8 @@ Type safe router for Vue.js
|
|
|
7
7
|
[![Discord chat][discord-badge]][discord-url]
|
|
8
8
|
[![Open in StackBlitz][stackblitz-badge]][stackblitz-url]
|
|
9
9
|
|
|
10
|
+
<img src="https://kitbag.dev/kitbag-logo.svg" width="20%" />
|
|
11
|
+
|
|
10
12
|
## Getting Started
|
|
11
13
|
|
|
12
14
|
Get Started with our [documentation](https://kitbag-router.netlify.app/)
|
|
@@ -162,4 +164,4 @@ This component gives the router the power to change the URL without reloading th
|
|
|
162
164
|
[discord-badge]: https://img.shields.io/discord/1079625926024900739?logo=discord&label=Discord
|
|
163
165
|
[discord-url]: https://discord.gg/UT7JrAxU
|
|
164
166
|
[stackblitz-badge]: https://developer.stackblitz.com/img/open_in_stackblitz_small.svg
|
|
165
|
-
[stackblitz-url]: https://stackblitz.com/~/github.com/kitbagjs/router-preview
|
|
167
|
+
[stackblitz-url]: https://stackblitz.com/~/github.com/kitbagjs/router-preview
|
package/dist/kitbag-router.d.ts
CHANGED
|
@@ -270,27 +270,34 @@ declare type ExtractParamsFromPathString<TPath extends string, TParams extends R
|
|
|
270
270
|
|
|
271
271
|
/**
|
|
272
272
|
* Extracts the actual type from a parameter type, handling getters, setters, and potential undefined values.
|
|
273
|
+
* This type also is responsible for narrowing possibly undefined values when the param has a default value.
|
|
273
274
|
* @template TParam - The parameter type.
|
|
274
275
|
* @returns The extracted type, or 'string' as a fallback.
|
|
275
276
|
*/
|
|
276
|
-
export declare type ExtractParamType<TParam extends Param
|
|
277
|
+
export declare type ExtractParamType<TParam extends Param> = TParam extends ParamGetSet<infer Type> ? TParam extends IsOptionalParam ? TParam extends ParamWithDefault ? Type : Type | undefined : Type : TParam extends ParamGetter ? ReturnType<TParam> : string;
|
|
277
278
|
|
|
278
279
|
/**
|
|
279
280
|
* Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
|
|
280
281
|
* @template TParams - The record of parameter types, possibly including undefined.
|
|
281
282
|
* @returns A new type with the appropriate properties marked as optional.
|
|
282
283
|
*/
|
|
283
|
-
export declare type ExtractParamTypes<TParams extends Record<string, Param
|
|
284
|
+
export declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
|
|
284
285
|
[K in keyof TParams]: ExtractParamType<TParams[K]>;
|
|
285
286
|
}>>;
|
|
286
287
|
|
|
288
|
+
declare type ExtractParamTypesWithoutLosingOptional<TParams extends Record<string, Param>> = Identity<MakeOptional<{
|
|
289
|
+
[K in keyof TParams]: ExtractParamTypeWithoutLosingOptional<TParams[K]>;
|
|
290
|
+
}>>;
|
|
291
|
+
|
|
292
|
+
declare type ExtractParamTypeWithoutLosingOptional<TParam extends Param> = TParam extends ParamGetSet<infer Type> ? TParam extends IsOptionalParam ? Type | undefined : Type : TParam extends ParamGetter ? ReturnType<TParam> : string;
|
|
293
|
+
|
|
287
294
|
/**
|
|
288
295
|
* Determines the type of a path parameter from a record of parameter types, considering optional parameters.
|
|
289
296
|
* @template TParam - The parameter name string.
|
|
290
297
|
* @template TParams - The record object mapping parameter names to their types.
|
|
291
298
|
* @returns The type associated with the parameter, or StringConstructor if unspecified; may be undefined for optional parameters.
|
|
292
299
|
*/
|
|
293
|
-
export declare type ExtractPathParamType<TParam extends string, TParams extends Record<string, Param | undefined>> = TParam extends `?${infer OptionalParam}` ? OptionalParam extends keyof TParams ? TParams[OptionalParam]
|
|
300
|
+
export declare type ExtractPathParamType<TParam extends string, TParams extends Record<string, Param | undefined>> = TParam extends `?${infer OptionalParam}` ? OptionalParam extends keyof TParams ? TParams[OptionalParam] extends Param ? OptionalParamGetSet<TParams[OptionalParam]> : OptionalParamGetSet<StringConstructor> : OptionalParamGetSet<StringConstructor> : TParam extends keyof TParams ? TParams[TParam] : StringConstructor;
|
|
294
301
|
|
|
295
302
|
declare type ExtractQueryParamsFromQueryString<TQuery extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TQuery extends `${string}=${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? MergeParams<{
|
|
296
303
|
[P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams>;
|
|
@@ -300,24 +307,27 @@ declare type ExtractRouteChildren<TRoute extends RouteProps> = TRoute extends Pa
|
|
|
300
307
|
|
|
301
308
|
/**
|
|
302
309
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
310
|
+
* This parameter object type represents the expected type when accessing params from router.route or useRoute.
|
|
303
311
|
* @template TRoute - The route type from which to extract and merge parameter types.
|
|
304
312
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
305
313
|
*/
|
|
306
|
-
export declare type ExtractRouteParamTypes<TRoute extends {
|
|
314
|
+
export declare type ExtractRouteParamTypes<TRoute> = TRoute extends {
|
|
307
315
|
path: {
|
|
308
|
-
params: Record<string,
|
|
316
|
+
params: infer PathParams extends Record<string, Param>;
|
|
309
317
|
};
|
|
310
318
|
query: {
|
|
311
|
-
params: Record<string,
|
|
319
|
+
params: infer QueryParams extends Record<string, Param>;
|
|
312
320
|
};
|
|
313
|
-
}
|
|
321
|
+
} ? ExtractParamTypes<MergeParams<PathParams, QueryParams>> : Record<string, unknown>;
|
|
322
|
+
|
|
323
|
+
declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extends {
|
|
314
324
|
path: {
|
|
315
|
-
params: infer PathParams extends Record<string, Param
|
|
325
|
+
params: infer PathParams extends Record<string, Param>;
|
|
316
326
|
};
|
|
317
327
|
query: {
|
|
318
|
-
params: infer QueryParams extends Record<string, Param
|
|
328
|
+
params: infer QueryParams extends Record<string, Param>;
|
|
319
329
|
};
|
|
320
|
-
} ?
|
|
330
|
+
} ? ExtractParamTypesWithoutLosingOptional<MergeParams<PathParams, QueryParams>> : Record<string, unknown>;
|
|
321
331
|
|
|
322
332
|
declare type Flatten<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends unknown[] ? Flatten<[...First, ...Flatten<Rest>]> : [First, ...Flatten<Rest>] : [];
|
|
323
333
|
|
|
@@ -340,6 +350,10 @@ declare type Identity<T> = T extends object ? {} & {
|
|
|
340
350
|
|
|
341
351
|
declare type IsEmptyObject<T> = T extends Record<string, never> ? (keyof T extends never ? true : false) : false;
|
|
342
352
|
|
|
353
|
+
declare type IsOptionalParam = {
|
|
354
|
+
[optionalParamKey]: true;
|
|
355
|
+
};
|
|
356
|
+
|
|
343
357
|
/**
|
|
344
358
|
* Type guard to check if a value conforms to the ParamGetSet type.
|
|
345
359
|
* @param value - The value to check.
|
|
@@ -354,6 +368,8 @@ export declare function isParamGetSet(value: Param): value is ParamGetSet;
|
|
|
354
368
|
*/
|
|
355
369
|
export declare function isParamGetter(value: Param): value is ParamGetter;
|
|
356
370
|
|
|
371
|
+
export declare function isParamWithDefault(param: Param): param is ParamWithDefault;
|
|
372
|
+
|
|
357
373
|
/**
|
|
358
374
|
* Type guard function to determine if a given route configuration is a parent route, based on the presence of children.
|
|
359
375
|
* @param value - The route configuration to check.
|
|
@@ -365,7 +381,7 @@ export declare function isParentRouteWithoutComponent(value: RouteProps): value
|
|
|
365
381
|
|
|
366
382
|
export declare function isRoute(route: unknown): route is RouterRoute;
|
|
367
383
|
|
|
368
|
-
export declare function isRoute<TRouteKey extends RegisteredRoutesKey>(route: unknown, routeKey: TRouteKey, options
|
|
384
|
+
export declare function isRoute<TRouteKey extends RegisteredRoutesKey>(route: unknown, routeKey: TRouteKey, options?: IsRouteOptions): route is RouterRoute<ResolvedRoute<RegisteredRouteMap[TRouteKey]>>;
|
|
369
385
|
|
|
370
386
|
declare type IsRouteOptions = {
|
|
371
387
|
exact?: boolean;
|
|
@@ -456,6 +472,10 @@ declare type OnlyRequiredProperties<T> = {
|
|
|
456
472
|
[K in keyof T as Extract<T[K], undefined> extends never ? K : never]: T[K];
|
|
457
473
|
};
|
|
458
474
|
|
|
475
|
+
declare type OptionalParamGetSet<TParam extends Param> = TParam extends ParamGetSet ? TParam & IsOptionalParam : ParamGetSet<ExtractParamType<TParam>> & IsOptionalParam;
|
|
476
|
+
|
|
477
|
+
declare const optionalParamKey: unique symbol;
|
|
478
|
+
|
|
459
479
|
export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor | DateConstructor | JSON;
|
|
460
480
|
|
|
461
481
|
export declare type ParamEnd = typeof paramEnd;
|
|
@@ -469,6 +489,7 @@ export declare type ParamExtras = {
|
|
|
469
489
|
export declare type ParamGetSet<T = any> = {
|
|
470
490
|
get: ParamGetter<T>;
|
|
471
491
|
set: ParamSetter<T>;
|
|
492
|
+
defaultValue?: T;
|
|
472
493
|
};
|
|
473
494
|
|
|
474
495
|
export declare type ParamGetter<T = any> = (value: string, extras: ParamExtras) => T;
|
|
@@ -479,6 +500,8 @@ export declare type ParamStart = typeof paramStart;
|
|
|
479
500
|
|
|
480
501
|
export declare const paramStart = "[";
|
|
481
502
|
|
|
503
|
+
export declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
|
|
504
|
+
|
|
482
505
|
/**
|
|
483
506
|
* Represents properties common to parent routes in a route configuration, including hooks, path, and optional query parameters.
|
|
484
507
|
*/
|
|
@@ -509,9 +532,9 @@ export declare type ParentRouteProps = Partial<WithComponent | WithComponents> &
|
|
|
509
532
|
meta?: RouteMeta;
|
|
510
533
|
};
|
|
511
534
|
|
|
512
|
-
declare type Path<TPath extends string =
|
|
535
|
+
declare type Path<TPath extends string = string, TParams extends PathParams<TPath> = Record<string, Param | undefined>> = {
|
|
513
536
|
path: TPath;
|
|
514
|
-
params: Identity<ExtractParamsFromPathString<TPath, TParams>>;
|
|
537
|
+
params: string extends TPath ? Record<string, Param> : Identity<ExtractParamsFromPathString<TPath, TParams>>;
|
|
515
538
|
toString: () => string;
|
|
516
539
|
};
|
|
517
540
|
|
|
@@ -548,9 +571,9 @@ declare type Props<TComponent extends Component> = TComponent extends Constructo
|
|
|
548
571
|
|
|
549
572
|
declare type PropsGetter<TComponent extends Component> = () => MaybePromise<Props<TComponent>>;
|
|
550
573
|
|
|
551
|
-
declare type Query<
|
|
552
|
-
query:
|
|
553
|
-
params: Identity<ExtractQueryParamsFromQueryString<
|
|
574
|
+
declare type Query<TQuery extends string = string, TQueryParams extends QueryParams<TQuery> = Record<string, Param | undefined>> = {
|
|
575
|
+
query: TQuery;
|
|
576
|
+
params: string extends TQuery ? Record<string, Param> : Identity<ExtractQueryParamsFromQueryString<TQuery, TQueryParams>>;
|
|
554
577
|
toString: () => string;
|
|
555
578
|
};
|
|
556
579
|
|
|
@@ -660,7 +683,7 @@ declare type ResolvedRoute<TRoute extends Route = BaseResolvedRoute> = DeepReado
|
|
|
660
683
|
* The specific route properties that were matched in the current route, including any ancestors.
|
|
661
684
|
* Order of routes will be from greatest ancestor to narrowest matched.
|
|
662
685
|
*/
|
|
663
|
-
matches:
|
|
686
|
+
matches: TRoute['matches'];
|
|
664
687
|
/**
|
|
665
688
|
* Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
|
|
666
689
|
*/
|
|
@@ -672,7 +695,7 @@ declare type ResolvedRoute<TRoute extends Route = BaseResolvedRoute> = DeepReado
|
|
|
672
695
|
/**
|
|
673
696
|
* Key value pair for route params, values will be the user provided value from current browser location.
|
|
674
697
|
*/
|
|
675
|
-
params: ExtractRouteParamTypes<TRoute>;
|
|
698
|
+
params: BaseResolvedRoute extends TRoute ? Record<string, unknown> : ExtractRouteParamTypes<TRoute>;
|
|
676
699
|
}>;
|
|
677
700
|
|
|
678
701
|
declare type ResolvedRouteQuery = {
|
|
@@ -799,7 +822,7 @@ declare type RouteHookSuccessResponse = {
|
|
|
799
822
|
export declare interface RouteMeta extends Record<string, unknown> {
|
|
800
823
|
}
|
|
801
824
|
|
|
802
|
-
declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> =
|
|
825
|
+
declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
|
|
803
826
|
|
|
804
827
|
/**
|
|
805
828
|
* Unifies the properties of both parent and child routes, ensuring type safety and consistency across route configurations.
|
|
@@ -1159,6 +1182,8 @@ declare type WithComponents = {
|
|
|
1159
1182
|
components: Record<string, Component>;
|
|
1160
1183
|
};
|
|
1161
1184
|
|
|
1185
|
+
export declare function withDefault<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
|
|
1186
|
+
|
|
1162
1187
|
/**
|
|
1163
1188
|
* Defines route hooks that can be applied before entering, updating, or leaving a route, as well as after these events.
|
|
1164
1189
|
*/
|