nuxt-typed-router 2.1.2 → 2.1.4

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
@@ -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 `useRoute` route autocomplete and params type-check
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
  [![Documentation](https://github.com/victorgarciaesgi/nuxt-typed-router/blob/master/.github/images/redirectDoc.svg?raw=true)](https://nuxt-typed-router.vercel.app/)
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": "2.1.2"
8
+ "version": "2.1.4"
9
9
  }
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
- const useRouter: typeof _useRouter;` : ""}
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(() => {
@@ -274,7 +277,13 @@ function createRuntimeRouterTypes() {
274
277
  name?: T;
275
278
  } & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
276
279
 
277
- type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
280
+ export type TypedNamedRouteLocation<T extends TypedRouteList> =
281
+ | (Omit<Exclude<RouteLocationRaw, string>, 'name' | 'params'> & TypedLocationAsRelativeRaw<T>)
282
+ | string;
283
+
284
+ type TypedRouteLocationRaw =
285
+ | (Omit<Exclude<RouteLocationRaw, string>, 'name' | 'params'> & TypedRouteNamedMapper)
286
+ | string;
278
287
 
279
288
  type _TypedRoute = Omit<RouteLocationNormalizedLoaded, 'name' | 'params'> &
280
289
  ResolvedTypedRouteNamedMapper;
@@ -337,6 +346,23 @@ function createRuntimeRouterTypes() {
337
346
  `;
338
347
  }
339
348
 
349
+ function createRuntimeNavigateToFunction() {
350
+ return `
351
+ ${watermarkTemplate}
352
+ import { navigateTo as defaultNavigateTo } from '#app';
353
+ import { NavigateToOptions } from 'nuxt/dist/app/composables/router';
354
+ import { NavigationFailure } from 'vue-router';
355
+ import type { TypedNamedRouteLocation } from './__router';
356
+ import { TypedRouteList } from './__routes';
357
+
358
+ export const navigateTo: <T extends TypedRouteList>(
359
+ to: TypedNamedRouteLocation<T>,
360
+ options?: NavigateToOptions
361
+ ) => Promise<void | NavigationFailure | TypedNamedRoute<T>> = defaultNavigateTo as any;
362
+
363
+ `;
364
+ }
365
+
340
366
  const { resolveConfig, format } = prettier;
341
367
  const defaultPrettierOptions = {
342
368
  printWidth: 100,
@@ -398,21 +424,20 @@ async function writeFile(path, content) {
398
424
  }
399
425
  }
400
426
 
401
- function handlePluginFileSave({
402
- nuxt,
403
- rootDir,
404
- routesDeclTemplate
405
- }) {
427
+ function handlePluginFileSave({ nuxt, routesDeclTemplate }) {
406
428
  const pluginName = "__typed-router.ts";
407
- nuxt.hook("build:done", async () => {
408
- const pluginFolder = `${rootDir}/plugins`;
429
+ const srcDir = nuxt.options.srcDir;
430
+ async function savePlugin() {
431
+ const pluginFolder = `${srcDir}/plugins`;
409
432
  await processPathAndWriteFile({
410
433
  outDir: pluginFolder,
411
- rootDir,
434
+ rootDir: srcDir,
412
435
  fileName: pluginName,
413
436
  content: createRuntimePluginFile(routesDeclTemplate)
414
437
  });
415
- });
438
+ }
439
+ nuxt.hook("build:done", savePlugin);
440
+ nuxt.hook("prepare:types", savePlugin);
416
441
  }
417
442
 
418
443
  let previousGeneratedRoutes = "";
@@ -439,6 +464,10 @@ async function saveGeneratedFiles({
439
464
  routesParams
440
465
  })
441
466
  },
467
+ {
468
+ fileName: "__utils.ts",
469
+ content: createRuntimeNavigateToFunction()
470
+ },
442
471
  {
443
472
  fileName: `__router.d.ts`,
444
473
  content: createRuntimeRouterTypes()
@@ -617,36 +646,58 @@ function startGenerator({ output, routesConfig }) {
617
646
  output.routesDeclTemplate += "}";
618
647
  }
619
648
 
620
- function createTypedRouter({ plugin, nuxt }) {
649
+ let hasLoggedNoPages = false;
650
+ let hasRoutesDefined = false;
651
+ async function createTypedRouter({
652
+ plugin,
653
+ nuxt,
654
+ routesConfig,
655
+ isHookCall = false
656
+ }) {
621
657
  try {
622
658
  const rootDir = nuxt.options.rootDir;
623
659
  const autoImport = nuxt.options.imports.autoImport ?? true;
660
+ if (!isHookCall) {
661
+ if (routesConfig) {
662
+ await nuxt.callHook("pages:extend", routesConfig);
663
+ return;
664
+ }
665
+ nuxt.hook("pages:extend", (routesConfig2) => {
666
+ createTypedRouter({ nuxt, plugin, routesConfig: routesConfig2, isHookCall: true });
667
+ });
668
+ nuxt.hook("modules:done", () => {
669
+ createTypedRouter({ nuxt, plugin, isHookCall: true });
670
+ });
671
+ return;
672
+ }
624
673
  extendPages(async (routes) => {
625
- if (routes.length) {
626
- const outputData = constructRouteMap(routes);
627
- if (plugin) {
628
- handlePluginFileSave({
629
- nuxt,
630
- routesDeclTemplate: outputData.routesDeclTemplate,
631
- rootDir
632
- });
633
- }
634
- await saveGeneratedFiles({
635
- autoImport,
636
- rootDir,
637
- outputData
674
+ hasRoutesDefined = true;
675
+ const outputData = constructRouteMap(routes);
676
+ if (plugin) {
677
+ handlePluginFileSave({
678
+ nuxt,
679
+ routesDeclTemplate: outputData.routesDeclTemplate
638
680
  });
639
- } else {
681
+ }
682
+ await saveGeneratedFiles({
683
+ autoImport,
684
+ rootDir,
685
+ outputData
686
+ });
687
+ });
688
+ setTimeout(() => {
689
+ if (!hasRoutesDefined && !hasLoggedNoPages) {
690
+ hasLoggedNoPages = true;
640
691
  console.log(
641
692
  logSymbols.warning,
642
693
  chalk.yellow(
643
- `[typed-router] No routes defined. Check if your ${chalk.underline(
694
+ `\u{1F6A6} No routes defined. Check if your ${chalk.underline(
644
695
  chalk.bold("pages")
645
- )} folder exists and remove ${chalk.underline(chalk.bold("app.vue"))}`
696
+ )} folder exists`
646
697
  )
647
698
  );
648
699
  }
649
- });
700
+ }, 3e3);
650
701
  } catch (e) {
651
702
  console.error(chalk.red("Error while generating routes definitions model"), "\n" + e);
652
703
  }
@@ -673,8 +724,7 @@ const module = defineNuxtModule({
673
724
  include: ["./typed-router/typed-router.d.ts"]
674
725
  };
675
726
  const typedRouterOptions = { nuxt, plugin };
676
- nuxt.hook("pages:extend", () => createTypedRouter(typedRouterOptions));
677
- createTypedRouter(typedRouterOptions);
727
+ createTypedRouter({ ...typedRouterOptions });
678
728
  }
679
729
  });
680
730
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
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.0.0",
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.27.2",
84
+ "vitest": "^0.28.1",
83
85
  "vue-router": "^4.1.6",
84
86
  "vue-tsc": "^1.0.24"
85
87
  }