nuxt-typed-router 1.0.4 → 1.1.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/README.md CHANGED
@@ -46,7 +46,7 @@ npm install -D nuxt-typed-router@legacy
46
46
 
47
47
  # Configuration
48
48
 
49
- First, register the module in the `nuxt.config.[js|ts]`
49
+ First, register the module in the `nuxt.config.ts`
50
50
 
51
51
  ```ts
52
52
  import TypedRouter from 'nuxt-typed-router';
@@ -77,10 +77,12 @@ interface ModuleOptions {
77
77
 
78
78
  # Generated files
79
79
 
80
- The module will create 2 files:
80
+ The module will generate 4 files each time you modify the `pages` folder :
81
81
 
82
- - `__routes.ts` with the global object of the route names inside.
83
- - `typed-router.d.ts` containing the global typecript definitions and exports
82
+ - `~/<outDir>/__routes.ts` with the global object of the route names inside.
83
+ - `~/<outDir>/__useTypedRouter.ts` Composable tu simply access your typed routes
84
+ - `~/<outDir>/typed-router.d.ts` containing the global typecript definitions and exports
85
+ - `~/plugins/__typed_router.ts` Plugin that will inject `$typedRouter` and `$routesList` (`@nuxt/kit` has problems registering plugin templates so this is a workaround)
84
86
 
85
87
  # Usage in Vue/Nuxt
86
88
 
@@ -122,7 +124,7 @@ Given this structure
122
124
  │ └── login.vue
123
125
  └── ...
124
126
 
125
- The generated file will look like this
127
+ The generated route list will look like this
126
128
 
127
129
  ```ts
