nuxt-typed-router 2.0.2 → 2.1.0-beta.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/dist/module.json +1 -1
- package/dist/module.mjs +39 -23
- package/package.json +6 -4
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -26,6 +26,8 @@ import type {
|
|
|
26
26
|
TypedRouteParams,
|
|
27
27
|
ResolvedTypedRouteNamedMapper,
|
|
28
28
|
} from './__routes';
|
|
29
|
+
import {useTypedRoute} from './__useTypedRoute';
|
|
30
|
+
import {useTypedRouter} from './__useTypedRouter';
|
|
29
31
|
`;
|
|
30
32
|
|
|
31
33
|
const staticTypeUtils = `
|
|
@@ -51,7 +53,7 @@ type TypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
|
51
53
|
|
|
52
54
|
type ResolvedTypedLocationAsRelativeRaw<T extends TypedRouteList> = {
|
|
53
55
|
name?: T;
|
|
54
|
-
} & ([TypedRouteParams[T]] extends [never] ? {} : { params:
|
|
56
|
+
} & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
|
|
55
57
|
|
|
56
58
|
type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
|
|
57
59
|
|
|
@@ -65,7 +67,8 @@ type _TypedNamedRoute<T extends TypedRouteList> = Omit<
|
|
|
65
67
|
|
|
66
68
|
/** Augmented Router interface */
|
|
67
69
|
interface _TypedRouter
|
|
68
|
-
extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace'> {
|
|
70
|
+
extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace', 'currentRoute'> {
|
|
71
|
+
readonly currentRoute: _TypedRoute;
|
|
69
72
|
/**
|
|
70
73
|
* Remove an existing route by its name.
|
|
71
74
|
*
|
|
@@ -117,6 +120,9 @@ declare global {
|
|
|
117
120
|
export interface TypedRouter extends _TypedRouter {}
|
|
118
121
|
export type TypedRoute = _TypedRoute;
|
|
119
122
|
export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
|
|
123
|
+
|
|
124
|
+
const useRoute: typeof useTypedRoute;
|
|
125
|
+
const useRouter: typeof useTypedRouter;
|
|
120
126
|
}
|
|
121
127
|
|
|
122
128
|
type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
|
|
@@ -215,7 +221,9 @@ function createTypedRouteNamedMapperExport(routesParams) {
|
|
|
215
221
|
return `export type TypedRouteNamedMapper =
|
|
216
222
|
${routesParams.map(
|
|
217
223
|
({ name, params }) => `{name: "${name}" ${params.length ? `, params${params.some((s) => s.required) ? "" : "?"}: {
|
|
218
|
-
${params.map(
|
|
224
|
+
${params.map(
|
|
225
|
+
({ key, required, catchAll }) => `"${key}"${required ? "" : "?"}: (string | number)${catchAll ? "[]" : ""}`
|
|
226
|
+
).join(",\n")}
|
|
219
227
|
}` : ""}}`
|
|
220
228
|
).join("|\n")}
|
|
221
229
|
`;
|
|
@@ -229,7 +237,7 @@ function createResolvedTypedRouteNamedMapperExport(routesParams) {
|
|
|
229
237
|
${routesParams.map(
|
|
230
238
|
({ name, params }) => `{name: "${name}" ${params.length ? `, params: {
|
|
231
239
|
${params.map(
|
|
232
|
-
({ key, notRequiredOnPage }) => `"${key}"${notRequiredOnPage ? "?" : ""}: string`
|
|
240
|
+
({ key, notRequiredOnPage, catchAll }) => `"${key}"${notRequiredOnPage ? "?" : ""}: string${catchAll ? "[]" : ""}`
|
|
233
241
|
).join(",\n")}
|
|
234
242
|
}` : ""}}`
|
|
235
243
|
).join("|\n")}
|
|
@@ -359,12 +367,12 @@ dirname(fileURLToPath(import.meta.url));
|
|
|
359
367
|
async function processPathAndWriteFile({
|
|
360
368
|
content,
|
|
361
369
|
fileName,
|
|
362
|
-
|
|
370
|
+
rootDir,
|
|
363
371
|
outDir
|
|
364
372
|
}) {
|
|
365
373
|
try {
|
|
366
374
|
const finalOutDir = outDir ?? `.nuxt/typed-router`;
|
|
367
|
-
const processedOutDir = resolve(
|
|
375
|
+
const processedOutDir = resolve(rootDir, finalOutDir);
|
|
368
376
|
const outputFile = resolve(process.cwd(), `${processedOutDir}/${fileName}`);
|
|
369
377
|
const formatedContent = await formatOutputWithPrettier(content);
|
|
370
378
|
if (fs.existsSync(outputFile)) {
|
|
@@ -391,15 +399,15 @@ async function writeFile(path, content) {
|
|
|
391
399
|
|
|
392
400
|
function handlePluginFileSave({
|
|
393
401
|
nuxt,
|
|
394
|
-
|
|
402
|
+
rootDir,
|
|
395
403
|
routesDeclTemplate
|
|
396
404
|
}) {
|
|
397
405
|
const pluginName = "__typed-router.ts";
|
|
398
406
|
nuxt.hook("build:done", async () => {
|
|
399
|
-
const pluginFolder = `${
|
|
407
|
+
const pluginFolder = `${rootDir}/plugins`;
|
|
400
408
|
await processPathAndWriteFile({
|
|
401
409
|
outDir: pluginFolder,
|
|
402
|
-
|
|
410
|
+
rootDir,
|
|
403
411
|
fileName: pluginName,
|
|
404
412
|
content: createRuntimePluginFile(routesDeclTemplate)
|
|
405
413
|
});
|
|
@@ -408,7 +416,7 @@ function handlePluginFileSave({
|
|
|
408
416
|
|
|
409
417
|
let previousGeneratedRoutes = "";
|
|
410
418
|
async function saveGeneratedFiles({
|
|
411
|
-
|
|
419
|
+
rootDir,
|
|
412
420
|
outputData: { routesDeclTemplate, routesList, routesObjectTemplate, routesParams }
|
|
413
421
|
}) {
|
|
414
422
|
const filesMap = [
|
|
@@ -439,7 +447,7 @@ async function saveGeneratedFiles({
|
|
|
439
447
|
}
|
|
440
448
|
];
|
|
441
449
|
await Promise.all(
|
|
442
|
-
filesMap.map(({ content, fileName }) => processPathAndWriteFile({
|
|
450
|
+
filesMap.map(({ content, fileName }) => processPathAndWriteFile({ rootDir, content, fileName }))
|
|
443
451
|
);
|
|
444
452
|
if (previousGeneratedRoutes !== routesList.join(",")) {
|
|
445
453
|
previousGeneratedRoutes = routesList.join(",");
|
|
@@ -451,24 +459,25 @@ function isItemLast(array, index) {
|
|
|
451
459
|
return array ? index === array.length - 1 : false;
|
|
452
460
|
}
|
|
453
461
|
|
|
454
|
-
const routeParamExtractRegxp = /(:(\w+)(\?)?)+/g;
|
|
462
|
+
const routeParamExtractRegxp = /(:(\w+)(\(.+\)[*+]?)?(\?)?)+/g;
|
|
455
463
|
function extractRouteParamsFromPath(path, isIndexFileForRouting, previousParams) {
|
|
456
464
|
let params = [];
|
|
457
465
|
let matches;
|
|
458
466
|
do {
|
|
459
467
|
matches = routeParamExtractRegxp.exec(path);
|
|
460
468
|
if (matches) {
|
|
461
|
-
const [_, mtch, key, optional] = matches;
|
|
469
|
+
const [_, mtch, key, catchAll, optional] = matches;
|
|
462
470
|
if (mtch) {
|
|
463
|
-
params.push({ name: key, optional: !!optional });
|
|
471
|
+
params.push({ name: key, optional: !!optional, catchAll: !!catchAll });
|
|
464
472
|
}
|
|
465
473
|
}
|
|
466
474
|
} while (matches);
|
|
467
475
|
let allMergedParams = params.map(
|
|
468
|
-
({ name, optional }) => ({
|
|
476
|
+
({ name, optional, catchAll }) => ({
|
|
469
477
|
key: name,
|
|
470
478
|
required: !optional,
|
|
471
|
-
notRequiredOnPage: optional
|
|
479
|
+
notRequiredOnPage: optional,
|
|
480
|
+
catchAll
|
|
472
481
|
})
|
|
473
482
|
);
|
|
474
483
|
if (previousParams?.length) {
|
|
@@ -602,16 +611,20 @@ function startGenerator({ output, routesConfig }) {
|
|
|
602
611
|
output.routesDeclTemplate += "}";
|
|
603
612
|
}
|
|
604
613
|
|
|
605
|
-
function createTypedRouter({
|
|
614
|
+
function createTypedRouter({ rootDir, plugin, nuxt }) {
|
|
606
615
|
try {
|
|
607
616
|
extendPages(async (routes) => {
|
|
608
617
|
if (routes.length) {
|
|
609
618
|
const outputData = constructRouteMap(routes);
|
|
610
619
|
if (plugin) {
|
|
611
|
-
handlePluginFileSave({
|
|
620
|
+
handlePluginFileSave({
|
|
621
|
+
nuxt,
|
|
622
|
+
routesDeclTemplate: outputData.routesDeclTemplate,
|
|
623
|
+
rootDir
|
|
624
|
+
});
|
|
612
625
|
}
|
|
613
626
|
await saveGeneratedFiles({
|
|
614
|
-
|
|
627
|
+
rootDir,
|
|
615
628
|
outputData
|
|
616
629
|
});
|
|
617
630
|
} else {
|
|
@@ -640,15 +653,18 @@ const module = defineNuxtModule({
|
|
|
640
653
|
plugin: false
|
|
641
654
|
},
|
|
642
655
|
setup(moduleOptions, nuxt) {
|
|
643
|
-
const
|
|
656
|
+
const rootDir = nuxt.options.rootDir;
|
|
644
657
|
const { plugin } = moduleOptions;
|
|
645
658
|
const { resolve } = createResolver(import.meta.url);
|
|
646
659
|
nuxt.options.alias = {
|
|
647
660
|
...nuxt.options.alias,
|
|
648
|
-
"@typed-router": resolve(`${
|
|
661
|
+
"@typed-router": resolve(`${rootDir}/.nuxt/typed-router`)
|
|
662
|
+
};
|
|
663
|
+
nuxt.options.typescript.tsConfig = {
|
|
664
|
+
include: ["./typed-router/typed-router.d.ts"]
|
|
649
665
|
};
|
|
650
|
-
nuxt.hook("pages:extend", () => createTypedRouter({
|
|
651
|
-
createTypedRouter({
|
|
666
|
+
nuxt.hook("pages:extend", () => createTypedRouter({ rootDir, nuxt, plugin }));
|
|
667
|
+
createTypedRouter({ rootDir, nuxt, plugin });
|
|
652
668
|
}
|
|
653
669
|
});
|
|
654
670
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-typed-router",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.1.0-beta.0",
|
|
4
4
|
"description": "Provide autocompletion for pages route names generated by Nuxt router",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/module.cjs",
|
|
@@ -59,10 +59,11 @@
|
|
|
59
59
|
"log-symbols": "^5.1.0",
|
|
60
60
|
"mkdirp": "^1.0.4",
|
|
61
61
|
"pathe": "1.0.0",
|
|
62
|
-
"prettier": "2.8.
|
|
62
|
+
"prettier": "2.8.3"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@nuxt/module-builder": "^0.2.1",
|
|
66
|
+
"@nuxt/test-utils": "^3.0.0",
|
|
66
67
|
"@nuxt/types": "^2.15.8",
|
|
67
68
|
"@nuxtjs/eslint-config-typescript": "^12.0.0",
|
|
68
69
|
"@types/lodash-es": "^4.17.6",
|
|
@@ -70,11 +71,12 @@
|
|
|
70
71
|
"@types/node": "^17.0.23",
|
|
71
72
|
"@types/prettier": "^2.7.2",
|
|
72
73
|
"cross-env": "^7.0.3",
|
|
73
|
-
"eslint": "8.
|
|
74
|
+
"eslint": "8.32.0",
|
|
74
75
|
"eslint-config-prettier": "^8.6.0",
|
|
76
|
+
"eslint-plugin-vue": "^9.9.0",
|
|
75
77
|
"nuxt": "3.0.0",
|
|
76
78
|
"typescript": "^4.9.4",
|
|
77
|
-
"vitest": "^0.27.
|
|
79
|
+
"vitest": "^0.27.2",
|
|
78
80
|
"vue-router": "^4.1.6"
|
|
79
81
|
}
|
|
80
82
|
}
|