@kitbag/router 0.20.10 → 0.20.12

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.
Files changed (45) hide show
  1. package/dist/components/routerLink.d.ts +1 -0
  2. package/dist/compositions/useRouterHooks.d.ts +2 -2
  3. package/dist/errors/contextRejectionError.d.ts +1 -2
  4. package/dist/errors/missingRouteContextError.d.ts +10 -0
  5. package/dist/kitbag-router.js +1480 -1352
  6. package/dist/kitbag-router.umd.cjs +3 -3
  7. package/dist/main.d.ts +1 -0
  8. package/dist/models/RouteHooks.d.ts +3 -0
  9. package/dist/models/RouterRouteHooks.d.ts +9 -10
  10. package/dist/services/createCallbackContext.d.ts +2 -2
  11. package/dist/services/createComponentHooks.d.ts +4 -2
  12. package/dist/services/createExternalRoute.d.ts +3 -2
  13. package/dist/services/createRejection.d.ts +6 -0
  14. package/dist/services/createRoute.d.ts +4 -3
  15. package/dist/services/createRouteHooks.d.ts +15 -0
  16. package/dist/services/createRouter.d.ts +2 -2
  17. package/dist/services/createRouterAssets.d.ts +3 -2
  18. package/dist/services/createRouterCallbackContext.d.ts +8 -6
  19. package/dist/services/createRouterHooks.d.ts +17 -20
  20. package/dist/services/createRouterPlugin.d.ts +3 -3
  21. package/dist/services/createRouterPlugin.spec-d.d.ts +1 -0
  22. package/dist/services/createRouterReject.d.ts +4 -6
  23. package/dist/services/getGlobalRouteHooks.d.ts +2 -3
  24. package/dist/services/getRouteHooks.d.ts +3 -9
  25. package/dist/services/getRouteHooksDeprecated.d.ts +10 -0
  26. package/dist/services/routeMatchRules.d.ts +1 -0
  27. package/dist/services/routeRegex.d.ts +1 -0
  28. package/dist/services/zod.d.ts +3 -3
  29. package/dist/types/createRouteOptions.d.ts +14 -5
  30. package/dist/types/hooks.d.ts +79 -7
  31. package/dist/types/props.d.ts +13 -7
  32. package/dist/types/register.d.ts +2 -1
  33. package/dist/types/rejection.d.ts +15 -0
  34. package/dist/types/resolved.d.ts +14 -1
  35. package/dist/types/route.d.ts +28 -2
  36. package/dist/types/routeContext.d.ts +9 -0
  37. package/dist/types/router.d.ts +14 -58
  38. package/dist/types/routerPlugin.d.ts +106 -10
  39. package/dist/types/routerReject.d.ts +2 -2
  40. package/dist/types/routerRoute.d.ts +27 -0
  41. package/dist/types/utilities.d.ts +0 -4
  42. package/dist/utilities/checkMissingContext.d.ts +2 -0
  43. package/dist/utilities/checkMissingContext.spec copy.d.ts +1 -0
  44. package/dist/utilities/testHelpers.d.ts +11 -14
  45. package/package.json +8 -8
@@ -1,6 +1,7 @@
1
1
  import { Route } from '../types/route';
2
2
  import { WithParams } from './withParams';
3
3
  export declare function splitByMatches(string: string, regexp: RegExp): string[];
4
+ export declare function generateRouteHostRegexPattern(route: Route): RegExp;
4
5
  export declare function generateRoutePathRegexPattern(route: Route): RegExp;
5
6
  export declare function generateRouteQueryRegexPatterns(route: Route): RegExp[];
6
7
  export declare function replaceParamSyntaxWithCatchAlls(value: string): string;
@@ -1,11 +1,11 @@
1
1
  import { ParamGetSet } from '../types/paramTypes';
2
2
  import { Routes } from '../types/route';
3
3
  import { StandardSchemaV1 } from '@standard-schema/spec';
4
- import { ZodSchema } from 'zod';
4
+ import { ZodType } from 'zod';
5
5
  export interface ZodSchemaLike extends StandardSchemaV1<any> {
6
6
  parse: (input: any) => any;
7
7
  }
8
8
  export declare function zotParamsDetected(routes: Routes): boolean;
9
9
  export declare function initZod(): Promise<void>;
10
- export declare function isZodParam(value: unknown): value is ZodSchema;
11
- export declare function createZodParam<T>(schema: ZodSchema<T>): ParamGetSet<T>;
10
+ export declare function isZodParam(value: unknown): value is ZodType;
11
+ export declare function createZodParam<T>(schema: ZodType<T>): ParamGetSet<T>;
@@ -12,12 +12,13 @@ import { ResolvedRoute } from './resolved';
12
12
  import { ComponentProps } from '../services/component';
13
13
  import { PropsCallbackContext } from './props';
14
14
  import { MaybePromise } from './utilities';
15
- import { RouterView } from '../main';
16
15
  import { ToMeta } from './meta';
17
16
  import { ToState } from './state';
18
17
  import { ToName } from './name';
19
18
  import { WithHooks } from './hooks';
20
19
  import { ToWithParams, WithParams } from '../services/withParams';
