Skip to content

Commit

Permalink
feat(axios): add return type for swr client
Browse files Browse the repository at this point in the history
  • Loading branch information
CPatchane committed Mar 9, 2022
1 parent 40345ed commit 42c40c9
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/core/generators/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ const generateAxiosImplementation = (
)
: '';

return `const ${operationName} = (\n ${toObjectString(
return `
export type ${pascal(
operationName,
)}Result = NonNullable<AsyncReturnType<typeof ${operationName}>>
const ${operationName} = (\n ${toObjectString(
props,
'implementation',
)}\n ${
Expand All @@ -110,7 +115,12 @@ const generateAxiosImplementation = (
isFormUrlEncoded,
});

return `const ${operationName} = <TData = AxiosResponse<${
return `
export type ${pascal(
operationName,
)}Result = AsyncReturnType<typeof ${operationName}>
const ${operationName} = <TData = AxiosResponse<${
response.definition.success || 'unknown'
}>>(\n ${toObjectString(props, 'implementation')} ${
isRequestOptions ? `options?: AxiosRequestConfig\n` : ''
Expand All @@ -137,7 +147,12 @@ export const generateAxiosHeader = ({
isRequestOptions: boolean;
isMutator: boolean;
noFunction?: boolean;
}) => `${
}) => `
// 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\n
${
isRequestOptions && isMutator
? `// eslint-disable-next-line @typescript-eslint/no-explicit-any
type SecondParameter<T extends (...args: any) => any> = T extends (
Expand Down

0 comments on commit 42c40c9

Please sign in to comment.