128
130
  export const routerPagesNames = {
@@ -158,7 +160,8 @@ export type TypedRouteList =
158
160
 
159
161
  ```vue
160
162
  <script lang="ts">
161
- import { useTypedRouter } from 'nuxt-typed-router/hook';
163
+ // The path here is `~/generated` because I set `outDir: './generated'` in my module options
164
+ import { useTypedRouter } from '~/generated';
162
165
 
163
166
  export default defineComponent({
164
167
  setup() {
@@ -210,7 +213,7 @@ Exemple with `pinia` store here
210
213
 
211
214
  ```ts
212
215
  import pinia from 'pinia';
213
- import { useTypedRouter } from 'nuxt-typed-router/hook';
216
+ import { useTypedRouter } from '~/generated';
214
217
 
215
218
  export const useFooStore = defineStore('foo', {
216
219
  actions: {
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.4"
8
+ "version": "1.1.0"
9
9
  }
package/dist/module.mjs CHANGED
@@ -192,8 +192,12 @@ function walkThoughRoutes({
192
192
  }
193
193
 
194
194
  const signatureTemplate = `/**
195
- * Generated by nuxt-typed-router. Do not modify
196
- * */`;
195
+ * ---------------------
196
+ * \u{1F697}\u{1F6A6} Generated by nuxt-typed-router. Do not modify !
197
+ * ---------------------
198
+ * */
199
+
200
+ `;
197
201
  const staticDeclImports = `
198
202
  import type {
199
203
  NavigationFailure,
@@ -227,7 +231,7 @@ const staticDeclarations = `
227
231
  TypedLocationAsRelativeRaw<T> &
228
232
  RouteLocationOptions;
229
233
 
230
- interface TypedRouter {
234
+ export interface TypedRouter {
231
235
  /**
232
236
  * Remove an existing route by its name.
233
237
  *
@@ -288,18 +292,11 @@ const staticDeclarations = `
288
292
  $routesList: RouteListDecl;
289
293
  }
290
294
  }
291
- declare module 'nuxt-typed-router/hook' {
292
- declare const useTypedRouter: () => {
293
- /** Export of $router with type check */
294
- router: TypedRouter,
295
- /** Contains a typed dictionnary of all your route names (for syntax sugar) */
296
- routes: RouteListDecl
297
- };
298
- }
299
295
  `;
300
296
 
301
297
  function createRuntimePluginFile(routesDeclTemplate) {
302
298
  return `
299
+ ${signatureTemplate}
303
300
  import { defineNuxtPlugin } from '#app';
304
301
 
305
302
  export default defineNuxtPlugin((nuxtApp) => {
@@ -316,10 +313,24 @@ function createRuntimePluginFile(routesDeclTemplate) {
316
313
  }
317
314
  function createRuntimeHookFile(routesDeclTemplate) {
318
315
  return `
319
- import { getCurrentInstance } from 'vue';
316
+ ${signatureTemplate}
320
317
  import { useNuxtApp } from '#app';
318
+ import { TypedRouter, RouteListDecl } from './typed-router';
321
319
 
322
- export const useTypedRouter = () => {
320
+ /** Returns instances of $typedRouter and $routesList fully typed to use in your components or your Vuex/Pinia store
321
+ *
322
+ * @exemple
323
+ *
324
+ * \`\`\`ts
325
+ * const { router, routes } = useTypedRouter();
326
+ * \`\`\`
327
+ */
328
+ export const useTypedRouter = (): {
329
+ /** Export of $router with type check */
330
+ router: TypedRouter,
331
+ /** Contains a typed dictionnary of all your route names (for syntax sugar) */
332
+ routes: RouteListDecl
333
+ } => {
323
334
  const { $router } = useNuxtApp();
324
335
 
325
336
  const routesList = ${routesDeclTemplate};
@@ -327,11 +338,18 @@ function createRuntimeHookFile(routesDeclTemplate) {
327
338
  return {
328
339
  router: $router,
329
340
  routes: routesList,
330
- };
341
+ } as any;
331
342
  };
332
343
 
333
344
  `;
334
345
  }
346
+ function createRuntimeIndexFile() {
347
+ return `
348
+ ${signatureTemplate}
349
+ export * from './__routes';
350
+ export * from './__useTypedRouter';
351
+ `;
352
+ }
335
353
  function createRuntimeRoutesFile({
336
354
  routesList,
337
355
  routesObjectTemplate,
@@ -385,9 +403,12 @@ function routeHook(outDir, routesObjectName, srcDir, nuxt) {
385
403
  const pluginFolder = `${srcDir}/plugins`;
386
404
  await saveRouteFiles(pluginFolder, pluginName, createRuntimePluginFile(routesDeclTemplate));
387
405
  });
388
- await saveRouteFiles(runtimeDir, "useTypedRouter.mjs", createRuntimeHookFile(routesDeclTemplate));
389
- await saveRouteFiles(outDir, `__routes.ts`, createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName }));
390
- await saveRouteFiles(outDir, `typed-router.d.ts`, createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams }));
406
+ await Promise.all([
407
+ saveRouteFiles(outDir, "__useTypedRouter.ts", createRuntimeHookFile(routesDeclTemplate)),
408
+ saveRouteFiles(outDir, `__routes.ts`, createRuntimeRoutesFile({ routesList, routesObjectTemplate, routesObjectName })),
409
+ saveRouteFiles(outDir, `typed-router.d.ts`, createDeclarationRoutesFile({ routesDeclTemplate, routesList, routesParams })),
410
+ saveRouteFiles(outDir, "index.ts", createRuntimeIndexFile())
411
+ ]);
391
412
  console.log(logSymbols.success, `[typed-router] Routes definitions generated`);
392
413
  } else {
393
414
  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"))}`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Provide autocompletion for pages route names generated by Nuxt router",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -17,18 +17,13 @@
17
17
  ".": {
18
18
  "import": "./dist/module.mjs",
19
19
  "require": "./dist/module.cjs"
20
- },
21
- "./hook": {
22
- "import": "./hook.mjs"
23
20
  }
24
21
  },
25
22
  "main": "./dist/module.cjs",
26
23
  "types": "./main.d.ts",
27
24
  "files": [
28
25
  "dist",
29
- "main.d.ts",
30
- "hook.mjs",
31
- "hook.d.ts"
26
+ "main.d.ts"
32
27
  ],
33
28
  "publishConfig": {
34
29
  "access": "public"
@@ -1,12 +0,0 @@
1
- import { defineNuxtPlugin } from '#app';
2
-
3
- export default defineNuxtPlugin((nuxtApp) => {
4
- const routesList = {};
5
-
6
- return {
7
- provide: {
8
- typedRouter: nuxtApp.$router,
9
- routesList,
10
- },
11
- };
12
- });
@@ -1,12 +0,0 @@
1
- import { defineNuxtPlugin } from '#app';
2
-
3
- export default defineNuxtPlugin((nuxtApp) => {
4
- const routesList = {};
5
-
6
- return {
7
- provide: {
8
- typedRouter: nuxtApp.$router,
9
- routesList,
10
- },
11
- };
12
- });
@@ -1,35 +0,0 @@
1
- import { getCurrentInstance } from 'vue';
2
- import { useNuxtApp } from '#app';
3
-
4
- export const useTypedRouter = () => {
5
- const { $router } = useNuxtApp();
6
-
7
- const routesList = {
8
- activate: 'activate',
9
- index: 'index',
10
- childOne: {
11
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
12
- user: { index: 'parent-child-one-child-one-sub-one-user' },
13
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
14
- index: 'parent-child-one',
15
- },
16
- childTwo: {
17
- childTwoId: 'parent-child-two-id',
18
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
19
- index: 'parent-child-two',
20
- profile: {
21
- id: {
22
- slug: { index: 'parent-child-two-profile-id-slug' },
23
- index: 'parent-child-two-profile-id',
24
- },
25
- index: 'parent-child-two-profile',
26
- },
27
- },
28
- rootPage: 'rootPage',
29
- };
30
-
31
- return {
32
- router: $router,
33
- routes: routesList,
34
- };
35
- };
@@ -1,35 +0,0 @@
1
- import { getCurrentInstance } from 'vue';
2
- import { useNuxtApp } from '#app';
3
-
4
- export const useTypedRouter = () => {
5
- const { $router } = useNuxtApp();
6
-
7
- const routesList = {
8
- activate: 'activate',
9
- index: 'index',
10
- childOne: {
11
- childOneChildOneSubOne: 'parent-child-one-child-one-sub-one',
12
- user: { index: 'parent-child-one-child-one-sub-one-user' },
13
- childOneChildOneSubTwo: 'parent-child-one-child-one-sub-two',
14
- index: 'parent-child-one',
15
- },
16
- childTwo: {
17
- childTwoId: 'parent-child-two-id',
18
- childTwoChildOneSubOne: 'parent-child-two-child-one-sub-one',
19
- index: 'parent-child-two',
20
- profile: {
21
- id: {
22
- slug: { index: 'parent-child-two-profile-id-slug' },
23
- index: 'parent-child-two-profile-id',
24
- },
25
- index: 'parent-child-two-profile',
26
- },
27
- },
28
- rootPage: 'rootPage',
29
- };
30
-
31
- return {
32
- router: $router,
33
- routes: routesList,
34
- };
35
- };
package/hook.d.ts DELETED
@@ -1,4 +0,0 @@
1
- /** @deprecated Do not import or use. Dummy export to fool Typescript into thinking it's a module */
2
- declare const doNotImportItsToFoolTypescript = 'foo';
3
-
4
- export { doNotImportItsToFoolTypescript };
package/hook.mjs DELETED
@@ -1 +0,0 @@
1
- export { useTypedRouter } from './dist/runtime/useTypedRouter.mjs';