@kitbag/router 0.5.4 → 0.7.0

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.
@@ -88,6 +88,8 @@ export declare type AfterRouteHookResponse<TRoutes extends Routes> = RouteHookSu
88
88
 
89
89
  declare type AllPropertiesAreOptional<T> = Record<string, unknown> extends T ? true : IsEmptyObject<OnlyRequiredProperties<T>>;
90
90
 
91
+ declare type AsNamedRoute<T extends Route> = IsRouteUnnamed<T> extends true ? never : T;
92
+
91
93
  /**
92
94
  * Represents a function called before a route change, potentially altering the routing operation.
93
95
  * @param to - {@link ResolvedRoute} The resolved route the router is navigating to.
@@ -119,33 +121,7 @@ declare const builtInRejections: ['NotFound'];
119
121
 
120
122
  declare type BuiltInRejectionType = typeof builtInRejections[number];
121
123
 
122
- /**
123
- * Represents properties for child routes, including required component, name, and path.
124
- */
125
- export declare type ChildRouteProps = (WithComponent | WithComponents) & WithHooks & {
126
- /**
127
- * Name for route, used to create route keys and in navigation.
128
- */
129
- name: string;
130
- /**
131
- * Children routes, expected type comes from `createRoutes()`
132
- */
133
- disabled?: boolean;
134
- /**
135
- * Path part of URL.
136
- */
137
- path?: string | Path;
138
- /**
139
- * Query (aka search) part of URL.
140
- */
141
- query?: string | Query;
142
- /**
143
- * Represents additional metadata associated with a route, customizable via declaration merging.
144
- */
145
- meta?: RouteMeta;
146
- };
147
-
148
- declare type CombineName<TParentName extends string | undefined, TChildName extends string | undefined> = StringHasValue<TParentName> extends true ? StringHasValue<TChildName> extends true ? `${TParentName}.${TChildName}` : TParentName : StringHasValue<TChildName> extends true ? TChildName : '';
124
+ declare type CombineKey<TParentKey extends string | undefined, TChildKey extends string | undefined> = StringHasValue<TParentKey> extends true ? StringHasValue<TChildKey> extends true ? `${TParentKey}.${TChildKey}` : TParentKey : StringHasValue<TChildKey> extends true ? TChildKey : '';
149
125
 
150
126
  declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TParent> extends {
151
127
  path: infer TParentPath extends string;
@@ -153,15 +129,15 @@ declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TPa
153
129
  } ? ToPath<TChild> extends {
154
130
  path: infer TChildPath extends string;
155
131
  params: infer TChildParams extends Record<string, unknown>;
156
- } ? MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>> extends PathParamsWithParamNameExtracted<`${TParentPath}${TChildPath}`> ? Path<`${TParentPath}${TChildPath}`, MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>>> : Path<'', {}> : Path<'', {}> : Path<'', {}>;
132
+ } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends PathParamsWithParamNameExtracted<`${TParentPath}${TChildPath}`> ? Path<`${TParentPath}${TChildPath}`, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : Path<'', {}> : Path<'', {}> : Path<'', {}>;
157
133
 
158
- declare type CombineQuery<TParent extends Query | undefined, TChild extends Query | undefined> = ToQuery<TParent> extends {
134
+ declare type CombineQuery<TParent extends Query, TChild extends Query> = ToQuery<TParent> extends {
159
135
  query: infer TParentQuery extends string;
160
136
  params: infer TParentParams extends Record<string, unknown>;
161
137
  } ? ToQuery<TChild> extends {
162
138
  query: infer TChildQuery extends string;
163
139
  params: infer TChildParams extends Record<string, unknown>;
164
- } ? MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>> extends QueryParamsWithParamNameExtracted<CombineQueryString<TParentQuery, TChildQuery>> ? Query<CombineQueryString<TParentQuery, TChildQuery>, MergeParams<RemoveLeadingQuestionMarkFromKeys<TParentParams>, RemoveLeadingQuestionMarkFromKeys<TChildParams>>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
140
+ } ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends QueryParamsWithParamNameExtracted<CombineQueryString<TParentQuery, TChildQuery>> ? Query<CombineQueryString<TParentQuery, TChildQuery>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
165
141
 
166
142
  declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
167
143
 
@@ -174,22 +150,26 @@ declare type CombineQueryString<TParent extends string | undefined, TChild exten
174
150
  *
175
151
  * @example
176
152
  * ```ts
177
- * import { createRoutes, component } from '@kitbag/router'
153
+ * import { createRoute, component } from '@kitbag/router'
178
154
  *
179
- * export const routes = createRoutes([
180
- * {
181
- * name: 'User',
182
- * path: '/',
183
- * component: component(User, () => ({ userId: 1 }))
184
- * },
185
- * ])
155
+ * export const routes = createRoute({
156
+ * name: 'User',
157
+ * path: '/',
158
+ * component: component(User, () => ({ userId: 1 }))
159
+ * })
186
160
  * ```
187
161
  */
