nuxt-typed-router 2.0.3 → 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 CHANGED
@@ -5,5 +5,5 @@
5
5
  "nuxt": "^3.0.0-rc.1",
6
6
  "bridge": false
7
7
  },
8
- "version": "2.0.3"
8
+ "version": "2.1.0-beta.0"
9
9
  }
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: Required<TypedRouteParams[T]> });
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(({ key, required }) => `"${key}"${required ? "" : "?"}: string | number`).join(",\n")}
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")}
@@ -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) {
@@ -651,6 +660,9 @@ const module = defineNuxtModule({
651
660
  ...nuxt.options.alias,
652
661
  "@typed-router": resolve(`${rootDir}/.nuxt/typed-router`)
653
662
  };
663
+ nuxt.options.typescript.tsConfig = {
664
+ include: ["./typed-router/typed-router.d.ts"]
665
+ };
654
666
  nuxt.hook("pages:extend", () => createTypedRouter({ rootDir, nuxt, plugin }));
655
667
  createTypedRouter({ rootDir, nuxt, plugin });
656
668
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "2.0.3",
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.1"
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.31.0",
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.0",
79
+ "vitest": "^0.27.2",
78
80
  "vue-router": "^4.1.6"
79
81
  }
80
82
  }