nuxt-typed-router 2.1.1 → 2.1.2

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.1.1"
8
+ "version": "2.1.2"
9
9
  }
package/dist/module.mjs CHANGED
@@ -8,28 +8,6 @@ import { dirname, resolve } from 'pathe';
8
8
  import mkdirp from 'mkdirp';
9
9
  import { camelCase } from 'lodash-es';
10
10
 
11
- const staticTypesImports = `
12
- import type {
13
- NavigationFailure,
14
- RouteLocation,
15
- RouteLocationNormalizedLoaded,
16
- RouteLocationOptions,
17
- RouteQueryAndHash,
18
- RouteLocationRaw,
19
- Router,
20
- } from 'vue-router';
21
- import type { DefineComponent } from 'vue';
22
- import type { NuxtLinkProps } from '#app';
23
- import type {
24
- TypedRouteList,
25
- TypedRouteNamedMapper,
26
- TypedRouteParams,
27
- ResolvedTypedRouteNamedMapper,
28
- } from './__routes';
29
- import {useRoute as _useRoute} from './__useTypedRoute';
30
- import {useRouter as _useRouter} from './__useTypedRouter';
31
- `;
32
-
33
11
  const watermarkTemplate = `
34
12
  // @ts-nocheck
35
13
  // eslint-disable
@@ -41,138 +19,49 @@ const watermarkTemplate = `
41
19
 
42
20
  `;
43
21
 
44
- function createRuntimeTypeUtils(autoImport) {
45
- return `
46
- // Type utils
47
- type ExtractRequiredParameters<T extends Record<string, any>> = Pick<
48
- T,
49
- { [K in keyof T]: undefined extends T[K] ? never : K }[keyof T]
50
- >;
51
-
52
- type HasOneRequiredParameter<T extends TypedRouteList> = [TypedRouteParams[T]] extends [never]
53
- ? false
54
- : [keyof ExtractRequiredParameters<TypedRouteParams[T]>] extends [undefined]
55
- ? false
56
- : true;
57
-
58
- type TypedLocationAsRelativeRaw<T extends TypedRouteList> = {
59
- name?: T;
60
- } & ([TypedRouteParams[T]] extends [never]
61
- ? {}
62
- : HasOneRequiredParameter<T> extends false
63
- ? { params?: TypedRouteParams[T] }
64
- : { params: TypedRouteParams[T] });
65
-
66
- type ResolvedTypedLocationAsRelativeRaw<T extends TypedRouteList> = {
67
- name?: T;
68
- } & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
69
-
70
- type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
71
-
72
- type _TypedRoute = Omit<RouteLocationNormalizedLoaded, 'name' | 'params'> &
73
- ResolvedTypedRouteNamedMapper;
74
- type _TypedNamedRoute<T extends TypedRouteList> = Omit<
75
- RouteLocationNormalizedLoaded,
76
- 'name' | 'params'
77
- > &
78
- ResolvedTypedLocationAsRelativeRaw<T>;
79
-
80
- /** Augmented Router interface */
81
- interface _TypedRouter
82
- extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace', 'currentRoute'> {
83
- readonly currentRoute: _TypedRoute;
84
- /**
85
- * Remove an existing route by its name.
86
- *
87
- * @param name - Name of the route to remove
88
- */
89
- removeRoute(name: TypedRouteList): void;
90
- /**
91
- * Checks if a route with a given name exists
92
- *
93
- * @param name - Name of the route to check
94
- */
95
- hasRoute(name: TypedRouteList): boolean;
96
- /**
97
- * Returns the {@link RouteLocation | normalized version} of a
98
- * {@link RouteLocationRaw | route location}. Also includes an \`href\` property
99
- * that includes any existing \`base\`. By default the \`currentLocation\` used is
100
- * \`route.currentRoute\` and should only be overriden in advanced use cases.
101
- *
102
- * @param to - Raw route location to resolve
103
- * @param currentLocation - Optional current location to resolve against
104
- */
105
- resolve(
106
- to: TypedRouteLocationRaw,
107
- currentLocation?: RouteLocationNormalizedLoaded
108
- ): RouteLocation & {
109
- href: string;
110
- };
111
- /**
112
- * Programmatically navigate to a new URL by pushing an entry in the history
113
- * stack.
114
- *
115
- * @param to - Route location to navigate to
116
- */
117
- push(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
118
- /**
119
- * Programmatically navigate to a new URL by replacing the current entry in
120
- * the history stack.
121
- *
122
- * @param to - Route location to navigate to
123
- */
124
- replace(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
125
- }
126
-
127
- export interface TypedRouter extends _TypedRouter {}
128
- export type TypedRoute = _TypedRoute;
129
- export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
130
-
131
- declare global {
132
- export interface TypedRouter extends _TypedRouter {}
133
- export type TypedRoute = _TypedRoute;
134
- export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
135
-
136
- ${autoImport ? `const useRoute: typeof _useRoute;
137
- const useRouter: typeof _useRouter;` : ""}
138
- }
139
-
140
- type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
141
- to: string | Omit<Exclude<RouteLocationRaw, string>, 'name'> & TypedRouteNamedMapper;
142
- };
143
-
144
- type _NuxtLink = DefineComponent<
145
- TypedNuxtLinkProps,
146
- {},
147
- {},
148
- import('vue').ComputedOptions,
149
- import('vue').MethodOptions,
150
- import('vue').ComponentOptionsMixin,
151
- import('vue').ComponentOptionsMixin,
152
- {},
153
- string,
154
- import('vue').VNodeProps &
155
- import('vue').AllowedComponentProps &
156
- import('vue').ComponentCustomProps,
157
- Readonly<TypedNuxtLinkProps>,
158
- {}
159
- >;
160
-
161
- declare module '@vue/runtime-core' {
162
- export interface GlobalComponents {
163
- NuxtLink: _NuxtLink;
164
- }
165
- }
166
- `;
167
- }
168
-
169
22
  function createDeclarationRoutesFile(autoImport) {
170
23
  return `