20
+ import { RouteContext, ToRouteContext } from './routeContext';
21
+ import { RouterViewProps } from '../components/routerView';
21
22
  export type WithHost<THost extends string | WithParams = string | WithParams> = {
22
23
  /**
23
24
  * Host part of URL.
@@ -89,6 +90,10 @@ export type CreateRouteOptions<TName extends string | undefined = string | undef
89
90
  * An object of named components to render using named views
90
91
  */
91
92
  components?: Record<string, Component>;
93
+ /**
94
+ * Related routes and rejections for the route. The context is exposed to the hooks and props callback functions for this route.
95
+ */
96
+ context?: RouteContext[];
92
97
  /**
93
98
  * Props have been moved to the second argument of `createRoute`. This property can no longer be used.
94
99
  *
@@ -96,14 +101,15 @@ export type CreateRouteOptions<TName extends string | undefined = string | undef
96
101
  */
97
102
  props?: never;
98
103
  };
99
- export type PropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponent extends Component = Component> = (route: ResolvedRoute<ToRoute<TOptions, undefined>>, context: PropsCallbackContext<TOptions['parent']>) => MaybePromise<ComponentProps<TComponent>>;
104
+ export type PropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponent extends Component = Component> = (route: ResolvedRoute<ToRoute<TOptions, undefined>>, context: PropsCallbackContext<TOptions>) => MaybePromise<ComponentProps<TComponent>>;
105
+ export type RouterViewPropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions> = (route: ResolvedRoute<ToRoute<TOptions, undefined>>, context: PropsCallbackContext<TOptions>) => MaybePromise<RouterViewProps & Record<string, unknown>>;
100
106
  type ComponentPropsAreOptional<TComponent extends Component> = Partial<ComponentProps<TComponent>> extends ComponentProps<TComponent> ? true : false;
101
107
  type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
102
108
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
103
109
  } & {
104
110
  [K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends false ? K : never]: PropsGetter<TOptions, TComponents[K]>;
105
111
  };
106
- export type CreateRouteProps<TOptions extends CreateRouteOptions = CreateRouteOptions> = TOptions['component'] extends Component ? PropsGetter<TOptions, TOptions['component']> : TOptions['components'] extends Record<string, Component> ? RoutePropsRecord<TOptions, TOptions['components']> : PropsGetter<TOptions, typeof RouterView>;
112
+ export type CreateRouteProps<TOptions extends CreateRouteOptions = CreateRouteOptions> = TOptions['component'] extends Component ? PropsGetter<TOptions, TOptions['component']> : TOptions['components'] extends Record<string, Component> ? RoutePropsRecord<TOptions, TOptions['components']> : RouterViewPropsGetter<TOptions>;
107
113
  type ToMatch<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = Omit<TOptions, 'props' | 'meta'> & {
108
114
  id: string;
109
115
  props: TProps;
@@ -117,8 +123,11 @@ type ToMatches<TOptions extends CreateRouteOptions, TProps extends CreateRoutePr
117
123
  } ? [...TParent['matches'], ToMatch<TOptions, TProps>] : [ToMatch<TOptions, TProps>];
118
124
  export type ToRoute<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = CreateRouteOptions extends TOptions ? Route : TOptions extends {
119
125
  parent: infer TParent extends Route;
120
- } ? Route<ToName<TOptions['name']>, ToWithParams<TParent['host']>, CombinePath<ToWithParams<TParent['path']>, ToWithParams<TOptions['path']>>, CombineQuery<ToWithParams<TParent['query']>, ToWithParams<TOptions['query']>>, CombineHash<ToWithParams<TParent['hash']>, ToWithParams<TOptions['hash']>>, CombineMeta<ToMeta<TParent['meta']>, ToMeta<TOptions['meta']>>, CombineState<ToState<TParent['state']>, ToState<TOptions['state']>>, ToMatches<TOptions, TProps>> : Route<ToName<TOptions['name']>, TOptions extends {
126
+ } ? Route<ToName<TOptions['name']>, ToWithParams<TParent['host']>, CombinePath<ToWithParams<TParent['path']>, ToWithParams<TOptions['path']>>, CombineQuery<ToWithParams<TParent['query']>, ToWithParams<TOptions['query']>>, CombineHash<ToWithParams<TParent['hash']>, ToWithParams<TOptions['hash']>>, CombineMeta<ToMeta<TParent['meta']>, ToMeta<TOptions['meta']>>, CombineState<ToState<TParent['state']>, ToState<TOptions['state']>>, ToMatches<TOptions, TProps>, [
127
+ ...ToRouteContext<TParent['context']>,
128
+ ...ToRouteContext<TOptions['context']>
129
+ ]> : Route<ToName<TOptions['name']>, TOptions extends {
121
130
  host: string | WithParams;
122
- } ? ToWithParams<TOptions['host']> : WithParams<'', {}>, ToWithParams<TOptions['path']>, ToWithParams<TOptions['query']>, ToWithParams<TOptions['hash']>, ToMeta<TOptions['meta']>, ToState<TOptions['state']>, ToMatches<TOptions, TProps>>;
131
+ } ? ToWithParams<TOptions['host']> : WithParams<'', {}>, ToWithParams<TOptions['path']>, ToWithParams<TOptions['query']>, ToWithParams<TOptions['hash']>, ToMeta<TOptions['meta']>, ToState<TOptions['state']>, ToMatches<TOptions, TProps>, ToRouteContext<TOptions['context']>>;
123
132
  export declare function combineRoutes(parent: Route, child: Route): Route;
124
133
  export {};
@@ -1,9 +1,13 @@
1
1
  import { RouterRouteHooks } from '../models/RouterRouteHooks';
2
2
  import { CallbackAbortResponse, CallbackContext, CallbackContextAbort, CallbackPushResponse, CallbackRejectResponse, CallbackSuccessResponse } from '../services/createCallbackContext';