188
- export declare function component<TComponent extends Component>(component: TComponent, props: PropsGetter<TComponent>): Component;
162
+ export declare function component<TComponent extends Component>(component: TComponent, props: ComponentPropsGetter<TComponent>): Component;
163
+
164
+ export declare type ComponentProps<TComponent extends Component> = TComponent extends Constructor ? InstanceType<TComponent>['$props'] : TComponent extends AsyncComponentLoader<infer T extends Component> ? ComponentProps<T> : TComponent extends FunctionalComponent<infer T> ? T : never;
165
+
166
+ declare type ComponentPropsGetter<TComponent extends Component> = () => MaybePromise<ComponentProps<TComponent>>;
189
167
 
190
168
  declare type Constructor = new (...args: any) => any;
191
169
 
192
- export declare function createExternalRoutes<const TRoutes extends ExternalRouteProps[]>(routes: TRoutes): FlattenRoutes<TRoutes>;
170
+ export declare function createExternalRoute<const THost extends string | Host, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptions<TName, TPath, TQuery> & WithHost<THost> & WithoutParent): Route<ToKey<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>>;
171
+
172
+ export declare function createExternalRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptions<TName, TPath, TQuery> & WithoutHost & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
193
173
 
194
174
  export declare function createParam<TParam extends ParamWithDefault>(param: TParam): TParam;
195
175
 
@@ -197,25 +177,63 @@ export declare function createParam<TParam extends Param>(param: TParam): ParamG
197
177
 
198
178
  export declare function createParam<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
199
179
 
180
+ export declare function createRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
181
+
182
+ export declare function createRoute<const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithoutComponents & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
183
+
184
+ export declare function createRoute<TComponent extends Component, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery>> & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
185
+
186
+ export declare function createRoute<TComponent extends Component, const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponent<TComponent, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
187
+
188
+ export declare function createRoute<TComponents extends Record<string, Component>, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery>> & WithoutParent): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
189
+
190
+ export declare function createRoute<TComponents extends Record<string, Component>, const TParent extends Route, const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const TMeta extends RouteMeta = RouteMeta>(options: CreateRouteOptions<TName, TPath, TQuery, TMeta> & WithHooks & WithComponents<TComponents, RouteParams<TPath, TQuery, TParent>> & WithParent<TParent>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
191
+
192
+ export declare type CreateRouteOptions<TName extends string | undefined = string | undefined, TPath extends string | Path | undefined = string | Path | undefined, TQuery extends string | Query | undefined = string | Query | undefined, TMeta extends RouteMeta = RouteMeta> = {
193
+ /**
194
+ * Name for route, used to create route keys and in navigation.
195
+ */
196
+ name?: TName;
197
+ /**
198
+ * Path part of URL.
199
+ */
200
+ path?: TPath;
201
+ /**
202
+ * Query (aka search) part of URL.
203
+ */
204
+ query?: TQuery;
205
+ /**
206
+ * Represents additional metadata associated with a route, customizable via declaration merging.
207
+ */
208
+ meta?: TMeta;
209
+ };
210
+
211
+ /**
212
+ * The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
213
+ */
214
+ export declare type CreateRouteOptionsMatched = CreateRouteOptions & WithHooks & (WithHost | WithoutHost) & (WithComponent | WithComponents | WithoutComponents) & (WithParent | WithoutParent) & {
215
+ meta: RouteMeta;
216
+ };
217
+
200
218
  /**
201
219
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
202
220
  *
203
221
  * @param routes - {@link Routes} An array of route definitions specifying the configuration of routes in the application.
204
- * Use createRoutes method to create the route definitions.
222
+ * Use createRoute method to create the route definitions.
205
223
  * @param options - {@link RouterOptions} for the router, including history mode and initial URL settings.
206
224
  * @returns Router instance
207
225
  *
208
226
  * @example
209
227
  * ```ts
210
- * import { createRoutes, createRouter } from '@kitbag/router'
228
+ * import { createRoute, createRouter } from '@kitbag/router'
211
229
  *
212
230
  * const Home = { template: '<div>Home</div>' }
213
231
  * const About = { template: '<div>About</div>' }
214
232
  *
215
- * export const routes = createRoutes([
216
- * { name: 'home', path: '/', component: Home },
217
- * { name: 'path', path: '/about', component: About },
218
- * ])
233
+ * export const routes = [
234
+ * createRoute({ name: 'home', path: '/', component: Home }),
235
+ * createRoute({ name: 'path', path: '/about', component: About }),
236
+ * ]
219
237
  *
220
238
  * const router = createRouter(routes)
221
239
  * ```
@@ -224,15 +242,6 @@ export declare function createRouter<const T extends Routes>(routes: T, options?
224
242
 
225
243
  export declare function createRouter<const T extends Routes>(arrayOfRoutes: T[], options?: RouterOptions): Router<T>;
226
244
 
227
- /**
228
- * Creates an array of routes from a defined set of route properties, handling hierarchical route combinations.
229
- * This function also validates for duplicate parameter keys across the combined routes.
230
- *
231
- * @param routesProps - An array of route properties used to configure and create routes.
232
- * @returns An array of fully configured Route instances.
233
- */
234
- export declare function createRoutes<const TRoutes extends RouteProps[]>(routes: TRoutes): FlattenRoutes<TRoutes>;
235
-
236
245
  /**
237
246
  * An error thrown when duplicate parameters are detected in a route when creating a router.
238
247
  * When defining routes, param names must be unique. This includes params defined in a path
@@ -246,78 +255,20 @@ export declare class DuplicateParamsError extends Error {
246
255
  constructor(paramName: string);
247
256
  }
248
257
 
249
- declare type ExternalChildRoutes = Route<string, never>[];
250
-
251
- declare type ExternalRouteChildProps = {
252
- /**
253
- * Represents the host for this route. Used for external routes.
254
- */
255
- host?: string | Host;
256
- /**
257
- * Name for route, used to create route keys and in navigation.
258
- */
259
- name: string;
260
- /**
261
- * Children routes, expected type comes from `createRoutes()`
262
- */
263
- disabled?: boolean;
264
- /**
265
- * Path part of URL.
266
- */
267
- path?: string | Path;
268
- /**
269
- * Query (aka search) part of URL.
270
- */
271
- query?: string | Query;
272
- /**
273
- * Children routes, cannot have a value for a child route.
274
- */
275
- children?: never;
276
- };
277
-
278
- declare type ExternalRouteParentProps = {
279
- /**
280
- * Represents the host for this route. Used for external routes.
281
- */
282
- host?: string | Host;
283
- /**
284
- * Name for route, used to create route keys and in navigation.
285
- */
286
- name?: string;
287
- /**
288
- * Disabled routes will not ever match but can still provide physical structure, nested children behave normally.
289
- */
290
- disabled?: boolean;
291
- /**
292
- * Path part of URL.
293
- */
294
- path?: string | Path;
295
- /**
296
- * Query (aka search) part of URL.
297
- */
298
- query?: string | Query;
299
- /**
300
- * Children routes, expected type comes from `createExternalRoutes()`
301
- */
302
- children: ExternalChildRoutes;
303
- };
304
-
305
- declare type ExternalRouteProps = ExternalRouteParentProps | ExternalRouteChildProps;
306
-
307
258
  /**
308
259
  * Extracts the parameter name from a string, handling optional parameters denoted by a leading '?'.
309
260
  * @template TParam - The string from which to extract the parameter name.
310
261
  * @returns The extracted parameter name, or never if the parameter string is empty.
311
262
  */
