diff --git a/src/core/generators/axios.ts b/src/core/generators/axios.ts index a31a32d94..fdf1cbddd 100644 --- a/src/core/generators/axios.ts +++ b/src/core/generators/axios.ts @@ -127,11 +127,9 @@ const generateAxiosImplementation = ( returnTypesToWrite.set( operationName, - (title?: string) => - `export type ${pascal(operationName)}Result = AsyncReturnType<${ - title - ? `ReturnType['${operationName}']` - : `typeof ${operationName}` + () => + `export type ${pascal(operationName)}Result = AxiosResponse<${ + response.definition.success || 'unknown' }>`, ); @@ -179,6 +177,7 @@ export const generateAxiosFooter: ClientFooterBuilder = ({ operationNames, title, noFunction, + hasMutator, }) => { const functionFooter = `return {${operationNames.join(',')}}};\n`; const returnTypesArr = operationNames @@ -188,14 +187,18 @@ export const generateAxiosFooter: ClientFooterBuilder = ({ : ''; }) .filter(Boolean); - const returnTypes = returnTypesArr.length + let returnTypes = hasMutator ? `\n// eslint-disable-next-line @typescript-eslint/no-explicit-any type AsyncReturnType< T extends (...args: any) => Promise > = T extends (...args: any) => Promise ? R : any; -\n${returnTypesArr.join('\n')}` +\n` : ''; + if (returnTypesArr.length) { + returnTypes += returnTypesArr.join('\n'); + } + return noFunction ? returnTypes : functionFooter + returnTypes; }; diff --git a/src/core/generators/client.ts b/src/core/generators/client.ts index 9d2cd9d57..c22731b36 100644 --- a/src/core/generators/client.ts +++ b/src/core/generators/client.ts @@ -182,11 +182,13 @@ export const generateClientFooter = ({ operationNames, title, customTitleFunc, + hasMutator, }: { outputClient: OutputClient | OutputClientFunc; operationNames: string[]; title: string; customTitleFunc?: (title: string) => string; + hasMutator: boolean; }): GeneratorClientExtra => { const titles = generateClientTitle(outputClient, title, customTitleFunc); const { footer } = getGeneratorClient(outputClient); @@ -201,10 +203,10 @@ export const generateClientFooter = ({ '[WARN] Passing an array of strings for operations names to the footer function is deprecated and will be removed in a future major release. Please pass them in an object instead: { operationNames: string[] }.', ); } else { - implementation = footer({ operationNames, title: titles.implementation }); + implementation = footer({ operationNames, title: titles.implementation, hasMutator }); } } catch (e) { - implementation = footer({ operationNames, title: titles.implementation }); + implementation = footer({ operationNames, title: titles.implementation, hasMutator }); } return { diff --git a/src/core/writers/target.ts b/src/core/writers/target.ts index c9e849210..0c03c7ef3 100644 --- a/src/core/writers/target.ts +++ b/src/core/writers/target.ts @@ -63,6 +63,7 @@ export const generateTarget = ( operationNames, title: pascal(info.title), customTitleFunc: options.override.title, + hasMutator: !!acc.mutators.length, }); acc.implementation += footer.implementation; acc.implementationMSW.handler += footer.implementationMSW; diff --git a/src/core/writers/targetTags.ts b/src/core/writers/targetTags.ts index 47db5b7b0..a08c0dcad 100644 --- a/src/core/writers/targetTags.ts +++ b/src/core/writers/targetTags.ts @@ -96,7 +96,9 @@ export const generateTargetForTags = ( operationNames, title: pascal(tag), customTitleFunc: options.override.title, + hasMutator: !!target.mutators?.length, }); + const header = generateClientHeader({ outputClient: options.client, isRequestOptions: options.override.requestOptions !== false, diff --git a/src/types/generator.ts b/src/types/generator.ts index f21dee85b..610e0e54c 100644 --- a/src/types/generator.ts +++ b/src/types/generator.ts @@ -129,7 +129,7 @@ export type GeneratorMutator = { hasSecondArg: boolean; hasThirdArg: boolean; isHook: boolean; - bodyTypeName?: string + bodyTypeName?: string; }; export type ClientBuilder = ( @@ -148,13 +148,12 @@ export type ClientHeaderBuilder = (params: { provideIn: boolean | 'root' | 'any'; }) => string; -export type ClientFooterBuilder = ( - params: { - noFunction?: boolean | undefined; - operationNames: string[]; - title?: string; - }, -) => string; +export type ClientFooterBuilder = (params: { + noFunction?: boolean | undefined; + operationNames: string[]; + title?: string; + hasMutator: boolean; +}) => string; export type ClientTitleBuilder = (title: string) => string;