nuxt-typed-router 1.2.3 → 1.2.5

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 CHANGED
@@ -58,7 +58,7 @@ First, register the module in the `nuxt.config.ts`
58
58
 
59
59
  ```ts
60
60
  export default defineNuxtConfig({
61
- buildModules: ['nuxt-typed-router'],
61
+ modules: ['nuxt-typed-router'],
62
62
  nuxtTypedRouter: {
63
63
  // options
64
64
  },
package/dist/module.d.ts CHANGED
@@ -9,8 +9,11 @@ interface ModuleOptions {
9
9
  * @default "routerPagesNames"
10
10
  * */
11
11
  routesObjectName?: string;
12
- /** @deprecated */
13
- stripAtFromName?: boolean;
12
+ /**
13
+ * Set to false if you don't want a plugin generated
14
+ * @default true
15
+ */
16
+ plugin?: boolean;
14
17
  }
15
18
 
16
19
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
package/dist/module.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "nuxt": "^3.0.0-rc.1",
6
6
  "bridge": false
7
7
  },
8
- "version": "1.2.3"
8
+ "version": "1.2.5"
9
9
  }
package/dist/module.mjs CHANGED
@@ -96,13 +96,17 @@ const routeParamExtractRegxp = /:(\w+)/;
96
96
  function extractRouteParamsFromPath(path, previousParams) {
97
97
  const params = path.match(routeParamExtractRegxp) ?? [];
98
98
  params?.shift();
99
- let allMergedParams = params.map((m) => ({
100
- key: m,
101
- type: "string | number",
102
- required: true
103
- }));
99
+ let allMergedParams = params.map(
100
+ (m) => ({
101
+ key: m,
102
+ type: "string | number",
103
+ required: true
104
+ })
105
+ );
104
106
  if (previousParams?.length) {
105
- allMergedParams = allMergedParams.concat(previousParams.map((m) => ({ ...m, required: false })));
107
+ allMergedParams = allMergedParams.concat(
108
+ previousParams.map((m) => ({ ...m, required: false }))
109
+ );
106
110
  }
107
111
  return allMergedParams;
108
112
  }
@@ -165,15 +169,17 @@ function walkThoughRoutes({
165
169
  output.routesObjectTemplate += `${nameKey}:{`;
166
170
  output.routesDeclTemplate += `"${nameKey}":{`;
167
171
  const allRouteParams = extractRouteParamsFromPath(route.path, previousParams);
168
- childrenChunks?.map((routeConfig, index) => walkThoughRoutes({
169
- route: routeConfig,
170
- level: level + 1,
171
- siblings: extractUnMatchingSiblings(route, siblings),
172
- parentName: nameKey,
173
- previousParams: allRouteParams,
174
- output,
175
- isLast: isItemLast(childrenChunks, index)
176
- }));
172
+ childrenChunks?.map(
173
+ (routeConfig, index) => walkThoughRoutes({
174
+ route: routeConfig,
175
+ level: level + 1,
176
+ siblings: extractUnMatchingSiblings(route, siblings),
177
+ parentName: nameKey,
178
+ previousParams: allRouteParams,
179
+ output,
180
+ isLast: isItemLast(childrenChunks, index)
181
+ })
182
+ );
177
183
  output.routesObjectTemplate += "},";
178
184
  output.routesDeclTemplate += `}${isLast ? "" : ","}`;
179
185
  } else if (route.name) {
@@ -226,7 +232,7 @@ const staticDeclarations = `
226
232
  TypedLocationAsRelativeRaw<T> &
227
233
  RouteLocationOptions;
228
234
 
229
- export interface TypedRouter {
235
+ interface _TypedRouter {
230
236
  /**
231
237
  * Remove an existing route by its name.
232
238
  *
@@ -273,19 +279,10 @@ const staticDeclarations = `
273
279
  to: TypedRouteLocationRaw<T>
274
280
  ): Promise<NavigationFailure | void | undefined>;
275
281
  }
276
-
277
- declare module 'nuxt/dist/app/nuxt' {
278
- export interface NuxtApp {
279
- $typedRouter: TypedRouter;
280
- $routesList: RouteListDecl;
281
- }
282
- }
283
282
 
284
- declare module '@vue/runtime-core' {
285
- interface ComponentCustomProperties {
286
- $typedRouter: TypedRouter;
287
- $routesList: RouteListDecl;
288
- }
283
+ export interface TypedRouter extends _TypedRouter {}
284
+ declare global {
285
+ export interface TypedRouter extends _TypedRouter {}
289
286
  }
290
287
  `;
291
288
 
@@ -294,12 +291,13 @@ function createRuntimePluginFile(routesDeclTemplate) {
294
291
  ${signatureTemplate}
295
292
  import { defineNuxtPlugin } from '#app';
296
293
 
297
- export default defineNuxtPlugin((nuxtApp) => {
294
+ export default defineNuxtPlugin(() => {
295
+ const router = useRouter();
298
296
  const routesList = ${routesDeclTemplate};
299
297
 
300
298
  return {
301
299
  provide: {
302
- typedRouter: nuxtApp.$router,
300
+ typedRouter: router as TypedRouter,
303
301
  routesList,
304
302
  },
305
303
  };
@@ -379,31 +377,62 @@ function createTypedRouteListExport(routesList) {
379
377
  }
380
378
  function createTypedRouteParamsExport(routesParams) {
381
379
  return `export type TypedRouteParams = {
382
- ${routesParams.map(({ name, params }) => `"${name}": ${params.length ? `{
380
+ ${routesParams.map(
381
+ ({ name, params }) => `"${name}": ${params.length ? `{
383
382
  ${params.map(({ key, required, type }) => `"${key}"${required ? "" : "?"}: ${type}`).join(",\n")}
384
- }` : "never"}`).join(",\n")}
383
+ }` : "never"}`
384
+ ).join(",\n")}
385
385
  }`;
386
386
  }
387
387
 
388
- function routeHook(outDir, routesObjectName, srcDir, nuxt) {
388
+ function routeHook({ outDir, plugin, routesObjectName }, srcDir, nuxt) {
389
389
  try {
390
390
  extendPages(async (routes) => {
391
391
  if (routes.length) {
392
392
  const { routesDeclTemplate, routesList, routesObjectTemplate, routesParams } = constructRouteMap(routes);
393
- const pluginName = "__typed-router.ts";
394
- nuxt.hook("build:done", async () => {
395
- const pluginFolder = `${srcDir}/plugins`;
396
- await saveRouteFiles(pluginFolder, srcDir, pluginName, createRuntimePluginFile(routesDeclTemplate));
397
- });
393
+ if (plugin) {
394
+ const pluginName = "__typed-router.ts";
395
+ nuxt.hook("build:done", async () => {
396
+ const pluginFolder = `${srcDir}/plugins`;
397
+ await saveRouteFiles(
398
+ pluginFolder,
399
+ srcDir,
400
+ pluginName,
401
+ createRuntimePluginFile(routesDeclTemplate)
402
+ );
403
+ });
404
+ }
398
405
  await Promise.all([
399
- saveRouteFiles(outDir, srcDir, "__useTypedRouter.ts", createRuntimeHookFile(routesDeclTemplate)),
400
- saveRouteFiles(outDir, srcDir, `__routes.ts`, createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName })),
401
- saveRouteFiles(outDir, srcDir, `typed-router.d.ts`, createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams })),
406
+ saveRouteFiles(
407
+ outDir,
408
+ srcDir,
409
+ "__useTypedRouter.ts",
410
+ createRuntimeHookFile(routesDeclTemplate)
411
+ ),
412
+ saveRouteFiles(
413
+ outDir,
414
+ srcDir,
415
+ `__routes.ts`,
416
+ createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName })
417
+ ),
418
+ saveRouteFiles(
419
+ outDir,
420
+ srcDir,
421
+ `typed-router.d.ts`,
422
+ createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams })
423
+ ),
402
424
  saveRouteFiles(outDir, srcDir, "index.ts", createRuntimeIndexFile())
403
425
  ]);
404
426
  console.log(logSymbols.success, `[typed-router] Routes definitions generated`);
405
427
  } else {
406
- console.log(logSymbols.warning, chalk.yellow(`[typed-router] No routes defined. Check if your ${chalk.underline(chalk.bold("pages"))} folder exists and remove ${chalk.underline(chalk.bold("app.vue"))}`));
428
+ console.log(
429
+ logSymbols.warning,
430
+ chalk.yellow(
431
+ `[typed-router] No routes defined. Check if your ${chalk.underline(
432
+ chalk.bold("pages")
433
+ )} folder exists and remove ${chalk.underline(chalk.bold("app.vue"))}`
434
+ )
435
+ );
407
436
  }
408
437
  });
409
438
  } catch (e) {
@@ -423,9 +452,8 @@ const module = defineNuxtModule({
423
452
  },
424
453
  setup(moduleOptions, nuxt) {
425
454
  const srcDir = nuxt.options.srcDir;
426
- const { outDir, routesObjectName } = moduleOptions;
427
- nuxt.hook("pages:extend", () => routeHook(outDir, routesObjectName, srcDir, nuxt));
428
- routeHook(outDir, routesObjectName, srcDir, nuxt);
455
+ const { plugin = true, ...otherOptions } = moduleOptions;
456
+ nuxt.hook("pages:extend", () => routeHook({ ...otherOptions, plugin }, srcDir, nuxt));
429
457
  }
430
458
  });
431
459
 
package/dist/types.d.ts CHANGED
@@ -7,4 +7,4 @@ declare module '@nuxt/schema' {
7
7
  }
8
8
 
9
9
 
10
- export { default } from './module'
10
+ export { ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,30 +1,29 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Provide autocompletion for pages route names generated by Nuxt router",
5
5
  "type": "module",
6
- "scripts": {
7
- "play": "nuxi dev playground",
8
- "play:build": "nuxi build playground",
9
- "prepack": "nuxt-module-build",
10
- "build:local": "nuxt-module-build --stub && cross-env NUXT_BUILD_TYPE=stub nuxi dev playground",
11
- "init:build": "nuxt-module-build --stub && nuxi prepare playground",
12
- "build:test": "cross-env NUXT_BUILD_TYPE=stub yarn prepack && yarn play:build",
13
- "test": "yarn build:test && vitest run",
14
- "test:watch": "yarn build:test && vitest"
15
- },
6
+ "main": "./dist/module.cjs",
7
+ "types": "./main.d.ts",
16
8
  "exports": {
17
9
  ".": {
18
10
  "import": "./dist/module.mjs",
19
11
  "require": "./dist/module.cjs"
20
12
  }
21
13
  },
22
- "main": "./dist/module.cjs",
23
- "types": "./main.d.ts",
24
14
  "files": [
25
15
  "dist",
26
16
  "main.d.ts"
27
17
  ],
18
+ "scripts": {
19
+ "prepack": "nuxt-module-build",
20
+ "dev": "nuxi dev playground",
21
+ "dev:build": "nuxi build playground",
22
+ "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
23
+ "build:test": "cross-env NUXT_BUILD_TYPE=stub yarn prepack && yarn dev:build",
24
+ "test": "yarn dev:prepare && yarn build:test && vitest run",
25
+ "test:watch": "yarn build:test && vitest"
26
+ },
28
27
  "publishConfig": {
29
28
  "access": "public"
30
29
  },
@@ -51,28 +50,28 @@
51
50
  "url": "https://github.com/victorgarciaesgi/nuxt-typed-router/issues"
52
51
  },
53
52
  "dependencies": {
54
- "@nuxt/kit": "^3.0.0-rc.9",
55
- "chalk": "^4.1.2",
53
+ "@nuxt/kit": "3.0.0",
54
+ "chalk": "^5.2.0",
56
55
  "lodash-es": "^4.17.21",
57
56
  "log-symbols": "^5.1.0",
58
57
  "mkdirp": "^1.0.4",
59
- "pathe": "^0.2.0",
60
- "prettier": "^2.7.1"
58
+ "pathe": "1.0.0",
59
+ "prettier": "2.8.1"
61
60
  },
62
61
  "devDependencies": {
63
- "@nuxt/module-builder": "latest",
62
+ "@nuxt/module-builder": "^0.2.1",
64
63
  "@nuxt/types": "^2.15.8",
65
- "@nuxtjs/eslint-config-typescript": "latest",
64
+ "@nuxtjs/eslint-config-typescript": "^12.0.0",
66
65
  "@types/lodash-es": "^4.17.6",
67
66
  "@types/mkdirp": "^1.0.2",
68
67
  "@types/node": "^17.0.23",
69
- "@types/prettier": "^2.7.0",
68
+ "@types/prettier": "^2.7.2",
70
69
  "cross-env": "^7.0.3",
71
- "eslint": "8.23.0",
70
+ "eslint": "8.30.0",
72
71
  "eslint-config-prettier": "^8.5.0",
73
- "nuxt": "^3.0.0-rc.9",
74
- "typescript": "^4.6.3",
75
- "vitest": "^0.22.1",
76
- "vue-router": "^4.1.5"
72
+ "nuxt": "3.0.0",
73
+ "typescript": "^4.9.4",
74
+ "vitest": "^0.26.1",
75
+ "vue-router": "^4.1.6"
77
76
  }
78
77
  }