nuxt-typed-router 1.0.2 → 1.0.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 CHANGED
@@ -84,9 +84,13 @@ The module will create 2 files:
84
84
 
85
85
  # Usage in Vue/Nuxt
86
86
 
87
+ ## Check out this demo and clone it! [nuxt-typed-router-demo](https://github.com/victorgarciaesgi/nuxt-typed-router-demo)
88
+
89
+ <br/>
90
+
87
91
  ### **_Requirements_**
88
92
 
89
- You have to specify the output dir of the generated files in your configuration
93
+ You can specify the output dir of the generated files in your configuration. It defaults to `<srcDir>/generated`
90
94
 
91
95
  ```ts
92
96
  import TypedRouter from 'nuxt-typed-router';
@@ -146,6 +150,8 @@ export type TypedRouteList =
146
150
  | 'index-user';
147
151
  ```
148
152
 
153
+ > nuxt-typed-router will also create a plugin in your `<srcDir>/plugins` folder with the injected `$typedRouter` and `$routesList` helpers
154
+
149
155
  # Usage with `useTypedRouter` hook
150
156
 
151
157
  `useTypedRouter` is an exported composable from nuxt-typed-router. It contains a clone of `vue-router` but with stritly typed route names and params type-check
@@ -171,7 +177,7 @@ export default defineComponent({
171
177
  </script>
172
178
  ```
173
179
 
174
- # Usage with `$typedRouter` injected method
180
+ # Usage with `$typedRouter` and `$routesList` injected helpers
175
181
 
176
182
  `$typedRouter` is an injected clone of vue-router `$router`, but fully typed with all your routes.
177
183
  It's available anywhere you have access to Nuxt context
@@ -183,10 +189,10 @@ import { defineComponent } from 'vue';
183
189
  export default defineComponent({
184
190
  name: 'Index',
185
191
  setup() {
186
- const { $typedRouter } = useNuxtApp();
192
+ const { $typedRouter, $routesList } = useNuxtApp();
187
193
 
188
194
  function navigate() {
189
- $typedRouter.push({ name: 'activate' });
195
+ $typedRouter.push({ name: $routesList.activate });
190
196
  }
191
197
 
192
198
  return {
@@ -197,6 +203,25 @@ export default defineComponent({
197
203
  </script>
198
204
  ```
199
205
 
206
+ # Usage outside Vue component
207
+
208
+ You can import the `useTypedRouter` composable from where it's generated.
209
+ Exemple with `pinia` store here
210
+
211
+ ```ts
212
+ import pinia from 'pinia';
213
+ import { useTypedRouter } from 'nuxt-typed-router/hook';
214
+
215
+ export const useFooStore = defineStore('foo', {
216
+ actions: {
217
+ bar() {
218
+ const { router, routes } = useTypedRouter();
219
+ router.push({ name: routes.index.user, params: { user: 2 } });
220
+ },
221
+ },
222
+ });
223
+ ```
224
+
200
225
  ## Development
201
226
 
202
227
  1. Clone this repository
package/dist/module.json CHANGED
@@ -5,5 +5,5 @@
5
5
  "nuxt": "^3.0.0",
6
6
  "bridge": false
7
7
  },
8
- "version": "1.0.2"
8
+ "version": "1.0.3"
9
9
  }
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { extendPages, addPluginTemplate, defineNuxtModule } from '@nuxt/kit';
1
+ import { extendPages, defineNuxtModule } from '@nuxt/kit';
2
2
  import { fileURLToPath } from 'url';
3
3
  import chalk from 'chalk';
4
4
  import logSymbols from 'log-symbols';
