Skip to content

Commit

Permalink
fix(axios-client): better result type without mutators
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Apr 4, 2022
1 parent fc1973b commit ee09ac8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
17 changes: 10 additions & 7 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ const generateAxiosImplementation = (

returnTypesToWrite.set(
operationName,
(title?: string) =>
`export type ${pascal(operationName)}Result = AsyncReturnType<${
title
? `ReturnType<typeof ${title}>['${operationName}']`
: `typeof ${operationName}`
() =>
`export type ${pascal(operationName)}Result = AxiosResponse<${
response.definition.success || 'unknown'
}>`,
);

Expand Down Expand Up @@ -179,6 +177,7 @@ export const generateAxiosFooter: ClientFooterBuilder = ({
operationNames,
title,
noFunction,
hasMutator,
}) => {
const functionFooter = `return {${operationNames.join(',')}}};\n`;
const returnTypesArr = operationNames
Expand All @@ -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<any>
> = T extends (...args: any) => Promise<infer R> ? R : any;
\n${returnTypesArr.join('\n')}`
\n`
: '';

if (returnTypesArr.length) {
returnTypes += returnTypesArr.join('\n');
}

return noFunction ? returnTypes : functionFooter + returnTypes;
};

Expand Down
6 changes: 4 additions & 2 deletions src/core/generators/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/core/writers/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/core/writers/targetTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 7 additions & 8 deletions src/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export type GeneratorMutator = {
hasSecondArg: boolean;
hasThirdArg: boolean;
isHook: boolean;
bodyTypeName?: string
bodyTypeName?: string;
};

export type ClientBuilder = (
Expand All @@ -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;

Expand Down

1 comment on commit ee09ac8

@vercel
Copy link

@vercel vercel bot commented on ee09ac8 Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.