@kitbag/router 0.18.0 → 0.18.1

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
+ } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends Record<string, Param | undefined> ? WithParams<CombinePathString<TParentPath, TChildPath>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<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
+ } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends Record<string, Param | undefined> ? WithParams<CombineQueryString<TParentQuery, TChildQuery>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<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
 
@@ -372,8 +372,6 @@ declare type EmptyRouterPlugin = {
372
372
  */
373
373
  declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
374
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>;
376
-
377
375
  /**
378
376
  * Extracts the actual type from a parameter type, handling getters and setters.
379
377
  * @template TParam - The parameter type.
@@ -391,38 +389,44 @@ declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity
391
389
  }>>;
392
390
 
393
391
  /**
394
- * Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
392
+ * Does everything that ExtractParamTypes does, but takes into consideration optional properties.
393
+ * Differs from ExtractParamTypes in that it also reads the string `value` from WithParams to determine what should be optional.
395
394
  * @template TParams - The record of parameter types, possibly including undefined.
396
395
  * @returns A new type with the appropriate properties marked as optional.
397
396
  */
398
- declare type ExtractParamTypesWithOptional<TParams extends Record<string, Param>> = Identity<MakeOptional<{
397
+ declare type ExtractParamTypesOptionalReading<TParams extends Record<string, Param>> = {
399
398
  [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
- }>>;
399
+ };
401
400
 
402
401
  /**
403
- * 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.
402
+ * Does everything that ExtractParamTypes does, but takes into consideration optional properties.
403
+ * Differs from ExtractParamTypesOptionalReading in that optional params with defaults will remain optional.
404
+ * @template TParams - The record of parameter types, possibly including undefined.
405
+ * @returns A new type with the appropriate properties marked as optional.
406
+ */
407
+ declare type ExtractParamTypesOptionalWriting<TParams extends Record<string, Param>> = {
408
+ [K in keyof TParams as ExtractParamName<K>]: K extends `?${string}` ? ExtractParamType<TParams[K]> | undefined : ExtractParamType<TParams[K]>;
409
+ };
410
+
411
+ /**
412
+ * Does everything that ExtractRouteParamTypes does, but takes into consideration optional properties.
413
+ * Differs from ExtractRouteParamTypes in that it also reads the string `value` from WithParams to determine what should be optional.
405
414
  * @template TRoute - The route type from which to extract and merge parameter types.
406
415
  * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
407
416
  */
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>;
417
+ declare type ExtractRouteParamTypesOptionalReading<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesOptionalReading<TRoute['host']['params']> & ExtractParamTypesOptionalReading<TRoute['path']['params']> & ExtractParamTypesOptionalReading<TRoute['query']['params']> & ExtractParamTypesOptionalReading<TRoute['hash']['params']>>>;
422
418
 
423
- declare type ExtractRouteStateParamsAsOptional<T extends Record<string, Param>> = ExtractParamTypesWithOptional<{
424
- [K in keyof T as K extends string ? `?${K}` : never]: T[K];
425
- }>;
419
+ /**
420
+ * Does everything that ExtractRouteParamTypes does, but takes into consideration optional properties.
421
+ * Differs from ExtractRouteParamTypesOptionalReading in that optional params with defaults will remain optional.
422
+ * @template TRoute - The route type from which to extract and merge parameter types.
423
+ * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
424
+ */
425
+ declare type ExtractRouteParamTypesOptionalWriting<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesOptionalWriting<TRoute['host']['params']> & ExtractParamTypesOptionalWriting<TRoute['path']['params']> & ExtractParamTypesOptionalWriting<TRoute['query']['params']> & ExtractParamTypesOptionalWriting<TRoute['hash']['params']>>>;
426
+
427
+ declare type ExtractRouteStateParamsAsOptional<TParams extends Record<string, Param>> = {
428
+ [K in keyof TParams]: ExtractParamType<TParams[K]> | undefined;
429
+ };
426
430
 
427
431
  declare type ExtractStateParams<TRoute> = TRoute extends {
428
432
  state: infer TState extends Record<string, Param>;
@@ -622,10 +626,6 @@ declare type ParamStart = typeof paramStart;
622
626
 
623
627
  declare const paramStart = "[";
624
628
 
625
- declare type ParamsWithParamNameExtracted<TValue extends string> = {
626
- [K in keyof ExtractParamsFromString<TValue> as ExtractParamName<K>]?: Param;
627
- };
628
-
629
629
  declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
630
630
 
631
631
  /**
@@ -792,7 +792,7 @@ export declare type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
792
792
  /**
793
793
  * Key value pair for route params, values will be the user provided value from current browser location.
794
794
  */
795
- params: ExtractRouteParamTypesWithOptional<TRoute>;
795
+ params: ExtractRouteParamTypesOptionalReading<TRoute>;
796
796
  /**
797
797
  * Type for additional data intended to be stored in history state.
798
798
  */
@@ -913,7 +913,7 @@ export declare type RouteMeta<T = Register> = T extends {
913
913
  routeMeta: infer RouteMeta extends Record<string, unknown>;
914
914
  } ? RouteMeta : Record<string, unknown>;
915
915
 
916
- declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithOptional<RouteGetByKey<TRoutes, TKey>>;
916
+ declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = RouteGetByKey<TRoutes, TKey> extends Route ? ExtractRouteParamTypesOptionalWriting<RouteGetByKey<TRoutes, TKey>> : Record<string, unknown>;
917
917
 
918
918
  declare type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
919
919
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
@@ -1391,15 +1391,19 @@ declare type WithoutParent = {
1391
1391
  parent?: never;
1392
1392
  };
1393
1393
 
1394
- declare type WithParams<TValue extends string = string, TParams extends ParamsWithParamNameExtracted<TValue> = Record<string, Param | undefined>> = {
1394
+ declare type WithParams<TValue extends string = string, TParams extends Record<string, Param | undefined> = Record<string, Param | undefined>> = {
1395
1395
  value: TValue;
1396
- params: string extends TValue ? Record<string, Param> : Identity<ExtractParamsFromString<TValue, TParams>>;
1396
+ params: string extends TValue ? Record<string, Param> : Identity<WithParamsParamsOutput<TValue, TParams>>;
1397
1397
  };
1398
1398
 
1399
- export declare function withParams<const TValue extends string, const TParams extends ParamsWithParamNameExtracted<TValue>>(value: TValue, params: TParams): WithParams<TValue, TParams>;
1399
+ export declare function withParams<const TValue extends string, const TParams extends MakeOptional<WithParamsParamsInput<TValue>>>(value: TValue, params: TParams): WithParams<TValue, TParams>;
1400
1400
 
1401
1401
  export declare function withParams(): WithParams<'', {}>;
1402
1402
 
1403
+ declare type WithParamsParamsInput<TValue extends string> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? Record<ExtractParamName<TParam>, Param | undefined> & WithParamsParamsInput<Rest> : {};
1404
+
1405
+ declare type WithParamsParamsOutput<TValue extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TValue extends `${string}${ParamStart}${infer TParam}${ParamEnd}${infer Rest}` ? Record<TParam, ExtractWithParamsParamType<TParam, TParams>> & WithParamsParamsOutput<Rest, TParams> : {};
1406
+
1403
1407
  declare type WithParent<TParent extends Route = Route> = {
1404
1408
  parent: TParent;
1405
1409
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kitbag/router",
3
3
  "private": false,
4
- "version": "0.18.0",
4
+ "version": "0.18.1",
5
5
  "bugs": {
6
6
  "url": "https://github.com/kitbagjs/router/issues"
7
7
  },