@kitbag/router 0.18.0 → 0.18.2

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.
@@ -220,7 +220,7 @@ declare type CombinePath<TParent extends WithParams, TChild extends WithParams>
220
220
  } ? ToWithParams<TChild> extends {
221
221
  value: infer TChildPath extends string;
222
222
  params: infer TChildParams extends Record<string, unknown>;
223
- } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends ParamsWithParamNameExtracted<CombinePathString<TParentPath, TChildPath>> ? WithParams<CombinePathString<TParentPath, TChildPath>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : WithParams<'', {}> : WithParams<'', {}> : WithParams<'', {}>;
223
+ } ? TParentParams & TChildParams extends Record<string, Param | undefined> ? WithParams<CombinePathString<TParentPath, TChildPath>, TParentParams & TChildParams> : WithParams<CombinePathString<TParentPath, TChildPath>, {}> : WithParams<'', {}> : WithParams<'', {}>;
224
224
 
225
225
  declare type CombinePathString<TParent extends string, TChild extends string> = `${TParent}${TChild}`;
226
226
 
@@ -230,7 +230,7 @@ declare type CombineQuery<TParent extends WithParams, TChild extends WithParams>
230
230
  } ? ToWithParams<TChild> extends {
231
231
  value: infer TChildQuery extends string;
232
232
  params: infer TChildParams extends Record<string, unknown>;
233
- } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends ParamsWithParamNameExtracted<CombineQueryString<TParentQuery, TChildQuery>> ? WithParams<CombineQueryString<TParentQuery, TChildQuery>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : WithParams<'', {}> : WithParams<'', {}> : WithParams<'', {}>;
233
+ } ? TParentParams & TChildParams extends Record<string, Param | undefined> ? WithParams<CombineQueryString<TParentQuery, TChildQuery>, TParentParams & TChildParams> : WithParams<CombineQueryString<TParentQuery, TChildQuery>, {}> : WithParams<'', {}> : WithParams<'', {}>;
234
234
 
235
235
  declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
236
236
 
@@ -250,9 +250,9 @@ export declare type CreatedRouteOptions = Omit<CreateRouteOptions, 'props'> & Wi
250
250
  props?: unknown;
251
251
  };
252
252
 
253
- export declare function createExternalRoute<const THost extends string | WithParams, const TName extends string | undefined = undefined, const TPath extends string | WithParams | undefined = undefined, const TQuery extends string | WithParams | undefined = undefined, const THash extends string | WithParams | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery> & WithHost<THost> & WithoutParent): Route<ToName<TName>, ToWithParams<THost>, ToWithParams<TPath>, ToWithParams<TQuery>, ToWithParams<THash>, TMeta>;
253
+ export declare function createExternalRoute<const THost extends string | WithParams, const TName extends string | undefined = undefined, const TPath extends string | WithParams | undefined = undefined, const TQuery extends string | WithParams | undefined = undefined, const THash extends string | WithParams | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithHost<THost> & WithoutParent): Route<ToName<TName>, ToWithParams<THost>, ToWithParams<TPath>, ToWithParams<TQuery>, ToWithParams<THash>, TMeta>;
254
254
 
255
- export declare function createExternalRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | WithParams | undefined = undefined, const TQuery extends string | WithParams | undefined = undefined, const THash extends string | WithParams | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery> & WithoutHost & WithParent<TParent>): Route<ToName<TName>, WithParams<'', {}>, CombinePath<TParent['path'], ToWithParams<TPath>>, CombineQuery<TParent['query'], ToWithParams<TQuery>>, CombineHash<TParent['hash'], ToWithParams<THash>>, CombineMeta<TMeta, TParent['meta']>>;
255
+ export declare function createExternalRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | WithParams | undefined = undefined, const TQuery extends string | WithParams | undefined = undefined, const THash extends string | WithParams | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta> & WithoutHost & WithParent<TParent>): Route<ToName<TName>, WithParams<'', {}>, CombinePath<TParent['path'], ToWithParams<TPath>>, CombineQuery<TParent['query'], ToWithParams<TQuery>>, CombineHash<TParent['hash'], ToWithParams<THash>>, CombineMeta<TMeta, TParent['meta']>>;
256
256
 
257
257
  export declare function createParam<TParam extends ParamWithDefault>(param: TParam): TParam;
258
258
 