3
- import { ResolvedRoute } from './resolved';
3
+ import { ResolvedRoute, RouterResolvedRouteUnion } from './resolved';
4
4
  import { MaybeArray, MaybePromise } from './utilities';
5
5
  import { Routes } from './route';
6
- import { RouterAfterRouteHook, RouterBeforeRouteHook } from './router';
6
+ import { RouterReject } from './routerReject';
7
+ import { RouterPush } from './routerPush';
8
+ import { RouterReplace } from './routerReplace';
9
+ import { Rejection } from './rejection';
10
+ import { RouteContext, RouteContextToRejection, RouteContextToRoute } from './routeContext';
7
11
  /**
8
12
  * Defines route hooks that can be applied before entering, updating, or leaving a route, as well as after these events.
9
13
  */
@@ -33,6 +37,38 @@ export type WithHooks = {
33
37
  */
34
38
  onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
35
39
  };
40
+ export type InternalRouteHooks<TContext extends RouteContext[] | undefined = undefined> = {
41
+ /**
42
+ * Registers a route hook to be called before the route is entered.
43
+ */
44
+ onBeforeRouteEnter: AddRouterBeforeRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
45
+ /**
46
+ * Registers a route hook to be called before the route is left.
47
+ */
48
+ onBeforeRouteLeave: AddRouterBeforeRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
49
+ /**
50
+ * Registers a route hook to be called before the route is updated.
51
+ */
52
+ onBeforeRouteUpdate: AddRouterBeforeRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
53
+ /**
54
+ * Registers a route hook to be called after the route is entered.
55
+ */
56
+ onAfterRouteEnter: AddRouterAfterRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
57
+ /**
58
+ * Registers a route hook to be called after the route is left.
59
+ */
60
+ onAfterRouteLeave: AddRouterAfterRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
61
+ /**
62
+ * Registers a route hook to be called after the route is updated.
63
+ */
64
+ onAfterRouteUpdate: AddRouterAfterRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
65
+ };
66
+ export type ExternalRouteHooks<TContext extends RouteContext[] | undefined = undefined> = {
67
+ /**
68
+ * Registers a route hook to be called before the route is entered.
69
+ */
70
+ onBeforeRouteEnter: AddRouterBeforeRouteHook<RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
71
+ };
36
72
  export type BeforeHookContext = {
37
73
  to: ResolvedRoute;
38
74
  from: ResolvedRoute | null;
@@ -44,19 +80,19 @@ export type AfterHookContext = {
44
80
  };
45
81
  export type RouteHookAfterRunner = (context: AfterHookContext) => Promise<AfterRouteHookResponse>;
46
82
  export type RouteHookTiming = 'global' | 'component';
47
- type BeforeRouteHookRegistration<TRoutes extends Routes, TRejections extends PropertyKey> = {
83
+ type BeforeRouteHookRegistration<TRoutes extends Routes, TRejections extends Rejection[]> = {
48
84
  lifecycle: 'onBeforeRouteEnter' | 'onBeforeRouteUpdate' | 'onBeforeRouteLeave';
49
85
  hook: RouterBeforeRouteHook<TRoutes, TRejections>;
50
86
  depth: number;
51
87
  };
52
- export type AddComponentBeforeRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (hook: BeforeRouteHookRegistration<TRoutes, TRejections>) => RouteHookRemove;
53
- type AfterRouteHookRegistration<TRoutes extends Routes, TRejections extends PropertyKey> = {
88
+ export type AddComponentBeforeRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: BeforeRouteHookRegistration<TRoutes, TRejections>) => RouteHookRemove;
89
+ type AfterRouteHookRegistration<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
54
90
  lifecycle: 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'onAfterRouteLeave';
55
91
  hook: RouterAfterRouteHook<TRoutes, TRejections>;
56
92
  depth: number;
57
93
  };
58
- export type AddComponentAfterRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (hook: AfterRouteHookRegistration<TRoutes, TRejections>) => RouteHookRemove;
59
- export type AddGlobalRouteHooks<TRoutes extends Routes, TRejections extends PropertyKey> = (hooks: RouterRouteHooks<TRoutes, TRejections>) => void;
94
+ export type AddComponentAfterRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: AfterRouteHookRegistration<TRoutes, TRejections>) => RouteHookRemove;
95
+ export type AddGlobalRouteHooks = (hooks: RouterRouteHooks) => void;
60
96
  /**
61
97
  * Context provided to route hooks, containing context of previous route and functions for triggering rejections and push/replace to another route.
62
98
  */
@@ -126,4 +162,40 @@ export type AfterRouteHookResponse = CallbackSuccessResponse | CallbackPushRespo
126
162
  * @template TRoutes - The type of the routes configuration.
127
163
  */
128
164
  export type RouteHookResponse = BeforeRouteHookResponse | AfterRouteHookResponse;
