@kitbag/router 0.20.12 → 0.21.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 +29 -87
- package/dist/components/routerLink.d.ts +3 -3
- package/dist/errors/contextAbortError.d.ts +2 -2
- package/dist/errors/contextPushError.d.ts +2 -2
- package/dist/errors/contextRejectionError.d.ts +2 -2
- package/dist/keys.d.ts +1 -3
- package/dist/kitbag-router.js +1787 -1853
- package/dist/kitbag-router.umd.cjs +3 -3
- package/dist/main.d.ts +0 -16
- package/dist/models/hooks.d.ts +10 -0
- package/dist/services/createComponentHooks.d.ts +7 -7
- package/dist/services/createExternalRoute.d.ts +3 -2
- package/dist/services/createHooksFactory.d.ts +15 -0
- package/dist/services/createPropStore.d.ts +2 -2
- package/dist/services/createRoute.d.ts +2 -1
- package/dist/services/createRouterAssets.d.ts +5 -5
- package/dist/services/createRouterCallbackContext.d.ts +8 -6
- package/dist/services/createRouterHooks.d.ts +14 -14
- package/dist/services/createRouterPlugin.d.ts +2 -2
- package/dist/services/createUrlParts.d.ts +13 -0
- package/dist/services/getGlobalHooksForRouter.d.ts +2 -3
- package/dist/services/getGlobalRouteHooks.d.ts +3 -3
- package/dist/services/getRouteHooks.d.ts +3 -3
- package/dist/services/hooks.d.ts +2 -2
- package/dist/services/urlCombine.d.ts +2 -1
- package/dist/services/urlCreator.d.ts +2 -1
- package/dist/services/urlParser.d.ts +1 -1
- package/dist/types/callbackContext.d.ts +15 -0
- package/dist/types/createRouteOptions.d.ts +6 -13
- package/dist/types/hooks.d.ts +56 -136
- package/dist/types/params.d.ts +11 -5
- package/dist/types/props.d.ts +8 -8
- package/dist/types/register.d.ts +1 -41
- package/dist/types/rejection.d.ts +6 -2
- package/dist/types/resolved.d.ts +2 -2
- package/dist/types/route.d.ts +5 -4
- package/dist/types/routeContext.d.ts +11 -5
- package/dist/types/router.d.ts +11 -11
- package/dist/types/routerAbort.d.ts +1 -0
- package/dist/types/routerPlugin.d.ts +17 -41
- package/dist/types/routerReject.d.ts +2 -2
- package/dist/types/url.d.ts +0 -9
- package/dist/types/urlParts.d.ts +53 -0
- package/dist/utilities/makeOptional.d.ts +1 -0
- package/dist/utilities/testHelpers.d.ts +279 -11
- package/package.json +4 -2
- package/dist/models/RouteHooks.d.ts +0 -12
- package/dist/models/RouterRouteHooks.d.ts +0 -10
- package/dist/services/createCallbackContext.d.ts +0 -44
- package/dist/services/createRouteHooks.d.ts +0 -15
- package/dist/services/getRouteHooksDeprecated.d.ts +0 -10
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RouterPush } from './routerPush';
|
|
2
|
+
export type CallbackContextSuccess = {
|
|
3
|
+
status: 'SUCCESS';
|
|
4
|
+
};
|
|
5
|
+
export type CallbackContextPush = {
|
|
6
|
+
status: 'PUSH';
|
|
7
|
+
to: Parameters<RouterPush>;
|
|
8
|
+
};
|
|
9
|
+
export type CallbackContextReject = {
|
|
10
|
+
status: 'REJECT';
|
|
11
|
+
type: string;
|
|
12
|
+
};
|
|
13
|
+
export type CallbackContextAbort = {
|
|
14
|
+
status: 'ABORT';
|
|
15
|
+
};
|
|
@@ -15,7 +15,6 @@ import { MaybePromise } from './utilities';
|
|
|
15
15
|
import { ToMeta } from './meta';
|
|
16
16
|
import { ToState } from './state';
|
|
17
17
|
import { ToName } from './name';
|
|
18
|
-
import { WithHooks } from './hooks';
|
|
19
18
|
import { ToWithParams, WithParams } from '../services/withParams';
|
|
20
19
|
import { RouteContext, ToRouteContext } from './routeContext';
|
|
21
20
|
import { RouterViewProps } from '../components/routerView';
|
|
@@ -47,7 +46,7 @@ export declare function isWithComponents<T extends Record<string, unknown>>(opti
|
|
|
47
46
|
export declare function isWithComponentPropsRecord<T extends Record<string, unknown>>(options: T): options is T & {
|
|
48
47
|
props: RoutePropsRecord;
|
|
49
48
|
};
|
|
50
|
-
export type CreateRouteOptions<TName extends string | undefined = string | undefined, TPath extends string | WithParams | undefined = string | WithParams | undefined, TQuery extends string | WithParams | undefined = string | WithParams | undefined, THash extends string | WithParams | undefined = string | WithParams | undefined, TMeta extends RouteMeta = RouteMeta> =
|
|
49
|
+
export type CreateRouteOptions<TName extends string | undefined = string | undefined, TPath extends string | WithParams | undefined = string | WithParams | undefined, TQuery extends string | WithParams | undefined = string | WithParams | undefined, THash extends string | WithParams | undefined = string | WithParams | undefined, TMeta extends RouteMeta = RouteMeta> = {
|
|
51
50
|
/**
|
|
52
51
|
* Name for route, used to create route keys and in navigation.
|
|
53
52
|
*/
|
|
@@ -94,15 +93,9 @@ export type CreateRouteOptions<TName extends string | undefined = string | undef
|
|
|
94
93
|
* Related routes and rejections for the route. The context is exposed to the hooks and props callback functions for this route.
|
|
95
94
|
*/
|
|
96
95
|
context?: RouteContext[];
|
|
97
|
-
/**
|
|
98
|
-
* Props have been moved to the second argument of `createRoute`. This property can no longer be used.
|
|
99
|
-
*
|
|
100
|
-
* @deprecated
|
|
101
|
-
*/
|
|
102
|
-
props?: never;
|
|
103
96
|
};
|
|
104
|
-
export type PropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponent extends Component = Component> = (route: ResolvedRoute<ToRoute<TOptions
|
|
105
|
-
export type RouterViewPropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions> = (route: ResolvedRoute<ToRoute<TOptions
|
|
97
|
+
export type PropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponent extends Component = Component> = (route: ResolvedRoute<ToRoute<TOptions>>, context: PropsCallbackContext<ToRoute<TOptions>, TOptions>) => MaybePromise<ComponentProps<TComponent>>;
|
|
98
|
+
export type RouterViewPropsGetter<TOptions extends CreateRouteOptions = CreateRouteOptions> = (route: ResolvedRoute<ToRoute<TOptions>>, context: PropsCallbackContext<ToRoute<TOptions>, TOptions>) => MaybePromise<RouterViewProps & Record<string, unknown>>;
|
|
106
99
|
type ComponentPropsAreOptional<TComponent extends Component> = Partial<ComponentProps<TComponent>> extends ComponentProps<TComponent> ? true : false;
|
|
107
100
|
type RoutePropsRecord<TOptions extends CreateRouteOptions = CreateRouteOptions, TComponents extends Record<string, Component> = Record<string, Component>> = {
|
|
108
101
|
[K in keyof TComponents as ComponentPropsAreOptional<TComponents[K]> extends true ? K : never]?: PropsGetter<TOptions, TComponents[K]>;
|
|
@@ -121,13 +114,13 @@ type ToMatch<TOptions extends CreateRouteOptions, TProps extends CreateRouteProp
|
|
|
121
114
|
type ToMatches<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = TOptions extends {
|
|
122
115
|
parent: infer TParent extends Route;
|
|
123
116
|
} ? [...TParent['matches'], ToMatch<TOptions, TProps>] : [ToMatch<TOptions, TProps>];
|
|
124
|
-
export type ToRoute<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined> = CreateRouteOptions extends TOptions ? Route : TOptions extends {
|
|
117
|
+
export type ToRoute<TOptions extends CreateRouteOptions, TProps extends CreateRouteProps<TOptions> | undefined = undefined> = CreateRouteOptions extends TOptions ? Route : TOptions extends {
|
|
125
118
|
parent: infer TParent extends Route;
|
|
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>, [
|
|
119
|
+
} ? 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, CreateRouteProps<TOptions> extends TProps ? undefined : TProps>, [
|
|
127
120
|
...ToRouteContext<TParent['context']>,
|
|
128
121
|
...ToRouteContext<TOptions['context']>
|
|
129
122
|
]> : Route<ToName<TOptions['name']>, TOptions extends {
|
|
130
123
|
host: string | WithParams;
|
|
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']>>;
|
|
124
|
+
} ? ToWithParams<TOptions['host']> : WithParams<'', {}>, ToWithParams<TOptions['path']>, ToWithParams<TOptions['query']>, ToWithParams<TOptions['hash']>, ToMeta<TOptions['meta']>, ToState<TOptions['state']>, ToMatches<TOptions, CreateRouteProps<TOptions> extends TProps ? undefined : TProps>, ToRouteContext<TOptions['context']>>;
|
|
132
125
|
export declare function combineRoutes(parent: Route, child: Route): Route;
|
|
133
126
|
export {};
|
package/dist/types/hooks.d.ts
CHANGED
|
@@ -1,201 +1,121 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CallbackAbortResponse, CallbackContext, CallbackContextAbort, CallbackPushResponse, CallbackRejectResponse, CallbackSuccessResponse } from '../services/createCallbackContext';
|
|
1
|
+
import { Hooks } from '../models/hooks';
|
|
3
2
|
import { ResolvedRoute, RouterResolvedRouteUnion } from './resolved';
|
|
4
|
-
import {
|
|
5
|
-
import { Routes } from './route';
|
|
3
|
+
import { MaybePromise } from './utilities';
|
|
4
|
+
import { Route, Routes } from './route';
|
|
6
5
|
import { RouterReject } from './routerReject';
|
|
7
6
|
import { RouterPush } from './routerPush';
|
|
8
7
|
import { RouterReplace } from './routerReplace';
|
|
9
|
-
import {
|
|
8
|
+
import { Rejections } from './rejection';
|
|
10
9
|
import { RouteContext, RouteContextToRejection, RouteContextToRoute } from './routeContext';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export type
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Use router.onBeforeRouteEnter instead
|
|
17
|
-
*/
|
|
18
|
-
onBeforeRouteEnter?: MaybeArray<BeforeRouteHook>;
|
|
19
|
-
/**
|
|
20
|
-
* @deprecated Use router.onBeforeRouteUpdate instead
|
|
21
|
-
*/
|
|
22
|
-
onBeforeRouteUpdate?: MaybeArray<BeforeRouteHook>;
|
|
23
|
-
/**
|
|
24
|
-
* @deprecated Use router.onBeforeRouteLeave instead
|
|
25
|
-
*/
|
|
26
|
-
onBeforeRouteLeave?: MaybeArray<BeforeRouteHook>;
|
|
27
|
-
/**
|
|
28
|
-
* @deprecated Use router.onAfterRouteEnter instead
|
|
29
|
-
*/
|
|
30
|
-
onAfterRouteEnter?: MaybeArray<AfterRouteHook>;
|
|
31
|
-
/**
|
|
32
|
-
* @deprecated Use router.onAfterRouteUpdate instead
|
|
33
|
-
*/
|
|
34
|
-
onAfterRouteUpdate?: MaybeArray<AfterRouteHook>;
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use router.onAfterRouteLeave instead
|
|
37
|
-
*/
|
|
38
|
-
onAfterRouteLeave?: MaybeArray<AfterRouteHook>;
|
|
39
|
-
};
|
|
40
|
-
export type InternalRouteHooks<TContext extends RouteContext[] | undefined = undefined> = {
|
|
10
|
+
import { RouterAbort } from './routerAbort';
|
|
11
|
+
import { CallbackContextAbort, CallbackContextPush, CallbackContextReject, CallbackContextSuccess } from './callbackContext';
|
|
12
|
+
import { RouteUpdate } from './routeUpdate';
|
|
13
|
+
export type InternalRouteHooks<TRoute extends Route = Route, TContext extends RouteContext[] | undefined = undefined> = {
|
|
41
14
|
/**
|
|
42
15
|
* Registers a route hook to be called before the route is entered.
|
|
43
16
|
*/
|
|
44
|
-
onBeforeRouteEnter:
|
|
17
|
+
onBeforeRouteEnter: AddBeforeHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
45
18
|
/**
|
|
46
19
|
* Registers a route hook to be called before the route is left.
|
|
47
20
|
*/
|
|
48
|
-
onBeforeRouteLeave:
|
|
21
|
+
onBeforeRouteLeave: AddBeforeHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
49
22
|
/**
|
|
50
23
|
* Registers a route hook to be called before the route is updated.
|
|
51
24
|
*/
|
|
52
|
-
onBeforeRouteUpdate:
|
|
25
|
+
onBeforeRouteUpdate: AddBeforeHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
53
26
|
/**
|
|
54
27
|
* Registers a route hook to be called after the route is entered.
|
|
55
28
|
*/
|
|
56
|
-
onAfterRouteEnter:
|
|
29
|
+
onAfterRouteEnter: AddAfterHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
57
30
|
/**
|
|
58
31
|
* Registers a route hook to be called after the route is left.
|
|
59
32
|
*/
|
|
60
|
-
onAfterRouteLeave:
|
|
33
|
+
onAfterRouteLeave: AddAfterHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
61
34
|
/**
|
|
62
35
|
* Registers a route hook to be called after the route is updated.
|
|
63
36
|
*/
|
|
64
|
-
onAfterRouteUpdate:
|
|
37
|
+
onAfterRouteUpdate: AddAfterHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
65
38
|
};
|
|
66
|
-
export type ExternalRouteHooks<TContext extends RouteContext[] | undefined = undefined> = {
|
|
39
|
+
export type ExternalRouteHooks<TRoute extends Route = Route, TContext extends RouteContext[] | undefined = undefined> = {
|
|
67
40
|
/**
|
|
68
41
|
* Registers a route hook to be called before the route is entered.
|
|
69
42
|
*/
|
|
70
|
-
onBeforeRouteEnter:
|
|
71
|
-
};
|
|
72
|
-
export type BeforeHookContext = {
|
|
73
|
-
to: ResolvedRoute;
|
|
74
|
-
from: ResolvedRoute | null;
|
|
75
|
-
};
|
|
76
|
-
export type RouteHookBeforeRunner = (context: BeforeHookContext) => Promise<BeforeRouteHookResponse>;
|
|
77
|
-
export type AfterHookContext = {
|
|
78
|
-
to: ResolvedRoute;
|
|
79
|
-
from: ResolvedRoute | null;
|
|
43
|
+
onBeforeRouteEnter: AddBeforeHook<TRoute, [TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>>;
|
|
80
44
|
};
|
|
81
|
-
export type
|
|
82
|
-
|
|
83
|
-
type BeforeRouteHookRegistration<TRoutes extends Routes, TRejections extends Rejection[]> = {
|
|
45
|
+
export type HookTiming = 'global' | 'component';
|
|
46
|
+
type BeforeHookRegistration<TRoute extends Route, TRoutes extends Routes, TRejections extends Rejections> = {
|
|
84
47
|
lifecycle: 'onBeforeRouteEnter' | 'onBeforeRouteUpdate' | 'onBeforeRouteLeave';
|
|
85
|
-
hook:
|
|
48
|
+
hook: BeforeHook<TRoute, TRoutes, TRejections>;
|
|
86
49
|
depth: number;
|
|
87
50
|
};
|
|
88
|
-
export type
|
|
89
|
-
type
|
|
51
|
+
export type AddComponentBeforeHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (hook: BeforeHookRegistration<TRoute, TRoutes, TRejections>) => HookRemove;
|
|
52
|
+
type AfterHookRegistration<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = {
|
|
90
53
|
lifecycle: 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'onAfterRouteLeave';
|
|
91
|
-
hook:
|
|
54
|
+
hook: AfterHook<TRoute, TRoutes, TRejections>;
|
|
92
55
|
depth: number;
|
|
93
56
|
};
|
|
94
|
-
export type
|
|
95
|
-
export type
|
|
96
|
-
/**
|
|
97
|
-
* Context provided to route hooks, containing context of previous route and functions for triggering rejections and push/replace to another route.
|
|
98
|
-
*/
|
|
99
|
-
type RouteHookContext = {
|
|
100
|
-
from: ResolvedRoute | null;
|
|
101
|
-
reject: CallbackContext['reject'];
|
|
102
|
-
push: CallbackContext['push'];
|
|
103
|
-
replace: CallbackContext['replace'];
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* Context provided to route hooks, containing context of previous route and functions for triggering rejections, push/replace to another route,
|
|
107
|
-
* as well as aborting current route change.
|
|
108
|
-
*/
|
|
109
|
-
export type BeforeRouteHookContext = RouteHookContext & {
|
|
110
|
-
abort: CallbackContextAbort;
|
|
111
|
-
};
|
|
112
|
-
/**
|
|
113
|
-
* Context provided to route hooks, containing context of previous route and functions for triggering rejections and push/replace to another route.
|
|
114
|
-
*/
|
|
115
|
-
export type AfterRouteHookContext = RouteHookContext;
|
|
57
|
+
export type AddComponentAfterHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (hook: AfterHookRegistration<TRoute, TRoutes, TRejections>) => HookRemove;
|
|
58
|
+
export type AddGlobalHooks = (hooks: Hooks) => void;
|
|
116
59
|
/**
|
|
117
|
-
*
|
|
118
|
-
* @param to - {@link ResolvedRoute} The resolved route the router is navigating to.
|
|
119
|
-
* @param context - {@link BeforeRouteHookContext} The context providing functions and state for the routing operation.
|
|
120
|
-
* @returns Possibly a promise that resolves when the hook's logic has completed.
|
|
60
|
+
* A function to remove a previously added route hook.
|
|
121
61
|
*/
|
|
122
|
-
export type
|
|
123
|
-
/**
|
|
124
|
-
* Represents a function called after a route change has occurred.
|
|
125
|
-
* @param to - {@link ResolvedRoute} The resolved route the router has navigated to.
|
|
126
|
-
* @param context - {@link AfterRouteHookContext} The context providing functions and state for the routing operation.
|
|
127
|
-
* @returns Possibly a promise that resolves when the hook's logic has completed.
|
|
128
|
-
*/
|
|
129
|
-
export type AfterRouteHook = (to: ResolvedRoute, context: AfterRouteHookContext) => MaybePromise<void>;
|
|
130
|
-
/**
|
|
131
|
-
* Generic type representing a route hook, which can be either before or after a route change.
|
|
132
|
-
*/
|
|
133
|
-
export type RouteHook = BeforeRouteHook | AfterRouteHook;
|
|
134
|
-
/**
|
|
135
|
-
* A function to remove a previously registered route hook.
|
|
136
|
-
*/
|
|
137
|
-
export type RouteHookRemove = () => void;
|
|
62
|
+
export type HookRemove = () => void;
|
|
138
63
|
/**
|
|
139
64
|
* Enumerates the lifecycle events for before route hooks.
|
|
140
65
|
*/
|
|
141
|
-
export type
|
|
66
|
+
export type BeforeHookLifecycle = 'onBeforeRouteEnter' | 'onBeforeRouteUpdate' | 'onBeforeRouteLeave';
|
|
142
67
|
/**
|
|
143
68
|
* Enumerates the lifecycle events for after route hooks.
|
|
144
69
|
*/
|
|
145
|
-
export type
|
|
70
|
+
export type AfterHookLifecycle = 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'onAfterRouteLeave';
|
|
146
71
|
/**
|
|
147
72
|
* Union type for all route hook lifecycle events.
|
|
148
73
|
*/
|
|
149
|
-
export type
|
|
150
|
-
|
|
151
|
-
* Type for responses from a before route hook, which may indicate different outcomes such as success, push, reject, or abort.
|
|
152
|
-
* @template TRoutes - The type of the routes configuration.
|
|
153
|
-
*/
|
|
154
|
-
export type BeforeRouteHookResponse = CallbackSuccessResponse | CallbackPushResponse | CallbackRejectResponse | CallbackAbortResponse;
|
|
155
|
-
/**
|
|
156
|
-
* Type for responses from an after route hook, which may indicate different outcomes such as success, push, or reject.
|
|
157
|
-
* @template TRoutes - The type of the routes configuration.
|
|
158
|
-
*/
|
|
159
|
-
export type AfterRouteHookResponse = CallbackSuccessResponse | CallbackPushResponse | CallbackRejectResponse;
|
|
160
|
-
/**
|
|
161
|
-
* Union type for all possible route hook responses, covering both before and after scenarios.
|
|
162
|
-
* @template TRoutes - The type of the routes configuration.
|
|
163
|
-
*/
|
|
164
|
-
export type RouteHookResponse = BeforeRouteHookResponse | AfterRouteHookResponse;
|
|
165
|
-
type RouterHookContext<TRoutes extends Routes, TRejections extends Rejection[]> = {
|
|
74
|
+
export type HookLifecycle = BeforeHookLifecycle | AfterHookLifecycle;
|
|
75
|
+
type AfterHookContext<TRoute extends Route, TRoutes extends Routes, TRejections extends Rejections> = {
|
|
166
76
|
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
167
77
|
reject: RouterReject<TRejections>;
|
|
168
78
|
push: RouterPush<TRoutes>;
|
|
169
79
|
replace: RouterReplace<TRoutes>;
|
|
80
|
+
update: RouteUpdate<ResolvedRoute<TRoute>>;
|
|
170
81
|
};
|
|
171
|
-
type
|
|
172
|
-
|
|
82
|
+
type BeforeHookContext<TRoute extends Route, TRoutes extends Routes, TRejections extends Rejections> = {
|
|
83
|
+
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
84
|
+
reject: RouterReject<TRejections>;
|
|
85
|
+
push: RouterPush<TRoutes>;
|
|
86
|
+
replace: RouterReplace<TRoutes>;
|
|
87
|
+
update: RouteUpdate<ResolvedRoute<TRoute>>;
|
|
88
|
+
abort: RouterAbort;
|
|
173
89
|
};
|
|
174
|
-
export type
|
|
90
|
+
export type BeforeHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (to: RouterResolvedRouteUnion<TRoutes>, context: BeforeHookContext<TRoute, TRoutes, TRejections>) => MaybePromise<void>;
|
|
91
|
+
export type AddBeforeHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (hook: BeforeHook<TRoute, TRoutes, TRejections>) => HookRemove;
|
|
92
|
+
export type AfterHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (to: RouterResolvedRouteUnion<TRoutes>, context: AfterHookContext<TRoute, TRoutes, TRejections>) => MaybePromise<void>;
|
|
93
|
+
export type AddAfterHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (hook: AfterHook<TRoute, TRoutes, TRejections>) => HookRemove;
|
|
94
|
+
export type BeforeHookResponse = CallbackContextSuccess | CallbackContextPush | CallbackContextReject | CallbackContextAbort;
|
|
95
|
+
export type AfterHookResponse = CallbackContextSuccess | CallbackContextPush | CallbackContextReject;
|
|
96
|
+
export type BeforeHookRunner = <TRoutes extends Routes>(context: {
|
|
175
97
|
to: RouterResolvedRouteUnion<TRoutes>;
|
|
176
98
|
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
177
|
-
}
|
|
178
|
-
export type
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
export type
|
|
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[]> = {
|
|
99
|
+
}) => Promise<BeforeHookResponse>;
|
|
100
|
+
export type AfterHookRunner = <TRoutes extends Routes>(context: {
|
|
101
|
+
to: RouterResolvedRouteUnion<TRoutes>;
|
|
102
|
+
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
103
|
+
}) => Promise<AfterHookResponse>;
|
|
104
|
+
export type ErrorHookContext<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = {
|
|
186
105
|
to: RouterResolvedRouteUnion<TRoutes>;
|
|
187
106
|
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
188
107
|
source: 'props' | 'hook' | 'component';
|
|
189
108
|
reject: RouterReject<TRejections>;
|
|
190
109
|
push: RouterPush<TRoutes>;
|
|
191
110
|
replace: RouterReplace<TRoutes>;
|
|
111
|
+
update: RouteUpdate<ResolvedRoute<TRoute>>;
|
|
192
112
|
};
|
|
193
|
-
export type
|
|
194
|
-
export type
|
|
195
|
-
export type
|
|
113
|
+
export type ErrorHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (error: unknown, context: ErrorHookContext<TRoute, TRoutes, TRejections>) => void;
|
|
114
|
+
export type AddErrorHook<TRoute extends Route = Route, TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections> = (hook: ErrorHook<TRoute, TRoutes, TRejections>) => HookRemove;
|
|
115
|
+
export type ErrorHookRunnerContext<TRoutes extends Routes = Routes> = {
|
|
196
116
|
to: RouterResolvedRouteUnion<TRoutes>;
|
|
197
117
|
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
198
118
|
source: 'props' | 'hook';
|
|
199
119
|
};
|
|
200
|
-
export type
|
|
120
|
+
export type ErrorHookRunner = (error: unknown, context: ErrorHookRunnerContext) => void;
|
|
201
121
|
export {};
|
package/dist/types/params.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { LiteralParam, Param, ParamGetSet, ParamGetter } from './paramTypes';
|
|
2
2
|
import { Identity } from './utilities';
|
|
3
|
-
import { MakeOptional } from '../utilities/makeOptional';
|
|
4
|
-
import { Route } from './route';
|
|
3
|
+
import { MakeOptional, UnionToIntersection } from '../utilities/makeOptional';
|
|
5
4
|
import { WithParams } from '../services/withParams';
|
|
6
5
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
7
6
|
export declare const paramStart = "[";
|
|
@@ -32,19 +31,26 @@ export declare function isLiteralParam(value: Param): value is LiteralParam;
|
|
|
32
31
|
* @returns The extracted parameter name, or never if the parameter string is empty.
|
|
33
32
|
*/
|
|
34
33
|
export type ExtractParamName<TParam extends PropertyKey> = TParam extends string ? TParam extends `?${infer Param}` ? Param extends '' ? never : Param : TParam extends '' ? never : TParam : never;
|
|
34
|
+
type ExtractWithParams<TParts extends Record<string, unknown>> = {
|
|
35
|
+
[K in keyof TParts as TParts[K] extends WithParams ? K : never]: TParts[K] extends WithParams ? TParts[K] : never;
|
|
36
|
+
};
|
|
35
37
|
/**
|
|
36
38
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
37
|
-
* @template
|
|
39
|
+
* @template Parts - The route from which to extract and merge parameter types.
|
|
38
40
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
39
41
|
*/
|
|
40
|
-
export type ExtractRouteParamTypesReading<
|
|
42
|
+
export type ExtractRouteParamTypesReading<TParts extends Record<PropertyKey, unknown>> = Identity<MakeOptional<UnionToIntersection<{
|
|
43
|
+
[K in keyof ExtractWithParams<TParts>]: ExtractParamTypesReading<ExtractWithParams<TParts>[K]>;
|
|
44
|
+
}[keyof ExtractWithParams<TParts>]>>>;
|
|
41
45
|
/**
|
|
42
46
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
43
47
|
* Differs from ExtractRouteParamTypesReading in that optional params with defaults will remain optional.
|
|
44
48
|
* @template TRoute - The route type from which to extract and merge parameter types.
|
|
45
49
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
46
50
|
*/
|
|
47
|
-
export type ExtractRouteParamTypesWriting<
|
|
51
|
+
export type ExtractRouteParamTypesWriting<TParts extends Record<string, unknown>> = Identity<MakeOptional<UnionToIntersection<{
|
|
52
|
+
[K in keyof ExtractWithParams<TParts>]: ExtractParamTypesWriting<ExtractWithParams<TParts>[K]>;
|
|
53
|
+
}[keyof ExtractWithParams<TParts>]>>>;
|
|
48
54
|
/**
|
|
49
55
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
50
56
|
* @template TParams - The record of parameter types, possibly including undefined.
|
package/dist/types/props.d.ts
CHANGED
|
@@ -3,19 +3,19 @@ import { Route } from './route';
|
|
|
3
3
|
import { RouterReject } from './routerReject';
|
|
4
4
|
import { RouterPush } from './routerPush';
|
|
5
5
|
import { RouterReplace } from './routerReplace';
|
|
6
|
-
import {
|
|
6
|
+
import { ExtractRouteContextRejections, ExtractRouteContextRoutes } from './routeContext';
|
|
7
|
+
import { ResolvedRoute } from './resolved';
|
|
8
|
+
import { RouteUpdate } from './routeUpdate';
|
|
7
9
|
/**
|
|
8
10
|
* Context provided to props callback functions
|
|
9
11
|
*/
|
|
10
|
-
export type PropsCallbackContext<TOptions extends CreateRouteOptions = CreateRouteOptions> = {
|
|
11
|
-
reject: RouterReject<
|
|
12
|
-
push: RouterPush<
|
|
13
|
-
replace: RouterReplace<
|
|
12
|
+
export type PropsCallbackContext<TRoute extends Route = Route, TOptions extends CreateRouteOptions = CreateRouteOptions> = {
|
|
13
|
+
reject: RouterReject<ExtractRouteContextRejections<TOptions>>;
|
|
14
|
+
push: RouterPush<[TRoute] | ExtractRouteContextRoutes<TOptions>>;
|
|
15
|
+
replace: RouterReplace<[TRoute] | ExtractRouteContextRoutes<TOptions>>;
|
|
16
|
+
update: RouteUpdate<ResolvedRoute<TRoute>>;
|
|
14
17
|
parent: PropsCallbackParent<TOptions['parent']>;
|
|
15
18
|
};
|
|
16
|
-
type ExtractRouteContext<TOptions extends CreateRouteOptions> = TOptions extends {
|
|
17
|
-
context: infer TContext extends RouteContext[];
|
|
18
|
-
} ? TContext : [];
|
|
19
19
|
export type PropsCallbackParent<TParent extends Route | undefined = Route | undefined> = Route | undefined extends TParent ? undefined | {
|
|
20
20
|
name: string;
|
|
21
21
|
props: unknown;
|
package/dist/types/register.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Route, Routes } from './route';
|
|
3
|
-
import { Router, RouterOptions } from './router';
|
|
4
|
-
import { RouterPush } from './routerPush';
|
|
5
|
-
import { RouterReplace } from './routerReplace';
|
|
6
|
-
import { RoutesName } from './routesMap';
|
|
7
|
-
import { RejectionType } from './rejection';
|
|
1
|
+
import { Router } from './router';
|
|
8
2
|
/**
|
|
9
3
|
* Represents the state of currently registered router, and route meta. Used to provide correct type context for
|
|
10
4
|
* components like `RouterLink`, as well as for composables like `useRouter`, `useRoute`, and hooks.
|
|
@@ -27,43 +21,9 @@ export interface Register {
|
|
|
27
21
|
export type RegisteredRouter<T = Register> = T extends {
|
|
28
22
|
router: infer TRouter;
|
|
29
23
|
} ? TRouter : Router;
|
|
30
|
-
/**
|
|
31
|
-
* Represents the Router routes property within {@link Register}
|
|
32
|
-
* @deprecated will be removed in a future version
|
|
33
|
-
*/
|
|
34
|
-
export type RegisteredRoutes<T = Register> = T extends {
|
|
35
|
-
router: Router<infer TRoutes extends Routes>;
|
|
36
|
-
} ? TRoutes : Route[];
|
|
37
|
-
/**
|
|
38
|
-
* Represents the possible Rejections registered within {@link Register}
|
|
39
|
-
* @deprecated use `createRouter(..., { rejections: {}}) instead
|
|
40
|
-
*/
|
|
41
|
-
export type RegisteredRejectionType<T = Register> = T extends {
|
|
42
|
-
router: Router<any, infer TOptions extends RouterOptions>;
|
|
43
|
-
} ? RejectionType<TOptions['rejections']> | BuiltInRejectionType : BuiltInRejectionType;
|
|
44
24
|
/**
|
|
45
25
|
* Represents additional metadata associated with a route, customizable via declaration merging.
|
|
46
26
|
*/
|
|
47
27
|
export type RouteMeta<T = Register> = T extends {
|
|
48
28
|
routeMeta: infer RouteMeta extends Record<string, unknown>;
|
|
49
29
|
} ? RouteMeta : Record<string, unknown>;
|
|
50
|
-
/**
|
|
51
|
-
* Represents the type for router `push`, with types for routes registered within {@link Register}
|
|
52
|
-
* @deprecated will be removed in a future version
|
|
53
|
-
*/
|
|
54
|
-
export type RegisteredRouterPush = RouterPush<RegisteredRoutes>;
|
|
55
|
-
/**
|
|
56
|
-
* Represents the type for router `replace`, with types for routes registered within {@link Register}
|
|
57
|
-
* @deprecated will be removed in a future version
|
|
58
|
-
*/
|
|
59
|
-
export type RegisteredRouterReplace = RouterReplace<RegisteredRoutes>;
|
|
60
|
-
/**
|
|
61
|
-
* Type for Router Reject method. Triggers rejections registered within {@link Register}
|
|
62
|
-
* @deprecated will be removed in a future version
|
|
63
|
-
*/
|
|
64
|
-
export type RegisteredRouterReject = (type: RegisteredRejectionType) => void;
|
|
65
|
-
/**
|
|
66
|
-
* Represents the union of all possible route names registered within {@link Register}
|
|
67
|
-
* @deprecated will be removed in a future version
|
|
68
|
-
*/
|
|
69
|
-
export type RegisteredRoutesName = RoutesName<RegisteredRoutes>;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { Component } from 'vue';
|
|
2
|
+
/**
|
|
3
|
+
* Represents an immutable array of Rejection instances.
|
|
4
|
+
*/
|
|
5
|
+
export type Rejections = readonly Rejection[];
|
|
2
6
|
export type Rejection<TType extends string = string> = {
|
|
3
7
|
/**
|
|
4
8
|
* The type of rejection.
|
|
@@ -9,7 +13,7 @@ export type Rejection<TType extends string = string> = {
|
|
|
9
13
|
*/
|
|
10
14
|
component: Component;
|
|
11
15
|
};
|
|
12
|
-
export type RejectionType<TRejections extends
|
|
16
|
+
export type RejectionType<TRejections extends Rejections | undefined> = unknown extends TRejections ? never : Rejections extends TRejections ? string : undefined extends TRejections ? string : TRejections extends Rejections ? TRejections[number]['type'] : never;
|
|
13
17
|
export type ExtractRejections<T> = T extends {
|
|
14
|
-
rejections: infer TRejections extends
|
|
18
|
+
rejections: infer TRejections extends Rejections;
|
|
15
19
|
} ? TRejections : [];
|
package/dist/types/resolved.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Hooks } from '../models/hooks';
|
|
2
2
|
import { ExtractRouteParamTypesReading } from './params';
|
|
3
3
|
import { Route, Routes } from './route';
|
|
4
4
|
import { ExtractRouteStateParamsAsOptional } from './state';
|
|
@@ -50,7 +50,7 @@ export type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
|
|
|
50
50
|
* Order of routes will be from greatest ancestor to narrowest matched.
|
|
51
51
|
* @internal
|
|
52
52
|
*/
|
|
53
|
-
hooks:
|
|
53
|
+
hooks: Hooks[];
|
|
54
54
|
}>;
|
|
55
55
|
/**
|
|
56
56
|
* This type is the same as `ResolvedRoute<TRoutes[number]>` while remaining distributive
|
package/dist/types/route.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ import { PrefetchConfig } from './prefetch';
|
|
|
4
4
|
import { RouteMeta } from './register';
|
|
5
5
|
import { LastInArray } from './utilities';
|
|
6
6
|
import { CreateRouteOptions } from './createRouteOptions';
|
|
7
|
-
import { WithHooks } from './hooks';
|
|
8
7
|
import { RouteContext } from './routeContext';
|
|
9
|
-
import {
|
|
8
|
+
import { Hooks } from '../models/hooks';
|
|
9
|
+
import { Url } from '../main';
|
|
10
10
|
/**
|
|
11
11
|
* Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
|
|
12
12
|
*/
|
|
@@ -14,7 +14,7 @@ export type Routes = readonly Route[];
|
|
|
14
14
|
/**
|
|
15
15
|
* The Route properties originally provided to `createRoute`. The only change is normalizing meta to always default to an empty object.
|
|
16
16
|
*/
|
|
17
|
-
export type CreatedRouteOptions = Omit<CreateRouteOptions, 'props'> &
|
|
17
|
+
export type CreatedRouteOptions = Omit<CreateRouteOptions, 'props'> & {
|
|
18
18
|
id: string;
|
|
19
19
|
props?: unknown;
|
|
20
20
|
};
|
|
@@ -79,12 +79,13 @@ export type Route<TName extends string = string, THost extends WithParams = With
|
|
|
79
79
|
* Order of routes will be from greatest ancestor to narrowest matched.
|
|
80
80
|
* @internal
|
|
81
81
|
*/
|
|
82
|
-
hooks:
|
|
82
|
+
hooks: Hooks[];
|
|
83
83
|
/**
|
|
84
84
|
* A value that represents how many parents a route has. Used for route matching
|
|
85
85
|
* @internal
|
|
86
86
|
*/
|
|
87
87
|
depth: number;
|
|
88
|
+
assemble: (params: any) => Url;
|
|
88
89
|
};
|
|
89
90
|
export type GenericRoute = {
|
|
90
91
|
id: string;
|
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CreateRouteOptions } from './createRouteOptions';
|
|
2
|
+
import { Rejection, Rejections } from './rejection';
|
|
3
|
+
import { GenericRoute, Routes } from './route';
|
|
3
4
|
export type RouteContext = GenericRoute | Rejection;
|
|
4
|
-
export type ToRouteContext<TContext extends RouteContext[] | undefined> = TContext extends RouteContext[] ? TContext : [];
|
|
5
|
-
export type
|
|
5
|
+
export type ToRouteContext<TContext extends RouteContext[] | readonly RouteContext[] | undefined> = TContext extends RouteContext[] ? TContext : [];
|
|
6
|
+
export type ExtractRouteContext<TOptions extends CreateRouteOptions> = TOptions extends {
|
|
7
|
+
context: infer TContext extends RouteContext[];
|
|
8
|
+
} ? TContext : [];
|
|
9
|
+
export type ExtractRouteContextRoutes<TOptions extends CreateRouteOptions> = RouteContextToRoute<ExtractRouteContext<TOptions>>;
|
|
10
|
+
export type ExtractRouteContextRejections<TOptions extends CreateRouteOptions> = RouteContextToRejection<ExtractRouteContext<TOptions>>;
|
|
11
|
+
export type RouteContextToRoute<TContext extends RouteContext[] | undefined> = RouteContext[] extends TContext ? Routes : undefined extends TContext ? Routes : FilterRouteContextRoutes<TContext>;
|
|
6
12
|
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 ?
|
|
13
|
+
export type RouteContextToRejection<TContext extends RouteContext[] | undefined> = RouteContext[] extends TContext ? Rejections : undefined extends TContext ? Rejections : FilterRouteContextRejections<TContext>;
|
|
8
14
|
type FilterRouteContextRejections<TContext extends RouteContext[] | undefined> = TContext extends [infer First, ...infer Rest extends RouteContext[]] ? First extends Rejection ? [First, ...FilterRouteContextRejections<Rest>] : FilterRouteContextRejections<Rest> : [];
|
|
9
15
|
export {};
|