@@ -370,9 +370,7 @@ declare type EmptyRouterPlugin = {
370
370
  * @template TParam - The string from which to extract the parameter name.
371
371
  * @returns The extracted parameter name, or never if the parameter string is empty.
372
372
  */
373
- declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
374
-
375
- declare type ExtractParamsFromString<TValue extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TValue extends `${string}${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? Record<Param, ExtractWithParamsParamType<Param, TParams>> & ExtractParamsFromString<Rest, TParams> : Record<never, never>;
373
+ declare type ExtractParamName<TParam extends PropertyKey> = TParam extends string ? TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam : never;
376
374
 
377
375
  /**
378
376
  * Extracts the actual type from a parameter type, handling getters and setters.
@@ -381,61 +379,52 @@ declare type ExtractParamsFromString<TValue extends string, TParams extends Reco
381
379
  */
382
380
  declare type ExtractParamType<TParam extends Param> = TParam extends ParamGetSet<infer Type> ? Type : TParam extends ParamGetter ? ReturnType<TParam> : TParam extends ZodSchemaLike<infer Type> ? Type : TParam extends LiteralParam ? TParam : string;
383
381
 
382
+ declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
383
+ [K in keyof TParams as ExtractParamName<K>]: ExtractParamType<TParams[K]>;
384
+ }>>;
385
+
384
386
  /**
385
- * Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
387
+ * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
386
388
  * @template TParams - The record of parameter types, possibly including undefined.
387
389
  * @returns A new type with the appropriate properties marked as optional.
388
390
  */
389
- declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
390
- [K in keyof TParams as ExtractParamName<K>]: ExtractParamType<TParams[K]>;
391
- }>>;
391
+ declare type ExtractParamTypesReading<TWithParams extends WithParams> = {
392
+ [K in keyof TWithParams['params']]: TWithParams['value'] extends `${string}${ParamStart}?${K & string}${ParamEnd}${string}` ? TWithParams['params'][K] extends Required<ParamGetSet> ? ExtractParamType<TWithParams['params'][K]> : ExtractParamType<TWithParams['params'][K]> | undefined : ExtractParamType<TWithParams['params'][K]>;
393
+ };
392
394
 
393
395
  /**
394
- * Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
396
+ * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
397
+ * Differs from ExtractParamTypesReading in that optional params with defaults will remain optional.
395
398
  * @template TParams - The record of parameter types, possibly including undefined.
396
399
  * @returns A new type with the appropriate properties marked as optional.
397
400
  */
398
- declare type ExtractParamTypesWithOptional<TParams extends Record<string, Param>> = Identity<MakeOptional<{
399
- [K in keyof TParams as ExtractParamName<K>]: K extends `?${string}` ? TParams[K] extends Required<ParamGetSet> ? ExtractParamType<TParams[K]> : ExtractParamType<TParams[K]> | undefined : ExtractParamType<TParams[K]>;
400
- }>>;
401
+ declare type ExtractParamTypesWriting<TWithParams extends WithParams> = {
402
+ [K in keyof TWithParams['params']]: TWithParams['value'] extends `${string}${ParamStart}?${K & string}${ParamEnd}${string}` ? ExtractParamType<TWithParams['params'][K]> | undefined : ExtractParamType<TWithParams['params'][K]>;
403
+ };
401
404
 
402
405
  /**
403
406
  * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
404
- * This parameter object type represents the expected type when accessing params from router.route or useRoute.
405
407
  * @template TRoute - The route type from which to extract and merge parameter types.
406
408
  * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
407
409
  */
408
- declare type ExtractRouteParamTypesWithOptional<TRoute> = TRoute extends {
409
- host: {
410
- params: infer HostParams extends Record<string, Param>;
411
- };
412
- path: {
413
- params: infer PathParams extends Record<string, Param>;
414
- };
415
- query: {
416
- params: infer QueryParams extends Record<string, Param>;
417
- };
418
- hash: {
419
- params: infer HashParams extends Record<string, Param>;
420
- };
421
- } ? ExtractParamTypesWithOptional<HostParams & PathParams & QueryParams & HashParams> : Record<string, unknown>;
410
+ declare type ExtractRouteParamTypesReading<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesReading<TRoute['host']> & ExtractParamTypesReading<TRoute['path']> & ExtractParamTypesReading<TRoute['query']> & ExtractParamTypesReading<TRoute['hash']>>>;
422
411
 
423
- declare type ExtractRouteStateParamsAsOptional<T extends Record<string, Param>> = ExtractParamTypesWithOptional<{
424
- [K in keyof T as K extends string ? `?${K}` : never]: T[K];
425
- }>;
412
+ /**
413
+ * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
414
+ * Differs from ExtractRouteParamTypesReading in that optional params with defaults will remain optional.
415
+ * @template TRoute - The route type from which to extract and merge parameter types.
416
+ * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
417
+ */
418
+ declare type ExtractRouteParamTypesWriting<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesWriting<TRoute['host']> & ExtractParamTypesWriting<TRoute['path']> & ExtractParamTypesWriting<TRoute['query']> & ExtractParamTypesWriting<TRoute['hash']>>>;
419
+
420
+ declare type ExtractRouteStateParamsAsOptional<TParams extends Record<string, Param>> = {
421
+ [K in keyof TParams]: ExtractParamType<TParams[K]> | undefined;
422
+ };
426
423
 
427
424
  declare type ExtractStateParams<TRoute> = TRoute extends {
428
425
  state: infer TState extends Record<string, Param>;
429
426
  } ? ExtractParamTypes<TState> : Record<string, unknown>;
430
427
 
431
- /**
432
- * Determines the type of a path parameter from a record of parameter types, considering optional parameters.
433
- * @template TParam - The parameter name string.
434
- * @template TParams - The record object mapping parameter names to their types.
435
- * @returns The type associated with the parameter, or StringConstructor if unspecified; may be undefined for optional parameters.
436
- */
437
- declare type ExtractWithParamsParamType<TParam extends string, TParams extends Record<string, Param | undefined>> = TParam extends `?${infer OptionalParam}` ? OptionalParam extends keyof TParams ? TParams[OptionalParam] : StringConstructor : TParam extends keyof TParams ? TParams[TParam] : StringConstructor;
438
-
439
428
  declare type GetParentPropsReturnType<TParent extends Route | undefined = Route | undefined> = TParent extends Route ? TParent['matched']['props'] extends PropsGetter ? ReturnType<TParent['matched']['props']> : TParent['matched']['props'] extends Record<string, PropsGetter> ? {
440
429
  [K in keyof TParent['matched']['props']]: ReturnType<TParent['matched']['props'][K]>;
441
430
  } : undefined : undefined;
@@ -622,10 +611,6 @@ declare type ParamStart = typeof paramStart;
622
611
 
623
612
  declare const paramStart = "[";
624
613
 
625
- declare type ParamsWithParamNameExtracted<TValue extends string> = {
626
- [K in keyof ExtractParamsFromString<TValue> as ExtractParamName<K>]?: Param;
627
- };
628
-
629
614
  declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
630
615
 
631
616
  /**
@@ -753,12 +738,6 @@ export declare type RegisteredRoutes<T = Register> = T extends {
753
738
  */
754
739
  export declare type RegisteredRoutesName = RoutesName<RegisteredRoutes>;
755
740
 
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
-
762
741
  /**
763
742
  * Represents a route that the router has matched to current browser location.
764
743
  * @template TRoute - Underlying Route that has been resolved.
@@ -792,7 +771,7 @@ export declare type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
792
771
  /**
793
772
  * Key value pair for route params, values will be the user provided value from current browser location.
794
773
  */
795
- params: ExtractRouteParamTypesWithOptional<TRoute>;
774
+ params: ExtractRouteParamTypesReading<TRoute>;
796
775
  /**
797
776
  * Type for additional data intended to be stored in history state.
798
777
  */
@@ -913,7 +892,7 @@ export declare type RouteMeta<T = Register> = T extends {
913
892
  routeMeta: infer RouteMeta extends Record<string, unknown>;
914
893
  } ? RouteMeta : Record<string, unknown>;
915
894
 
916
- declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithOptional<RouteGetByKey<TRoutes, TKey>>;
895
+ declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = RouteGetByKey<TRoutes, TKey> extends Route ? ExtractRouteParamTypesWriting<RouteGetByKey<TRoutes, TKey>> : Record<string, unknown>;
917
896
 
918
897
  declare type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
919
898
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
@@ -1391,15 +1370,19 @@ declare type WithoutParent = {
1391
1370
  parent?: never;
1392
1371
  };
1393
1372
 
1394
- declare type WithParams<TValue extends string = string, TParams extends ParamsWithParamNameExtracted<TValue> = Record<string, Param | undefined>> = {
1373
+ declare type WithParams<TValue extends string = string, TParams extends Record<string, Param | undefined> = Record<string, Param | undefined>> = {
1395
1374
  value: TValue;
1396
- params: string extends TValue ? Record<string, Param> : Identity<ExtractParamsFromString<TValue, TParams>>;
1375
+ params: string extends TValue ? Record<string, Param> : Identity<WithParamsParamsOutput<TValue, TParams>>;
1397
1376
  };
1398
1377
 
1399
- export declare function withParams<const TValue extends string, const TParams extends ParamsWithParamNameExtracted<TValue>>(value: TValue, params: TParams): WithParams<TValue, TParams>;
1378
+ export declare function withParams<const TValue extends string, const TParams extends MakeOptional<WithParamsParamsInput<TValue>>>(value: TValue, params: TParams): WithParams<TValue, TParams>;
1400
1379
 
1401
1380
  export declare function withParams(): WithParams<'', {}>;
1402
1381
 
1382
+ declare type WithParamsParamsInput<TValue extends string> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? Record<ExtractParamName<TParam>, Param | undefined> & WithParamsParamsInput<Rest> : {};
1383
+
1384
+ declare type WithParamsParamsOutput<TValue extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? ExtractParamName<TParam> extends keyof TParams ? Record<ExtractParamName<TParam>, TParams[ExtractParamName<TParam>]> & WithParamsParamsOutput<Rest, TParams> : Record<ExtractParamName<TParam>, StringConstructor> & WithParamsParamsOutput<Rest, TParams> : {};
1385
+
1403
1386
  declare type WithParent<TParent extends Route = Route> = {
1404
1387
  parent: TParent;
1405
1388
  };