165
+ type RouterHookContext<TRoutes extends Routes, TRejections extends Rejection[]> = {
166
+ from: RouterResolvedRouteUnion<TRoutes> | null;
167
+ reject: RouterReject<TRejections>;
168
+ push: RouterPush<TRoutes>;
169
+ replace: RouterReplace<TRoutes>;
170
+ };
171
+ type RouterBeforeRouteHookContext<TRoutes extends Routes, TRejections extends Rejection[]> = RouterHookContext<TRoutes, TRejections> & {
172
+ abort: CallbackContextAbort;
173
+ };
174
+ export type HookContext<TRoutes extends Routes = Routes> = {
175
+ to: RouterResolvedRouteUnion<TRoutes>;
176
+ from: RouterResolvedRouteUnion<TRoutes> | null;
177
+ };
178
+ export type RouterBeforeRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (to: RouterResolvedRouteUnion<TRoutes>, context: RouterBeforeRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
179
+ export type AddRouterBeforeRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: RouterBeforeRouteHook<TRoutes, TRejections>) => RouteHookRemove;
180
+ type RouterAfterRouteHookContext<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = RouterHookContext<TRoutes, TRejections>;
181
+ export type RouterAfterRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (to: RouterResolvedRouteUnion<TRoutes>, context: RouterAfterRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
182
+ export type AddRouterAfterRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: RouterAfterRouteHook<TRoutes, TRejections>) => RouteHookRemove;
183
+ export type RouterRouteHookBeforeRunner = (context: HookContext) => Promise<BeforeRouteHookResponse>;
184
+ export type RouterRouteHookAfterRunner = (context: HookContext) => Promise<AfterRouteHookResponse>;
185
+ export type RouterErrorHookContext<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
186
+ to: RouterResolvedRouteUnion<TRoutes>;
187
+ from: RouterResolvedRouteUnion<TRoutes> | null;
188
+ source: 'props' | 'hook' | 'component';
189
+ reject: RouterReject<TRejections>;
190
+ push: RouterPush<TRoutes>;
191
+ replace: RouterReplace<TRoutes>;
192
+ };
193
+ export type RouterErrorHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (error: unknown, context: RouterErrorHookContext<TRoutes, TRejections>) => void;
194
+ export type AddRouterErrorHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: RouterErrorHook<TRoutes, TRejections>) => RouteHookRemove;
195
+ export type RouterRouteHookErrorRunnerContext<TRoutes extends Routes = Routes> = {
196
+ to: RouterResolvedRouteUnion<TRoutes>;
197
+ from: RouterResolvedRouteUnion<TRoutes> | null;
198
+ source: 'props' | 'hook';
199
+ };
200
+ export type RouterRouteHookErrorRunner = (error: unknown, context: RouterRouteHookErrorRunnerContext) => void;
129
201
  export {};
@@ -1,15 +1,21 @@
1
- import { CallbackContext } from '../services/createCallbackContext';
2
- import { PropsGetter } from './createRouteOptions';
1
+ import { CreateRouteOptions, PropsGetter } from './createRouteOptions';
3
2
  import { Route } from './route';
3
+ import { RouterReject } from './routerReject';
4
+ import { RouterPush } from './routerPush';
5
+ import { RouterReplace } from './routerReplace';
6
+ import { RouteContext, RouteContextToRejection, RouteContextToRoute } from './routeContext';
4
7
  /**
5
8
  * Context provided to props callback functions
6
9
  */
7
- export type PropsCallbackContext<TParent extends Route | undefined = Route | undefined> = {
8
- push: CallbackContext['push'];
9
- replace: CallbackContext['replace'];
10
- reject: CallbackContext['reject'];
11
- parent: PropsCallbackParent<TParent>;
10
+ export type PropsCallbackContext<TOptions extends CreateRouteOptions = CreateRouteOptions> = {
11
+ reject: RouterReject<RouteContextToRejection<ExtractRouteContext<TOptions>>>;
12
+ push: RouterPush<RouteContextToRoute<ExtractRouteContext<TOptions>>>;
13
+ replace: RouterReplace<RouteContextToRoute<ExtractRouteContext<TOptions>>>;
14
+ parent: PropsCallbackParent<TOptions['parent']>;
12
15
  };
16
+ type ExtractRouteContext<TOptions extends CreateRouteOptions> = TOptions extends {
17
+ context: infer TContext extends RouteContext[];
18
+ } ? TContext : [];
13
19
  export type PropsCallbackParent<TParent extends Route | undefined = Route | undefined> = Route | undefined extends TParent ? undefined | {
14
20
  name: string;
15
21
  props: unknown;
@@ -4,6 +4,7 @@ import { Router, RouterOptions } from './router';
4
4
  import { RouterPush } from './routerPush';
5
5
  import { RouterReplace } from './routerReplace';
6
6
  import { RoutesName } from './routesMap';
7
+ import { RejectionType } from './rejection';
7
8
  /**
8
9
  * Represents the state of currently registered router, and route meta. Used to provide correct type context for
9
10
  * components like `RouterLink`, as well as for composables like `useRouter`, `useRoute`, and hooks.
@@ -39,7 +40,7 @@ export type RegisteredRoutes<T = Register> = T extends {
39
40
  */
40
41
  export type RegisteredRejectionType<T = Register> = T extends {
41
42
  router: Router<any, infer TOptions extends RouterOptions>;
42
- } ? keyof TOptions['rejections'] | BuiltInRejectionType : BuiltInRejectionType;
43
+ } ? RejectionType<TOptions['rejections']> | BuiltInRejectionType : BuiltInRejectionType;
43
44
  /**
44
45
  * Represents additional metadata associated with a route, customizable via declaration merging.
45
46
  */
@@ -0,0 +1,15 @@
1
+ import { Component } from 'vue';
2
+ export type Rejection<TType extends string = string> = {
3
+ /**
4
+ * The type of rejection.
5
+ */
6
+ type: TType;
7
+ /**
8
+ * The component to render when the rejection occurs.
9
+ */
10
+ component: Component;
11
+ };
12
+ export type RejectionType<TRejections extends Rejection[] | undefined> = unknown extends TRejections ? never : Rejection[] extends TRejections ? string : undefined extends TRejections ? string : TRejections extends Rejection[] ? TRejections[number]['type'] : never;
13
+ export type ExtractRejections<T> = T extends {
14
+ rejections: infer TRejections extends Rejection[];
15
+ } ? TRejections : [];
@@ -1,5 +1,6 @@
1
+ import { RouterRouteHooks } from '../models/RouterRouteHooks';
1
2
  import { ExtractRouteParamTypesReading } from './params';
2
- import { Route } from './route';
3
+ import { Route, Routes } from './route';
3
4
  import { ExtractRouteStateParamsAsOptional } from './state';
4
5
  import { Url } from './url';
5
6
  /**
@@ -44,4 +45,16 @@ export type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
44
45
  * String value of the resolved URL.
45
46
  */
46
47
  href: Url;
48
+ /**
49
+ * The stores for routes including ancestors.
50
+ * Order of routes will be from greatest ancestor to narrowest matched.
51
+ * @internal
52
+ */
53
+ hooks: RouterRouteHooks[];
47
54
  }>;