312
- export declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
263
+ declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
313
264
 
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<{
265
+ declare type ExtractParamsFromHostString<THost extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = THost extends `${string}${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? {
315
266
  [P in Param]: ExtractPathParamType<Param, TParams>;
316
- }, ExtractParamsFromHostString<Rest, TParams>> : Record<never, never>;
267
+ } & ExtractParamsFromHostString<Rest, TParams> : Record<never, never>;
317
268
 
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<{
269
+ declare type ExtractParamsFromPathString<TPath extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TPath extends `${string}${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? {
319
270
  [P in Param]: ExtractPathParamType<Param, TParams>;
320
- }, ExtractParamsFromPathString<Rest, TParams>> : Record<never, never>;
271
+ } & ExtractParamsFromPathString<Rest, TParams> : Record<never, never>;
321
272
 
322
273
  /**
323
274
  * Extracts the actual type from a parameter type, handling getters, setters, and potential undefined values.
@@ -325,14 +276,14 @@ declare type ExtractParamsFromPathString<TPath extends string, TParams extends R
325
276
  * @template TParam - The parameter type.
326
277
  * @returns The extracted type, or 'string' as a fallback.
327
278
  */
328
- export declare type ExtractParamType<TParam extends Param, TParamKey extends PropertyKey = string> = TParam extends ParamGetSet<infer Type> ? TParamKey extends `?${string}` ? TParam extends ParamWithDefault ? Type : Type | undefined : Type : TParam extends ParamGetter ? TParamKey extends `?${string}` ? ReturnType<TParam> | undefined : ReturnType<TParam> : TParamKey extends `?${string}` ? string | undefined : string;
279
+ declare type ExtractParamType<TParam extends Param, TParamKey extends PropertyKey = string> = TParam extends ParamGetSet<infer Type> ? TParamKey extends `?${string}` ? TParam extends ParamWithDefault ? Type : Type | undefined : Type : TParam extends ParamGetter ? TParamKey extends `?${string}` ? ReturnType<TParam> | undefined : ReturnType<TParam> : TParamKey extends `?${string}` ? string | undefined : string;
329
280
 
330
281
  /**
331
282
  * Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
332
283
  * @template TParams - The record of parameter types, possibly including undefined.
333
284
  * @returns A new type with the appropriate properties marked as optional.
334
285
  */
335
- export declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
286
+ declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
336
287
  [K in keyof TParams as ExtractParamName<K>]: ExtractParamType<TParams[K], K>;
337
288
  }>>;
338
289
 
@@ -348,13 +299,11 @@ declare type ExtractParamTypeWithoutLosingOptional<TParam extends Param, TParamK
348
299
  * @template TParams - The record object mapping parameter names to their types.
