nuxt-typed-router 2.1.2 → 2.1.3
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 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +76 -32
- package/package.json +8 -6
package/README.md
CHANGED
|
@@ -19,11 +19,14 @@
|
|
|
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
21
|
- `NuxtLink` route autocomplete and params type-check
|
|
22
|
-
- `useRouter` and `
|
|
22
|
+
- `useRouter`, `useRoute` and `navigateTo` route autocomplete and params type-check
|
|
23
23
|
- Supports optional params and catchAll routes
|
|
24
24
|
- Infer route params based on route name
|
|
25
25
|
- Supports routes defined in `config.extendRoutes`
|
|
26
26
|
|
|
27
|
+
> ⚠️ Since `v2.1.x`, `useTypedRouter` and `useTypedRoute` are no longer exported.
|
|
28
|
+
The package can now override types from `useRouter`, `useRoute` and `navigateTo`
|
|
29
|
+
|
|
27
30
|
<br/>
|
|
28
31
|
|
|
29
32
|
<br/>
|
|
@@ -33,6 +36,8 @@
|
|
|
33
36
|
<br/>
|
|
34
37
|
|
|
35
38
|
|
|
39
|
+
|
|
40
|
+
|
|
36
41
|
# Documentation
|
|
37
42
|
|
|
38
43
|
[](https://nuxt-typed-router.vercel.app/)
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -29,11 +29,13 @@ function createDeclarationRoutesFile(autoImport) {
|
|
|
29
29
|
import type { TypedRouteNamedMapper } from './__routes';
|
|
30
30
|
import { useRoute as _useRoute } from './__useTypedRoute';
|
|
31
31
|
import { useRouter as _useRouter } from './__useTypedRouter';
|
|
32
|
+
import { navigateTo as _navigateTo } from './__utils';
|
|
32
33
|
|
|
33
34
|
declare global {
|
|
34
35
|
|
|
35
36
|
${autoImport ? `const useRoute: typeof _useRoute;
|
|
36
|
-
|
|
37
|
+
const useRouter: typeof _useRouter;
|
|
38
|
+
const navigateTo: typeof _navigateTo;` : ""}
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
|
|
@@ -72,14 +74,15 @@ function createRuntimeIndexFile() {
|
|
|
72
74
|
export type { TypedRouteList, TypedRouteNamedMapper, TypedRouteParams } from './__routes';
|
|
73
75
|
export {useRouter} from './__useTypedRouter';
|
|
74
76
|
export {useRoute} from './__useTypedRoute';
|
|
75
|
-
export type {TypedRoute, TypedRouter, TypedNamedRoute } from './__router'
|
|
77
|
+
export type {TypedRoute, TypedRouter, TypedNamedRoute } from './__router';
|
|
78
|
+
export * from './__utils';
|
|
76
79
|
`;
|
|
77
80
|
}
|
|
78
81
|
|
|
79
82
|
function createRuntimePluginFile(routesDeclTemplate) {
|
|
80
83
|
return `
|
|
81
84
|
${watermarkTemplate}
|
|
82
|
-
import { defineNuxtPlugin } from '#app';
|
|
85
|
+
import { defineNuxtPlugin, useRouter, useRoute } from '#app';
|
|
83
86
|
import {TypedRouter, TypedRoute} from '@typed-router';
|
|
84
87
|
|
|
85
88
|
export default defineNuxtPlugin(() => {
|
|
@@ -337,6 +340,23 @@ function createRuntimeRouterTypes() {
|
|
|
337
340
|
`;
|
|
338
341
|
}
|
|
339
342
|
|
|
343
|
+
function createRuntimeNavigateToFunction() {
|
|
344
|
+
return `
|
|
345
|
+
${watermarkTemplate}
|
|
346
|
+
import { navigateTo as defaultNavigateTo } from '#app';
|
|
347
|
+
import { NavigateToOptions } from 'nuxt/dist/app/composables/router';
|
|
348
|
+
import { NavigationFailure } from 'vue-router';
|
|
349
|
+
import type { ResolvedTypedLocationAsRelativeRaw, TypedNamedRoute } from './__router';
|
|
350
|
+
import { TypedRouteList } from './__routes';
|
|
351
|
+
|
|
352
|
+
export const navigateTo: <T extends TypedRouteList>(
|
|
353
|
+
to: ResolvedTypedLocationAsRelativeRaw<T>,
|
|
354
|
+
options?: NavigateToOptions
|
|
355
|
+
) => Promise<void | NavigationFailure | TypedNamedRoute<T>> = defaultNavigateTo as any;
|
|
356
|
+
|
|
357
|
+
`;
|
|
358
|
+
}
|
|
359
|
+
|
|
340
360
|
const { resolveConfig, format } = prettier;
|
|
341
361
|
const defaultPrettierOptions = {
|
|
342
362
|
printWidth: 100,
|
|
@@ -398,21 +418,20 @@ async function writeFile(path, content) {
|
|
|
398
418
|
}
|
|
399
419
|
}
|
|
400
420
|
|
|
401
|
-
function handlePluginFileSave({
|
|
402
|
-
nuxt,
|
|
403
|
-
rootDir,
|
|
404
|
-
routesDeclTemplate
|
|
405
|
-
}) {
|
|
421
|
+
function handlePluginFileSave({ nuxt, routesDeclTemplate }) {
|
|
406
422
|
const pluginName = "__typed-router.ts";
|
|
407
|
-
|
|
408
|
-
|
|
423
|
+
const srcDir = nuxt.options.srcDir;
|
|
424
|
+
async function savePlugin() {
|
|
425
|
+
const pluginFolder = `${srcDir}/plugins`;
|
|
409
426
|
await processPathAndWriteFile({
|
|
410
427
|
outDir: pluginFolder,
|
|
411
|
-
rootDir,
|
|
428
|
+
rootDir: srcDir,
|
|
412
429
|
fileName: pluginName,
|
|
413
430
|
content: createRuntimePluginFile(routesDeclTemplate)
|
|
414
431
|
});
|
|
415
|
-
}
|
|
432
|
+
}
|
|
433
|
+
nuxt.hook("build:done", savePlugin);
|
|
434
|
+
nuxt.hook("prepare:types", savePlugin);
|
|
416
435
|
}
|
|
417
436
|
|
|
418
437
|
let previousGeneratedRoutes = "";
|
|
@@ -439,6 +458,10 @@ async function saveGeneratedFiles({
|
|
|
439
458
|
routesParams
|
|
440
459
|
})
|
|
441
460
|
},
|
|
461
|
+
{
|
|
462
|
+
fileName: "__utils.ts",
|
|
463
|
+
content: createRuntimeNavigateToFunction()
|
|
464
|
+
},
|
|
442
465
|
{
|
|
443
466
|
fileName: `__router.d.ts`,
|
|
444
467
|
content: createRuntimeRouterTypes()
|
|
@@ -617,36 +640,58 @@ function startGenerator({ output, routesConfig }) {
|
|
|
617
640
|
output.routesDeclTemplate += "}";
|
|
618
641
|
}
|
|
619
642
|
|
|
620
|
-
|
|
643
|
+
let hasLoggedNoPages = false;
|
|
644
|
+
let hasRoutesDefined = false;
|
|
645
|
+
async function createTypedRouter({
|
|
646
|
+
plugin,
|
|
647
|
+
nuxt,
|
|
648
|
+
routesConfig,
|
|
649
|
+
isHookCall = false
|
|
650
|
+
}) {
|
|
621
651
|
try {
|
|
622
652
|
const rootDir = nuxt.options.rootDir;
|
|
623
653
|
const autoImport = nuxt.options.imports.autoImport ?? true;
|
|
654
|
+
if (!isHookCall) {
|
|
655
|
+
if (routesConfig) {
|
|
656
|
+
await nuxt.callHook("pages:extend", routesConfig);
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
nuxt.hook("pages:extend", (routesConfig2) => {
|
|
660
|
+
createTypedRouter({ nuxt, plugin, routesConfig: routesConfig2, isHookCall: true });
|
|
661
|
+
});
|
|
662
|
+
nuxt.hook("modules:done", () => {
|
|
663
|
+
createTypedRouter({ nuxt, plugin, isHookCall: true });
|
|
664
|
+
});
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
624
667
|
extendPages(async (routes) => {
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
rootDir
|
|
632
|
-
});
|
|
633
|
-
}
|
|
634
|
-
await saveGeneratedFiles({
|
|
635
|
-
autoImport,
|
|
636
|
-
rootDir,
|
|
637
|
-
outputData
|
|
668
|
+
hasRoutesDefined = true;
|
|
669
|
+
const outputData = constructRouteMap(routes);
|
|
670
|
+
if (plugin) {
|
|
671
|
+
handlePluginFileSave({
|
|
672
|
+
nuxt,
|
|
673
|
+
routesDeclTemplate: outputData.routesDeclTemplate
|
|
638
674
|
});
|
|
639
|
-
}
|
|
675
|
+
}
|
|
676
|
+
await saveGeneratedFiles({
|
|
677
|
+
autoImport,
|
|
678
|
+
rootDir,
|
|
679
|
+
outputData
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
setTimeout(() => {
|
|
683
|
+
if (!hasRoutesDefined && !hasLoggedNoPages) {
|
|
684
|
+
hasLoggedNoPages = true;
|
|
640
685
|
console.log(
|
|
641
686
|
logSymbols.warning,
|
|
642
687
|
chalk.yellow(
|
|
643
|
-
|
|
688
|
+
`\u{1F6A6} No routes defined. Check if your ${chalk.underline(
|
|
644
689
|
chalk.bold("pages")
|
|
645
|
-
)} folder exists
|
|
690
|
+
)} folder exists`
|
|
646
691
|
)
|
|
647
692
|
);
|
|
648
693
|
}
|
|
649
|
-
});
|
|
694
|
+
}, 3e3);
|
|
650
695
|
} catch (e) {
|
|
651
696
|
console.error(chalk.red("Error while generating routes definitions model"), "\n" + e);
|
|
652
697
|
}
|
|
@@ -673,8 +718,7 @@ const module = defineNuxtModule({
|
|
|
673
718
|
include: ["./typed-router/typed-router.d.ts"]
|
|
674
719
|
};
|
|
675
720
|
const typedRouterOptions = { nuxt, plugin };
|
|
676
|
-
|
|
677
|
-
createTypedRouter(typedRouterOptions);
|
|
721
|
+
createTypedRouter({ ...typedRouterOptions });
|
|
678
722
|
}
|
|
679
723
|
});
|
|
680
724
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-typed-router",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"description": "Provide autocompletion for pages route names generated by Nuxt router",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/module.cjs",
|
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
"dev:build": "nuxi build playground",
|
|
22
22
|
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground && pnpm run test:prepare-fixtures",
|
|
23
23
|
"build:test": "cross-env NUXT_BUILD_TYPE=stub pnpm run prepack && pnpm run dev:build",
|
|
24
|
-
"test:prepare-fixtures": "nuxi prepare test/fixtures/simple",
|
|
24
|
+
"test:prepare-fixtures": "nuxi prepare test/fixtures/simple && nuxi generate test/fixtures/complex",
|
|
25
25
|
"test:fixtures": " vitest run --dir test",
|
|
26
|
-
"test:types": "pnpm run test:vue && vitest typecheck --run --dir test",
|
|
26
|
+
"test:types": "pnpm run typecheck && pnpm run test:vue && vitest typecheck --run --dir test",
|
|
27
27
|
"test:vue": "vue-tsc -p test/fixtures/simple/tsconfig.json --noEmit && vue-tsc -p test/fixtures/complex/tsconfig.json --noEmit",
|
|
28
28
|
"test": "pnpm run dev:prepare && pnpm run test:fixtures && pnpm run test:types",
|
|
29
29
|
"docs:dev": "cd docs && pnpm run dev",
|
|
30
|
-
"docs:build": "npm run dev:prepare && cd docs && nuxi generate"
|
|
30
|
+
"docs:build": "npm run dev:prepare && cd docs && nuxi generate",
|
|
31
|
+
"typecheck": "tsc --noEmit"
|
|
31
32
|
},
|
|
32
33
|
"publishConfig": {
|
|
33
34
|
"access": "public"
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
"lodash-es": "^4.17.21",
|
|
62
63
|
"log-symbols": "^5.1.0",
|
|
63
64
|
"mkdirp": "^1.0.4",
|
|
64
|
-
"pathe": "1.
|
|
65
|
+
"pathe": "1.1.0",
|
|
65
66
|
"prettier": "2.8.3"
|
|
66
67
|
},
|
|
67
68
|
"devDependencies": {
|
|
@@ -78,8 +79,9 @@
|
|
|
78
79
|
"eslint-config-prettier": "^8.6.0",
|
|
79
80
|
"eslint-plugin-vue": "^9.9.0",
|
|
80
81
|
"nuxt": "3.0.0",
|
|
82
|
+
"playwright": "1.29.2",
|
|
81
83
|
"typescript": "^4.9.4",
|
|
82
|
-
"vitest": "^0.
|
|
84
|
+
"vitest": "^0.28.1",
|
|
83
85
|
"vue-router": "^4.1.6",
|
|
84
86
|
"vue-tsc": "^1.0.24"
|
|
85
87
|
}
|