diff --git a/.changeset/mighty-months-rhyme.md b/.changeset/mighty-months-rhyme.md new file mode 100644 index 000000000..f83af9cb8 --- /dev/null +++ b/.changeset/mighty-months-rhyme.md @@ -0,0 +1,7 @@ +--- +"@kubb/swagger-tanstack-query": patch +"@kubb/swagger-client": patch +"@kubb/swagger-swr": patch +--- + +use of `dataReturnType` to set res.data or res diff --git a/packages/swagger-client/src/components/Client.tsx b/packages/swagger-client/src/components/Client.tsx index 848a3fc75..3a68fb4f3 100644 --- a/packages/swagger-client/src/components/Client.tsx +++ b/packages/swagger-client/src/components/Client.tsx @@ -64,25 +64,12 @@ function Template({ const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}` - if (client.dataReturnType === 'full') { - return ( - - {` - return client<${client.generics}>({ -${resolvedClientOptions} - })`} - - ) - } - return ( {` -const { data: resData } = await client<${client.generics}>({ +return client<${client.generics}>({ ${resolvedClientOptions} -}) - -return resData`} +}).then(res => ${client.dataReturnType === 'data' ? 'res.data' : 'res'})`} ) } diff --git a/packages/swagger-swr/src/components/Mutation.tsx b/packages/swagger-swr/src/components/Mutation.tsx index 308331b42..11c782648 100644 --- a/packages/swagger-swr/src/components/Mutation.tsx +++ b/packages/swagger-swr/src/components/Mutation.tsx @@ -45,6 +45,7 @@ type TemplateProps = { withHeaders: boolean path: URLPath } + dataReturnType: NonNullable } function Template({ @@ -55,6 +56,7 @@ function Template({ JSDoc, client, hook, + dataReturnType, }: TemplateProps): ReactNode { const clientOptions = [ `method: "${client.method}"`, @@ -78,7 +80,7 @@ function Template({ (_url${client.withData ? ', { arg: data }' : ''}) => { return client<${client.generics}>({ ${resolvedClientOptions} - }) + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}) }, mutationOptions ) @@ -97,7 +99,7 @@ function Template({ (_url${client.withData ? ', { arg: data }' : ''}) => { return client<${client.generics}>({ ${resolvedClientOptions} - }) + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}) }, mutationOptions ) @@ -120,6 +122,7 @@ type Props = { export function Mutation({ Template = defaultTemplates.default, }: Props): ReactNode { + const { options: { dataReturnType } } = usePlugin() const operation = useOperation() const name = useOperationName({ type: 'function' }) const schemas = useSchemas() @@ -187,6 +190,7 @@ export function Mutation({ hook={hook} params={params.toString()} returnType={`SWRMutationResponse<${resultGenerics.join(', ')}>`} + dataReturnType={dataReturnType} /> ) } diff --git a/packages/swagger-tanstack-query/src/components/Mutation.tsx b/packages/swagger-tanstack-query/src/components/Mutation.tsx index be8ea03b9..722a57be3 100644 --- a/packages/swagger-tanstack-query/src/components/Mutation.tsx +++ b/packages/swagger-tanstack-query/src/components/Mutation.tsx @@ -50,6 +50,7 @@ type TemplateProps = { withHeaders: boolean path: URLPath } + dataReturnType: NonNullable } function Template({ @@ -60,6 +61,7 @@ function Template({ JSDoc, client, hook, + dataReturnType, }: TemplateProps): ReactNode { const clientOptions = [ `method: "${client.method}"`, @@ -82,7 +84,7 @@ function Template({ ${hook.children || ''} return client<${client.generics}>({ ${resolvedClientOptions} - }).then(res => res as TData) + }).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'}) }, ...mutationOptions })`} @@ -232,6 +234,7 @@ export function Mutation({ optionsType, Template = defaultTemplates.react, }: Props): ReactNode { + const { options: { dataReturnType } } = usePlugin() const operation = useOperation() const name = useOperationName({ type: 'function' }) @@ -308,6 +311,7 @@ export function Mutation({ hook={hook} params={params.toString()} returnType={`${resultType}<${resultGenerics.join(', ')}>`} + dataReturnType={dataReturnType} context={{ factory }} /> diff --git a/packages/swagger-tanstack-query/src/components/Query.tsx b/packages/swagger-tanstack-query/src/components/Query.tsx index a752dae87..881cafba8 100644 --- a/packages/swagger-tanstack-query/src/components/Query.tsx +++ b/packages/swagger-tanstack-query/src/components/Query.tsx @@ -255,7 +255,7 @@ export function Query({ QueryKeyTemplate = QueryKey.templates.react, QueryOptionsTemplate = QueryOptions.templates.react, }: Props): ReactNode { - const { key: pluginKey } = usePlugin() + const { key: pluginKey, options: { dataReturnType } } = usePlugin() const operation = useOperation() const schemas = useSchemas() const name = useOperationName({ type: 'function' }) @@ -363,7 +363,14 @@ export function Query({ return ( <> - +