@@ -49,8 +49,6 @@ const PARAMS_SERIALIZER_DEPENDENCIES: GeneratorDependency[] = [
4949 } ,
5050] ;
5151
52- const returnTypesToWrite = new Map < string , ( title ?: string ) => string > ( ) ;
53-
5452export const getAxiosDependencies : ClientDependenciesBuilder = (
5553 hasGlobalMutator ,
5654 hasParamsSerializerOptions : boolean ,
@@ -147,17 +145,14 @@ const generateAxiosImplementation = (
147145 )
148146 : '' ;
149147
150- returnTypesToWrite . set (
151- operationName ,
152- ( title ?: string ) =>
153- `export type ${ pascal (
154- operationName ,
155- ) } Result = NonNullable<Awaited<ReturnType<${
156- title
157- ? `ReturnType<typeof ${ title } >['${ operationName } ']`
158- : `typeof ${ operationName } `
159- } >>>`,
160- ) ;
148+ const returnType = ( title ?: string ) =>
149+ `export type ${ pascal (
150+ operationName ,
151+ ) } Result = NonNullable<Awaited<ReturnType<${
152+ title
153+ ? `ReturnType<typeof ${ title } >['${ operationName } ']`
154+ : `typeof ${ operationName } `
155+ } >>>`;
161156
162157 const propsImplementation =
163158 mutator . bodyTypeName && body . definition
@@ -167,16 +162,19 @@ const generateAxiosImplementation = (
167162 )
168163 : toObjectString ( props , 'implementation' ) ;
169164
170- return `const ${ operationName } = (\n ${ propsImplementation } \n ${
171- isRequestOptions && mutator . hasSecondArg
172- ? `options${ context . output . optionsParamRequired ? '' : '?' } : SecondParameter<typeof ${ mutator . name } <${ response . definition . success || 'unknown' } >>,`
173- : ''
174- } ) => {${ bodyForm }
165+ return {
166+ implementation : `const ${ operationName } = (\n ${ propsImplementation } \n ${
167+ isRequestOptions && mutator . hasSecondArg
168+ ? `options${ context . output . optionsParamRequired ? '' : '?' } : SecondParameter<typeof ${ mutator . name } <${ response . definition . success || 'unknown' } >>,`
169+ : ''
170+ } ) => {${ bodyForm }
175171 return ${ mutator . name } <${ response . definition . success || 'unknown' } >(
176172 ${ mutatorConfig } ,
177173 ${ requestOptions } );
178174 }
179- ` ;
175+ ` ,
176+ returnType,
177+ } ;
180178 }
181179
182180 const options = generateOptions ( {
@@ -195,28 +193,28 @@ const generateAxiosImplementation = (
195193 hasSignal : false ,
196194 } ) ;
197195
198- returnTypesToWrite . set (
199- operationName ,
200- ( ) =>
201- `export type ${ pascal ( operationName ) } Result = AxiosResponse<${
202- response . definition . success || 'unknown'
203- } >`,
204- ) ;
196+ const returnType = ( ) =>
197+ `export type ${ pascal ( operationName ) } Result = AxiosResponse<${
198+ response . definition . success || 'unknown'
199+ } >`;
205200
206201 // In factory mode, use the axiosInstance parameter
207202 // In functions mode with global import, .default may be needed based on tsconfig
208203 const axiosRef = isFactoryMode
209204 ? 'axiosInstance'
210205 : `axios${ isSyntheticDefaultImportsAllowed ? '' : '.default' } ` ;
211206
212- return `const ${ operationName } = (\n ${ toObjectString ( props , 'implementation' ) } ${
213- isRequestOptions
214- ? `options${ context . output . optionsParamRequired ? '' : '?' } : AxiosRequestConfig\n`
215- : ''
216- } ): Promise<AxiosResponse<${ response . definition . success || 'unknown' } >> => {${ bodyForm }
207+ return {
208+ implementation : `const ${ operationName } = (\n ${ toObjectString ( props , 'implementation' ) } ${
209+ isRequestOptions
210+ ? `options${ context . output . optionsParamRequired ? '' : '?' } : AxiosRequestConfig\n`
211+ : ''
212+ } ): Promise<AxiosResponse<${ response . definition . success || 'unknown' } >> => {${ bodyForm }
217213 return ${ axiosRef } .${ verb } (${ options } );
218214 }
219- ` ;
215+ ` ,
216+ returnType,
217+ } ;
220218} ;
221219
222220export const generateAxiosTitle : ClientTitleBuilder = ( title ) => {
258256
259257export const generateAxiosFooter : ClientFooterBuilder = ( {
260258 operationNames,
259+ operations,
261260 title,
262261 noFunction,
263262 hasMutator,
@@ -275,13 +274,11 @@ export const generateAxiosFooter: ClientFooterBuilder = ({
275274\n` ;
276275 }
277276
278- for ( const operationName of operationNames ) {
279- if ( returnTypesToWrite . has ( operationName ) ) {
280- // Map.has ensures Map.get will not return undefined, but TS still complains
281- // bug https://github.com/microsoft/TypeScript/issues/13086
282- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
283- const func = returnTypesToWrite . get ( operationName ) ! ;
284- footer += func ( noFunction ? undefined : title ) + '\n' ;
277+ if ( operations ) {
278+ for ( const operation of operations ) {
279+ if ( operation . types ?. result ) {
280+ footer += operation . types . result ( noFunction ? undefined : title ) + '\n' ;
281+ }
285282 }
286283 }
287284
@@ -294,27 +291,35 @@ export const generateAxios = (
294291 isFactoryMode = false ,
295292) => {
296293 const imports = generateVerbImports ( verbOptions ) ;
297- const implementation = generateAxiosImplementation (
294+ const { implementation, returnType } = generateAxiosImplementation (
298295 verbOptions ,
299296 options ,
300297 isFactoryMode ,
301298 ) ;
302299
303- return { implementation, imports } ;
300+ return { implementation, imports, returnType } ;
304301} ;
305302
306303// Factory mode generator - axios is optional parameter
307304export const generateAxiosFactory : ClientBuilder = ( verbOptions , options ) => {
308- const { implementation, imports } = generateAxios ( verbOptions , options , true ) ;
309- return { implementation, imports } ;
305+ const { implementation, imports, returnType } = generateAxios (
306+ verbOptions ,
307+ options ,
308+ true ,
309+ ) ;
310+ return { implementation, imports, returnType } ;
310311} ;
311312
312313export const generateAxiosFunctions : ClientBuilder = ( verbOptions , options ) => {
313- const { implementation, imports } = generateAxios ( verbOptions , options ) ;
314+ const { implementation, imports, returnType } = generateAxios (
315+ verbOptions ,
316+ options ,
317+ ) ;
314318
315319 return {
316320 implementation : 'export ' + implementation ,
317321 imports,
322+ returnType,
318323 } ;
319324} ;
320325
0 commit comments