nuxt-typed-router 2.1.0-beta.0 → 2.1.0-beta.2
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 +6 -6
- package/dist/module.json +1 -1
- package/dist/module.mjs +145 -135
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
|
|
19
19
|
## Provide a type safe router to Nuxt with auto-generated typed definitions for route names and autocompletion for route params
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
- Supports routes defined in `config.extendRoutes`
|
|
24
|
-
-
|
|
25
|
-
-
|
|
21
|
+
- Provides types check and autocomplete to `NuxtLink`
|
|
22
|
+
- Provides types check for `useRouter` and `useRoute`
|
|
23
|
+
- Supports routes defined in `config.extendRoutes`
|
|
24
|
+
- Supports optional params and catchAll routes
|
|
25
|
+
- Infer route params based on route name
|
|
26
26
|
|
|
27
27
|
<br/>
|
|
28
28
|
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
|
|
36
36
|
# Documentation
|
|
37
37
|
|
|
38
|
-
[](https://nuxt-typed-router.vercel.app/)
|
|
39
39
|
|
|
40
40
|
# Play with it
|
|
41
41
|
[](https://stackblitz.com/edit/github-7e4xvw?file=store/testRouter.ts)
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -30,129 +30,6 @@ import {useTypedRoute} from './__useTypedRoute';
|
|
|
30
30
|
import {useTypedRouter} from './__useTypedRouter';
|
|
31
31
|
`;
|
|
32
32
|
|
|
33
|
-
const staticTypeUtils = `
|
|
34
|
-
// Type utils
|
|
35
|
-
type ExtractRequiredParameters<T extends Record<string, any>> = Pick<
|
|
36
|
-
T,
|
|
37
|
-
{ [K in keyof T]: undefined extends T[K] ? never : K }[keyof T]
|
|
38
|
-
>;
|
|
39
|
-
|
|
40
|
-
type HasOneRequiredParameter<T extends TypedRouteList> = [TypedRouteParams[T]] extends [never]
|
|
41
|
-
? false
|
|
42
|
-
: [keyof ExtractRequiredParameters<TypedRouteParams[T]>] extends [undefined]
|
|
43
|
-
? false
|
|
44
|
-
: true;
|
|
45
|
-
|
|
46
|
-
type TypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
47
|
-
name?: T;
|
|
48
|
-
} & ([TypedRouteParams[T]] extends [never]
|
|
49
|
-
? {}
|
|
50
|
-
: HasOneRequiredParameter<T> extends false
|
|
51
|
-
? { params?: TypedRouteParams[T] }
|
|
52
|
-
: { params: TypedRouteParams[T] });
|
|
53
|
-
|
|
54
|
-
type ResolvedTypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
55
|
-
name?: T;
|
|
56
|
-
} & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
|
|
57
|
-
|
|
58
|
-
type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
|
|
59
|
-
|
|
60
|
-
type _TypedRoute = Omit<RouteLocationNormalizedLoaded, 'name' | 'params'> &
|
|
61
|
-
ResolvedTypedRouteNamedMapper;
|
|
62
|
-
type _TypedNamedRoute<T extends TypedRouteList> = Omit<
|
|
63
|
-
RouteLocationNormalizedLoaded,
|
|
64
|
-
'name' | 'params'
|
|
65
|
-
> &
|
|
66
|
-
ResolvedTypedLocationAsRelativeRaw<T>;
|
|
67
|
-
|
|
68
|
-
/** Augmented Router interface */
|
|
69
|
-
interface _TypedRouter
|
|
70
|
-
extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace', 'currentRoute'> {
|
|
71
|
-
readonly currentRoute: _TypedRoute;
|
|
72
|
-
/**
|
|
73
|
-
* Remove an existing route by its name.
|
|
74
|
-
*
|
|
75
|
-
* @param name - Name of the route to remove
|
|
76
|
-
*/
|
|
77
|
-
removeRoute(name: TypedRouteList): void;
|
|
78
|
-
/**
|
|
79
|
-
* Checks if a route with a given name exists
|
|
80
|
-
*
|
|
81
|
-
* @param name - Name of the route to check
|
|
82
|
-
*/
|
|
83
|
-
hasRoute(name: TypedRouteList): boolean;
|
|
84
|
-
/**
|
|
85
|
-
* Returns the {@link RouteLocation | normalized version} of a
|
|
86
|
-
* {@link RouteLocationRaw | route location}. Also includes an \`href\` property
|
|
87
|
-
* that includes any existing \`base\`. By default the \`currentLocation\` used is
|
|
88
|
-
* \`route.currentRoute\` and should only be overriden in advanced use cases.
|
|
89
|
-
*
|
|
90
|
-
* @param to - Raw route location to resolve
|
|
91
|
-
* @param currentLocation - Optional current location to resolve against
|
|
92
|
-
*/
|
|
93
|
-
resolve(
|
|
94
|
-
to: TypedRouteLocationRaw,
|
|
95
|
-
currentLocation?: RouteLocationNormalizedLoaded
|
|
96
|
-
): RouteLocation & {
|
|
97
|
-
href: string;
|
|
98
|
-
};
|
|
99
|
-
/**
|
|
100
|
-
* Programmatically navigate to a new URL by pushing an entry in the history
|
|
101
|
-
* stack.
|
|
102
|
-
*
|
|
103
|
-
* @param to - Route location to navigate to
|
|
104
|
-
*/
|
|
105
|
-
push(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
|
|
106
|
-
/**
|
|
107
|
-
* Programmatically navigate to a new URL by replacing the current entry in
|
|
108
|
-
* the history stack.
|
|
109
|
-
*
|
|
110
|
-
* @param to - Route location to navigate to
|
|
111
|
-
*/
|
|
112
|
-
replace(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface TypedRouter extends _TypedRouter {}
|
|
116
|
-
export type TypedRoute = _TypedRoute;
|
|
117
|
-
export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
|
|
118
|
-
|
|
119
|
-
declare global {
|
|
120
|
-
export interface TypedRouter extends _TypedRouter {}
|
|
121
|
-
export type TypedRoute = _TypedRoute;
|
|
122
|
-
export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
|
|
123
|
-
|
|
124
|
-
const useRoute: typeof useTypedRoute;
|
|
125
|
-
const useRouter: typeof useTypedRouter;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
|
|
129
|
-
to: string | Omit<Exclude<RouteLocationRaw, string>, 'name'> & TypedRouteNamedMapper;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
type _NuxtLink = DefineComponent<
|
|
133
|
-
TypedNuxtLinkProps,
|
|
134
|
-
{},
|
|
135
|
-
{},
|
|
136
|
-
import('vue').ComputedOptions,
|
|
137
|
-
import('vue').MethodOptions,
|
|
138
|
-
import('vue').ComponentOptionsMixin,
|
|
139
|
-
import('vue').ComponentOptionsMixin,
|
|
140
|
-
{},
|
|
141
|
-
string,
|
|
142
|
-
import('vue').VNodeProps &
|
|
143
|
-
import('vue').AllowedComponentProps &
|
|
144
|
-
import('vue').ComponentCustomProps,
|
|
145
|
-
Readonly<TypedNuxtLinkProps>,
|
|
146
|
-
{}
|
|
147
|
-
>;
|
|
148
|
-
|
|
149
|
-
declare module '@vue/runtime-core' {
|
|
150
|
-
export interface GlobalComponents {
|
|
151
|
-
NuxtLink: _NuxtLink;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
`;
|
|
155
|
-
|
|
156
33
|
const watermarkTemplate = `
|
|
157
34
|
// @ts-nocheck
|
|
158
35
|
// eslint-disable
|
|
@@ -164,13 +41,138 @@ const watermarkTemplate = `
|
|
|
164
41
|
|
|
165
42
|
`;
|
|
166
43
|
|
|
167
|
-
function
|
|
44
|
+
function createRuntimeTypeUtils(autoImport) {
|
|
45
|
+
return `
|
|
46
|
+
// Type utils
|
|
47
|
+
type ExtractRequiredParameters<T extends Record<string, any>> = Pick<
|
|
48
|
+
T,
|
|
49
|
+
{ [K in keyof T]: undefined extends T[K] ? never : K }[keyof T]
|
|
50
|
+
>;
|
|
51
|
+
|
|
52
|
+
type HasOneRequiredParameter<T extends TypedRouteList> = [TypedRouteParams[T]] extends [never]
|
|
53
|
+
? false
|
|
54
|
+
: [keyof ExtractRequiredParameters<TypedRouteParams[T]>] extends [undefined]
|
|
55
|
+
? false
|
|
56
|
+
: true;
|
|
57
|
+
|
|
58
|
+
type TypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
59
|
+
name?: T;
|
|
60
|
+
} & ([TypedRouteParams[T]] extends [never]
|
|
61
|
+
? {}
|
|
62
|
+
: HasOneRequiredParameter<T> extends false
|
|
63
|
+
? { params?: TypedRouteParams[T] }
|
|
64
|
+
: { params: TypedRouteParams[T] });
|
|
65
|
+
|
|
66
|
+
type ResolvedTypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
67
|
+
name?: T;
|
|
68
|
+
} & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
|
|
69
|
+
|
|
70
|
+
type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
|
|
71
|
+
|
|
72
|
+
type _TypedRoute = Omit<RouteLocationNormalizedLoaded, 'name' | 'params'> &
|
|
73
|
+
ResolvedTypedRouteNamedMapper;
|
|
74
|
+
type _TypedNamedRoute<T extends TypedRouteList> = Omit<
|
|
75
|
+
RouteLocationNormalizedLoaded,
|
|
76
|
+
'name' | 'params'
|
|
77
|
+
> &
|
|
78
|
+
ResolvedTypedLocationAsRelativeRaw<T>;
|
|
79
|
+
|
|
80
|
+
/** Augmented Router interface */
|
|
81
|
+
interface _TypedRouter
|
|
82
|
+
extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace', 'currentRoute'> {
|
|
83
|
+
readonly currentRoute: _TypedRoute;
|
|
84
|
+
/**
|
|
85
|
+
* Remove an existing route by its name.
|
|
86
|
+
*
|
|
87
|
+
* @param name - Name of the route to remove
|
|
88
|
+
*/
|
|
89
|
+
removeRoute(name: TypedRouteList): void;
|
|
90
|
+
/**
|
|
91
|
+
* Checks if a route with a given name exists
|
|
92
|
+
*
|
|
93
|
+
* @param name - Name of the route to check
|
|
94
|
+
*/
|
|
95
|
+
hasRoute(name: TypedRouteList): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Returns the {@link RouteLocation | normalized version} of a
|
|
98
|
+
* {@link RouteLocationRaw | route location}. Also includes an \`href\` property
|
|
99
|
+
* that includes any existing \`base\`. By default the \`currentLocation\` used is
|
|
100
|
+
* \`route.currentRoute\` and should only be overriden in advanced use cases.
|
|
101
|
+
*
|
|
102
|
+
* @param to - Raw route location to resolve
|
|
103
|
+
* @param currentLocation - Optional current location to resolve against
|
|
104
|
+
*/
|
|
105
|
+
resolve(
|
|
106
|
+
to: TypedRouteLocationRaw,
|
|
107
|
+
currentLocation?: RouteLocationNormalizedLoaded
|
|
108
|
+
): RouteLocation & {
|
|
109
|
+
href: string;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Programmatically navigate to a new URL by pushing an entry in the history
|
|
113
|
+
* stack.
|
|
114
|
+
*
|
|
115
|
+
* @param to - Route location to navigate to
|
|
116
|
+
*/
|
|
117
|
+
push(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
|
|
118
|
+
/**
|
|
119
|
+
* Programmatically navigate to a new URL by replacing the current entry in
|
|
120
|
+
* the history stack.
|
|
121
|
+
*
|
|
122
|
+
* @param to - Route location to navigate to
|
|
123
|
+
*/
|
|
124
|
+
replace(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface TypedRouter extends _TypedRouter {}
|
|
128
|
+
export type TypedRoute = _TypedRoute;
|
|
129
|
+
export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
|
|
130
|
+
|
|
131
|
+
declare global {
|
|
132
|
+
export interface TypedRouter extends _TypedRouter {}
|
|
133
|
+
export type TypedRoute = _TypedRoute;
|
|
134
|
+
export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
|
|
135
|
+
|
|
136
|
+
${autoImport ? `const useRoute: typeof useTypedRoute;
|
|
137
|
+
const useRouter: typeof useTypedRouter;` : ""}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
|
|
141
|
+
to: string | Omit<Exclude<RouteLocationRaw, string>, 'name'> & TypedRouteNamedMapper;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
type _NuxtLink = DefineComponent<
|
|
145
|
+
TypedNuxtLinkProps,
|
|
146
|
+
{},
|
|
147
|
+
{},
|
|
148
|
+
import('vue').ComputedOptions,
|
|
149
|
+
import('vue').MethodOptions,
|
|
150
|
+
import('vue').ComponentOptionsMixin,
|
|
151
|
+
import('vue').ComponentOptionsMixin,
|
|
152
|
+
{},
|
|
153
|
+
string,
|
|
154
|
+
import('vue').VNodeProps &
|
|
155
|
+
import('vue').AllowedComponentProps &
|
|
156
|
+
import('vue').ComponentCustomProps,
|
|
157
|
+
Readonly<TypedNuxtLinkProps>,
|
|
158
|
+
{}
|
|
159
|
+
>;
|
|
160
|
+
|
|
161
|
+
declare module '@vue/runtime-core' {
|
|
162
|
+
export interface GlobalComponents {
|
|
163
|
+
NuxtLink: _NuxtLink;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function createDeclarationRoutesFile(autoImport) {
|
|
168
170
|
return `
|
|
169
171
|
${watermarkTemplate}
|
|
170
172
|
|
|
171
173
|
${staticTypesImports}
|
|
172
174
|
|
|
173
|
-
${
|
|
175
|
+
${createRuntimeTypeUtils(autoImport)}
|
|
174
176
|
`;
|
|
175
177
|
}
|
|
176
178
|
|
|
@@ -179,8 +181,8 @@ function createRuntimeIndexFile() {
|
|
|
179
181
|
${watermarkTemplate}
|
|
180
182
|
export { routesNames } from './__routes';
|
|
181
183
|
export type { TypedRouteList } from './__routes';
|
|
182
|
-
export
|
|
183
|
-
export
|
|
184
|
+
export {useRouter} from './__useTypedRouter';
|
|
185
|
+
export {useRoute} from './__useTypedRoute';
|
|
184
186
|
`;
|
|
185
187
|
}
|
|
186
188
|
|
|
@@ -280,7 +282,7 @@ function createUseTypedRouteFile(routesDeclTemplate) {
|
|
|
280
282
|
return `
|
|
281
283
|
${watermarkTemplate}
|
|
282
284
|
import { useRoute } from '#app';
|
|
283
|
-
import { TypedRouteList } from './__routes';
|
|
285
|
+
import type { TypedRouteList } from './__routes';
|
|
284
286
|
|
|
285
287
|
/** Acts the same as \`useRoute\`, but typed.
|
|
286
288
|
*
|
|
@@ -310,14 +312,16 @@ export function useTypedRoute<T extends TypedRouteList = never>(
|
|
|
310
312
|
return route as any;
|
|
311
313
|
}
|
|
312
314
|
|
|
313
|
-
|
|
315
|
+
export const useRoute = useTypedRoute;
|
|
316
|
+
|
|
317
|
+
`;
|
|
314
318
|
}
|
|
315
319
|
|
|
316
320
|
function createRuntimeUseTypedRouterFile(routesDeclTemplate) {
|
|
317
321
|
return `
|
|
318
322
|
${watermarkTemplate}
|
|
319
323
|
import { useRouter } from '#app';
|
|
320
|
-
import { TypedRouter } from './typed-router';
|
|
324
|
+
import type { TypedRouter } from './typed-router';
|
|
321
325
|
|
|
322
326
|
/** Returns instances of $typedRouter and $routesList fully typed to use in your components or your Vuex/Pinia store
|
|
323
327
|
*
|
|
@@ -333,6 +337,7 @@ function createRuntimeUseTypedRouterFile(routesDeclTemplate) {
|
|
|
333
337
|
return router;
|
|
334
338
|
};
|
|
335
339
|
|
|
340
|
+
export const useRouter = useTypedRouter;
|
|
336
341
|
`;
|
|
337
342
|
}
|
|
338
343
|
|
|
@@ -417,6 +422,7 @@ function handlePluginFileSave({
|
|
|
417
422
|
let previousGeneratedRoutes = "";
|
|
418
423
|
async function saveGeneratedFiles({
|
|
419
424
|
rootDir,
|
|
425
|
+
autoImport,
|
|
420
426
|
outputData: { routesDeclTemplate, routesList, routesObjectTemplate, routesParams }
|
|
421
427
|
}) {
|
|
422
428
|
const filesMap = [
|
|
@@ -439,7 +445,7 @@ async function saveGeneratedFiles({
|
|
|
439
445
|
},
|
|
440
446
|
{
|
|
441
447
|
fileName: `typed-router.d.ts`,
|
|
442
|
-
content: createDeclarationRoutesFile()
|
|
448
|
+
content: createDeclarationRoutesFile(autoImport)
|
|
443
449
|
},
|
|
444
450
|
{
|
|
445
451
|
fileName: "index.ts",
|
|
@@ -451,7 +457,7 @@ async function saveGeneratedFiles({
|
|
|
451
457
|
);
|
|
452
458
|
if (previousGeneratedRoutes !== routesList.join(",")) {
|
|
453
459
|
previousGeneratedRoutes = routesList.join(",");
|
|
454
|
-
console.log(logSymbols.success, `
|
|
460
|
+
console.log(logSymbols.success, `Router autocompletions generated \u{1F6A6}`);
|
|
455
461
|
}
|
|
456
462
|
}
|
|
457
463
|
|
|
@@ -611,8 +617,10 @@ function startGenerator({ output, routesConfig }) {
|
|
|
611
617
|
output.routesDeclTemplate += "}";
|
|
612
618
|
}
|
|
613
619
|
|
|
614
|
-
function createTypedRouter({
|
|
620
|
+
function createTypedRouter({ plugin, nuxt }) {
|
|
615
621
|
try {
|
|
622
|
+
const rootDir = nuxt.options.rootDir;
|
|
623
|
+
const autoImport = nuxt.options.imports.autoImport ?? true;
|
|
616
624
|
extendPages(async (routes) => {
|
|
617
625
|
if (routes.length) {
|
|
618
626
|
const outputData = constructRouteMap(routes);
|
|
@@ -624,6 +632,7 @@ function createTypedRouter({ rootDir, plugin, nuxt }) {
|
|
|
624
632
|
});
|
|
625
633
|
}
|
|
626
634
|
await saveGeneratedFiles({
|
|
635
|
+
autoImport,
|
|
627
636
|
rootDir,
|
|
628
637
|
outputData
|
|
629
638
|
});
|
|
@@ -663,8 +672,9 @@ const module = defineNuxtModule({
|
|
|
663
672
|
nuxt.options.typescript.tsConfig = {
|
|
664
673
|
include: ["./typed-router/typed-router.d.ts"]
|
|
665
674
|
};
|
|
666
|
-
|
|
667
|
-
|
|
675
|
+
const typedRouterOptions = { nuxt, plugin };
|
|
676
|
+
nuxt.hook("pages:extend", () => createTypedRouter(typedRouterOptions));
|
|
677
|
+
createTypedRouter(typedRouterOptions);
|
|
668
678
|
}
|
|
669
679
|
});
|
|
670
680
|
|