349
300
  * @returns The type associated with the parameter, or StringConstructor if unspecified; may be undefined for optional parameters.
350
301
  */
351
- export declare type ExtractPathParamType<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;
302
+ declare type ExtractPathParamType<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;
352
303
 
353
- declare type ExtractQueryParamsFromQueryString<TQuery extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TQuery extends `${string}=${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? MergeParams<{
304
+ declare type ExtractQueryParamsFromQueryString<TQuery extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TQuery extends `${string}=${ParamStart}${infer Param}${ParamEnd}${infer Rest}` ? {
354
305
  [P in Param]: ExtractPathParamType<Param, TParams>;
355
- }, ExtractQueryParamsFromQueryString<Rest, TParams>> : Record<never, never>;
356
-
357
- declare type ExtractRouteChildren<TRoute extends RouteProps | ExternalRouteProps> = TRoute extends ParentRouteProps ? TRoute['children'] extends Route[] ? TRoute['children'] : [] : [];
306
+ } & ExtractQueryParamsFromQueryString<Rest, TParams> : Record<never, never>;
358
307
 
359
308
  /**
360
309
  * Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
@@ -362,14 +311,17 @@ declare type ExtractRouteChildren<TRoute extends RouteProps | ExternalRouteProps
362
311
  * @template TRoute - The route type from which to extract and merge parameter types.
363
312
  * @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
364
313
  */
365
- export declare type ExtractRouteParamTypes<TRoute> = TRoute extends {
314
+ declare type ExtractRouteParamTypes<TRoute> = TRoute extends {
366
315
  path: {
367
316
  params: infer PathParams extends Record<string, Param>;
368
317
  };
369
318
  query: {
370
319
  params: infer QueryParams extends Record<string, Param>;
371
320
  };
372
- } ? ExtractParamTypes<MergeParams<PathParams, QueryParams>> : {};
321
+ host: {
322
+ params: infer HostParams extends Record<string, Param>;
323
+ };
324
+ } ? ExtractParamTypes<HostParams & PathParams & QueryParams> : {};
373
325
 
374
326
  declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extends {
375
327
  host: {
@@ -381,26 +333,7 @@ declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extend
381
333
  query: {
382
334
  params: infer QueryParams extends Record<string, Param>;
383
335
  };
384
- } ? ExtractParamTypesWithoutLosingOptional<MergeParams<HostParams, MergeParams<PathParams, QueryParams>>> : Record<string, unknown>;
385
-
386
- declare type Flatten<T extends any[]> = T extends [infer First, ...infer Rest] ? First extends unknown[] ? Flatten<[...First, ...Flatten<Rest>]> : [First, ...Flatten<Rest>] : [];
387
-
388
- declare type FlattenRoute<TRoute extends RouteProps | ExternalRouteProps, TKey extends string = TRoute extends {
389
- name: infer T extends string;
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>> = [
393
- Route<TKey, THost, TPath, TQuery, TDisabled>,
394
- ...{
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']>;
396
- }
397
- ];
398
-
399
- declare type FlattenRoutes<TRoutes extends Readonly<RouteProps[] | ExternalRouteProps[]>> = Flatten<[
400
- ...{
401
- [K in keyof TRoutes]: FlattenRoute<TRoutes[K]>;
402
- }
403
- ]>;
336
+ } ? ExtractParamTypesWithoutLosingOptional<HostParams & PathParams & QueryParams> : Record<string, unknown>;
404
337
 
405
338
  declare type Host<THost extends string = string, TParams extends HostParamsWithParamNameExtracted<THost> = Record<string, Param | undefined>> = {
406
339
  host: THost;
@@ -418,31 +351,8 @@ declare type Identity<T> = T extends object ? {} & {
418
351
 
419
352
  declare type IsEmptyObject<T> = T extends Record<string, never> ? (keyof T extends never ? true : false) : false;
420
353
 
421
- /**
422
- * Type guard to check if a value conforms to the ParamGetSet type.
423
- * @param value - The value to check.
424
- * @returns True if the value is an object with both 'get' and 'set' functions defined.
425
- */
426
- export declare function isParamGetSet(value: Param): value is ParamGetSet;
427
-
428
- /**
429
- * Type guard to check if a value conforms to the ParamGetter type.
430
- * @param value - The value to check.
431
- * @returns True if the value is a function that is not a constructor.
432
- */
433
- export declare function isParamGetter(value: Param): value is ParamGetter;
434
-
435
354
  export declare function isParamWithDefault(param: Param): param is ParamWithDefault;
436
355
 
437
- /**
438
- * Type guard function to determine if a given route configuration is a parent route, based on the presence of children.
439
- * @param value - The route configuration to check.
440
- * @returns True if the route configuration has children, indicating it is a parent route.
441
- */
442
- export declare function isParentRoute(value: RouteProps): value is ParentRouteProps;
443
-
444
- export declare function isParentRouteWithoutComponent(value: RouteProps): value is Omit<ParentRouteProps, 'component' | 'components'>;
445
-
446
356
  export declare function isRoute(route: unknown): route is RouterRoute;
447
357
 
448
358
  export declare function isRoute<TRoute extends RouterRoute, TRouteKey extends TRoute['key']>(route: TRoute, routeKey: TRouteKey, options: IsRouteOptions & {
@@ -467,24 +377,12 @@ export declare function isRoute<TRouteKey extends RegisteredRoutesKey>(route: un
467
377
 
468
378
  export declare function isRoute(route: unknown, routeKey?: string, options?: IsRouteOptions): boolean;
469
379
 
470
- declare type IsRouteDisabled<T extends Route> = T extends {
471
- disabled: true;
472
- } ? true : false;
473
-
474
380
  declare type IsRouteOptions = {
475
381
  exact?: boolean;
476
382
  };
477
383
 
478
384
  declare type IsRouteUnnamed<T extends Route> = StringHasValue<T['key']> extends true ? false : true;
479
385
 
480
- export declare function isRouteWithComponent(value: RouteProps): value is RouteProps & WithComponent;
481
-
482
- export declare function isRouteWithComponent(value: Readonly<RouteProps>): value is Readonly<RouteProps & WithComponent>;
483
-
484
- export declare function isRouteWithComponents(value: RouteProps): value is RouteProps & WithComponents;
485
-
486
- export declare function isRouteWithComponents(value: Readonly<RouteProps>): value is Readonly<RouteProps & WithComponents>;
487
-
488
386
  declare type MakeOptional<T> = {
489
387
  [P in WithOptionalProperties<T>]?: T[P];
490
388
  } & {
@@ -495,16 +393,6 @@ declare type MaybeArray<T> = T | T[];
495
393
 
496
394
  declare type MaybePromise<T> = T | Promise<T>;
497
395
 
498
- /**
499
- * Merges two parameter type records, ensuring no overlap in properties.
500
- * @template TAlpha - The first record type.
501
- * @template TBeta - The second record type.
502
- * @returns A new record type combining properties from both inputs without overlaps.
503
- */
504
- export declare type MergeParams<TAlpha extends Record<string, unknown>, TBeta extends Record<string, unknown>> = {
505
- [K in keyof TAlpha | keyof TBeta]: K extends keyof TAlpha & keyof TBeta ? never : K extends keyof TAlpha ? TAlpha[K] : K extends keyof TBeta ? TBeta[K] : never;
506
- };
507
-
508
396
  /**
509
397
  * Registers a hook that is called after a route has been entered. Must be called during setup.
510
398
  * This allows performing actions right after the component becomes active, such as fetching data or setting up event listeners.
@@ -556,9 +444,9 @@ declare type OnlyRequiredProperties<T> = {
556
444
 
557
445
  export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor | DateConstructor | JSON;
558
446
 
559
- export declare type ParamEnd = typeof paramEnd;
447
+ declare type ParamEnd = typeof paramEnd;
560
448
 
561
- export declare const paramEnd = "]";
449
+ declare const paramEnd = "]";
562
450
 
563
451
  export declare type ParamExtras = {
564
452
  invalid: (message?: string) => never;
@@ -574,41 +462,15 @@ export declare type ParamGetter<T = any> = (value: string, extras: ParamExtras)
574
462
 
575
463
  export declare type ParamSetter<T = any> = (value: T, extras: ParamExtras) => string;
576
464
 
577
- export declare type ParamStart = typeof paramStart;
465
+ declare type ParamStart = typeof paramStart;
578
466
 
579
- export declare const paramStart = "[";
467
+ declare const paramStart = "[";
580
468
 
581
469
  export declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
582
470
 
583
- /**
584
- * Represents properties common to parent routes in a route configuration, including hooks, path, and optional query parameters.
585
- */
586
- export declare type ParentRouteProps = Partial<WithComponent | WithComponents> & WithHooks & {
587
- /**
588
- * Name for route, used to create route keys and in navigation.
589
- */
590
- name?: string;
591
- /**
592
- * Path part of URL.
593
- */
594
- path?: string | Path;
595
- /**
596
- * Query (aka search) part of URL.
597
- */
598
- query?: string | Query;
599
- /**
600
- * Disabled routes will not ever match but can still provide physical structure, nested children behave normally.
601
- */
602
- disabled?: boolean;
603
- /**
604
- * Children routes, expected type comes from `createRoutes()`
605
- */
606
- children: Routes;
607
- /**
608
- * Represents additional metadata associated with a route, customizable via declaration merging.
609
- */
610
- meta?: RouteMeta;
611
- };
471
+ declare type ParentPath<TParent extends Route | undefined> = TParent extends Route ? TParent['path'] : Path<'', {}>;
472
+
473
+ declare type ParentQuery<TParent extends Route | undefined> = TParent extends Route ? TParent['query'] : Query<'', {}>;
612
474
 
613
475
  declare type Path<TPath extends string = string, TParams extends PathParamsWithParamNameExtracted<TPath> = Record<string, Param | undefined>> = {
614
476
  path: TPath;
@@ -628,15 +490,13 @@ declare type Path<TPath extends string = string, TParams extends PathParamsWithP
628
490
  *
629
491
  * @example
630
492
  * ```ts
