@kitbag/router 0.5.4 → 0.6.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.
- package/README.md +27 -27
- package/dist/kitbag-router.d.ts +130 -271
- package/dist/kitbag-router.js +722 -752
- package/dist/kitbag-router.umd.cjs +1 -1
- package/package.json +1 -1
package/dist/kitbag-router.d.ts
CHANGED
|
@@ -119,33 +119,7 @@ declare const builtInRejections: ['NotFound'];
|
|
|
119
119
|
|
|
120
120
|
declare type BuiltInRejectionType = typeof builtInRejections[number];
|
|
121
121
|
|
|
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 : '';
|
|
122
|
+
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
123
|
|
|
150
124
|
declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TParent> extends {
|
|
151
125
|
path: infer TParentPath extends string;
|
|
@@ -153,15 +127,15 @@ declare type CombinePath<TParent extends Path, TChild extends Path> = ToPath<TPa
|
|
|
153
127
|
} ? ToPath<TChild> extends {
|
|
154
128
|
path: infer TChildPath extends string;
|
|
155
129
|
params: infer TChildParams extends Record<string, unknown>;
|
|
156
|
-
} ?
|
|
130
|
+
} ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends PathParamsWithParamNameExtracted<`${TParentPath}${TChildPath}`> ? Path<`${TParentPath}${TChildPath}`, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : Path<'', {}> : Path<'', {}> : Path<'', {}>;
|
|
157
131
|
|
|
158
|
-
declare type CombineQuery<TParent extends Query
|
|
132
|
+
declare type CombineQuery<TParent extends Query, TChild extends Query> = ToQuery<TParent> extends {
|
|
159
133
|
query: infer TParentQuery extends string;
|
|
160
134
|
params: infer TParentParams extends Record<string, unknown>;
|
|
161
135
|
} ? ToQuery<TChild> extends {
|
|
162
136
|
query: infer TChildQuery extends string;
|
|
163
137
|
params: infer TChildParams extends Record<string, unknown>;
|
|
164
|
-
} ?
|
|
138
|
+
} ? RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams> extends QueryParamsWithParamNameExtracted<CombineQueryString<TParentQuery, TChildQuery>> ? Query<CombineQueryString<TParentQuery, TChildQuery>, RemoveLeadingQuestionMarkFromKeys<TParentParams> & RemoveLeadingQuestionMarkFromKeys<TChildParams>> : Query<'', {}> : Query<'', {}> : Query<'', {}>;
|
|
165
139
|
|
|
166
140
|
declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
|
|
167
141
|
|
|
@@ -174,22 +148,22 @@ declare type CombineQueryString<TParent extends string | undefined, TChild exten
|
|
|
174
148
|
*
|
|
175
149
|
* @example
|
|
176
150
|
* ```ts
|
|
177
|
-
* import {
|
|
151
|
+
* import { createRoute, component } from '@kitbag/router'
|
|
178
152
|
*
|
|
179
|
-
* export const routes =
|
|
180
|
-
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* },
|
|
185
|
-
* ])
|
|
153
|
+
* export const routes = createRoute({
|
|
154
|
+
* name: 'User',
|
|
155
|
+
* path: '/',
|
|
156
|
+
* component: component(User, () => ({ userId: 1 }))
|
|
157
|
+
* })
|
|
186
158
|
* ```
|
|
187
159
|
*/
|
|
188
160
|
export declare function component<TComponent extends Component>(component: TComponent, props: PropsGetter<TComponent>): Component;
|
|
189
161
|
|
|
190
162
|
declare type Constructor = new (...args: any) => any;
|
|
191
163
|
|
|
192
|
-
export declare function
|
|
164
|
+
export declare function createExternalRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined, const THost extends string | Host | undefined = undefined>(options: CreateRouteOptionsWithoutParent<TName, TPath, TQuery, THost>): Route<ToKey<TName>, ToHost<THost>, ToPath<TPath>, ToQuery<TQuery>>;
|
|
165
|
+
|
|
166
|
+
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: CreateRouteOptionsWithParent<TParent, TName, TPath, TQuery, Host<'', {}>>): Route<CombineKey<TParent['key'], ToKey<TName>>, ToHost<Host<'', {}>>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
|
|
193
167
|
|
|
194
168
|
export declare function createParam<TParam extends ParamWithDefault>(param: TParam): TParam;
|
|
195
169
|
|
|
@@ -197,25 +171,68 @@ export declare function createParam<TParam extends Param>(param: TParam): ParamG
|
|
|
197
171
|
|
|
198
172
|
export declare function createParam<TParam extends Param>(param: TParam, defaultValue: ExtractParamType<TParam>): ParamWithDefault<TParam>;
|
|
199
173
|
|
|
174
|
+
export declare function createRoute<const TName extends string | undefined = undefined, const TPath extends string | Path | undefined = undefined, const TQuery extends string | Query | undefined = undefined>(options: CreateRouteOptionsWithoutParent<TName, TPath, TQuery>): Route<ToKey<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>>;
|
|
175
|
+
|
|
176
|
+
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>(options: CreateRouteOptionsWithParent<TParent, TName, TPath, TQuery>): Route<CombineKey<TParent['key'], ToKey<TName>>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>>;
|
|
177
|
+
|
|
178
|
+
export declare type CreateRouteOptions<TName extends string | undefined = string, TPath extends string | Path | undefined = string | Path | undefined, TQuery extends string | Query | undefined = string | Query | undefined, THost extends string | Host | undefined = string | Host | undefined, _TParent extends Route | undefined = undefined> = WithComponent & WithHooks & {
|
|
179
|
+
/**
|
|
180
|
+
* Name for route, used to create route keys and in navigation.
|
|
181
|
+
*/
|
|
182
|
+
name?: TName;
|
|
183
|
+
/**
|
|
184
|
+
* Host part of URL.
|
|
185
|
+
*/
|
|
186
|
+
host?: THost;
|
|
187
|
+
/**
|
|
188
|
+
* Path part of URL.
|
|
189
|
+
*/
|
|
190
|
+
path?: TPath;
|
|
191
|
+
/**
|
|
192
|
+
* Query (aka search) part of URL.
|
|
193
|
+
*/
|
|
194
|
+
query?: TQuery;
|
|
195
|
+
/**
|
|
196
|
+
* Represents additional metadata associated with a route, customizable via declaration merging.
|
|
197
|
+
*/
|
|
198
|
+
meta?: RouteMeta;
|
|
199
|
+
disabled?: boolean;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
|
|
204
|
+
*/
|
|
205
|
+
export declare type CreateRouteOptionsWithMeta = CreateRouteOptions & {
|
|
206
|
+
meta: RouteMeta;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
declare type CreateRouteOptionsWithoutParent<TName extends string | undefined = undefined, TPath extends string | Path | undefined = undefined, TQuery extends string | Query | undefined = undefined, THost extends string | Host | undefined = undefined> = CreateRouteOptions<TName, TPath, TQuery, THost> & {
|
|
210
|
+
parent?: never;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
declare type CreateRouteOptionsWithParent<TParent extends Route, TName extends string | undefined = undefined, TPath extends string | Path | undefined = undefined, TQuery extends string | Query | undefined = undefined, THost extends string | Host | undefined = undefined> = CreateRouteOptions<TName, TPath, TQuery, THost, TParent> & {
|
|
214
|
+
parent: TParent;
|
|
215
|
+
};
|
|
216
|
+
|
|
200
217
|
/**
|
|
201
218
|
* Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
|
|
202
219
|
*
|
|
203
220
|
* @param routes - {@link Routes} An array of route definitions specifying the configuration of routes in the application.
|
|
204
|
-
* Use
|
|
221
|
+
* Use createRoute method to create the route definitions.
|
|
205
222
|
* @param options - {@link RouterOptions} for the router, including history mode and initial URL settings.
|
|
206
223
|
* @returns Router instance
|
|
207
224
|
*
|
|
208
225
|
* @example
|
|
209
226
|
* ```ts
|
|
210
|
-
* import {
|
|
227
|
+
* import { createRoute, createRouter } from '@kitbag/router'
|
|
211
228
|
*
|
|
212
229
|
* const Home = { template: '<div>Home</div>' }
|
|
213
230
|
* const About = { template: '<div>About</div>' }
|
|
214
231
|
*
|
|
215
|
-
* export const routes =
|
|
216
|
-
* { name: 'home', path: '/', component: Home },
|
|
217
|
-
* { name: 'path', path: '/about', component: About },
|
|
218
|
-
* ]
|
|
232
|
+
* export const routes = [
|
|
233
|
+
* createRoute({ name: 'home', path: '/', component: Home }),
|
|
234
|
+
* createRoute({ name: 'path', path: '/about', component: About }),
|
|
235
|
+
* ]
|
|
219
236
|
*
|
|
220
237
|
* const router = createRouter(routes)
|
|
221
238
|
* ```
|
|
@@ -224,15 +241,6 @@ export declare function createRouter<const T extends Routes>(routes: T, options?
|
|
|
224
241
|
|
|
225
242
|
export declare function createRouter<const T extends Routes>(arrayOfRoutes: T[], options?: RouterOptions): Router<T>;
|
|
226
243
|
|
|
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
244
|
/**
|
|
237
245
|
* An error thrown when duplicate parameters are detected in a route when creating a router.
|
|
238
246
|
* When defining routes, param names must be unique. This includes params defined in a path
|
|
@@ -246,78 +254,20 @@ export declare class DuplicateParamsError extends Error {
|
|
|
246
254
|
constructor(paramName: string);
|
|
247
255
|
}
|
|
248
256
|
|
|
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
257
|
/**
|
|
308
258
|
* Extracts the parameter name from a string, handling optional parameters denoted by a leading '?'.
|
|
309
259
|
* @template TParam - The string from which to extract the parameter name.
|
|
310
260
|
* @returns The extracted parameter name, or never if the parameter string is empty.
|
|
311
261
|
*/
|
|
312
|
-
|
|
262
|
+
declare type ExtractParamName<TParam extends PropertyKey> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
|
|
313
263
|
|
|
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}` ?
|
|
264
|
+
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
265
|
[P in Param]: ExtractPathParamType<Param, TParams>;
|
|
316
|
-
}
|
|
266
|
+
} & ExtractParamsFromHostString<Rest, TParams> : Record<never, never>;
|
|
317
267
|
|
|
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}` ?
|
|
268
|
+
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
269
|
[P in Param]: ExtractPathParamType<Param, TParams>;
|
|
320
|
-
}
|
|
270
|
+
} & ExtractParamsFromPathString<Rest, TParams> : Record<never, never>;
|
|
321
271
|
|
|
322
272
|
/**
|
|
323
273
|
* Extracts the actual type from a parameter type, handling getters, setters, and potential undefined values.
|
|
@@ -325,14 +275,14 @@ declare type ExtractParamsFromPathString<TPath extends string, TParams extends R
|
|
|
325
275
|
* @template TParam - The parameter type.
|
|
326
276
|
* @returns The extracted type, or 'string' as a fallback.
|
|
327
277
|
*/
|
|
328
|
-
|
|
278
|
+
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
279
|
|
|
330
280
|
/**
|
|
331
281
|
* Transforms a record of parameter types into a type with optional properties where the original type allows undefined.
|
|
332
282
|
* @template TParams - The record of parameter types, possibly including undefined.
|
|
333
283
|
* @returns A new type with the appropriate properties marked as optional.
|
|
334
284
|
*/
|
|
335
|
-
|
|
285
|
+
declare type ExtractParamTypes<TParams extends Record<string, Param>> = Identity<MakeOptional<{
|
|
336
286
|
[K in keyof TParams as ExtractParamName<K>]: ExtractParamType<TParams[K], K>;
|
|
337
287
|
}>>;
|
|
338
288
|
|
|
@@ -348,13 +298,11 @@ declare type ExtractParamTypeWithoutLosingOptional<TParam extends Param, TParamK
|
|
|
348
298
|
* @template TParams - The record object mapping parameter names to their types.
|
|
349
299
|
* @returns The type associated with the parameter, or StringConstructor if unspecified; may be undefined for optional parameters.
|
|
350
300
|
*/
|
|
351
|
-
|
|
301
|
+
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
302
|
|
|
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}` ?
|
|
303
|
+
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
304
|
[P in Param]: ExtractPathParamType<Param, TParams>;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
declare type ExtractRouteChildren<TRoute extends RouteProps | ExternalRouteProps> = TRoute extends ParentRouteProps ? TRoute['children'] extends Route[] ? TRoute['children'] : [] : [];
|
|
305
|
+
} & ExtractQueryParamsFromQueryString<Rest, TParams> : Record<never, never>;
|
|
358
306
|
|
|
359
307
|
/**
|
|
360
308
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
@@ -362,14 +310,17 @@ declare type ExtractRouteChildren<TRoute extends RouteProps | ExternalRouteProps
|
|
|
362
310
|
* @template TRoute - The route type from which to extract and merge parameter types.
|
|
363
311
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
364
312
|
*/
|
|
365
|
-
|
|
313
|
+
declare type ExtractRouteParamTypes<TRoute> = TRoute extends {
|
|
366
314
|
path: {
|
|
367
315
|
params: infer PathParams extends Record<string, Param>;
|
|
368
316
|
};
|
|
369
317
|
query: {
|
|
370
318
|
params: infer QueryParams extends Record<string, Param>;
|
|
371
319
|
};
|
|
372
|
-
|
|
320
|
+
host: {
|
|
321
|
+
params: infer HostParams extends Record<string, Param>;
|
|
322
|
+
};
|
|
323
|
+
} ? ExtractParamTypes<HostParams & PathParams & QueryParams> : {};
|
|
373
324
|
|
|
374
325
|
declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extends {
|
|
375
326
|
host: {
|
|
@@ -381,26 +332,7 @@ declare type ExtractRouteParamTypesWithoutLosingOptional<TRoute> = TRoute extend
|
|
|
381
332
|
query: {
|
|
382
333
|
params: infer QueryParams extends Record<string, Param>;
|
|
383
334
|
};
|
|
384
|
-
} ? ExtractParamTypesWithoutLosingOptional<
|
|
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
|
-
]>;
|
|
335
|
+
} ? ExtractParamTypesWithoutLosingOptional<HostParams & PathParams & QueryParams> : Record<string, unknown>;
|
|
404
336
|
|
|
405
337
|
declare type Host<THost extends string = string, TParams extends HostParamsWithParamNameExtracted<THost> = Record<string, Param | undefined>> = {
|
|
406
338
|
host: THost;
|
|
@@ -418,31 +350,8 @@ declare type Identity<T> = T extends object ? {} & {
|
|
|
418
350
|
|
|
419
351
|
declare type IsEmptyObject<T> = T extends Record<string, never> ? (keyof T extends never ? true : false) : false;
|
|
420
352
|
|
|
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
353
|
export declare function isParamWithDefault(param: Param): param is ParamWithDefault;
|
|
436
354
|
|
|
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
355
|
export declare function isRoute(route: unknown): route is RouterRoute;
|
|
447
356
|
|
|
448
357
|
export declare function isRoute<TRoute extends RouterRoute, TRouteKey extends TRoute['key']>(route: TRoute, routeKey: TRouteKey, options: IsRouteOptions & {
|
|
@@ -477,14 +386,6 @@ declare type IsRouteOptions = {
|
|
|
477
386
|
|
|
478
387
|
declare type IsRouteUnnamed<T extends Route> = StringHasValue<T['key']> extends true ? false : true;
|
|
479
388
|
|
|
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
389
|
declare type MakeOptional<T> = {
|
|
489
390
|
[P in WithOptionalProperties<T>]?: T[P];
|
|
490
391
|
} & {
|
|
@@ -495,16 +396,6 @@ declare type MaybeArray<T> = T | T[];
|
|
|
495
396
|
|
|
496
397
|
declare type MaybePromise<T> = T | Promise<T>;
|
|
497
398
|
|
|
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
399
|
/**
|
|
509
400
|
* Registers a hook that is called after a route has been entered. Must be called during setup.
|
|
510
401
|
* This allows performing actions right after the component becomes active, such as fetching data or setting up event listeners.
|
|
@@ -556,9 +447,9 @@ declare type OnlyRequiredProperties<T> = {
|
|
|
556
447
|
|
|
557
448
|
export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor | DateConstructor | JSON;
|
|
558
449
|
|
|
559
|
-
|
|
450
|
+
declare type ParamEnd = typeof paramEnd;
|
|
560
451
|
|
|
561
|
-
|
|
452
|
+
declare const paramEnd = "]";
|
|
562
453
|
|
|
563
454
|
export declare type ParamExtras = {
|
|
564
455
|
invalid: (message?: string) => never;
|
|
@@ -574,42 +465,12 @@ export declare type ParamGetter<T = any> = (value: string, extras: ParamExtras)
|
|
|
574
465
|
|
|
575
466
|
export declare type ParamSetter<T = any> = (value: T, extras: ParamExtras) => string;
|
|
576
467
|
|
|
577
|
-
|
|
468
|
+
declare type ParamStart = typeof paramStart;
|
|
578
469
|
|
|
579
|
-
|
|
470
|
+
declare const paramStart = "[";
|
|
580
471
|
|
|
581
472
|
export declare type ParamWithDefault<TParam extends Param = Param> = Required<ParamGetSet<ExtractParamType<TParam>>>;
|
|
582
473
|
|
|
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
|
-
};
|
|
612
|
-
|
|
613
474
|
declare type Path<TPath extends string = string, TParams extends PathParamsWithParamNameExtracted<TPath> = Record<string, Param | undefined>> = {
|
|
614
475
|
path: TPath;
|
|
615
476
|
params: string extends TPath ? Record<string, Param> : Identity<ExtractParamsFromPathString<TPath, TParams>>;
|
|
@@ -628,15 +489,13 @@ declare type Path<TPath extends string = string, TParams extends PathParamsWithP
|
|
|
628
489
|
*
|
|
629
490
|
* @example
|
|
630
491
|
* ```ts
|
|
631
|
-
* import {
|
|
492
|
+
* import { createRoute, path } from '@kitbag/router'
|
|
632
493
|
*
|
|
633
|
-
* export const routes =
|
|
634
|
-
*
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
* },
|
|
639
|
-
* ])
|
|
494
|
+
* export const routes = createRoute({
|
|
495
|
+
* name: 'home',
|
|
496
|
+
* path: path('/[foo]', { foo: Number }),
|
|
497
|
+
* component: Home
|
|
498
|
+
* })
|
|
640
499
|
* ```
|
|
641
500
|
*/
|
|
642
501
|
export declare function path<TPath extends string, TParams extends PathParamsWithParamNameExtracted<TPath>>(path: TPath, params: Identity<TParams>): Path<TPath, TParams>;
|
|
@@ -667,15 +526,13 @@ declare type Query<TQuery extends string = string, TQueryParams extends QueryPar
|
|
|
667
526
|
*
|
|
668
527
|
* @example
|
|
669
528
|
* ```ts
|
|
670
|
-
* import {
|
|
529
|
+
* import { createRoute, query } from '@kitbag/router'
|
|
671
530
|
*
|
|
672
|
-
* export const routes =
|
|
673
|
-
*
|
|
674
|
-
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
* },
|
|
678
|
-
* ])
|
|
531
|
+
* export const routes = createRoute({
|
|
532
|
+
* name: 'home',
|
|
533
|
+
* query: query('bar=[bar]', { bar: Boolean }),
|
|
534
|
+
* component: Home
|
|
535
|
+
* })
|
|
679
536
|
* ```
|
|
680
537
|
*/
|
|
681
538
|
export declare function query<TQuery extends string, TParams extends QueryParamsWithParamNameExtracted<TQuery>>(query: TQuery, params: Identity<TParams>): Query<TQuery, TParams>;
|
|
@@ -793,7 +650,7 @@ declare type ResolvedRouteQuery = {
|
|
|
793
650
|
};
|
|
794
651
|
|
|
795
652
|
/**
|
|
796
|
-
* Represents the structure of a route within the application. Return value of `
|
|
653
|
+
* Represents the structure of a route within the application. Return value of `createRoute`
|
|
797
654
|
* @template TKey - Represents the unique key identifying the route, typically a string.
|
|
798
655
|
* @template TPath - The type or structure of the route's path.
|
|
799
656
|
* @template TQuery - The type or structure of the query parameters associated with the route.
|
|
@@ -803,12 +660,12 @@ export declare type Route<TKey extends string = string, THost extends Host = Hos
|
|
|
803
660
|
/**
|
|
804
661
|
* The specific route properties that were matched in the current route.
|
|
805
662
|
*/
|
|
806
|
-
matched:
|
|
663
|
+
matched: CreateRouteOptionsWithMeta;
|
|
807
664
|
/**
|
|
808
665
|
* The specific route properties that were matched in the current route, including any ancestors.
|
|
809
666
|
* Order of routes will be from greatest ancestor to narrowest matched.
|
|
810
667
|
*/
|
|
811
|
-
matches:
|
|
668
|
+
matches: CreateRouteOptionsWithMeta[];
|
|
812
669
|
/**
|
|
813
670
|
* Unique identifier for the route, generated by joining route `name` by period. Key is used for routing and for matching.
|
|
814
671
|
*/
|
|
@@ -919,18 +776,6 @@ export declare interface RouteMeta extends Record<string, unknown> {
|
|
|
919
776
|
|
|
920
777
|
declare type RouteParamsByKey<TRoutes extends Routes, TKey extends string> = ExtractRouteParamTypesWithoutLosingOptional<RouteGetByKey<TRoutes, TKey>>;
|
|
921
778
|
|
|
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
|
-
};
|
|
933
|
-
|
|
934
779
|
export declare type Router<TRoutes extends Routes = any> = Plugin_2 & {
|
|
935
780
|
/**
|
|
936
781
|
* Manages the current route state.
|
|
@@ -1131,13 +976,19 @@ name?: string | undefined;
|
|
|
1131
976
|
default?: ((props: {
|
|
1132
977
|
route: Readonly<{
|
|
1133
978
|
key: string;
|
|
1134
|
-
matched:
|
|
1135
|
-
matches:
|
|
979
|
+
matched: CreateRouteOptionsWithMeta;
|
|
980
|
+
matches: CreateRouteOptionsWithMeta[];
|
|
1136
981
|
query: ResolvedRouteQuery;
|
|
1137
|
-
params: Writable< {
|
|
982
|
+
params: Writable< {
|
|
983
|
+
[x: string]: any;
|
|
984
|
+
[x: number]: any;
|
|
985
|
+
}>;
|
|
1138
986
|
update: {
|
|
1139
987
|
(key: string, value: unknown, options?: RouterPushOptions | undefined): Promise<void>;
|
|
1140
|
-
(params: Partial<{
|
|
988
|
+
(params: Partial<{
|
|
989
|
+
[x: string]: any;
|
|
990
|
+
[x: number]: any;
|
|
991
|
+
}>, options?: RouterPushOptions | undefined): Promise<void>;
|
|
1141
992
|
};
|
|
1142
993
|
}>;
|
|
1143
994
|
component: Component;
|
|
@@ -1152,13 +1003,19 @@ name?: string | undefined;
|
|
|
1152
1003
|
default?: ((props: {
|
|
1153
1004
|
route: Readonly<{
|
|
1154
1005
|
key: string;
|
|
1155
|
-
matched:
|
|
1156
|
-
matches:
|
|
1006
|
+
matched: CreateRouteOptionsWithMeta;
|
|
1007
|
+
matches: CreateRouteOptionsWithMeta[];
|
|
1157
1008
|
query: ResolvedRouteQuery;
|
|
1158
|
-
params: Writable< {
|
|
1009
|
+
params: Writable< {
|
|
1010
|
+
[x: string]: any;
|
|
1011
|
+
[x: number]: any;
|
|
1012
|
+
}>;
|
|
1159
1013
|
update: {
|
|
1160
1014
|
(key: string, value: unknown, options?: RouterPushOptions | undefined): Promise<void>;
|
|
1161
|
-
(params: Partial<{
|
|
1015
|
+
(params: Partial<{
|
|
1016
|
+
[x: string]: any;
|
|
1017
|
+
[x: number]: any;
|
|
1018
|
+
}>, options?: RouterPushOptions | undefined): Promise<void>;
|
|
1162
1019
|
};
|
|
1163
1020
|
}>;
|
|
1164
1021
|
component: Component;
|
|
@@ -1172,7 +1029,7 @@ name?: string | undefined;
|
|
|
1172
1029
|
}>;
|
|
1173
1030
|
|
|
1174
1031
|
/**
|
|
1175
|
-
* Represents an immutable array of Route instances. Return value of `
|
|
1032
|
+
* Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
|
|
1176
1033
|
*/
|
|
1177
1034
|
export declare type Routes = Readonly<Route[]>;
|
|
1178
1035
|
|
|
@@ -1192,11 +1049,11 @@ declare type RouteUpdate<TRoute extends ResolvedRoute = ResolvedRoute> = Resolve
|
|
|
1192
1049
|
|
|
1193
1050
|
declare type StringHasValue<T> = string extends T ? true : '' extends T ? false : T extends string ? true : false;
|
|
1194
1051
|
|
|
1195
|
-
export declare function throwIfDuplicateParamsAreFound(routes: Route[]): void;
|
|
1196
|
-
|
|
1197
1052
|
declare type ToCallback = (resolve: RegisteredRouter['resolve']) => string;
|
|
1198
1053
|
|
|
1199
|
-
declare type ToHost<T extends string | Host> = T extends string ? Host<T, {}> : T;
|
|
1054
|
+
declare type ToHost<T extends string | Host | undefined> = T extends string ? Host<T, {}> : T extends undefined ? Host<'', {}> : unknown extends T ? Host<'', {}> : T;
|
|
1055
|
+
|
|
1056
|
+
declare type ToKey<T extends string | undefined> = T extends string ? T : '';
|
|
1200
1057
|
|
|
1201
1058
|
declare type ToPath<T extends string | Path | undefined> = T extends string ? Path<T, {}> : T extends undefined ? Path<'', {}> : unknown extends T ? Path<'', {}> : T;
|
|
1202
1059
|
|
|
@@ -1306,14 +1163,9 @@ export declare class UseRouteInvalidError extends Error {
|
|
|
1306
1163
|
*/
|
|
1307
1164
|
export declare function useRouter(): RegisteredRouter;
|
|
1308
1165
|
|
|
1309
|
-
declare type WithComponent =
|
|
1310
|
-
/**
|
|
1311
|
-
* A Vue component, which can be either synchronous or asynchronous components.
|
|
1312
|
-
*/
|
|
1313
|
-
component: Component;
|
|
1314
|
-
};
|
|
1166
|
+
declare type WithComponent = Partial<WithSingleComponent | WithComponentRecord>;
|
|
1315
1167
|
|
|
1316
|
-
declare type
|
|
1168
|
+
declare type WithComponentRecord = {
|
|
1317
1169
|
/**
|
|
1318
1170
|
* Multiple components for named views, which can be either synchronous or asynchronous components.
|
|
1319
1171
|
*/
|
|
@@ -1338,6 +1190,13 @@ declare type WithOptionalProperties<T> = {
|
|
|
1338
1190
|
[P in keyof T]-?: undefined extends T[P] ? P : never;
|
|
1339
1191
|
}[keyof T];
|
|
1340
1192
|
|
|
1193
|
+
declare type WithSingleComponent = {
|
|
1194
|
+
/**
|
|
1195
|
+
* A Vue component, which can be either synchronous or asynchronous components.
|
|
1196
|
+
*/
|
|
1197
|
+
component: Component;
|
|
1198
|
+
};
|
|
1199
|
+
|
|
1341
1200
|
declare type Writable<T> = {
|
|
1342
1201
|
-readonly [P in keyof T]: T[P];
|
|
1343
1202
|
};
|