@kitbag/router 0.1.1 → 0.2.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.
@@ -4,6 +4,7 @@ import { ComponentOptionsMixin } from 'vue';
4
4
  import { DeepReadonly } from 'vue';
5
5
  import { DefineComponent } from 'vue';
6
6
  import { ExtractPropTypes } from 'vue';
7
+ import { FunctionalComponent } from 'vue';
7
8
  import { InjectionKey } from 'vue';
8
9
  import { Plugin as Plugin_2 } from 'vue';
9
10
  import { PropType } from 'vue';
@@ -145,9 +146,9 @@ export declare type ChildRouteProps = WithHooks & {
145
146
  */
146
147
  query?: string | Query;
147
148
  /**
148
- * Type alias for Vue components, which can be either synchronous or asynchronous components.
149
+ * A Vue component, which can be either synchronous or asynchronous components.
149
150
  */
150
- component: RouteComponent;
151
+ component: Component;
151
152
  /**
152
153
  * Represents additional metadata associated with a route, customizable via declaration merging.
153
154
  */
@@ -174,6 +175,30 @@ declare type CombineQuery<TParent extends Query | undefined, TChild extends Quer
174
175
 
175
176
  declare type CombineQueryString<TParent extends string | undefined, TChild extends string | undefined> = StringHasValue<TParent> extends true ? StringHasValue<TChild> extends true ? `${TParent}&${TChild}` : TParent : TChild;
176
177
 
178
+ /**
179
+ * Creates a component wrapper which has no props itself but mounts another component within while binding its props
180
+ *
181
+ * @param component The component to mount
182
+ * @param props A callback that returns the props or attributes to bind to the component
183
+ * @returns A component
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * import { createRoutes, component } from '@kitbag/router'
188
+ *
189
+ * export const routes = createRoutes([
190
+ * {
191
+ * name: 'User',
192
+ * path: '/',
193
+ * component: component(User, () => ({ userId: 1 }))
194
+ * },
195
+ * ])
196
+ * ```
197
+ */
198
+ export declare function component<TComponent extends Component>(component: TComponent, props: PropsGetter<TComponent>): Component;
199
+
200
+ declare type Constructor = new (...args: any) => any;
201
+
177
202
  /**
178
203
  * Creates a router instance for a Vue application, equipped with methods for route handling, lifecycle hooks, and state management.
179
204
  *
@@ -228,11 +253,9 @@ export declare class DuplicateParamsError extends Error {
228
253
  */
229
254
  export declare type ExtractParamName<TParam extends string> = TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam;
230
255
 
231
- declare type ExtractParamsFromPathString<TPath extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TPath extends `${infer Path}${ParamEnd}` ? ExtractParamsFromPathString<Path, TParams> : TPath extends `${string}:${infer Param}${ParamEnd}${infer Rest}` ? MergeParams<{
232
- [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams>;
233
- }, ExtractParamsFromPathString<Rest, TParams>> : TPath extends `${string}:${infer Param}` ? {
256
+ 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<{
234
257
  [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams>;
235
- } : Record<never, never>;
258
+ }, ExtractParamsFromPathString<Rest, TParams>> : Record<never, never>;
236
259
 
237
260
  /**
238
261
  * Extracts the actual type from a parameter type, handling getters, setters, and potential undefined values.
@@ -258,11 +281,9 @@ export declare type ExtractParamTypes<TParams extends Record<string, Param | und
258
281
  */
259
282
  export declare type ExtractPathParamType<TParam extends string, TParams extends Record<string, Param | undefined>> = TParam extends `?${infer OptionalParam}` ? OptionalParam extends keyof TParams ? TParams[OptionalParam] | undefined : StringConstructor | undefined : TParam extends keyof TParams ? TParams[TParam] : StringConstructor;
260
283
 
261
- declare type ExtractQueryParamsFromQueryString<TQuery extends string, TParams extends Record<string, Param | undefined> = Record<never, never>> = TQuery extends `${string}=:${infer Param}&${infer Rest}` ? MergeParams<{
284
+ 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<{
262
285
  [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams>;
263
- }, ExtractQueryParamsFromQueryString<Rest, TParams>> : TQuery extends `${string}:${infer Param}` ? {
264
- [P in ExtractParamName<Param>]: ExtractPathParamType<Param, TParams>;
265
- } : Record<never, never>;
286
+ }, ExtractQueryParamsFromQueryString<Rest, TParams>> : Record<never, never>;
266
287
 
267
288
  declare type ExtractRouteChildren<TRoute extends RouteProps> = TRoute extends ParentRouteProps ? TRoute['children'] extends Route[] ? TRoute['children'] : [] : [];
268
289
 
@@ -400,9 +421,11 @@ declare type OnlyRequiredProperties<T> = {
400
421
  [K in keyof T as Extract<T[K], undefined> extends never ? K : never]: T[K];
401
422
  };
402
423
 
403
- export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor;
424
+ export declare type Param = ParamGetter | ParamGetSet | RegExp | BooleanConstructor | NumberConstructor | StringConstructor | DateConstructor | JSON;
425
+
426
+ export declare type ParamEnd = typeof paramEnd;
404
427
 
405
- declare type ParamEnd = '/';
428
+ export declare const paramEnd = "]";
406
429
 
407
430
  export declare type ParamExtras = {
408
431
  invalid: (message?: string) => never;
@@ -417,6 +440,10 @@ export declare type ParamGetter<T = any> = (value: string, extras: ParamExtras)
417
440
 
418
441
  export declare type ParamSetter<T = any> = (value: T, extras: ParamExtras) => string;
419
442
 
443
+ export declare type ParamStart = typeof paramStart;
444
+
445
+ export declare const paramStart = "[";
446
+
420
447
  /**
421
448
  * Represents properties common to parent routes in a route configuration, including hooks, path, and optional query parameters.
422
449
  */
@@ -442,18 +469,18 @@ export declare type ParentRouteProps = WithHooks & {
442
469
  */
443
470
  children: Routes;
444
471
  /**
445
- * Type alias for Vue components, which can be either synchronous or asynchronous components.
472
+ * A Vue component, which can be either synchronous or asynchronous components.
446
473
  */
447
- component?: RouteComponent;
474
+ component?: Component;
448
475
  /**
449
476
  * Represents additional metadata associated with a route, customizable via declaration merging.
450
477
  */
451
478
  meta?: RouteMeta;
452
479
  };
453
480
 
454
- declare type Path<T extends string = any, P extends PathParams<T> = any> = {
455
- path: T;
456
- params: Identity<ExtractParamsFromPathString<T, P>>;
481
+ declare type Path<TPath extends string = any, TParams extends PathParams<TPath> = any> = {
482
+ path: TPath;
483
+ params: Identity<ExtractParamsFromPathString<TPath, TParams>>;
457
484
  toString: () => string;
458
485
  };
459
486
 
@@ -474,7 +501,7 @@ declare type Path<T extends string = any, P extends PathParams<T> = any> = {
474
501
  * export const routes = createRoutes([
475
502
  * {
476
503
  * name: 'home',
477
- * path: path('/:foo', { foo: Number }),
504
+ * path: path('/[foo]', { foo: Number }),
478
505
  * component: Home
479
506
  * },
480
507
  * ])
@@ -482,10 +509,14 @@ declare type Path<T extends string = any, P extends PathParams<T> = any> = {
482
509
  */
483
510
  export declare function path<TPath extends string, TParams extends PathParams<TPath>>(path: TPath, params: Identity<TParams>): Path<TPath, TParams>;
484
511
 
485
- declare type PathParams<T extends string> = {
486
- [K in keyof ExtractParamsFromPathString<T>]?: Param;
512
+ declare type PathParams<TPath extends string> = {
513
+ [K in keyof ExtractParamsFromPathString<TPath>]?: Param;
487
514
  };
488
515
 
516
+ 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;
517
+
518
+ declare type PropsGetter<TComponent extends Component> = () => MaybePromise<Props<TComponent>>;
519
+
489
520
  declare type Query<T extends string = any, P extends QueryParams<T> = any> = {
490
521
  query: T;
491
522
  params: Identity<ExtractQueryParamsFromQueryString<T, P>>;
@@ -509,7 +540,7 @@ declare type Query<T extends string = any, P extends QueryParams<T> = any> = {
509
540
  * export const routes = createRoutes([
510
541
  * {
511
542
  * name: 'home',
512
- * query: query('?bar=:bar', { bar: Boolean }),
543
+ * query: query('bar=[bar]', { bar: Boolean }),
513
544
  * component: Home
514
545
  * },
515
546
  * ])
@@ -654,11 +685,6 @@ export declare type Route<TKey extends string | undefined = any, TPath extends s
654
685
  disabled: TDisabled extends boolean ? TDisabled : false;
655
686
  };
656
687
 
657
- /**
658
- * Type alias for Vue components, which can be either synchronous or asynchronous components.
659
- */
660
- export declare type RouteComponent = Component | DefineComponent | AsyncComponentLoader;
661
-
662
688
  declare type RouteGetByKey<TRoutes extends Routes, TKey extends RoutesKey<TRoutes>> = RoutesMap<TRoutes>[TKey];
663
689
 
664
690
  /**
@@ -897,11 +923,11 @@ export declare type RouterReject = (type: RouterRejectionType) => void;
897
923
 
898
924
  declare type RouterRejection = Ref<null | {
899
925
  type: RouterRejectionType;
900
- component: RouteComponent;
926
+ component: Component;
901
927
  }>;
902
928
 
903
929
  declare type RouterRejectionComponents = {
904
- rejections?: Partial<Record<RouterRejectionType, RouteComponent>>;
930
+ rejections?: Partial<Record<RouterRejectionType, Component>>;
905
931
  };
906
932
 
907
933
  export declare const routerRejectionKey: InjectionKey<RouterRejection>;
@@ -936,12 +962,12 @@ declare type RouterRoute<TRoute extends ResolvedRoute = ResolvedRoute> = Omit<Re
936
962
  export declare const RouterView: __VLS_WithTemplateSlots<DefineComponent< {}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<ExtractPropTypes< {}>>, {}, {}>, Readonly<{
937
963
  default?: ((props: {
938
964
  route: RouterRoute;
939
- component: RouteComponent;
965
+ component: Component;
940
966
  }) => unknown) | undefined;
941
967
  }> & {
942
968
  default?: ((props: {
943
969
  route: RouterRoute;
944
- component: RouteComponent;
970
+ component: Component;
945
971
  }) => unknown) | undefined;
946
972
  }>;
947
973