@kitbag/router 0.5.2 → 0.5.4

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.
@@ -134,7 +134,7 @@ export declare type ChildRouteProps = (WithComponent | WithComponents) & WithHoo
134
134
  /**
135
135
  * Path part of URL.
136
136
  */
137
- path: string | Path;
137
+ path?: string | Path;
138
138
  /**
139
139
  * Query (aka search) part of URL.
140
140
  */
@@ -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
  */
@@ -262,7 +264,7 @@ declare type ExternalRouteChildProps = {
262
264
  /**
263
265
  * Path part of URL.
264
266
  */
265
- path: string | Path;
267
+ path?: string | Path;
266
268
  /**
267
269
  * Query (aka search) part of URL.
268
270
  */
@@ -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
  */
@@ -289,7 +291,7 @@ declare type ExternalRouteParentProps = {
289
291
  /**
290
292
  * Path part of URL.
291
293
  */
292
- path: string | Path;
294
+ path?: string | Path;
293
295
  /**
294
296
  * Query (aka search) part of URL.
295
297
  */
@@ -297,13 +299,11 @@ declare type ExternalRouteParentProps = {
297
299
  /**
298
300
  * Children routes, expected type comes from `createExternalRoutes()`
299
301
  */
300
- children: ExternalRoutes;
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>> : Record<string, unknown>;
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 string = TRoute extends {
384
- host: infer T extends string;
385
- } ? T : '', 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>> = [
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;
@@ -574,7 +591,7 @@ export declare type ParentRouteProps = Partial<WithComponent | WithComponents> &
574
591
  /**
575
592
  * Path part of URL.
576
593
  */
577
- path: string | Path;
594
+ path?: string | Path;
578
595
  /**
579
596
  * Query (aka search) part of URL.
580
597
  */
@@ -729,7 +746,7 @@ export declare type RegisteredRouterState = Register extends {
729
746
  */
730
747
  export declare type RegisteredRoutes = Register extends {
731
748
  router: Router<infer TRoutes extends Routes>;
732
- } ? TRoutes : Route<string, '', Path<'', {}>, Query<'', {}>, false>[];
749
+ } ? TRoutes : Route<string, Host, Path, Query, false>[];
733
750
 
734
751
  /**
735
752
  * Represents the union of all possible RouteKeys registered within {@link Register}
@@ -782,7 +799,7 @@ declare type ResolvedRouteQuery = {
782
799
  * @template TQuery - The type or structure of the query parameters associated with the route.
783
800
  * @template TDisabled - Indicates whether the route is disabled, which could affect routing logic.
784
801
  */
785
- export declare type Route<TKey extends string = string, THost extends string = string, TPath extends Path = Path, TQuery extends Query = Query, TDisabled extends boolean = boolean> = {
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> = {
786
803
  /**
787
804
  * The specific route properties that were matched in the current route.
788
805
  */
@@ -1179,7 +1196,9 @@ export declare function throwIfDuplicateParamsAreFound(routes: Route[]): void;
1179
1196
 
1180
1197
  declare type ToCallback = (resolve: RegisteredRouter['resolve']) => string;
1181
1198
 
1182
- declare type ToPath<T extends string | Path> = T extends string ? Path<T, {}> : T;
1199
+ declare type ToHost<T extends string | Host> = T extends string ? Host<T, {}> : T;
1200
+
1201
+ declare type ToPath<T extends string | Path | undefined> = T extends string ? Path<T, {}> : T extends undefined ? Path<'', {}> : unknown extends T ? Path<'', {}> : T;
1183
1202
 
1184
1203
  declare type ToQuery<T extends string | Query | undefined> = T extends string ? Query<T, {}> : T extends undefined ? Query<'', {}> : unknown extends T ? Query<'', {}> : T;
1185
1204