@@ -10,13 +10,10 @@ import openapiTS, {
10
10
import {
11
11
addImports ,
12
12
addServerImports ,
13
- addServerTemplate ,
14
13
addTemplate ,
15
14
createResolver ,
16
- resolvePath ,
17
15
} from '@nuxt/kit' ;
18
- import { pascalCase } from 'es-toolkit' ;
19
- import { ensureArray } from './runtime/fetchUtils' ;
16
+ import { pascalCase , toMerged } from 'es-toolkit' ;
20
17
21
18
type GenerateArgs = {
22
19
moduleConfig : ResolvedConfig ;
@@ -39,7 +36,7 @@ export const generate = async ({ moduleConfig, nuxt }: GenerateArgs) => {
39
36
for ( const [ collectionName , apiConfig ] of apis ) {
40
37
const openApiTsFilePath = `${ moduleFolderName } /${ collectionName } /${ openApiTsFileName } .ts` ;
41
38
42
- addTemplate ( {
39
+ const { dst : openAPITSTypesDST } = addTemplate ( {
43
40
filename : openApiTsFilePath ,
44
41
getContents : async ( ) => {
45
42
const openApiTs = await getOpenApiTs ( {
@@ -62,9 +59,12 @@ export const generate = async ({ moduleConfig, nuxt }: GenerateArgs) => {
62
59
const useClientName = `use${ pascalCasedName } Fetch` ;
63
60
const useLazyClientName = `useLazy${ pascalCasedName } Fetch` ;
64
61
65
- const target = ensureArray ( apiConfig . for ?? moduleConfig . for ) ;
62
+ const clientConfig = resolveClientConfig (
63
+ moduleConfig . clients ,
64
+ apiConfig . clients ,
65
+ ) ;
66
66
67
- if ( target . includes ( ' nuxt' ) ) {
67
+ if ( clientConfig . nuxt !== false ) {
68
68
const nuxtClientPath = `${ moduleFolderName } /${ collectionName } /index.ts` ;
69
69
const { dst } = addTemplate ( {
70
70
filename : nuxtClientPath ,
@@ -124,7 +124,7 @@ export const ${useLazyClientName}: UseLazyFetch<${pathsTypeName}> = (path, opts?
124
124
125
125
addedNuxtFiles . add ( dst ) ;
126
126
127
- if ( apiConfig . autoImport ?? moduleConfig . autoImport )
127
+ if ( clientConfig . nuxt . autoImport )
128
128
addImports ( [
129
129
{
130
130
name : pathsTypeName ,
@@ -146,50 +146,55 @@ export const ${useLazyClientName}: UseLazyFetch<${pathsTypeName}> = (path, opts?
146
146
] ) ;
147
147
}
148
148
149
- if ( target . includes ( 'nitro' ) ) {
150
- const nitroClientPath = `${ moduleFolderName } /${ collectionName } /nitro.ts` ;
151
- const { filename } = addTemplate ( {
152
- filename : nitroClientPath ,
153
- getContents : ( ) =>
154
- `import type { paths as ${ pathsTypeName } } from './${ openApiTsFileName } ';
155
- import type { Fetch, SimplifiedFetchOptions } from '${ resolver . resolve ( './runtime/fetchTypes' ) } ';
156
- import { handleFetchPathParams } from '${ resolver . resolve ( './runtime/handlePathParams' ) } '
149
+ if ( clientConfig . nitro !== false ) {
150
+ const nitroClientPath = `${ moduleFolderName } /${ collectionName } /nitro` ;
151
+
152
+ addNitroTsFile ( nuxt , openAPITSTypesDST , false ) ;
153
+
154
+ const { dst } = addTemplate ( {
155
+ filename : `${ nitroClientPath } .ts` ,
156
+ getContents :
157
+ ( ) => `import type { paths as ${ pathsTypeName } } from './${ openApiTsFileName } '
158
+ import { handleFetchPathParams } from '${ resolver . resolve ( './runtime/server' ) } '
159
+ import type { NitroFetch, SimplifiedNitroFetchOptions } from '${ resolver . resolve ( './runtime/server' ) } '
157
160
158
161
export type { paths as ${ pathsTypeName } , components as ${ componentsTypeName } } from './${ openApiTsFileName } '
159
162
160
- ${ tsIgnoreError }
161
- export const ${ clientName } : Fetch <${ pathsTypeName } > = (path, opts? ) => {
162
- const options = (opts ?? {}) as SimplifiedFetchOptions
163
+ ${ tsIgnoreError }
164
+ export const ${ clientName } : NitroFetch <${ pathsTypeName } > = (path, opts) => {
165
+ const options = (opts ?? {}) as SimplifiedNitroFetchOptions
163
166
options.baseURL ??= "${ apiConfig . baseUrl } "
164
167
165
- let finalPath = path as string
168
+ let finalPath = path as string;
166
169
if (options.pathParams) {
167
170
finalPath = handleFetchPathParams(path, options.pathParams)
168
171
}
169
172
170
173
const { pathParams, ...rest } = options;
171
174
172
- ${ tsIgnoreError }
175
+ ${ tsIgnoreError }
173
176
return $fetch(finalPath, rest)
174
177
};` ,
178
+ write : true ,
175
179
} ) ;
176
180
177
- const fullPath = path . join ( nuxt . options . buildDir , filename ) ;
181
+ addNitroTsFile ( nuxt , dst , true ) ;
178
182
179
- addedNitroFiles . add ( fullPath ) ;
183
+ addedNitroFiles . add ( dst ) ;
180
184
181
- if ( apiConfig . autoImport ?? moduleConfig . autoImport )
185
+ if ( clientConfig . nitro . autoImport ) {
182
186
addServerImports ( [
183
187
{
184
188
name : pathsTypeName ,
185
- from : fullPath ,
189
+ from : dst ,
186
190
type : true ,
187
191
} ,
188
192
{
189
193
name : clientName ,
190
- from : fullPath ,
194
+ from : dst ,
191
195
} ,
192
196
] ) ;
197
+ }
193
198
}
194
199
}
195
200
@@ -216,6 +221,28 @@ export const ${clientName}: Fetch<${pathsTypeName}> = (path, opts?) => {
216
221
} ) ;
217
222
218
223
if ( addedNitroFiles . size > 0 ) {
224
+ const { dst } = addTemplate ( {
225
+ filename : `${ moduleFolderName } /nitro.ts` ,
226
+ getContents : ( ) => {
227
+ const result = addedNitroFiles
228
+ . values ( )
229
+ . map ( ( x ) => {
230
+ // get rid of '.ts' extension
231
+ const exportFrom = path . join ( path . dirname ( x ) , path . parse ( x ) . name ) ;
232
+ return `export * from "${ resolver . resolve ( exportFrom ) } ";` ;
233
+ } )
234
+ . toArray ( ) ;
235
+
236
+ result . unshift (
237
+ `export type * from "${ resolver . resolve ( './runtime/fetchTypes' ) } ";\nexport * from "${ resolver . resolve ( './runtime/fetchUtils' ) } "` ,
238
+ ) ;
239
+
240
+ return result . join ( '\n' ) ;
241
+ } ,
242
+ write : true ,
243
+ } ) ;
244
+
245
+ addNitroTsFile ( nuxt , dst , true ) ;
219
246
}
220
247
} ;
221
248
@@ -245,20 +272,23 @@ const getOpenApiTs = async ({
245
272
}
246
273
247
274
const openApiTsConfig = apiConfig . openApiTsConfig
248
- ? { ...apiConfig . openApiTsConfig , ...staticOpenApiTsConfig }
275
+ ? {
276
+ ...toMerged ( moduleConfig . openApiTsConfig , apiConfig . openApiTsConfig ) ,
277
+ ...staticOpenApiTsConfig ,
278
+ }
249
279
: { ...moduleConfig . openApiTsConfig , ...staticOpenApiTsConfig } ;
250
280
251
281
if ( apiConfig . openApi ) {
252
282
return await openapiTS ( apiConfig . openApi , openApiTsConfig ) ;
253
283
}
254
284
255
- const optionsFilePath = discoverOpenApiObjectFilePath ( {
285
+ const openAPIFilePath = discoverOpenApiObjectFilePath ( {
256
286
moduleConfig,
257
287
nuxt,
258
288
collectionName,
259
289
} ) ;
260
290
261
- return await openapiTS ( optionsFilePath , openApiTsConfig ) ;
291
+ return await openapiTS ( new URL ( openAPIFilePath ) , openApiTsConfig ) ;
262
292
} ;
263
293
264
294
type DiscoverOpenApiObjectFilePathArgs = {
@@ -298,3 +328,29 @@ const discoverOpenApiObjectFilePath = ({
298
328
`no openapi file found for "${ collectionName } ". Used file paths: ${ JSON . stringify ( triedPaths ) } ` ,
299
329
) ;
300
330
} ;
331
+
332
+ const addNitroTsFile = (
333
+ nuxt : Nuxt ,
334
+ file : string ,
335
+ nuxtIgnore : boolean = false ,
336
+ ) => {
337
+ nuxt . options . nitro . typescript ??= { } ;
338
+ nuxt . options . nitro . typescript . tsConfig ??= { } ;
339
+ nuxt . options . nitro . typescript . tsConfig . include ??= [ ] ;
340
+ nuxt . options . nitro . typescript . tsConfig . include . push ( file ) ;
341
+
342
+ if ( nuxtIgnore ) {
343
+ nuxt . options . typescript . tsConfig . exclude ??= [ ] ;
344
+ nuxt . options . typescript . tsConfig . exclude . push ( file ) ;
345
+ }
346
+ } ;
347
+
348
+ const resolveClientConfig = (
349
+ moduleConfig : ResolvedConfig [ 'clients' ] ,
350
+ apiConfig : ApiConfig [ 'clients' ] ,
351
+ ) => {
352
+ if ( apiConfig ) {
353
+ return toMerged ( moduleConfig , apiConfig ) ;
354
+ }
355
+ return moduleConfig ;
356
+ } ;
0 commit comments