@kitbag/router 0.17.7 → 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.
@@ -74,6 +74,7 @@ export declare type AddGlobalRouteHooks = (hooks: RouteHooks) => void;
74
74
  export declare type AfterHookContext = {
75
75
  to: ResolvedRoute;
76
76
  from: ResolvedRoute | null;
77
+ app: App | null;
77
78
  };
78
79
 
79
80
  /**
@@ -129,6 +130,7 @@ export declare function asUrl(value: string): Url;
129
130
  export declare type BeforeHookContext = {
130
131
  to: ResolvedRoute;
131
132
  from: ResolvedRoute | null;
133
+ app: App | null;
132
134
  };
133
135
 
134
136
  /**
@@ -218,7 +220,7 @@ declare type CombinePath<TParent extends WithParams, TChild extends WithParams>
218
220
  } ? ToWithParams<TChild> extends {
219
221
  value: infer TChildPath extends string;
220
222
  params: infer TChildParams extends Record<string, unknown>;
221
- } ? 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<'', {}>;
222
224
 
223
225
  declare type CombinePathString<TParent extends string, TChild extends string> = `${TParent}${TChild}`;
224
226
 
@@ -228,7 +230,7 @@ declare type CombineQuery<TParent extends WithParams, TChild extends WithParams>
228
230
  } ? ToWithParams<TChild> extends {
229
231
  value: infer TChildQuery extends string;
230
232
  params: infer TChildParams extends Record<string, unknown>;
231
- } ? 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<'', {}>;
232
234
 
233
235
  declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
234
236
 
@@ -370,8 +372,6 @@ declare type EmptyRouterPlugin = {
370
372
  */
371
373
  declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
372
374
 
373
- 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>;
374
-
375
375
  /**
376
376
  * Extracts the actual type from a parameter type, handling getters and setters.
377
377
  * @template TParam - The parameter type.
@@ -389,38 +389,44 @@ declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity
389
389
  }>>;
390
390
 
391
391
  /**
392
- * 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.
393
394
  * @template TParams - The record of parameter types, possibly including undefined.
394
395
  * @returns A new type with the appropriate properties marked as optional.
395
396
  */
396
- declare type ExtractParamTypesWithOptional<TParams extends Record<string, Param>> = Identity<MakeOptional<{
397
+ declare type ExtractParamTypesOptionalReading<TParams extends Record<string, Param>> = {
397
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]>;
398
- }>>;
399
+ };
399
400
 
400
401
  /**
401
- * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
402
- * 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.
403
414
  * @template TRoute - The route type from which to extract and merge parameter types.
404
415
  * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
405
416
  */
406
- declare type ExtractRouteParamTypesWithOptional<TRoute> = TRoute extends {
407
- host: {
408
- params: infer HostParams extends Record<string, Param>;
409
- };
410
- path: {
411
- params: infer PathParams extends Record<string, Param>;
412
- };
413
- query: {
414
- params: infer QueryParams extends Record<string, Param>;
415
- };
416
- hash: {
417
- params: infer HashParams extends Record<string, Param>;
418
- };
419
- } ? 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']>>>;
420
418
 
421
- declare type ExtractRouteStateParamsAsOptional<T extends Record<string, Param>> = ExtractParamTypesWithOptional<{
422
- [K in keyof T as K extends string ? `?${K}` : never]: T[K];
423
- }>;
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
+ };
424
430
 
425
431
  declare type ExtractStateParams<TRoute> = TRoute extends {
426
432
  state: infer TState extends Record<string, Param>;
@@ -620,10 +626,6 @@ declare type ParamStart = typeof paramStart;
620
626
 
621
627
  declare const paramStart = "[";
622
628
 
623
- declare type ParamsWithParamNameExtracted<TValue extends string> = {
624
- [K in keyof ExtractParamsFromString<TValue> as ExtractParamName<K>]?: Param;
625
- };
626
-
627
629
  declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
628
630
 
629
631
  /**
@@ -704,7 +706,7 @@ export declare interface Register {
704
706
  * Represents the possible Rejections registered within {@link Register}
705
707
  */
706
708
  export declare type RegisteredRejectionType<T = Register> = T extends {
707
- router: Router<Routes, infer TOptions extends RouterOptions>;
709
+ router: Router<any, infer TOptions extends RouterOptions>;
708
710
  } ? keyof TOptions['rejections'] | BuiltInRejectionType : BuiltInRejectionType;
709
711
 
710
712
  /**
@@ -790,7 +792,7 @@ export declare type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
790
792
  /**
791
793
  * Key value pair for route params, values will be the user provided value from current browser location.
792
794
  */
793
- params: ExtractRouteParamTypesWithOptional<TRoute>;
795
+ params: ExtractRouteParamTypesOptionalReading<TRoute>;
794
796
  /**
795
797
  * Type for additional data intended to be stored in history state.
796
798
  */
@@ -911,7 +913,7 @@ export declare type RouteMeta<T = Register> = T extends {
911
913
  routeMeta: infer RouteMeta extends Record<string, unknown>;
912
914
  } ? RouteMeta : Record<string, unknown>;
913
915
 
914
- 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>;
915
917
 
916
918
  declare type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
917
919
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
@@ -1389,15 +1391,19 @@ declare type WithoutParent = {
1389
1391
  parent?: never;
1390
1392
  };
1391
1393
 
1392
- 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>> = {
1393
1395
  value: TValue;
1394
- params: string extends TValue ? Record<string, Param> : Identity<ExtractParamsFromString<TValue, TParams>>;
1396
+ params: string extends TValue ? Record<string, Param> : Identity<WithParamsParamsOutput<TValue, TParams>>;
1395
1397
  };
1396
1398
 
1397
- 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>;
1398
1400
 
1399
1401
  export declare function withParams(): WithParams<'', {}>;
1400
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
+
1401
1407
  declare type WithParent<TParent extends Route = Route> = {
1402
1408
  parent: TParent;
1403
1409
  };