631
- * import { createRoutes, path } from '@kitbag/router'
493
+ * import { createRoute, path } from '@kitbag/router'
632
494
  *
633
- * export const routes = createRoutes([
634
- * {
635
- * name: 'home',
636
- * path: path('/[foo]', { foo: Number }),
637
- * component: Home
638
- * },
639
- * ])
495
+ * export const routes = createRoute({
496
+ * name: 'home',
497
+ * path: path('/[foo]', { foo: Number }),
498
+ * component: Home
499
+ * })
640
500
  * ```
641
501
  */
642
502
  export declare function path<TPath extends string, TParams extends PathParamsWithParamNameExtracted<TPath>>(path: TPath, params: Identity<TParams>): Path<TPath, TParams>;
@@ -645,10 +505,6 @@ declare type PathParamsWithParamNameExtracted<TPath extends string> = {
645
505
  [K in keyof ExtractParamsFromPathString<TPath> as ExtractParamName<K>]?: Param;
646
506
  };
647
507
 
648
- declare type Props<TComponent extends Component> = TComponent extends Constructor ? InstanceType<TComponent>['$props'] : TComponent extends AsyncComponentLoader<infer T extends Component> ? Props<T> : TComponent extends FunctionalComponent<infer T> ? T : never;
649
-
650
- declare type PropsGetter<TComponent extends Component> = () => MaybePromise<Props<TComponent>>;
651
-
652
508
  declare type Query<TQuery extends string = string, TQueryParams extends QueryParamsWithParamNameExtracted<TQuery> = Record<string, Param | undefined>> = {
653
509
  query: TQuery;
654
510
  params: string extends TQuery ? Record<string, Param> : Identity<ExtractQueryParamsFromQueryString<TQuery, TQueryParams>>;
@@ -667,15 +523,13 @@ declare type Query<TQuery extends string = string, TQueryParams extends QueryPar
667
523
  *
668
524
  * @example
669
525
  * ```ts
