@kitbag/router 0.21.0 → 0.22.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 +9 -0
- package/dist/components/routerLink.d.ts +3 -3
- package/dist/compositions/useLink.d.ts +2 -2
- package/dist/devtools/createRouterDevtools.d.ts +13 -0
- package/dist/devtools/filters.d.ts +12 -0
- package/dist/devtools/getDevtoolsLabel.d.ts +13 -0
- package/dist/devtools/types.d.ts +10 -0
- package/dist/errors/invalidRouteRedirectError.d.ts +11 -0
- package/dist/errors/multipleRouteRedirectsError.d.ts +13 -0
- package/dist/kitbag-router.js +5256 -2004
- package/dist/kitbag-router.umd.cjs +3 -3
- package/dist/main.d.ts +1 -1
- package/dist/models/hooks.d.ts +9 -7
- package/dist/services/createComponentHooks.d.ts +5 -5
- package/dist/services/createExternalRoute.d.ts +3 -2
- package/dist/services/createRoute.d.ts +2 -1
- package/dist/services/createRouteHooks.d.ts +17 -0
- package/dist/services/createRouteRedirects.d.ts +10 -0
- package/dist/services/createRouterAssets.d.ts +5 -5
- package/dist/services/createRouterHooks.d.ts +8 -9
- package/dist/services/insertBaseRoute.d.ts +2 -2
- package/dist/services/routeMatchRules.d.ts +0 -1
- package/dist/services/routeMatchScore.d.ts +1 -1
- package/dist/services/routeRegex.d.ts +1 -0
- package/dist/services/urlAssembly.d.ts +2 -2
- package/dist/services/urlCombine.d.ts +2 -3
- package/dist/services/urlCreator.d.ts +2 -3
- package/dist/services/urlParser.d.ts +1 -1
- package/dist/types/hooks.d.ts +56 -28
- package/dist/types/params.d.ts +5 -11
- package/dist/types/redirects.d.ts +40 -0
- package/dist/types/resolved.d.ts +12 -2
- package/dist/types/route.d.ts +0 -2
- package/dist/types/router.d.ts +12 -7
- package/dist/types/routerLink.d.ts +3 -3
- package/dist/types/routerPush.d.ts +2 -2
- package/dist/types/routerReplace.d.ts +2 -2
- package/dist/types/urlString.d.ts +23 -0
- package/dist/types/useLink.d.ts +2 -2
- package/dist/utilities/checkDuplicateParams.spec.d.ts +1 -0
- package/dist/utilities/index.d.ts +1 -1
- package/dist/utilities/isNamedRoute.d.ts +4 -0
- package/dist/utilities/makeOptional.d.ts +0 -1
- package/dist/utilities/testHelpers.d.ts +450 -18
- package/package.json +7 -7
- package/dist/errors/missingRouteContextError.d.ts +0 -10
- package/dist/services/createHooksFactory.d.ts +0 -15
- package/dist/services/createUrlParts.d.ts +0 -13
- package/dist/types/url.d.ts +0 -14
- package/dist/types/urlParts.d.ts +0 -53
- package/dist/utilities/checkMissingContext.d.ts +0 -2
- /package/dist/{utilities/checkDuplicateKeys.spec.d.ts → services/createRouteRedirects.spec.d.ts} +0 -0
- /package/dist/{utilities/checkMissingContext.spec copy.d.ts → types/redirects.spec-d.d.ts} +0 -0
- /package/dist/utilities/{checkDuplicateKeys.d.ts → checkDuplicateParams.d.ts} +0 -0
|
@@ -7,7 +7,7 @@ import { createUseRouter } from '../compositions/useRouter';
|
|
|
7
7
|
import { createUseQueryValue } from '../compositions/useQueryValue';
|
|
8
8
|
import { createUseLink } from '../compositions/useLink';
|
|
9
9
|
import { createIsRoute } from '../guards/routes';
|
|
10
|
-
import {
|
|
10
|
+
import { AddBeforeLeaveHook, AddBeforeUpdateHook, AddAfterLeaveHook, AddAfterUpdateHook } from '../types/hooks';
|
|
11
11
|
export type RouterAssets<TRouter extends Router> = {
|
|
12
12
|
/**
|
|
13
13
|
* Registers a hook that is called before a route is left. Must be called from setup.
|
|
@@ -17,7 +17,7 @@ export type RouterAssets<TRouter extends Router> = {
|
|
|
17
17
|
* @returns {RouteHookRemove} A function that removes the added hook.
|
|
18
18
|
* @group Hooks
|
|
19
19
|
*/
|
|
20
|
-
onBeforeRouteLeave:
|
|
20
|
+
onBeforeRouteLeave: AddBeforeLeaveHook<RouterRoutes<TRouter>, RouterRejections<TRouter>>;
|
|
21
21
|
/**
|
|
22
22
|
* Registers a hook that is called before a route is updated. Must be called from setup.
|
|
23
23
|
* This is particularly useful for handling changes in route parameters or query while staying within the same component.
|
|
@@ -26,7 +26,7 @@ export type RouterAssets<TRouter extends Router> = {
|
|
|
26
26
|
* @returns {RouteHookRemove} A function that removes the added hook.
|
|
27
27
|
* @group Hooks
|
|
28
28
|
*/
|
|
29
|
-
onBeforeRouteUpdate:
|
|
29
|
+
onBeforeRouteUpdate: AddBeforeUpdateHook<RouterRoutes<TRouter>, RouterRejections<TRouter>>;
|
|
30
30
|
/**
|
|
31
31
|
* Registers a hook that is called after a route has been left. Must be called during setup.
|
|
32
32
|
* This can be used for cleanup actions after the component is no longer active, ensuring proper resource management.
|
|
@@ -35,7 +35,7 @@ export type RouterAssets<TRouter extends Router> = {
|
|
|
35
35
|
* @returns {RouteHookRemove} A function that removes the added hook.
|
|
36
36
|
* @group Hooks
|
|
37
37
|
*/
|
|
38
|
-
onAfterRouteLeave:
|
|
38
|
+
onAfterRouteLeave: AddAfterLeaveHook<RouterRoutes<TRouter>, RouterRejections<TRouter>>;
|
|
39
39
|
/**
|
|
40
40
|
* Registers a hook that is called after a route has been updated. Must be called during setup.
|
|
41
41
|
* This is ideal for responding to updates within the same route, such as parameter changes, without full component reloads.
|
|
@@ -44,7 +44,7 @@ export type RouterAssets<TRouter extends Router> = {
|
|
|
44
44
|
* @returns {RouteHookRemove} A function that removes the added hook.
|
|
45
45
|
* @group Hooks
|
|
46
46
|
*/
|
|
47
|
-
onAfterRouteUpdate:
|
|
47
|
+
onAfterRouteUpdate: AddAfterUpdateHook<RouterRoutes<TRouter>, RouterRejections<TRouter>>;
|
|
48
48
|
/**
|
|
49
49
|
* A guard to verify if a route or unknown value matches a given route name.
|
|
50
50
|
*
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { AddGlobalHooks,
|
|
1
|
+
import { AddGlobalHooks, AddComponentHook, AfterHookRunner, BeforeHookRunner, AddBeforeEnterHook, AddBeforeUpdateHook, AddBeforeLeaveHook, AddAfterEnterHook, AddAfterUpdateHook, AddAfterLeaveHook, ErrorHookRunner, AddErrorHook } from '../types/hooks';
|
|
2
2
|
import { HasVueAppStore } from './createVueAppStore';
|
|
3
3
|
export declare const getRouterHooksKey: (routerKey: import('vue').InjectionKey<import('../main').Router>) => import('vue').InjectionKey<RouterHooks>;
|
|
4
4
|
export type RouterHooks = HasVueAppStore & {
|
|
5
5
|
runBeforeRouteHooks: BeforeHookRunner;
|
|
6
6
|
runAfterRouteHooks: AfterHookRunner;
|
|
7
7
|
runErrorHooks: ErrorHookRunner;
|
|
8
|
-
|
|
9
|
-
addComponentAfterRouteHook: AddComponentAfterHook;
|
|
8
|
+
addComponentHook: AddComponentHook;
|
|
10
9
|
addGlobalRouteHooks: AddGlobalHooks;
|
|
11
|
-
onBeforeRouteEnter:
|
|
12
|
-
onBeforeRouteUpdate:
|
|
13
|
-
onBeforeRouteLeave:
|
|
14
|
-
onAfterRouteEnter:
|
|
15
|
-
onAfterRouteUpdate:
|
|
16
|
-
onAfterRouteLeave:
|
|
10
|
+
onBeforeRouteEnter: AddBeforeEnterHook;
|
|
11
|
+
onBeforeRouteUpdate: AddBeforeUpdateHook;
|
|
12
|
+
onBeforeRouteLeave: AddBeforeLeaveHook;
|
|
13
|
+
onAfterRouteEnter: AddAfterEnterHook;
|
|
14
|
+
onAfterRouteUpdate: AddAfterUpdateHook;
|
|
15
|
+
onAfterRouteLeave: AddAfterLeaveHook;
|
|
17
16
|
onError: AddErrorHook;
|
|
18
17
|
};
|
|
19
18
|
export declare function createRouterHooks(): RouterHooks;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function insertBaseRoute(
|
|
1
|
+
import { Route } from '../types/route';
|
|
2
|
+
export declare function insertBaseRoute(route: Route, base?: string): Route;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RouteMatchRule } from '../types/routeMatchRule';
|
|
2
|
-
export declare const isNamedRoute: RouteMatchRule;
|
|
3
2
|
export declare const routeHostMatches: RouteMatchRule;
|
|
4
3
|
export declare const routePathMatches: RouteMatchRule;
|
|
5
4
|
export declare const routeQueryMatches: RouteMatchRule;
|
|
@@ -3,5 +3,5 @@ import { QuerySource } from '../types/querySource';
|
|
|
3
3
|
type RouteSortMethod = (aRoute: Route, bRoute: Route) => number;
|
|
4
4
|
export declare function getRouteScoreSortMethod(url: string): RouteSortMethod;
|
|
5
5
|
export declare function countExpectedPathParams(route: Route, actualPath: string): number;
|
|
6
|
-
export declare function countExpectedQueryParams(route: Route,
|
|
6
|
+
export declare function countExpectedQueryParams(route: Route, actualQuery: QuerySource): number;
|
|
7
7
|
export {};
|
|
@@ -3,6 +3,7 @@ import { WithParams } from './withParams';
|
|
|
3
3
|
export declare function splitByMatches(string: string, regexp: RegExp): string[];
|
|
4
4
|
export declare function generateRouteHostRegexPattern(route: Route): RegExp;
|
|
5
5
|
export declare function generateRoutePathRegexPattern(route: Route): RegExp;
|
|
6
|
+
export declare function generateRouteHashRegexPattern(route: Route): RegExp;
|
|
6
7
|
export declare function generateRouteQueryRegexPatterns(route: Route): RegExp[];
|
|
7
8
|
export declare function replaceParamSyntaxWithCatchAlls(value: string): string;
|
|
8
9
|
export declare function replaceIndividualParamWithCaptureGroup(path: string, paramName: string): string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Route } from '../types/route';
|
|
2
|
-
import {
|
|
2
|
+
import { UrlString } from '../types/urlString';
|
|
3
3
|
import { QuerySource } from '../types/querySource';
|
|
4
4
|
type AssembleUrlOptions = {
|
|
5
5
|
params?: Record<string, unknown>;
|
|
6
6
|
query?: QuerySource;
|
|
7
7
|
hash?: string;
|
|
8
8
|
};
|
|
9
|
-
export declare function assembleUrl(route: Route, options?: AssembleUrlOptions):
|
|
9
|
+
export declare function assembleUrl(route: Route, options?: AssembleUrlOptions): UrlString;
|
|
10
10
|
export {};
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare function combineUrl(previous: Url | Partial<UrlParts>, updated: Url | Partial<UrlParts>): Url;
|
|
1
|
+
import { UrlString, UrlParts } from '../types/urlString';
|
|
2
|
+
export declare function combineUrl(previous: UrlString | Partial<UrlParts>, updated: UrlString | Partial<UrlParts>): UrlString;
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare function createUrl({ protocol, host, pathname, search, searchParams, hash }: Partial<UrlParts>): Url;
|
|
1
|
+
import { UrlString, UrlParts } from '../types/urlString';
|
|
2
|
+
export declare function createUrl({ protocol, host, pathname, search, searchParams, hash }: Partial<UrlParts>): UrlString;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { UrlParts } from '../types/
|
|
1
|
+
import { UrlParts } from '../types/urlString';
|
|
2
2
|
export declare function parseUrl(value: string): UrlParts;
|
package/dist/types/hooks.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Hooks } from '../models/hooks';
|
|
2
|
-
import { ResolvedRoute, RouterResolvedRouteUnion } from './resolved';
|
|
2
|
+
import { ResolvedRoute, RouterResolvedRouteUnion, ResolvedRouteUnion } from './resolved';
|
|
3
3
|
import { MaybePromise } from './utilities';
|
|
4
4
|
import { Route, Routes } from './route';
|
|
5
5
|
import { RouterReject } from './routerReject';
|
|
@@ -10,51 +10,55 @@ import { RouteContext, RouteContextToRejection, RouteContextToRoute } from './ro
|
|
|
10
10
|
import { RouterAbort } from './routerAbort';
|
|
11
11
|
import { CallbackContextAbort, CallbackContextPush, CallbackContextReject, CallbackContextSuccess } from './callbackContext';
|
|
12
12
|
import { RouteUpdate } from './routeUpdate';
|
|
13
|
-
export type InternalRouteHooks<TRoute extends Route = Route, TContext extends RouteContext[]
|
|
13
|
+
export type InternalRouteHooks<TRoute extends Route = Route, TContext extends RouteContext[] = []> = {
|
|
14
14
|
/**
|
|
15
15
|
* Registers a route hook to be called before the route is entered.
|
|
16
16
|
*/
|
|
17
|
-
onBeforeRouteEnter:
|
|
17
|
+
onBeforeRouteEnter: AddBeforeEnterHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, TRoute, Route>;
|
|
18
18
|
/**
|
|
19
19
|
* Registers a route hook to be called before the route is left.
|
|
20
20
|
*/
|
|
21
|
-
onBeforeRouteLeave:
|
|
21
|
+
onBeforeRouteLeave: AddBeforeLeaveHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, Route, TRoute>;
|
|
22
22
|
/**
|
|
23
23
|
* Registers a route hook to be called before the route is updated.
|
|
24
24
|
*/
|
|
25
|
-
onBeforeRouteUpdate:
|
|
25
|
+
onBeforeRouteUpdate: AddBeforeUpdateHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, TRoute, Route>;
|
|
26
26
|
/**
|
|
27
27
|
* Registers a route hook to be called after the route is entered.
|
|
28
28
|
*/
|
|
29
|
-
onAfterRouteEnter:
|
|
29
|
+
onAfterRouteEnter: AddAfterEnterHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, TRoute, Route>;
|
|
30
30
|
/**
|
|
31
31
|
* Registers a route hook to be called after the route is left.
|
|
32
32
|
*/
|
|
33
|
-
onAfterRouteLeave:
|
|
33
|
+
onAfterRouteLeave: AddAfterLeaveHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, Route, TRoute>;
|
|
34
34
|
/**
|
|
35
35
|
* Registers a route hook to be called after the route is updated.
|
|
36
36
|
*/
|
|
37
|
-
onAfterRouteUpdate:
|
|
37
|
+
onAfterRouteUpdate: AddAfterUpdateHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, TRoute, Route>;
|
|
38
38
|
};
|
|
39
39
|
export type ExternalRouteHooks<TRoute extends Route = Route, TContext extends RouteContext[] | undefined = undefined> = {
|
|
40
40
|
/**
|
|
41
41
|
* Registers a route hook to be called before the route is entered.
|
|
42
42
|
*/
|
|
43
|
-
onBeforeRouteEnter:
|
|
43
|
+
onBeforeRouteEnter: AddBeforeEnterHook<[TRoute] | RouteContextToRoute<TContext>, RouteContextToRejection<TContext>, TRoute, Route>;
|
|
44
44
|
};
|
|
45
45
|
export type HookTiming = 'global' | 'component';
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Union type for all component route hooks.
|
|
48
|
+
*/
|
|
49
|
+
export type ComponentHook = BeforeEnterHook | BeforeUpdateHook | BeforeLeaveHook | AfterEnterHook | AfterUpdateHook | AfterLeaveHook;
|
|
50
|
+
/**
|
|
51
|
+
* Registration object for adding a component route hook.
|
|
52
|
+
*/
|
|
53
|
+
export type ComponentHookRegistration = {
|
|
54
|
+
lifecycle: HookLifecycle;
|
|
55
|
+
hook: ComponentHook;
|
|
55
56
|
depth: number;
|
|
56
57
|
};
|
|
57
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Function to add a component route hook with depth-based condition checking.
|
|
60
|
+
*/
|
|
61
|
+
export type AddComponentHook = (registration: ComponentHookRegistration) => HookRemove;
|
|
58
62
|
export type AddGlobalHooks = (hooks: Hooks) => void;
|
|
59
63
|
/**
|
|
60
64
|
* A function to remove a previously added route hook.
|
|
@@ -73,24 +77,48 @@ export type AfterHookLifecycle = 'onAfterRouteEnter' | 'onAfterRouteUpdate' | 'o
|
|
|
73
77
|
*/
|
|
74
78
|
export type HookLifecycle = BeforeHookLifecycle | AfterHookLifecycle;
|
|
75
79
|
type AfterHookContext<TRoute extends Route, TRoutes extends Routes, TRejections extends Rejections> = {
|
|
76
|
-
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
77
80
|
reject: RouterReject<TRejections>;
|
|
78
81
|
push: RouterPush<TRoutes>;
|
|
79
82
|
replace: RouterReplace<TRoutes>;
|
|
80
|
-
update: RouteUpdate<
|
|
83
|
+
update: RouteUpdate<ResolvedRouteUnion<TRoute>>;
|
|
81
84
|
};
|
|
82
|
-
type BeforeHookContext<
|
|
83
|
-
from: RouterResolvedRouteUnion<TRoutes> | null;
|
|
85
|
+
type BeforeHookContext<TRouteTo extends Route, TRoutes extends Routes, TRejections extends Rejections> = {
|
|
84
86
|
reject: RouterReject<TRejections>;
|
|
85
87
|
push: RouterPush<TRoutes>;
|
|
86
88
|
replace: RouterReplace<TRoutes>;
|
|
87
|
-
update: RouteUpdate<
|
|
89
|
+
update: RouteUpdate<ResolvedRouteUnion<TRouteTo>>;
|
|
88
90
|
abort: RouterAbort;
|
|
89
91
|
};
|
|
90
|
-
export type
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
export type
|
|
92
|
+
export type BeforeEnterHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = BeforeHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
93
|
+
from: ResolvedRouteUnion<TRouteFrom> | null;
|
|
94
|
+
};
|
|
95
|
+
export type BeforeEnterHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: BeforeEnterHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
96
|
+
export type AddBeforeEnterHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: BeforeEnterHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
97
|
+
export type BeforeUpdateHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = BeforeHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
98
|
+
from: ResolvedRouteUnion<TRouteFrom> | null;
|
|
99
|
+
};
|
|
100
|
+
export type BeforeUpdateHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: BeforeUpdateHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
101
|
+
export type AddBeforeUpdateHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: BeforeUpdateHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
102
|
+
export type BeforeLeaveHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = BeforeHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
103
|
+
from: ResolvedRouteUnion<TRouteFrom>;
|
|
104
|
+
};
|
|
105
|
+
export type BeforeLeaveHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: BeforeLeaveHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
106
|
+
export type AddBeforeLeaveHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: BeforeLeaveHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
107
|
+
export type AfterEnterHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = AfterHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
108
|
+
from: ResolvedRouteUnion<TRouteFrom> | null;
|
|
109
|
+
};
|
|
110
|
+
export type AfterEnterHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: AfterEnterHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
111
|
+
export type AddAfterEnterHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: AfterEnterHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
112
|
+
export type AfterUpdateHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = AfterHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
113
|
+
from: ResolvedRouteUnion<TRouteFrom> | null;
|
|
114
|
+
};
|
|
115
|
+
export type AfterUpdateHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: AfterUpdateHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
116
|
+
export type AddAfterUpdateHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: AfterUpdateHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
117
|
+
export type AfterLeaveHookContext<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = AfterHookContext<TRouteTo, TRoutes, TRejections> & {
|
|
118
|
+
from: ResolvedRouteUnion<TRouteFrom>;
|
|
119
|
+
};
|
|
120
|
+
export type AfterLeaveHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: AfterLeaveHookContext<TRoutes, TRejections, TRouteTo, TRouteFrom>) => MaybePromise<void>;
|
|
121
|
+
export type AddAfterLeaveHook<TRoutes extends Routes = Routes, TRejections extends Rejections = Rejections, TRouteTo extends Route = TRoutes[number], TRouteFrom extends Route = TRoutes[number]> = (hook: AfterLeaveHook<TRoutes, TRejections, TRouteTo, TRouteFrom>) => HookRemove;
|
|
94
122
|
export type BeforeHookResponse = CallbackContextSuccess | CallbackContextPush | CallbackContextReject | CallbackContextAbort;
|
|
95
123
|
export type AfterHookResponse = CallbackContextSuccess | CallbackContextPush | CallbackContextReject;
|
|
96
124
|
export type BeforeHookRunner = <TRoutes extends Routes>(context: {
|
package/dist/types/params.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LiteralParam, Param, ParamGetSet, ParamGetter } from './paramTypes';
|
|
2
2
|
import { Identity } from './utilities';
|
|
3
|
-
import { MakeOptional
|
|
3
|
+
import { MakeOptional } from '../utilities/makeOptional';
|
|
4
|
+
import { Route } from './route';
|
|
4
5
|
import { WithParams } from '../services/withParams';
|
|
5
6
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
6
7
|
export declare const paramStart = "[";
|
|
@@ -31,26 +32,19 @@ export declare function isLiteralParam(value: Param): value is LiteralParam;
|
|
|
31
32
|
* @returns The extracted parameter name, or never if the parameter string is empty.
|
|
32
33
|
*/
|
|
33
34
|
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
|
-
};
|
|
37
35
|
/**
|
|
38
36
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
39
|
-
* @template
|
|
37
|
+
* @template TRoute - The route type from which to extract and merge parameter types.
|
|
40
38
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
41
39
|
*/
|
|
42
|
-
export type ExtractRouteParamTypesReading<
|
|
43
|
-
[K in keyof ExtractWithParams<TParts>]: ExtractParamTypesReading<ExtractWithParams<TParts>[K]>;
|
|
44
|
-
}[keyof ExtractWithParams<TParts>]>>>;
|
|
40
|
+
export type ExtractRouteParamTypesReading<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesReading<TRoute['host']> & ExtractParamTypesReading<TRoute['path']> & ExtractParamTypesReading<TRoute['query']> & ExtractParamTypesReading<TRoute['hash']>>>;
|
|
45
41
|
/**
|
|
46
42
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
47
43
|
* Differs from ExtractRouteParamTypesReading in that optional params with defaults will remain optional.
|
|
48
44
|
* @template TRoute - The route type from which to extract and merge parameter types.
|
|
49
45
|
* @returns A record of parameter names to their respective types, extracted and merged from both path and query parameters.
|
|
50
46
|
*/
|
|
51
|
-
export type ExtractRouteParamTypesWriting<
|
|
52
|
-
[K in keyof ExtractWithParams<TParts>]: ExtractParamTypesWriting<ExtractWithParams<TParts>[K]>;
|
|
53
|
-
}[keyof ExtractWithParams<TParts>]>>>;
|
|
47
|
+
export type ExtractRouteParamTypesWriting<TRoute extends Route> = Identity<MakeOptional<ExtractParamTypesWriting<TRoute['host']> & ExtractParamTypesWriting<TRoute['path']> & ExtractParamTypesWriting<TRoute['query']> & ExtractParamTypesWriting<TRoute['hash']>>>;
|
|
54
48
|
/**
|
|
55
49
|
* Extracts combined types of path and query parameters for a given route, creating a unified parameter object.
|
|
56
50
|
* @template TParams - The record of parameter types, possibly including undefined.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ExtractRouteParamTypesReading, ExtractRouteParamTypesWriting } from './params';
|
|
2
|
+
import { ResolvedRouteUnion } from './resolved';
|
|
3
|
+
import { Route, Routes } from './route';
|
|
4
|
+
import { RouterReplace } from './routerReplace';
|
|
5
|
+
import { AllPropertiesAreOptional, MaybePromise } from './utilities';
|
|
6
|
+
/**
|
|
7
|
+
* redirect is returned by createRouteHooks but is not part of the Route type, so we use this type to assert that it exists.
|
|
8
|
+
*/
|
|
9
|
+
type RouteWithRedirect<TRoute extends Route = Route> = TRoute & {
|
|
10
|
+
redirect: RouteRedirect;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Type guard to assert that a route has a redirect hook.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare function isRouteWithRedirect(route: Route): route is RouteWithRedirect;
|
|
17
|
+
export type RouteRedirects<TRoute extends Route = Route> = {
|
|
18
|
+
/**
|
|
19
|
+
* Creates a redirect to redirect the current route to another route.
|
|
20
|
+
*/
|
|
21
|
+
redirectTo: RouteRedirectTo<TRoute>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a redirect to redirect to the current route from another route.
|
|
24
|
+
*/
|
|
25
|
+
redirectFrom: RouteRedirectFrom<TRoute>;
|
|
26
|
+
};
|
|
27
|
+
type RedirectHookContext<TRoutes extends Routes> = {
|
|
28
|
+
replace: RouterReplace<TRoutes>;
|
|
29
|
+
};
|
|
30
|
+
export type RedirectHook<TRoutes extends Routes = Routes, TRouteTo extends Route = TRoutes[number]> = (to: ResolvedRouteUnion<TRouteTo>, context: RedirectHookContext<TRoutes>) => MaybePromise<void>;
|
|
31
|
+
export type RouteRedirectCallback<TRouteTo extends Route = Route, TRouteFrom extends Route = Route> = (params: ExtractRouteParamTypesReading<TRouteFrom>) => ExtractRouteParamTypesWriting<TRouteTo>;
|
|
32
|
+
/**
|
|
33
|
+
* This type is purposely wide to prevent type errors in RouteRedirectFrom where the TRouteTo generic cannot be inferred.
|
|
34
|
+
*/
|
|
35
|
+
export type RouteRedirect = (to: Route, callback?: (params: any) => any) => void;
|
|
36
|
+
export type RedirectToArgs<TRouteTo extends Route = Route, TRouteFrom extends Route = Route> = AllPropertiesAreOptional<ExtractRouteParamTypesWriting<TRouteTo>> extends true ? [to: TRouteTo, params?: RouteRedirectCallback<TRouteTo, TRouteFrom>] : [to: TRouteTo, params: RouteRedirectCallback<TRouteTo, TRouteFrom>];
|
|
37
|
+
export type RouteRedirectTo<TRouteFrom extends Route = Route> = <TRouteTo extends Route>(...args: RedirectToArgs<TRouteTo, TRouteFrom>) => void;
|
|
38
|
+
export type RedirectFromArgs<TRouteTo extends Route = Route, TRouteFrom extends Route = Route> = AllPropertiesAreOptional<ExtractRouteParamTypesWriting<TRouteTo>> extends true ? [from: TRouteFrom, params?: RouteRedirectCallback<TRouteTo, TRouteFrom>] : [from: TRouteFrom, params: RouteRedirectCallback<TRouteTo, TRouteFrom>];
|
|
39
|
+
export type RouteRedirectFrom<TRouteTo extends Route = Route> = <TRouteFrom extends Route>(...args: RedirectFromArgs<TRouteTo, TRouteFrom>) => void;
|
|
40
|
+
export {};
|
package/dist/types/resolved.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { Hooks } from '../models/hooks';
|
|
|
2
2
|
import { ExtractRouteParamTypesReading } from './params';
|
|
3
3
|
import { Route, Routes } from './route';
|
|
4
4
|
import { ExtractRouteStateParamsAsOptional } from './state';
|
|
5
|
-
import {
|
|
5
|
+
import { UrlString } from './urlString';
|
|
6
6
|
/**
|
|
7
7
|
* Represents a route that the router has matched to current browser location.
|
|
8
8
|
* @template TRoute - Underlying Route that has been resolved.
|
|
@@ -44,7 +44,7 @@ export type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
|
|
|
44
44
|
/**
|
|
45
45
|
* String value of the resolved URL.
|
|
46
46
|
*/
|
|
47
|
-
href:
|
|
47
|
+
href: UrlString;
|
|
48
48
|
/**
|
|
49
49
|
* The stores for routes including ancestors.
|
|
50
50
|
* Order of routes will be from greatest ancestor to narrowest matched.
|
|
@@ -58,3 +58,13 @@ export type ResolvedRoute<TRoute extends Route = Route> = Readonly<{
|
|
|
58
58
|
export type RouterResolvedRouteUnion<TRoutes extends Routes> = {
|
|
59
59
|
[K in keyof TRoutes]: ResolvedRoute<TRoutes[K]>;
|
|
60
60
|
}[number];
|
|
61
|
+
/**
|
|
62
|
+
* Converts a union of Route types to a union of ResolvedRoute types while preserving the discriminated union structure for narrowing.
|
|
63
|
+
* This is useful when you have a Route union (like `TRoutes[number]`) and need it to narrow properly.
|
|
64
|
+
* Uses a distributive conditional type to ensure unions are properly distributed.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* type RouteUnion = RouteA | RouteB
|
|
68
|
+
* type ResolvedUnion = ResolvedRouteUnion<RouteUnion> // ResolvedRoute<RouteA> | ResolvedRoute<RouteB>
|
|
69
|
+
*/
|
|
70
|
+
export type ResolvedRouteUnion<TRoute extends Route> = TRoute extends Route ? ResolvedRoute<TRoute> : never;
|
package/dist/types/route.d.ts
CHANGED
|
@@ -6,7 +6,6 @@ import { LastInArray } from './utilities';
|
|
|
6
6
|
import { CreateRouteOptions } from './createRouteOptions';
|
|
7
7
|
import { RouteContext } from './routeContext';
|
|
8
8
|
import { Hooks } from '../models/hooks';
|
|
9
|
-
import { Url } from '../main';
|
|
10
9
|
/**
|
|
11
10
|
* Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
|
|
12
11
|
*/
|
|
@@ -85,7 +84,6 @@ export type Route<TName extends string = string, THost extends WithParams = With
|
|
|
85
84
|
* @internal
|
|
86
85
|
*/
|
|
87
86
|
depth: number;
|
|
88
|
-
assemble: (params: any) => Url;
|
|
89
87
|
};
|
|
90
88
|
export type GenericRoute = {
|
|
91
89
|
id: string;
|
package/dist/types/router.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { App, InjectionKey, Ref } from 'vue';
|
|
2
2
|
import { RouterHistoryMode } from '../services/createRouterHistory';
|
|
3
3
|
import { RouterRoute } from './routerRoute';
|
|
4
|
-
import {
|
|
4
|
+
import { AddBeforeEnterHook, AddBeforeUpdateHook, AddBeforeLeaveHook, AddAfterEnterHook, AddAfterUpdateHook, AddAfterLeaveHook, AddErrorHook } from './hooks';
|
|
5
5
|
import { PrefetchConfig } from './prefetch';
|
|
6
6
|
import { ResolvedRoute } from './resolved';
|
|
7
7
|
import { Route, Routes } from './route';
|
|
@@ -99,27 +99,27 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
|
|
|
99
99
|
/**
|
|
100
100
|
* Registers a hook to be called before a route is entered.
|
|
101
101
|
*/
|
|
102
|
-
onBeforeRouteEnter:
|
|
102
|
+
onBeforeRouteEnter: AddBeforeEnterHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
103
103
|
/**
|
|
104
104
|
* Registers a hook to be called before a route is left.
|
|
105
105
|
*/
|
|
106
|
-
onBeforeRouteLeave:
|
|
106
|
+
onBeforeRouteLeave: AddBeforeLeaveHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
107
107
|
/**
|
|
108
108
|
* Registers a hook to be called before a route is updated.
|
|
109
109
|
*/
|
|
110
|
-
onBeforeRouteUpdate:
|
|
110
|
+
onBeforeRouteUpdate: AddBeforeUpdateHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
111
111
|
/**
|
|
112
112
|
* Registers a hook to be called after a route is entered.
|
|
113
113
|
*/
|
|
114
|
-
onAfterRouteEnter:
|
|
114
|
+
onAfterRouteEnter: AddAfterEnterHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
115
115
|
/**
|
|
116
116
|
* Registers a hook to be called after a route is left.
|
|
117
117
|
*/
|
|
118
|
-
onAfterRouteLeave:
|
|
118
|
+
onAfterRouteLeave: AddAfterLeaveHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
119
119
|
/**
|
|
120
120
|
* Registers a hook to be called after a route is updated.
|
|
121
121
|
*/
|
|
122
|
-
onAfterRouteUpdate:
|
|
122
|
+
onAfterRouteUpdate: AddAfterUpdateHook<TRoutes | TPlugin['routes'], ToRouteContext<TOptions['rejections']> | ToRouteContext<TPlugin['rejections']>>;
|
|
123
123
|
/**
|
|
124
124
|
* Registers a hook to be called when an error occurs.
|
|
125
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
|
|
@@ -151,6 +151,11 @@ export type Router<TRoutes extends Routes = any, TOptions extends RouterOptions
|
|
|
151
151
|
* @private
|
|
152
152
|
*/
|
|
153
153
|
key: InjectionKey<Router<TRoutes, TOptions, TPlugin>>;
|
|
154
|
+
/**
|
|
155
|
+
* Returns true if the router's devtools plugin has been installed
|
|
156
|
+
* @private
|
|
157
|
+
*/
|
|
158
|
+
hasDevtools: boolean;
|
|
154
159
|
};
|
|
155
160
|
/**
|
|
156
161
|
* This type is the same as `RouterRoute<ResolvedRoute<TRoutes[number]>>` while remaining distributive
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { PrefetchConfig } from './prefetch';
|
|
2
|
-
import {
|
|
2
|
+
import { UrlString } from './urlString';
|
|
3
3
|
import { ResolvedRoute } from './resolved';
|
|
4
4
|
import { Router } from './router';
|
|
5
5
|
import { RouterPushOptions } from './routerPush';
|
|
6
|
-
export type ToCallback<TRouter extends Router> = (resolve: TRouter['resolve']) => ResolvedRoute |
|
|
6
|
+
export type ToCallback<TRouter extends Router> = (resolve: TRouter['resolve']) => ResolvedRoute | UrlString | undefined;
|
|
7
7
|
export type RouterLinkProps<TRouter extends Router> = RouterPushOptions & {
|
|
8
8
|
/**
|
|
9
9
|
* The url string to navigate to or a callback that returns a url string
|
|
10
10
|
*/
|
|
11
|
-
to:
|
|
11
|
+
to: UrlString | ResolvedRoute | ToCallback<TRouter>;
|
|
12
12
|
/**
|
|
13
13
|
* Determines what assets are prefetched when router-link is rendered for this route. Overrides route level prefetch.
|
|
14
14
|
*/
|
|
@@ -2,7 +2,7 @@ import { Routes } from './route';
|
|
|
2
2
|
import { RoutesName } from './routesMap';
|
|
3
3
|
import { RouteParamsByKey } from './routeWithParams';
|
|
4
4
|
import { RouteStateByName } from './state';
|
|
5
|
-
import {
|
|
5
|
+
import { UrlString } from './urlString';
|
|
6
6
|
import { AllPropertiesAreOptional } from './utilities';
|
|
7
7
|
import { QuerySource } from './querySource';
|
|
8
8
|
import { ResolvedRoute } from './resolved';
|
|
@@ -28,6 +28,6 @@ type RouterPushArgs<TRoutes extends Routes, TSource extends RoutesName<TRoutes>>
|
|
|
28
28
|
export type RouterPush<TRoutes extends Routes = any> = {
|
|
29
29
|
<TSource extends RoutesName<TRoutes>>(name: TSource, ...args: RouterPushArgs<TRoutes, TSource>): Promise<void>;
|
|
30
30
|
(route: ResolvedRoute, options?: RouterPushOptions): Promise<void>;
|
|
31
|
-
(url:
|
|
31
|
+
(url: UrlString, options?: RouterPushOptions): Promise<void>;
|
|
32
32
|
};
|
|
33
33
|
export {};
|
|
@@ -2,7 +2,7 @@ import { Routes } from './route';
|
|
|
2
2
|
import { RoutesName } from './routesMap';
|
|
3
3
|
import { RouteParamsByKey } from './routeWithParams';
|
|
4
4
|
import { RouteStateByName } from './state';
|
|
5
|
-
import {
|
|
5
|
+
import { UrlString } from './urlString';
|
|
6
6
|
import { AllPropertiesAreOptional } from './utilities';
|
|
7
7
|
import { QuerySource } from './querySource';
|
|
8
8
|
import { ResolvedRoute } from './resolved';
|
|
@@ -15,6 +15,6 @@ type RouterReplaceArgs<TRoutes extends Routes, TSource extends RoutesName<TRoute
|
|
|
15
15
|
export type RouterReplace<TRoutes extends Routes> = {
|
|
16
16
|
<TSource extends RoutesName<TRoutes>>(name: TSource, ...args: RouterReplaceArgs<TRoutes, TSource>): Promise<void>;
|
|
17
17
|
(route: ResolvedRoute, options?: RouterReplaceOptions): Promise<void>;
|
|
18
|
-
(url:
|
|
18
|
+
(url: UrlString, options?: RouterReplaceOptions): Promise<void>;
|
|
19
19
|
};
|
|
20
20
|
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { QuerySource } from './querySource';
|
|
2
|
+
export type UrlString = `http://${string}` | `https://${string}` | `/${string}`;
|
|
3
|
+
export type UrlParts = {
|
|
4
|
+
protocol?: string;
|
|
5
|
+
host?: string;
|
|
6
|
+
pathname: string;
|
|
7
|
+
searchParams: QuerySource;
|
|
8
|
+
search: string;
|
|
9
|
+
hash: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A type guard for determining if a value is a valid URL.
|
|
13
|
+
* @param value - The value to check.
|
|
14
|
+
* @returns `true` if the value is a valid URL, otherwise `false`.
|
|
15
|
+
* @group Type Guards
|
|
16
|
+
*/
|
|
17
|
+
export declare function isUrlString(value: unknown): value is UrlString;
|
|
18
|
+
/**
|
|
19
|
+
* Converts a string to a valid URL.
|
|
20
|
+
* @param value - The string to convert.
|
|
21
|
+
* @returns The valid URL.
|
|
22
|
+
*/
|
|
23
|
+
export declare function asUrlString(value: string): UrlString;
|
package/dist/types/useLink.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { PrefetchConfig } from './prefetch';
|
|
|
3
3
|
import { ResolvedRoute } from './resolved';
|
|
4
4
|
import { RouterPushOptions } from './routerPush';
|
|
5
5
|
import { RouterReplaceOptions } from './routerReplace';
|
|
6
|
-
import {
|
|
6
|
+
import { UrlString } from './urlString';
|
|
7
7
|
export type UseLink = {
|
|
8
8
|
/**
|
|
9
9
|
* A template ref to bind to the dom for automatic prefetching
|
|
@@ -16,7 +16,7 @@ export type UseLink = {
|
|
|
16
16
|
/**
|
|
17
17
|
* Resolved URL with params interpolated and query applied. Same value as `router.resolve`.
|
|
18
18
|
*/
|
|
19
|
-
href: ComputedRef<
|
|
19
|
+
href: ComputedRef<UrlString | undefined>;
|
|
20
20
|
/**
|
|
21
21
|
* True if route matches current URL or is ancestor of route that matches current URL
|
|
22
22
|
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|