@@ -307,7 +307,7 @@ function createRuntimePluginFile(routesDeclTemplate) {
307
307
 
308
308
  return {
309
309
  provide: {
310
- typedRouter: nuxtApp.vueApp.$router,
310
+ typedRouter: nuxtApp.$router,
311
311
  routesList,
312
312
  },
313
313
  };
@@ -317,14 +317,7 @@ function createRuntimePluginFile(routesDeclTemplate) {
317
317
  function createRuntimeHookFile(routesDeclTemplate) {
318
318
  return `
319
319
  import { getCurrentInstance } from 'vue';
320
-
321
- function useNuxtApp() {
322
- const vm = getCurrentInstance();
323
- if (!vm) {
324
- throw new Error('nuxt instance unavailable');
325
- }
326
- return vm.appContext.app.$nuxt;
327
- }
320
+ import { useNuxtApp } from '#app';
328
321
 
329
322
  export const useTypedRouter = () => {
330
323
  const { $router } = useNuxtApp();
@@ -337,6 +330,8 @@ function createRuntimeHookFile(routesDeclTemplate) {
337
330
  };
338
331
  };
339
332
 
333
+ export default {};
334
+
340
335
  `;
341
336
  }
342
337
  function createRuntimeRoutesFile({
@@ -380,27 +375,25 @@ function createTypedRouteParamsExport(routesParams) {
380
375
  }
381
376
 
382
377
  const __dirname = dirname(fileURLToPath(import.meta.url));
383
- function routeHook(outDir, routesObjectName) {
378
+ function routeHook(outDir, routesObjectName, srcDir, nuxt) {
384
379
  try {
385
380
  extendPages(async (routes) => {
386
- const { routesDeclTemplate, routesList, routesObjectTemplate, routesParams } = constructRouteMap(routes);
387
- const pluginName = "typed-router.mjs";
388
- const runtimeDir = resolve(__dirname, process.env.NUXT_BUILD_TYPE === "stub" ? "../../dist/runtime" : "./runtime");
389
- const pluginPath = resolve(runtimeDir, pluginName);
390
- await Promise.all([
391
- saveRouteFiles(runtimeDir, "useTypedRouter.mjs", createRuntimeHookFile(routesDeclTemplate)),
392
- saveRouteFiles(runtimeDir, pluginName, createRuntimePluginFile(routesDeclTemplate))
393
- ]);
394
- addPluginTemplate({
395
- src: pluginPath,
396
- filename: pluginName,
397
- options: {
398
- routesList: "test"
399
- }
400
- });
401
- await saveRouteFiles(outDir, `__routes.ts`, createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName }));
402
- await saveRouteFiles(outDir, `typed-router.d.ts`, createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams }));
403
- console.log(logSymbols.success, `[typed-router] Routes definitions generated`);
381
+ if (routes.length) {
382
+ const { routesDeclTemplate, routesList, routesObjectTemplate, routesParams } = constructRouteMap(routes);
383
+ const pluginName = "__typed-router.ts";
384
+ const runtimeDir = resolve(__dirname, process.env.NUXT_BUILD_TYPE === "stub" ? "../../dist/runtime" : "./runtime");
385
+ const pluginPath = resolve(runtimeDir, pluginName);
386
+ nuxt.hook("build:done", async () => {
387
+ const pluginFolder = `${srcDir}/plugins`;
388
+ await saveRouteFiles(pluginFolder, pluginName, createRuntimePluginFile(routesDeclTemplate));
389
+ });
390
+ await saveRouteFiles(runtimeDir, "useTypedRouter.mjs", createRuntimeHookFile(routesDeclTemplate));
391
+ await saveRouteFiles(outDir, `__routes.ts`, createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName }));
392
+ await saveRouteFiles(outDir, `typed-router.d.ts`, createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams }));
393
+ console.log(logSymbols.success, `[typed-router] Routes definitions generated`);
394
+ } else {
395
+ 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"))}`));
396
+ }
404
397
  });
405
398
  } catch (e) {
406
399
  console.error(chalk.red("Error while generating routes definitions model"), "\n" + e);
@@ -418,10 +411,10 @@ const module = defineNuxtModule({
418
411
  stripAtFromName: false
419
412
  },
420
413
  setup(moduleOptions, nuxt) {
421
- const { outDir = `${nuxt.options.srcDir}/generated`, routesObjectName } = moduleOptions;
422
- nuxt.hook("pages:extend", () => routeHook(outDir, routesObjectName));
423
- nuxt.hook("build:extendRoutes", () => routeHook(outDir, routesObjectName));
424
- routeHook(outDir, routesObjectName);
414
+ const srcDir = nuxt.options.srcDir;
415
+ const { outDir = `${srcDir}/generated`, routesObjectName } = moduleOptions;
416
+ nuxt.hook("pages:extend", () => routeHook(outDir, routesObjectName, srcDir, nuxt));
417
+ routeHook(outDir, routesObjectName, srcDir, nuxt);
425
418
  }
426
419
  });
427
420
 
@@ -1,33 +1,11 @@
1
1
  import { defineNuxtPlugin } from '#app';
2
2
 