171
24
  ${watermarkTemplate}
172
25
 
173
- ${staticTypesImports}
174
-
175
- ${createRuntimeTypeUtils(autoImport)}
26
+ import type { NuxtLinkProps } from '#app';
27
+ import type { DefineComponent } from 'vue';
28
+ import type { RouteLocationRaw } from 'vue-router';
29
+ import type { TypedRouteNamedMapper } from './__routes';
30
+ import { useRoute as _useRoute } from './__useTypedRoute';
31
+ import { useRouter as _useRouter } from './__useTypedRouter';
32
+
33
+ declare global {
34
+
35
+ ${autoImport ? `const useRoute: typeof _useRoute;
36
+ const useRouter: typeof _useRouter;` : ""}
37
+ }
38
+
39
+ type TypedNuxtLinkProps = Omit<NuxtLinkProps, 'to'> & {
40
+ to: string | Omit<Exclude<RouteLocationRaw, string>, 'name'> & TypedRouteNamedMapper;
41
+ };
42
+
43
+ export type _NuxtLink = DefineComponent<
44
+ TypedNuxtLinkProps,
45
+ {},
46
+ {},
47
+ import('vue').ComputedOptions,
48
+ import('vue').MethodOptions,
49
+ import('vue').ComponentOptionsMixin,
50
+ import('vue').ComponentOptionsMixin,
51
+ {},
52
+ string,
53
+ import('vue').VNodeProps &
54
+ import('vue').AllowedComponentProps &
55
+ import('vue').ComponentCustomProps,
56
+ Readonly<TypedNuxtLinkProps>,
57
+ {}
58
+ >;
59
+
60
+ declare module '@vue/runtime-core' {
61
+ export interface GlobalComponents {
62
+ NuxtLink: _NuxtLink;
63
+ }
64
+ }
176
65
  `;
177
66
  }
178
67
 
@@ -180,9 +69,10 @@ function createRuntimeIndexFile() {
180
69
  return `
181
70
  ${watermarkTemplate}
182
71
  export { routesNames } from './__routes';
183
- export type { TypedRouteList } from './__routes';
72
+ export type { TypedRouteList, TypedRouteNamedMapper, TypedRouteParams } from './__routes';
184
73
  export {useRouter} from './__useTypedRouter';
185
74
  export {useRoute} from './__useTypedRoute';