670
- * import { createRoutes, query } from '@kitbag/router'
526
+ * import { createRoute, query } from '@kitbag/router'
671
527
  *
672
- * export const routes = createRoutes([
673
- * {
674
- * name: 'home',
675
- * query: query('bar=[bar]', { bar: Boolean }),
676
- * component: Home
677
- * },
678
- * ])
528
+ * export const routes = createRoute({
529
+ * name: 'home',
530
+ * query: query('bar=[bar]', { bar: Boolean }),
531
+ * component: Home
532
+ * })
679
533
  * ```
680
534
  */
681
535
  export declare function query<TQuery extends string, TParams extends QueryParamsWithParamNameExtracted<TQuery>>(query: TQuery, params: Identity<TParams>): Query<TQuery, TParams>;
@@ -685,7 +539,7 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
685
539
  };
686
540
 
687
541
  /**
688
- * Represents the state of currently registered router, and rejections. Used to provide correct type context for
542
+ * Represents the state of currently registered router, rejections, and route meta. Used to provide correct type context for
689
543
  * components like `RouterLink`, as well as for composables like `useRouter`, `useRoute`, and hooks.
690
544
  *
691
545
  * @example
@@ -693,6 +547,8 @@ declare type QueryParamsWithParamNameExtracted<T extends string> = {
693
547
  * declare module '@kitbag/router' {
694
548
  * interface Register {
695
549
  * router: typeof router
550
+ * rejections: ["NotAuthorized"],
551
+ * routeMeta: { public?: boolean }
696
552
  * }
697
553
  * }
698
554
  * ```
@@ -734,19 +590,12 @@ export declare type RegisteredRouterReplace = RouterReplace<RegisteredRoutes>;
734
590
  */
735
591
  export declare type RegisteredRouterRoute = RegisteredRouter['route'];
736
592
 
737
- /**
738
- * Represents the State property registered within {@link Register}
739
- */
740
- export declare type RegisteredRouterState = Register extends {
741
- state: infer TState;
742
- } ? TState : {};
743
-
744
593
  /**
745
594
  * Represents the Router routes property within {@link Register}
746
595
  */
747
596
  export declare type RegisteredRoutes = Register extends {
748
597
  router: Router<infer TRoutes extends Routes>;
749
- } ? TRoutes : Route<string, Host, Path, Query, false>[];
598
+ } ? TRoutes : Route[];
750
599
 