3
3
  export default defineNuxtPlugin((nuxtApp) => {
4
- const routesList = {
5
- activate: 'activate',
6
- index: 'index',
7
- childOne: {
8
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
9
- user: { index: 'parent-child-one-child-one-sub-one-user' },
10
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
11
- index: 'parent-child-one',
12
- },
13
- childTwo: {
14
- childTwoId: 'parent-child-two-id',
15
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
16
- index: 'parent-child-two',
17
- profile: {
18
- id: {
19
- slug: { index: 'parent-child-two-profile-id-slug' },
20
- index: 'parent-child-two-profile-id',
21
- },
22
- index: 'parent-child-two-profile',
23
- },
24
- },
25
- rootPage: 'rootPage',
26
- };
4
+ const routesList = {};
27
5
 
28
6
  return {
29
7
  provide: {
30
- typedRouter: nuxtApp.vueApp.$router,
8
+ typedRouter: nuxtApp.$router,
31
9
  routesList,
32
10
  },
33
11
  };
@@ -1,33 +1,11 @@
1
1
  import { defineNuxtPlugin } from '#app';
2
2
 
3
3
  export default defineNuxtPlugin((nuxtApp) => {
4
- const routesList = {
5
- activate: 'activate',
6
- index: 'index',
7
- childOne: {
8
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
9
- user: { index: 'parent-child-one-child-one-sub-one-user' },
10
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
11
- index: 'parent-child-one',
12
- },
13
- childTwo: {
14
- childTwoId: 'parent-child-two-id',
15
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
16
- index: 'parent-child-two',
17
- profile: {
18
- id: {
19
- slug: { index: 'parent-child-two-profile-id-slug' },
20
- index: 'parent-child-two-profile-id',
21
- },
22
- index: 'parent-child-two-profile',
23
- },
24
- },
25
- rootPage: 'rootPage',
26
- };
4
+ const routesList = {};
27
5
 
28
6
  return {
29
7
  provide: {
30
- typedRouter: nuxtApp.vueApp.$router,
8
+ typedRouter: nuxtApp.$router,
31
9
  routesList,
32
10
  },
33
11
  };
@@ -1,42 +1,15 @@
1
1
  import { getCurrentInstance } from 'vue';
2
-
3
- function useNuxtApp() {
4
- const vm = getCurrentInstance();
5
- if (!vm) {
6
- throw new Error('nuxt instance unavailable');
7
- }
8
- return vm.appContext.app.$nuxt;
9
- }
2
+ import { useNuxtApp } from '#app';
10
3
 
11
4
  export const useTypedRouter = () => {
12
5
  const { $router } = useNuxtApp();
13
6
 
14
- const routesList = {
15
- activate: 'activate',
16
- index: 'index',
17
- childOne: {
18
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
19
- user: { index: 'parent-child-one-child-one-sub-one-user' },
20
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
21
- index: 'parent-child-one',
22
- },
23
- childTwo: {
24
- childTwoId: 'parent-child-two-id',
25
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
26
- index: 'parent-child-two',
27
- profile: {
28
- id: {
29
- slug: { index: 'parent-child-two-profile-id-slug' },
30
- index: 'parent-child-two-profile-id',
31
- },
32
- index: 'parent-child-two-profile',
33
- },
34
- },
35
- rootPage: 'rootPage',
36
- };
7
+ const routesList = {};
37
8
 
38
9
  return {
39
10
  router: $router,
40
11
  routes: routesList,
41
12
  };
42
13
  };
14
+
15
+ export default {};
@@ -1,42 +1,15 @@
1
1
  import { getCurrentInstance } from 'vue';
2
-
3
- function useNuxtApp() {
4
- const vm = getCurrentInstance();
5
- if (!vm) {
6
- throw new Error('nuxt instance unavailable');
7
- }
8
- return vm.appContext.app.$nuxt;
9
- }
2
+ import { useNuxtApp } from '#app';
10
3
 
11
4
  export const useTypedRouter = () => {
12
5
  const { $router } = useNuxtApp();
13
6
 
14
- const routesList = {
15
- activate: 'activate',
16
- index: 'index',
17
- childOne: {
18
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
19
- user: { index: 'parent-child-one-child-one-sub-one-user' },
20
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
21
- index: 'parent-child-one',
22
- },
23
- childTwo: {
24
- childTwoId: 'parent-child-two-id',
25
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
26
- index: 'parent-child-two',
27
- profile: {
28
- id: {
29
- slug: { index: 'parent-child-two-profile-id-slug' },
30
- index: 'parent-child-two-profile-id',
31
- },
32
- index: 'parent-child-two-profile',
33
- },
34
- },
35
- rootPage: 'rootPage',
36
- };
7
+ const routesList = {};
37
8
 
38
9
  return {
39
10
  router: $router,
40
11
  routes: routesList,
41
12
  };
42
13
  };
14
+
15
+ export default {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Provide autocompletion for pages route names generated by Nuxt router",
5
5
  "type": "module",
6
6
  "scripts": {