Skip to content

Commit

Permalink
fix: use of dataReturnType to set res.data or res
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Dec 13, 2023
1 parent ab21161 commit 621360b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changeset/mighty-months-rhyme.md
Original file line number Diff line number Diff line change
@@ -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
17 changes: 2 additions & 15 deletions packages/swagger-client/src/components/Client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,12 @@ function Template({

const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}`

if (client.dataReturnType === 'full') {
return (
<Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
{`
return client<${client.generics}>({
${resolvedClientOptions}
})`}
</Function>
)
}

return (
<Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
{`
const { data: resData } = await client<${client.generics}>({
return client<${client.generics}>({
${resolvedClientOptions}
})
return resData`}
}).then(res => ${client.dataReturnType === 'data' ? 'res.data' : 'res'})`}
</Function>
)
}
Expand Down
8 changes: 6 additions & 2 deletions packages/swagger-swr/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type TemplateProps = {
withHeaders: boolean
path: URLPath
}
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
}

function Template({
Expand All @@ -55,6 +56,7 @@ function Template({
JSDoc,
client,
hook,
dataReturnType,
}: TemplateProps): ReactNode {
const clientOptions = [
`method: "${client.method}"`,
Expand All @@ -78,7 +80,7 @@ function Template({
(_url${client.withData ? ', { arg: data }' : ''}) => {
return client<${client.generics}>({
${resolvedClientOptions}
})
}).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'})
},
mutationOptions
)
Expand All @@ -97,7 +99,7 @@ function Template({
(_url${client.withData ? ', { arg: data }' : ''}) => {
return client<${client.generics}>({
${resolvedClientOptions}
})
}).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'})
},
mutationOptions
)
Expand All @@ -120,6 +122,7 @@ type Props = {
export function Mutation({
Template = defaultTemplates.default,
}: Props): ReactNode {
const { options: { dataReturnType } } = usePlugin<PluginOptions>()
const operation = useOperation()
const name = useOperationName({ type: 'function' })
const schemas = useSchemas()
Expand Down Expand Up @@ -187,6 +190,7 @@ export function Mutation({
hook={hook}
params={params.toString()}
returnType={`SWRMutationResponse<${resultGenerics.join(', ')}>`}
dataReturnType={dataReturnType}
/>
)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/swagger-tanstack-query/src/components/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type TemplateProps = {
withHeaders: boolean
path: URLPath
}
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
}

function Template({
Expand All @@ -60,6 +61,7 @@ function Template({
JSDoc,
client,
hook,
dataReturnType,
}: TemplateProps): ReactNode {
const clientOptions = [
`method: "${client.method}"`,
Expand All @@ -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
})`}
Expand Down Expand Up @@ -232,6 +234,7 @@ export function Mutation({
optionsType,
Template = defaultTemplates.react,
}: Props): ReactNode {
const { options: { dataReturnType } } = usePlugin<PluginOptions>()
const operation = useOperation()
const name = useOperationName({ type: 'function' })

Expand Down Expand Up @@ -308,6 +311,7 @@ export function Mutation({
hook={hook}
params={params.toString()}
returnType={`${resultType}<${resultGenerics.join(', ')}>`}
dataReturnType={dataReturnType}
context={{ factory }}
/>
</>
Expand Down
11 changes: 9 additions & 2 deletions packages/swagger-tanstack-query/src/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ export function Query({
QueryKeyTemplate = QueryKey.templates.react,
QueryOptionsTemplate = QueryOptions.templates.react,
}: Props): ReactNode {
const { key: pluginKey } = usePlugin<PluginOptions>()
const { key: pluginKey, options: { dataReturnType } } = usePlugin<PluginOptions>()
const operation = useOperation()
const schemas = useSchemas()
const name = useOperationName({ type: 'function' })
Expand Down Expand Up @@ -363,7 +363,14 @@ export function Query({
return (
<>
<QueryKey Template={QueryKeyTemplate} factory={factory} name={queryKey} typeName={queryKeyType} />
<QueryOptions Template={QueryOptionsTemplate} factory={factory} resultType={optionsType} infinite={infinite} suspense={suspense} />
<QueryOptions
Template={QueryOptionsTemplate}
factory={factory}
resultType={optionsType}
dataReturnType={dataReturnType}
infinite={infinite}
suspense={suspense}
/>

<Template
name={[name, infinite ? 'Infinite' : undefined, suspense ? 'Suspense' : undefined].filter(Boolean).join('')}
Expand Down
10 changes: 7 additions & 3 deletions packages/swagger-tanstack-query/src/components/QueryOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getASTParams, getParams, isRequired } from '@kubb/swagger/utils'

import type { HttpMethod } from '@kubb/swagger/oas'
import type { ReactNode } from 'react'
import type { Infinite, Suspense } from '../types.ts'
import type { Infinite, PluginOptions, Suspense } from '../types.ts'

type TemplateProps = {
/**
Expand Down Expand Up @@ -47,6 +47,7 @@ type TemplateProps = {
}
isV5: boolean
infinite?: Infinite
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
}

function Template({
Expand All @@ -59,6 +60,7 @@ function Template({
client,
infinite,
isV5,
dataReturnType,
}: TemplateProps): ReactNode {
const clientOptions = [
`method: "${client.method}"`,
Expand Down Expand Up @@ -96,7 +98,7 @@ function Template({
${hook.children || ''}
return client<${client.generics}>({
${resolvedClientOptions}
}).then(res => res?.data || res)
}).then(res => ${dataReturnType === 'data' ? 'res.data' : 'res'})
},
${resolvedQueryOptions}
}
Expand Down Expand Up @@ -242,9 +244,10 @@ type Props = {
* This will make it possible to override the default behaviour.
*/
Template?: React.ComponentType<FrameworkProps>
dataReturnType: NonNullable<PluginOptions['options']['dataReturnType']>
}

export function QueryOptions({ factory, infinite, suspense, resultType, Template = defaultTemplates.react }: Props): ReactNode {
export function QueryOptions({ factory, infinite, suspense, resultType, dataReturnType, Template = defaultTemplates.react }: Props): ReactNode {
const { key: pluginKey } = usePlugin()
const schemas = useSchemas()
const operation = useOperation()
Expand Down Expand Up @@ -324,6 +327,7 @@ export function QueryOptions({ factory, infinite, suspense, resultType, Template
hook={hook}
isV5={isV5}
infinite={infinite}
dataReturnType={dataReturnType}
context={{
factory,
queryKey,
Expand Down

0 comments on commit 621360b

Please sign in to comment.