751
600
  /**
752
601
  * Represents the union of all possible RouteKeys registered within {@link Register}
@@ -793,22 +642,23 @@ declare type ResolvedRouteQuery = {
793
642
  };
794
643
 
795
644
  /**
796
- * Represents the structure of a route within the application. Return value of `createRoutes`
645
+ * Represents the structure of a route within the application. Return value of `createRoute`
797
646
  * @template TKey - Represents the unique key identifying the route, typically a string.
798
647
  * @template TPath - The type or structure of the route's path.
799
648
  * @template TQuery - The type or structure of the query parameters associated with the route.
800
- * @template TDisabled - Indicates whether the route is disabled, which could affect routing logic.
801
649
  */
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> = {
650
+ export declare type Route<TKey extends string = string, THost extends Host = Host, TPath extends Path = Path, TQuery extends Query = Query, TMeta extends RouteMeta = RouteMeta> = {
803
651
  /**
804
652
  * The specific route properties that were matched in the current route.
805
653
  */
806
- matched: RoutePropsWithMeta;
654
+ matched: CreateRouteOptionsMatched & {
655
+ meta: TMeta;
656
+ };
807
657
  /**
808
658
  * The specific route properties that were matched in the current route, including any ancestors.
809
659
  * Order of routes will be from greatest ancestor to narrowest matched.
810
660
  */
811
- matches: RoutePropsWithMeta[];
661
+ matches: CreateRouteOptionsMatched[];
812
662
  /**
813
663
  * Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
814
664
  */
@@ -826,10 +676,6 @@ export declare type Route<TKey extends string = string, THost extends Host = Hos
826
676
  */
827
677
  query: TQuery;
828
678
  depth: number;
829
- /**
830
- * Indicates if the route is disabled.
831
- */
832
- disabled: TDisabled;
833
679
  };
834
680
 
835
681
  declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesKey<TRoutes>> = RoutesMap<TRoutes>[TKey];
@@ -901,35 +747,16 @@ declare type RouteHookSuccessResponse = {
901
747
  status: 'SUCCESS';
902
748
  };
903
749
 
904
- declare type RouteIsNamedAndNotDisabled<T extends Route> = IsRouteDisabled<T> extends true ? never : IsRouteUnnamed<T> extends true ? never : T;
905
-
906
750
  /**
907
751
  * Represents additional metadata associated with a route, customizable via declaration merging.
908
- * @example
909
- * ```ts
910
- * declare module '@kitbag/router' {
911
- * interface RouteMeta {
912
- * pageTitle?: string
913
- * }
914
- * }
915
- * ```
916
752
  */
917
- export declare interface RouteMeta extends Record<string, unknown> {
918
- }
753
+ export declare type RouteMeta = Register extends {
754
+ routeMeta: infer RouteMeta extends Record<string, unknown>;
755
+ } ? RouteMeta : Record<string, unknown>;
919
756
 
920
- declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
757
+ declare type RouteParams<TPath extends string | Path | undefined, TQuery extends string | Query | undefined, TParent extends Route | undefined = undefined> = ExtractParamTypes<Identity<CombinePath<ParentPath<TParent>, ToPath<TPath>>['params'] & CombineQuery<ParentQuery<TParent>, ToQuery<TQuery>>['params']>>;
921
758
 
922
- /**
923
- * Unifies the properties of both parent and child routes, ensuring type safety and consistency across route configurations.
924
- */
925
- export declare type RouteProps = Readonly<ParentRouteProps | ChildRouteProps>;
926
-
927
- /**
928
- * The Route properties originally provided to `createRoutes`. The only change is normalizing meta to always default to an empty object.
929
- */
930
- export declare type RoutePropsWithMeta = RouteProps & {
931
- meta: RouteMeta;
932
- };
759
+ declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
933
760
 
934
761
  export declare type Router<TRoutes extends Routes = any> = Plugin_2 & {
935
762
  /**
@@ -1131,13 +958,21 @@ name?: string | undefined;
1131
958
  default?: ((props: {
1132
959
  route: Readonly<{
1133
960
  key: string;
1134
- matched: RoutePropsWithMeta;
1135
- matches: RoutePropsWithMeta[];
961
+ matched: CreateRouteOptionsMatched & {
962
+ meta: Record<string, unknown>;
963
+ };
964
+ matches: CreateRouteOptionsMatched[];
1136
965
  query: ResolvedRouteQuery;
1137
- params: Writable< {}>;
966
+ params: Writable< {
967
+ [x: string]: any;
968
+ [x: number]: any;
969
+ }>;
1138
970
  update: {
1139
971
  (key: string, value: unknown, options?: RouterPushOptions | undefined): Promise<void>;
1140
- (params: Partial<{}>, options?: RouterPushOptions | undefined): Promise<void>;
972
+ (params: Partial<{
973
+ [x: string]: any;
974
+ [x: number]: any;
975
+ }>, options?: RouterPushOptions | undefined): Promise<void>;
1141
976
  };
1142
977
  }>;
1143
978
  component: Component;
@@ -1152,13 +987,21 @@ name?: string | undefined;
1152
987
  default?: ((props: {
1153
988
  route: Readonly<{
1154
989
  key: string;
1155
- matched: RoutePropsWithMeta;
1156
- matches: RoutePropsWithMeta[];
990
+ matched: CreateRouteOptionsMatched & {
991
+ meta: Record<string, unknown>;
992
+ };
993
+ matches: CreateRouteOptionsMatched[];
1157
994
  query: ResolvedRouteQuery;
1158
- params: Writable< {}>;
995
+ params: Writable< {
996
+ [x: string]: any;
997
+ [x: number]: any;
998
+ }>;
1159
999
  update: {
1160
1000
  (key: string, value: unknown, options?: RouterPushOptions | undefined): Promise<void>;
1161
- (params: Partial<{}>, options?: RouterPushOptions | undefined): Promise<void>;
1001
+ (params: Partial<{
1002
+ [x: string]: any;
1003
+ [x: number]: any;
1004
+ }>, options?: RouterPushOptions | undefined): Promise<void>;
1162
1005
  };
1163
1006
  }>;
1164
1007
  component: Component;
@@ -1172,14 +1015,14 @@ name?: string | undefined;
1172
1015
  }>;
1173
1016
 
1174
1017
  /**
1175
- * Represents an immutable array of Route instances. Return value of `createRoutes`, expected param for `createRouter`.
1018
+ * Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
1176
1019
  */
1177
1020
  export declare type Routes = Readonly<Route[]>;
1178
1021
 
1179
1022
  declare type RoutesKey<TRoutes extends Routes> = string & keyof RoutesMap<TRoutes>;
1180
1023
 
1181
1024
  declare type RoutesMap<TRoutes extends Routes = []> = {
1182
- [K in TRoutes[number] as RouteIsNamedAndNotDisabled<K>['key']]: RouteIsNamedAndNotDisabled<K>;
1025
+ [K in TRoutes[number] as AsNamedRoute<K>['key']]: AsNamedRoute<K>;
1183
1026
  };
1184
1027
 
1185
1028
  declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = ResolvedRoute extends TRoute ? {
@@ -1192,11 +1035,11 @@ declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = Resolve
1192
1035
 
1193
1036
  declare type StringHasValue<T> = string extends T ? true : '' extends T ? false : T extends string ? true : false;
1194
1037
 
1195
- export declare function throwIfDuplicateParamsAreFound(routes: Route[]): void;
1196
-
1197
1038
  declare type ToCallback = (resolve: RegisteredRouter['resolve']) => string;
1198
1039
 
1199
- declare type ToHost<T extends string | Host> = T extends string ? Host<T, {}> : T;
1040
+ declare type ToHost<T extends string | Host | undefined> = T extends string ? Host<T, {}> : T extends undefined ? Host<'', {}> : unknown extends T ? Host<'', {}> : T;
1041
+
1042
+ declare type ToKey<T extends string | undefined> = T extends string ? T : '';
1200
1043
 
1201
1044
  declare type ToPath<T extends string | Path | undefined> = T extends string ? Path<T, {}> : T extends undefined ? Path<'', {}> : unknown extends T ? Path<'', {}> : T;
1202
1045
 
@@ -1306,18 +1149,22 @@ export declare class UseRouteInvalidError extends Error {
1306
1149
  */
1307
1150
  export declare function useRouter(): RegisteredRouter;
1308
1151
 
1309
- declare type WithComponent = {
1152
+ declare type WithComponent<TComponent extends Component = Component, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1310
1153
  /**
1311
1154
  * A Vue component, which can be either synchronous or asynchronous components.
1312
1155
  */
1313
- component: Component;
1156
+ component: TComponent;
1157
+ props?: (params: TParams) => TComponent extends Component ? MaybePromise<ComponentProps<TComponent>> : {};
1314
1158
  };
1315
1159
 
1316
- declare type WithComponents = {
1160
+ declare type WithComponents<TComponents extends Record<string, Component> = Record<string, Component>, TParams extends Record<string, unknown> = Record<string, unknown>> = {
1317
1161
  /**
1318
1162
  * Multiple components for named views, which can be either synchronous or asynchronous components.
1319
1163
  */
1320
- components: Record<string, Component>;
1164
+ components: TComponents;
1165
+ props?: {
1166
+ [TKey in keyof TComponents]?: (params: TParams) => TComponents[TKey] extends Component ? MaybePromise<ComponentProps<TComponents[TKey]>> : {};
1167
+ };
1321
1168
  };
1322
1169
 
1323
1170
  export declare function withDefault<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
@@ -1334,10 +1181,35 @@ declare type WithHooks = {
1334
1181
  onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
1335
1182
  };
1336
1183
 
1184
+ declare type WithHost<THost extends string | Host = string | Host> = {
1185
+ /**
1186
+ * Host part of URL.
1187
+ */
1188
+ host: THost;
1189
+ };
1190
+
1337
1191
  declare type WithOptionalProperties<T> = {
1338
1192
  [P in keyof T]-?: undefined extends T[P] ? P : never;
1339
1193
  }[keyof T];
1340
1194
 
1195
+ declare type WithoutComponents = {
1196
+ component?: never;
1197
+ components?: never;
1198
+ props?: never;
1199
+ };
1200
+
1201
+ declare type WithoutHost = {
1202
+ host?: never;
1203
+ };
1204
+
1205
+ declare type WithoutParent = {
1206
+ parent?: never;
1207
+ };
1208
+
1209
+ declare type WithParent<TParent extends Route = Route> = {
1210
+ parent: TParent;
1211
+ };
1212
+
1341
1213
  declare type Writable<T> = {
1342
1214
  -readonly [P in keyof T]: T[P];
1343
1215
  };