@kitbag/router 0.5.1 → 0.5.3
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/dist/kitbag-router.d.ts +38 -21
- package/dist/kitbag-router.js +711 -673
- package/dist/kitbag-router.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/kitbag-router.d.ts
CHANGED
|
@@ -153,7 +153,7 @@ declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TPa
|
|
|
153
153
|
} ? ToPath<TChild> extends {
|
|
154
154
|
path: infer TChildPath extends string;
|
|
155
155
|
params: infer TChildParams extends Record<string, unknown>;
|
|
156
|
-
} ? MergeParams<TParentParams
|
|
156
|
+
} ? MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>> extends PathParamsWithParamNameExtracted<`${TParentPath}${TChildPath}`> ? Path<`${TParentPath}${TChildPath}`, MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>>> : Path<'', {}> : Path<'', {}> : Path<'', {}>;
|
|
157
157
|
|
|
158
158
|
declare type CombineQuery<TParent extends Query | undefined, TChild extends Query | undefined> = ToQuery<TParent> extends {
|
|
159
159
|
query: infer TParentQuery extends string;
|
|
@@ -161,7 +161,7 @@ declare type CombineQuery<TParent extends Query | undefined, TChild extends Quer
|
|
|
161
161
|
} ? ToQuery<TChild> extends {
|
|
162
162
|
query: infer TChildQuery extends string;
|
|
163
163
|
params: infer TChildParams extends Record<string, unknown>;
|
|
164
|
-
} ? MergeParams<TParentParams
|
|
164
|
+
} ? MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>> extends QueryParamsWithParamNameExtracted<CombineQueryString<TParentQuery, TChildQuery>> ? Query<CombineQueryString<TParentQuery, TChildQuery>, MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
|
|
165
165
|
|
|
166
166
|
declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
|
|
167
167
|
|
|
@@ -246,11 +246,13 @@ export declare class DuplicateParamsError extends Error {
|
|
|
246
246
|
constructor(paramName: string);
|
|
247
247
|
}
|
|
248
248
|
|
|
249
|
+
declare type ExternalChildRoutes = Route<string, never>[];
|
|
250
|
+
|
|
249
251
|
declare type ExternalRouteChildProps = {
|
|
250
252
|
/**
|
|
251
253
|
* Represents the host for this route. Used for external routes.
|
|
252
254
|
*/
|
|
253
|
-
host?: string;
|
|
255
|
+
host?: string | Host;
|
|
254
256
|
/**
|
|
255
257
|
* Name for route, used to create route keys and in navigation.
|
|
256
258
|
*/
|
|
@@ -277,7 +279,7 @@ declare type ExternalRouteParentProps = {
|
|
|
277
279
|
/**
|
|
278
280
|
* Represents the host for this route. Used for external routes.
|
|
279
281
|
*/
|
|
280
|
-
host?: string;
|
|
282
|
+
host?: string | Host;
|
|
281
283
|
/**
|
|
282
284
|
* Name for route, used to create route keys and in navigation.
|
|
283
285
|
*/
|
|
@@ -297,13 +299,11 @@ declare type ExternalRouteParentProps = {
|
|
|
297
299
|
/**
|
|
298
300
|
* Children routes, expected type comes from `createExternalRoutes()`
|
|
299
301
|
*/
|
|
300
|
-
children:
|
|
302
|
+
children: ExternalChildRoutes;
|
|
301
303
|
};
|
|
302
304
|
|
|
303
305
|
declare type ExternalRouteProps = ExternalRouteParentProps | ExternalRouteChildProps;
|
|
304
306
|
|
|
305
|
-
declare type ExternalRoutes = Route<string, '', Path, Query, boolean>[];
|
|
306
|
-
|
|
307
307
|
/**
|
|
308
308
|
* Extracts the parameter name from a string, handling optional parameters denoted by a leading '?'.
|
|
309
309
|
* @template TParam - The string from which to extract the parameter name.
|
|
@@ -311,6 +311,10 @@ declare type ExternalRoutes = Route<string, '', Path, Query, boolean>[];
|
|
|
311
311
|
*/
|
|
312
312
|
export declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
|
|
313
313
|
|
|
314
|
+
declare type ExtractParamsFromHostString<THost extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = THost extends `${string}${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? MergeParams<{
|
|
315
|
+
[P in Param]: ExtractPathParamType<Param, TParams>;
|
|
316
|
+
}, ExtractParamsFromHostString<Rest, TParams>> : Record<never, never>;
|
|
317
|
+
|
|
314
318
|
declare type ExtractParamsFromPathString<TPath extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TPath extends `${string}${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? MergeParams<{
|
|
315
319
|
[P in Param]: ExtractPathParamType<Param, TParams>;
|
|
316
320
|
}, ExtractParamsFromPathString<Rest, TParams>> : Record<never, never>;
|
|
@@ -368,21 +372,24 @@ export declare type ExtractRouteParamTypes<TRoute> = TRoute extends {
|
|
|
368
372
|
} ? ExtractParamTypes<MergeParams<PathParams, QueryParams>> : {};
|
|
369
373
|
|
|
370
374
|
declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extends {
|
|
375
|
+
host: {
|
|
376
|
+
params: infer HostParams extends Record<string, Param>;
|
|
377
|
+
};
|
|
371
378
|
path: {
|
|
372
379
|
params: infer PathParams extends Record<string, Param>;
|
|
373
380
|
};
|
|
374
381
|
query: {
|
|
375
382
|
params: infer QueryParams extends Record<string, Param>;
|
|
376
383
|
};
|
|
377
|
-
} ? ExtractParamTypesWithoutLosingOptional<MergeParams<PathParams, QueryParams
|
|
384
|
+
} ? ExtractParamTypesWithoutLosingOptional<MergeParams<HostParams, MergeParams<PathParams, QueryParams>>> : Record<string, unknown>;
|
|
378
385
|
|
|
379
386
|
declare type Flatten<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends unknown[] ? Flatten<[...First, ...Flatten<Rest>]> : [First, ...Flatten<Rest>] : [];
|
|
380
387
|
|
|
381
388
|
declare type FlattenRoute<TRoute extends RouteProps | ExternalRouteProps, TKey extends string = TRoute extends {
|
|
382
389
|
name: infer T extends string;
|
|
383
|
-
} ? T : '', THost extends
|
|
384
|
-
host: infer T extends string;
|
|
385
|
-
} ? T :
|
|
390
|
+
} ? T : '', THost extends Host = TRoute extends {
|
|
391
|
+
host: infer T extends Host | string;
|
|
392
|
+
} ? ToHost<T> : never, TPath extends Path = ToPath<TRoute['path']>, TQuery extends Query = ToQuery<TRoute['query']>, TDisabled extends boolean = TRoute['disabled'] extends boolean ? TRoute['disabled'] : false, TChildren extends Route[] = ExtractRouteChildren<TRoute>> = [
|
|
386
393
|
Route<TKey, THost, TPath, TQuery, TDisabled>,
|
|
387
394
|
...{
|
|
388
395
|
[K in keyof TChildren]: Route<CombineName<TKey, TChildren[K]['key']>, THost, CombinePath<TPath, TChildren[K]['path']>, CombineQuery<TQuery, TChildren[K]['query']>, TChildren[K]['disabled']>;
|
|
@@ -395,6 +402,16 @@ declare type FlattenRoutes<TRoutes extends Readonly<RouteProps[] | ExternalRoute
|
|
|
395
402
|
}
|
|
396
403
|
]>;
|
|
397
404
|
|
|
405
|
+
declare type Host<THost extends string = string, TParams extends HostParamsWithParamNameExtracted<THost> = Record<string, Param | undefined>> = {
|
|
406
|
+
host: THost;
|
|
407
|
+
params: string extends THost ? Record<string, Param> : Identity<ExtractParamsFromHostString<THost, TParams>>;
|
|
408
|
+
toString: () => string;
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
declare type HostParamsWithParamNameExtracted<THost extends string> = {
|
|
412
|
+
[K in keyof ExtractParamsFromHostString<THost> as ExtractParamName<K>]?: Param;
|
|
413
|
+
};
|
|
414
|
+
|
|
398
415
|
declare type Identity<T> = T extends object ? {} & {
|
|
399
416
|
[P in keyof T as T[P] extends never ? never : P]: T[P];
|
|
400
417
|
} : T;
|
|
@@ -624,10 +641,6 @@ declare type Path<TPath extends string = string, TParams extends PathParamsWithP
|
|
|
624
641
|
*/
|
|
625
642
|
export declare function path<TPath extends string, TParams extends PathParamsWithParamNameExtracted<TPath>>(path: TPath, params: Identity<TParams>): Path<TPath, TParams>;
|
|
626
643
|
|
|
627
|
-
declare type PathParams<TPath extends string> = {
|
|
628
|
-
[K in keyof ExtractParamsFromPathString<TPath>]?: Param;
|
|
629
|
-
};
|
|
630
|
-
|
|
631
644
|
declare type PathParamsWithParamNameExtracted<TPath extends string> = {
|
|
632
645
|
[K in keyof ExtractParamsFromPathString<TPath> as ExtractParamName<K>]?: Param;
|
|
633
646
|
};
|
|
@@ -667,10 +680,6 @@ declare type Query<TQuery extends string = string, TQueryParams extends QueryPar
|
|
|
667
680
|
*/
|
|
668
681
|
export declare function query<TQuery extends string, TParams extends QueryParamsWithParamNameExtracted<TQuery>>(query: TQuery, params: Identity<TParams>): Query<TQuery, TParams>;
|
|
669
682
|
|
|
670
|
-
declare type QueryParams<T extends string> = {
|
|
671
|
-
[K in keyof ExtractQueryParamsFromQueryString<T>]?: Param;
|
|
672
|
-
};
|
|
673
|
-
|
|
674
683
|
declare type QueryParamsWithParamNameExtracted<T extends string> = {
|
|
675
684
|
[K in keyof ExtractQueryParamsFromQueryString<T> as ExtractParamName<K>]?: Param;
|
|
676
685
|
};
|
|
@@ -737,13 +746,19 @@ export declare type RegisteredRouterState = Register extends {
|
|
|
737
746
|
*/
|
|
738
747
|
export declare type RegisteredRoutes = Register extends {
|
|
739
748
|
router: Router<infer TRoutes extends Routes>;
|
|
740
|
-
} ? TRoutes : Route<string,
|
|
749
|
+
} ? TRoutes : Route<string, Host, Path, Query, false>[];
|
|
741
750
|
|
|
742
751
|
/**
|
|
743
752
|
* Represents the union of all possible RouteKeys registered within {@link Register}
|
|
744
753
|
*/
|
|
745
754
|
export declare type RegisteredRoutesKey = RoutesKey<RegisteredRoutes>;
|
|
746
755
|
|
|
756
|
+
declare type RemoveLeadingQuestionMark<T extends PropertyKey> = T extends `?${infer TRest extends string}` ? TRest : T;
|
|
757
|
+
|
|
758
|
+
declare type RemoveLeadingQuestionMarkFromKeys<T extends Record<string, unknown>> = {
|
|
759
|
+
[K in keyof T as RemoveLeadingQuestionMark<K>]: T[K];
|
|
760
|
+
};
|
|
761
|
+
|
|
747
762
|
/**
|
|
748
763
|
* Represents a route that the router has matched to current browser location.
|
|
749
764
|
* @template TRoute - Underlying Route that has been resolved.
|
|
@@ -784,7 +799,7 @@ declare type ResolvedRouteQuery = {
|
|
|
784
799
|
* @template TQuery - The type or structure of the query parameters associated with the route.
|
|
785
800
|
* @template TDisabled - Indicates whether the route is disabled, which could affect routing logic.
|
|
786
801
|
*/
|
|
787
|
-
export declare type Route<TKey extends string = string, THost extends
|
|
802
|
+
export declare type Route<TKey extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, TDisabled extends boolean = boolean> = {
|
|
788
803
|
/**
|
|
789
804
|
* The specific route properties that were matched in the current route.
|
|
790
805
|
*/
|
|
@@ -1181,6 +1196,8 @@ export declare function throwIfDuplicateParamsAreFound(routes: Route[]): void;
|
|
|
1181
1196
|
|
|
1182
1197
|
declare type ToCallback = (resolve: RegisteredRouter['resolve']) => string;
|
|
1183
1198
|
|
|
1199
|
+
declare type ToHost<T extends string | Host> = T extends string ? Host<T, {}> : T;
|
|
1200
|
+
|
|
1184
1201
|
declare type ToPath<T extends string | Path> = T extends string ? Path<T, {}> : T;
|
|
1185
1202
|
|
|
1186
1203
|
declare type ToQuery<T extends string | Query | undefined> = T extends string ? Query<T, {}> : T extends undefined ? Query<'', {}> : unknown extends T ? Query<'', {}> : T;
|