75
+ export type {TypedRoute, TypedRouter, TypedNamedRoute } from './__router'
186
76
  `;
187
77
  }
188
78
 
@@ -190,6 +80,7 @@ function createRuntimePluginFile(routesDeclTemplate) {
190
80
  return `
191
81
  ${watermarkTemplate}
192
82
  import { defineNuxtPlugin } from '#app';
83
+ import {TypedRouter, TypedRoute} from '@typed-router';
193
84
 
194
85
  export default defineNuxtPlugin(() => {
195
86
  const router = useRouter();
@@ -271,7 +162,6 @@ function createRuntimeRoutesFile({
271
162
  *
272
163
  *
273
164
  * */
274
-
275
165
  ${createTypedRouteParamsExport(routesParams)}
276
166
 
277
167
  ${createTypedRouteNamedMapperExport(routesParams)}
@@ -285,6 +175,7 @@ function createUseTypedRouteFile(routesDeclTemplate) {
285
175
  ${watermarkTemplate}
286
176
  import { useRoute as defaultRoute } from '#app';
287
177
  import type { TypedRouteList } from './__routes';
178
+ import type {TypedRoute, TypedNamedRoute} from './__router'
288
179
 
289
180
  /** Acts the same as \`useRoute\`, but typed.
290
181
  *
@@ -322,7 +213,7 @@ function createRuntimeUseTypedRouterFile(routesDeclTemplate) {
322
213
  return `
323
214
  ${watermarkTemplate}
324
215
  import { useRouter as defaultRouter } from '#app';
325
- import type { TypedRouter } from './typed-router';
216
+ import type { TypedRouter } from './__router';
326
217
 
327
218
  /** Returns instances of $typedRouter and $routesList fully typed to use in your components or your Vuex/Pinia store
328
219
  *
@@ -341,6 +232,111 @@ function createRuntimeUseTypedRouterFile(routesDeclTemplate) {
341
232
  `;
342
233
  }
343
234
 
235
+ function createRuntimeRouterTypes() {
236
+ return `
237
+ import type {
238
+ NavigationFailure,
239
+ RouteLocation,
240
+ RouteLocationNormalizedLoaded,
241
+ RouteLocationOptions,
242
+ RouteLocationRaw,
243
+ RouteQueryAndHash,
244
+ Router,
245
+ } from 'vue-router';
246
+ import type {
247
+ ResolvedTypedRouteNamedMapper,
248
+ TypedRouteList,
249
+ TypedRouteNamedMapper,
250
+ TypedRouteParams,
251
+ } from './__routes';
252
+
253
+ // Type utils
254
+ type ExtractRequiredParameters<T extends Record<string, any>> = Pick<
255
+ T,
256
+ { [K in keyof T]: undefined extends T[K] ? never : K }[keyof T]
257
+ >;
258
+
259
+ type HasOneRequiredParameter<T extends TypedRouteList> = [TypedRouteParams[T]] extends [never]
260
+ ? false
261
+ : [keyof ExtractRequiredParameters<TypedRouteParams[T]>] extends [undefined]
262
+ ? false
263
+ : true;
264
+
265
+ type TypedLocationAsRelativeRaw<T extends TypedRouteList> = {
266
+ name?: T;
267
+ } & ([TypedRouteParams[T]] extends [never]
268
+ ? {}
269
+ : HasOneRequiredParameter<T> extends false
270
+ ? { params?: TypedRouteParams[T] }
271
+ : { params: TypedRouteParams[T] });
272
+
273
+ type ResolvedTypedLocationAsRelativeRaw<T extends TypedRouteList> = {
274
+ name?: T;
275
+ } & ([TypedRouteParams[T]] extends [never] ? {} : { params: TypedRouteParams[T] });
276
+
277
+ type TypedRouteLocationRaw = RouteQueryAndHash & TypedRouteNamedMapper & RouteLocationOptions;
278
+
279
+ type _TypedRoute = Omit<RouteLocationNormalizedLoaded, 'name' | 'params'> &
280
+ ResolvedTypedRouteNamedMapper;
281
+ type _TypedNamedRoute<T extends TypedRouteList> = Omit<
282
+ RouteLocationNormalizedLoaded,
283
+ 'name' | 'params'
284
+ > &
285
+ ResolvedTypedLocationAsRelativeRaw<T>;
286
+
287
+ /** Augmented Router interface */
288
+ interface _TypedRouter
289
+ extends Omit<Router, 'removeRoute' | 'hasRoute' | 'resolve' | 'push' | 'replace' | 'currentRoute'> {
290
+ readonly currentRoute: _TypedRoute;
291
+ /**
292
+ * Remove an existing route by its name.
293
+ *
294
+ * @param name - Name of the route to remove
295
+ */
296
+ removeRoute(name: TypedRouteList): void;
297
+ /**
298
+ * Checks if a route with a given name exists
299
+ *
300
+ * @param name - Name of the route to check
301
+ */
302
+ hasRoute(name: TypedRouteList): boolean;
303
+ /**
304
+ * Returns the {@link RouteLocation | normalized version} of a
305
+ * {@link RouteLocationRaw | route location}. Also includes an \`href\` property
306
+ * that includes any existing \`base\`. By default the \`currentLocation\` used is
307
+ * \`route.currentRoute\` and should only be overriden in advanced use cases.
308
+ *
309
+ * @param to - Raw route location to resolve
310
+ * @param currentLocation - Optional current location to resolve against
311
+ */
312
+ resolve(
313
+ to: TypedRouteLocationRaw,
314
+ currentLocation?: RouteLocationNormalizedLoaded
315
+ ): RouteLocation & {
316
+ href: string;
317
+ };
318
+ /**
319
+ * Programmatically navigate to a new URL by pushing an entry in the history
320
+ * stack.
321
+ *
322
+ * @param to - Route location to navigate to
323
+ */
324
+ push(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
325
+ /**
326
+ * Programmatically navigate to a new URL by replacing the current entry in
327
+ * the history stack.
328
+ *
329
+ * @param to - Route location to navigate to
330
+ */
331
+ replace(to: TypedRouteLocationRaw): Promise<NavigationFailure | void | undefined>;
332
+ }
333
+
334
+ export interface TypedRouter extends _TypedRouter {}
335
+ export type TypedRoute = _TypedRoute;
336
+ export type TypedNamedRoute<T extends TypedRouteList> = _TypedNamedRoute<T>;
337
+ `;
338
+ }
339
+
344
340
  const { resolveConfig, format } = prettier;
345
341
  const defaultPrettierOptions = {
346
342
  printWidth: 100,
@@ -443,6 +439,10 @@ async function saveGeneratedFiles({
443
439
  routesParams
444
440
  })
445
441
  },
442
+ {
443
+ fileName: `__router.d.ts`,
444
+ content: createRuntimeRouterTypes()
445
+ },
446
446
  {
447
447
  fileName: `typed-router.d.ts`,
448
448
  content: createDeclarationRoutesFile(autoImport)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-typed-router",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Provide autocompletion for pages route names generated by Nuxt router",
5
5
  "type": "module",
6
6
  "main": "./dist/module.cjs",
@@ -19,10 +19,12 @@
19
19
  "prepack": "nuxt-module-build",
20
20
  "dev": "nuxi dev playground",
21
21
  "dev:build": "nuxi build playground",
22
- "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
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:fixtures": "nuxt-module-build --stub && nuxi prepare test/fixtures/simple && vitest run --dir test",
25
- "test:types": "nuxi prepare test/fixtures/simple && cd test/fixtures/simple && vue-tsc --noEmit",
24
+ "test:prepare-fixtures": "nuxi prepare test/fixtures/simple",
25
+ "test:fixtures": " vitest run --dir test",
26
+ "test:types": "pnpm run test:vue && vitest typecheck --run --dir test",
27
+ "test:vue": "vue-tsc -p test/fixtures/simple/tsconfig.json --noEmit && vue-tsc -p test/fixtures/complex/tsconfig.json --noEmit",
26
28
  "test": "pnpm run dev:prepare && pnpm run test:fixtures && pnpm run test:types",
27
29
  "docs:dev": "cd docs && pnpm run dev",
28
30
  "docs:build": "npm run dev:prepare && cd docs && nuxi generate"