55
+ /**
56
+ * This type is the same as `ResolvedRoute<TRoutes[number]>` while remaining distributive
57
+ */
58
+ export type RouterResolvedRouteUnion<TRoutes extends Routes> = {
59
+ [K in keyof TRoutes]: ResolvedRoute<TRoutes[K]>;
60
+ }[number];
@@ -1,10 +1,12 @@
1
+ import { WithParams } from '../services/withParams';
1
2
  import { Param } from './paramTypes';
2
3
  import { PrefetchConfig } from './prefetch';
3
4
  import { RouteMeta } from './register';
4
5
  import { LastInArray } from './utilities';
5
6
  import { CreateRouteOptions } from './createRouteOptions';
6
7
  import { WithHooks } from './hooks';
7
- import { WithParams } from '../services/withParams';
8
+ import { RouteContext } from './routeContext';
9
+ import { RouterRouteHooks } from '../models/RouterRouteHooks';
8
10
  /**
9
11
  * Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
10
12
  */
@@ -22,7 +24,7 @@ export type CreatedRouteOptions = Omit<CreateRouteOptions, 'props'> & WithHooks
22
24
  * @template TPath - The type or structure of the route's path.
23
25
  * @template TQuery - The type or structure of the query parameters associated with the route.
24
26
  */
25
- export type Route<TName extends string = string, THost extends WithParams = WithParams, TPath extends WithParams = WithParams, TQuery extends WithParams = WithParams, THash extends WithParams = WithParams, TMeta extends RouteMeta = RouteMeta, TState extends Record<string, Param> = Record<string, Param>, TMatches extends CreatedRouteOptions[] = CreatedRouteOptions[]> = {
27
+ export type Route<TName extends string = string, THost extends WithParams = WithParams, TPath extends WithParams = WithParams, TQuery extends WithParams = WithParams, THash extends WithParams = WithParams, TMeta extends RouteMeta = RouteMeta, TState extends Record<string, Param> = Record<string, Param>, TMatches extends CreatedRouteOptions[] = CreatedRouteOptions[], TContext extends RouteContext[] = RouteContext[]> = {
26
28
  /**
27
29
  * Unique identifier for the route, generated by router.
28
30
  */
@@ -68,9 +70,33 @@ export type Route<TName extends string = string, THost extends WithParams = With
68
70
  * Determines what assets are prefetched when router-link is rendered for this route. Overrides router level prefetch.
69
71
  */
70
72
  prefetch?: PrefetchConfig;
73
+ /**
74
+ * Related routes and rejections for the route. The context is exposed to the hooks and props callback functions for this route.
75
+ */
76
+ context: TContext;
77
+ /**
78
+ * The stores for routes including ancestors.
79
+ * Order of routes will be from greatest ancestor to narrowest matched.
80
+ * @internal
81
+ */
82
+ hooks: RouterRouteHooks[];
71
83
  /**
72
84
  * A value that represents how many parents a route has. Used for route matching
73
85
  * @internal
74
86
  */
75
87
  depth: number;
76
88
  };
89
+ export type GenericRoute = {
90
+ id: string;
91
+ name: string;
92
+ host: WithParams;
93
+ matched: CreatedRouteOptions;
94
+ matches: CreatedRouteOptions[];
95
+ path: WithParams;
96
+ query: WithParams;
97
+ hash: WithParams;
98
+ meta: RouteMeta;
99
+ state: Record<string, Param>;
100
+ prefetch?: PrefetchConfig;
101
+ depth: number;
102
+ };
@@ -0,0 +1,9 @@
1
+ import { Rejection } from './rejection';
2
+ import { GenericRoute, Route } from './route';
3
+ export type RouteContext = GenericRoute | Rejection;
4
+ export type ToRouteContext<TContext extends RouteContext[] | undefined> = TContext extends RouteContext[] ? TContext : [];
5
+ export type RouteContextToRoute<TContext extends RouteContext[] | undefined> = RouteContext[] extends TContext ? Route[] : undefined extends TContext ? Route[] : FilterRouteContextRoutes<TContext>;
6
+ type FilterRouteContextRoutes<TContext extends RouteContext[] | undefined> = TContext extends [infer First, ...infer Rest extends RouteContext[]] ? First extends GenericRoute ? [First, ...FilterRouteContextRoutes<Rest>] : FilterRouteContextRoutes<Rest> : [];
7
+ export type RouteContextToRejection<TContext extends RouteContext[] | undefined> = RouteContext[] extends TContext ? Rejection[] : undefined extends TContext ? Rejection[] : FilterRouteContextRejections<TContext>;
8
+ type FilterRouteContextRejections<TContext extends RouteContext[] | undefined> = TContext extends [infer First, ...infer Rest extends RouteContext[]] ? First extends Rejection ? [First, ...FilterRouteContextRejections<Rest>] : FilterRouteContextRejections<Rest> : [];
9
+ export {};
@@ -1,7 +1,7 @@
1
- import { App, Component, InjectionKey, Ref } from 'vue';
1
+ import { App, InjectionKey, Ref } from 'vue';
2
2
  import { RouterHistoryMode } from '../services/createRouterHistory';
3
3
  import { RouterRoute } from './routerRoute';
4
- import { RouteHookRemove, WithHooks } from './hooks';
4
+ import { AddRouterAfterRouteHook, AddRouterBeforeRouteHook, AddRouterErrorHook, WithHooks } from './hooks';
5
5
  import { PrefetchConfig } from './prefetch';
6
6
  import { ResolvedRoute } from './resolved';
7
7
  import { Route, Routes } from './route';
@@ -10,10 +10,9 @@ import { RouterReplace } from './routerReplace';
10
10
  import { RouterResolve, RouterResolveOptions } from './routerResolve';
11
11
  import { RouterReject } from './routerReject';
12
12
  import { RouterPlugin } from './routerPlugin';
13
- import { KeysOfUnion, MaybePromise } from './utilities';
14
13
  import { RoutesName } from './routesMap';
15
- import { AfterRouteHookResponse, BeforeRouteHookResponse } from '../main';
16
- import { CallbackContextAbort } from '../services/createCallbackContext';
14
+ import { ToRouteContext } from './routeContext';
15
+ import { ExtractRejections, Rejection } from './rejection';
17
16
  /**
18
17
  * Options to initialize a {@link Router} instance.
19
18
  */
@@ -42,7 +41,7 @@ export type RouterOptions = WithHooks & {
42
41
  /**
43
42
  * Components assigned to each type of rejection your router supports.
44
43
  */
45
- rejections?: Partial<Record<string, Component>>;
44
+ rejections?: Rejection[];
46
45
  /**
47
46
  * When false, createRouterAssets must be used for component and hooks. Assets exported by the library
48
47
  * will not work with the created router instance.
@@ -80,7 +79,7 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
80
79
  /**
81
80
  * Handles route rejection based on a specified rejection type.
82
81
  */
83
- reject: RouterReject<keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
82
+ reject: RouterReject<[...ExtractRejections<TOptions>, ...ExtractRejections<TPlugin>]>;
84
83
  /**
85
84
  * Forces the router to re-evaluate the current route.
86
85
  */
@@ -100,32 +99,32 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
100
99
  /**
101
100
  * Registers a hook to be called before a route is entered.
102
101
  */
103
- onBeforeRouteEnter: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
102
+ onBeforeRouteEnter: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
104
103
  /**
105
104
  * Registers a hook to be called before a route is left.
106
105
  */
107
- onBeforeRouteLeave: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
106
+ onBeforeRouteLeave: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
108
107
  /**
109
108
  * Registers a hook to be called before a route is updated.
110
109
  */
111
- onBeforeRouteUpdate: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
110
+ onBeforeRouteUpdate: AddRouterBeforeRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
112
111
  /**
113
112
  * Registers a hook to be called after a route is entered.
114
113
  */
115
- onAfterRouteEnter: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
114
+ onAfterRouteEnter: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
116
115
  /**
117
116
  * Registers a hook to be called after a route is left.
118
117
  */
119
- onAfterRouteLeave: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
118
+ onAfterRouteLeave: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
120
119
  /**
121
120
  * Registers a hook to be called after a route is updated.
122
121
  */
123
- onAfterRouteUpdate: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
122
+ onAfterRouteUpdate: AddRouterAfterRouteHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
124
123
  /**
125
124
  * Registers a hook to be called when an error occurs.
126
125
  * If the hook returns true, the error is considered handled and the other hooks are not run. If all hooks return false the error is rethrown
127
126
  */
128
- onError: AddRouterErrorHook<TRoutes | TPlugin['routes'], keyof TOptions['rejections'] | KeysOfUnion<TPlugin['rejections']>>;
127
+ onError: AddRouterErrorHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
129
128
  /**
130
129
  * Given a URL, returns true if host does not match host stored on router instance
131
130
  */
@@ -153,55 +152,12 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
153
152
  */
154
153
  key: InjectionKey<Router<TRoutes, TOptions, TPlugin>>;
155
154
  };
156
- type RouterHookContext<TRoutes extends Routes, TRejections extends PropertyKey> = {
157
- from: RouterResolvedRouteUnion<TRoutes> | null;
158
- reject: RouterReject<TRejections>;
159
- push: RouterPush<TRoutes>;
160
- replace: RouterReplace<TRoutes>;
161
- };
162
- type RouterBeforeRouteHookContext<TRoutes extends Routes, TRejections extends PropertyKey> = RouterHookContext<TRoutes, TRejections> & {
163
- abort: CallbackContextAbort;
164
- };
165
- export type HookContext<TRoutes extends Routes> = {
166
- to: RouterResolvedRouteUnion<TRoutes>;
167
- from: RouterResolvedRouteUnion<TRoutes> | null;
168
- };
169
- export type RouterBeforeRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (to: RouterResolvedRouteUnion<TRoutes>, context: RouterBeforeRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
170
- export type AddRouterBeforeRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (hook: RouterBeforeRouteHook<TRoutes, TRejections>) => RouteHookRemove;
171
- type RouterAfterRouteHookContext<TRoutes extends Routes, TRejections extends PropertyKey> = RouterHookContext<TRoutes, TRejections>;
172
- export type RouterAfterRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (to: RouterResolvedRouteUnion<TRoutes>, context: RouterAfterRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
173
- export type AddRouterAfterRouteHook<TRoutes extends Routes, TRejections extends PropertyKey> = (hook: RouterAfterRouteHook<TRoutes, TRejections>) => RouteHookRemove;
174
- export type RouterRouteHookBeforeRunner<TRoutes extends Routes> = (context: HookContext<TRoutes>) => Promise<BeforeRouteHookResponse>;
175
- export type RouterRouteHookAfterRunner<TRoutes extends Routes> = (context: HookContext<TRoutes>) => Promise<AfterRouteHookResponse>;
176
- export type RouterErrorHookContext<TRoutes extends Routes, TRejections extends PropertyKey> = {
177
- to: RouterResolvedRouteUnion<TRoutes>;
178
- from: RouterResolvedRouteUnion<TRoutes> | null;
179
- source: 'props' | 'hook' | 'component';
180
- reject: RouterReject<TRejections>;
181
- push: RouterPush<TRoutes>;
182
- replace: RouterReplace<TRoutes>;
183
- };
184
- export type RouterErrorHook<TRoutes extends Routes, TRejections extends PropertyKey> = (error: unknown, context: RouterErrorHookContext<TRoutes, TRejections>) => void;
185
- export type AddRouterErrorHook<TRoutes extends Routes, TRejections extends PropertyKey> = (hook: RouterErrorHook<TRoutes, TRejections>) => RouteHookRemove;
186
- export type RouterRouteHookErrorRunnerContext<TRoutes extends Routes> = {
187
- to: RouterResolvedRouteUnion<TRoutes>;
188
- from: RouterResolvedRouteUnion<TRoutes> | null;
189
- source: 'props' | 'hook';
190
- };
191
- export type RouterRouteHookErrorRunner<TRoutes extends Routes> = (error: unknown, context: RouterRouteHookErrorRunnerContext<TRoutes>) => void;
192
155
  /**
193
156
  * This type is the same as `RouterRoute<ResolvedRoute<TRoutes[number]>>` while remaining distributive
194
157
  */
195
158
  export type RouterRouteUnion<TRoutes extends Routes> = {
196
159
  [K in keyof TRoutes]: RouterRoute<ResolvedRoute<TRoutes[K]>>;
197
160
  }[number];
198
- /**
199
- * This type is the same as `ResolvedRoute<TRoutes[number]>` while remaining distributive
200
- */
201
- type RouterResolvedRouteUnion<TRoutes extends Routes> = {
202
- [K in keyof TRoutes]: ResolvedRoute<TRoutes[K]>;
203
- }[number];
204
161
  export type RouterRoutes<TRouter extends Router> = TRouter extends Router<infer TRoutes extends Routes> ? TRoutes : Routes;
205
- export type RouterRejections<TRouter extends Router> = TRouter extends Router<any, infer TOptions extends RouterOptions, infer TPlugins extends RouterPlugin> ? keyof TOptions['rejections'] | KeysOfUnion<TPlugins['rejections']> : PropertyKey;
162
+ export type RouterRejections<TRouter extends Router> = TRouter extends Router<any, infer TOptions extends RouterOptions, infer TPlugins extends RouterPlugin> ? ExtractRejections<TOptions> | ExtractRejections<TPlugins> : [];
206
163
  export type RouterRouteName<TRouter extends Router> = TRouter extends Router<infer TRoutes extends Routes> ? RoutesName<TRoutes> : RoutesName<Route[]>;
207
- export {};
@@ -1,18 +1,114 @@
1
- import { Component } from 'vue';
2
- import { BeforeRouteHook, AfterRouteHook } from './hooks';
1
+ import { BeforeRouteHook, AfterRouteHook, RouteHookRemove } from './hooks';
3
2
  import { Routes } from './route';
4
- import { MaybeArray } from './utilities';
5
- export type EmptyRouterPlugin = {
6
- routes: [];
7
- rejections: {};
8
- };
9
- export type RouterPlugin<TRoutes extends Routes = Routes, TRejections extends Record<string, Component> = Record<string, Component>> = {
10
- routes: TRoutes;
11
- rejections: TRejections;
3
+ import { MaybeArray, MaybePromise } from './utilities';
4
+ import { Rejection } from './rejection';
5
+ import { RouterRouteHooks } from '../models/RouterRouteHooks';
6
+ import { ResolvedRoute } from './resolved';
7
+ import { RouterReject } from './routerReject';
8
+ import { RouterPush } from './routerPush';
9
+ import { RouterReplace } from './routerReplace';
10
+ import { CallbackContextAbort } from '../services/createRouterCallbackContext';
11
+ export type EmptyRouterPlugin = RouterPlugin<[], []>;
12
+ export type CreateRouterPluginOptions<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
13
+ routes?: TRoutes;
14
+ rejections?: TRejections;
15
+ /**
16
+ * @deprecated use plugin.onBeforeRouteEnter instead
17
+ */
12
18
  onBeforeRouteEnter?: MaybeArray<BeforeRouteHook>;
19
+ /**
20
+ * @deprecated use plugin.onAfterRouteEnter instead
21
+ */
13
22
  onAfterRouteEnter?: MaybeArray<AfterRouteHook>;
23
+ /**
24
+ * @deprecated use plugin.onBeforeRouteUpdate instead
25
+ */
14
26
  onBeforeRouteUpdate?: MaybeArray<BeforeRouteHook>;
27
+ /**
28
+ * @deprecated use plugin.onAfterRouteUpdate instead
29
+ */
15
30
  onAfterRouteUpdate?: MaybeArray<AfterRouteHook>;
31
+ /**
32
+ * @deprecated use plugin.onBeforeRouteLeave instead
33
+ */
16
34
  onBeforeRouteLeave?: MaybeArray<BeforeRouteHook>;
35
+ /**
36
+ * @deprecated use plugin.onAfterRouteLeave instead
37
+ */
17
38
  onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
18
39
  };
40
+ export type RouterPlugin<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
41
+ /**
42
+ * The routes supplied by the plugin.
43
+ * @internal
44
+ */
45
+ routes: TRoutes;
46
+ /**
47
+ * The rejections supplied by the plugin.
48
+ * @internal
49
+ */
50
+ rejections: TRejections;
51
+ /**
52
+ * The hooks supplied by the plugin.
53
+ * @internal
54
+ */
55
+ hooks: RouterRouteHooks;
56
+ };
57
+ type PluginBeforeRouteHookContext<TRoutes extends Routes, TRejections extends Rejection[]> = {
58
+ from: ResolvedRoute | null;
59
+ reject: RouterReject<TRejections>;
60
+ push: RouterPush<TRoutes>;
61
+ replace: RouterReplace<TRoutes>;
62
+ abort: CallbackContextAbort;
63
+ };
64
+ type PluginAfterRouteHookContext<TRoutes extends Routes, TRejections extends Rejection[]> = {
65
+ from: ResolvedRoute | null;
66
+ reject: RouterReject<TRejections>;
67
+ push: RouterPush<TRoutes>;
68
+ replace: RouterReplace<TRoutes>;
69
+ };
70
+ export type PluginBeforeRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (to: ResolvedRoute, context: PluginBeforeRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
71
+ export type PluginAfterRouteHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (to: ResolvedRoute, context: PluginAfterRouteHookContext<TRoutes, TRejections>) => MaybePromise<void>;
72
+ type AddPluginBeforeRouteHook<TRoutes extends Routes, TRejections extends Rejection[]> = (hook: PluginBeforeRouteHook<TRoutes, TRejections>) => RouteHookRemove;
73
+ type AddPluginAfterRouteHook<TRoutes extends Routes, TRejections extends Rejection[]> = (hook: PluginAfterRouteHook<TRoutes, TRejections>) => RouteHookRemove;
74
+ export type PluginErrorHookContext<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
75
+ to: ResolvedRoute;
76
+ from: ResolvedRoute | null;
77
+ source: 'props' | 'hook' | 'component';
78
+ reject: RouterReject<TRejections>;
79
+ push: RouterPush<TRoutes>;
80
+ replace: RouterReplace<TRoutes>;
81
+ };
82
+ export type PluginErrorHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (error: unknown, context: PluginErrorHookContext<TRoutes, TRejections>) => void;
83
+ export type AddPluginErrorHook<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = (hook: PluginErrorHook<TRoutes, TRejections>) => RouteHookRemove;
84
+ export type PluginRouteHooks<TRoutes extends Routes = Routes, TRejections extends Rejection[] = Rejection[]> = {
85
+ /**
86
+ * Registers a global hook to be called before a route is entered.
87
+ */
88
+ onBeforeRouteEnter: AddPluginBeforeRouteHook<TRoutes, TRejections>;
89
+ /**
90
+ * Registers a global hook to be called before a route is left.
91
+ */
92
+ onBeforeRouteLeave: AddPluginBeforeRouteHook<TRoutes, TRejections>;
93
+ /**
94
+ * Registers a global hook to be called before a route is updated.
95
+ */
96
+ onBeforeRouteUpdate: AddPluginBeforeRouteHook<TRoutes, TRejections>;
97
+ /**
98
+ * Registers a global hook to be called after a route is entered.
99
+ */
100
+ onAfterRouteEnter: AddPluginAfterRouteHook<TRoutes, TRejections>;
101
+ /**
102
+ * Registers a global hook to be called after a route is left.
103
+ */
104
+ onAfterRouteLeave: AddPluginAfterRouteHook<TRoutes, TRejections>;
105
+ /**
106
+ * Registers a global hook to be called after a route is updated.
107
+ */
108
+ onAfterRouteUpdate: AddPluginAfterRouteHook<TRoutes, TRejections>;
109
+ /**
110
+ * Registers a global hook to be called when an error occurs.
111
+ */
112
+ onError: AddPluginErrorHook<TRoutes, TRejections>;
113
+ };
114
+ export {};
@@ -1,3 +1,3 @@
1
1
  import { BuiltInRejectionType } from '../services/createRouterReject';
2
- import { AsString } from './utilities';
3
- export type RouterReject<TRejectionType extends PropertyKey> = (type: AsString<TRejectionType> | BuiltInRejectionType) => void;
2
+ import { Rejection, RejectionType } from './rejection';
3
+ export type RouterReject<TRejections extends Rejection[] | undefined> = <TSource extends (RejectionType<TRejections> | BuiltInRejectionType)>(type: TSource) => void;