@kubb/plugin-swr 5.0.0-beta.42 → 5.0.0-beta.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{components-CD7ZoS3B.cjs → components-DxWGjLWg.cjs} +231 -313
- package/dist/components-DxWGjLWg.cjs.map +1 -0
- package/dist/{components-BuLagnaM.js → components-mWBRE6E4.js} +232 -314
- package/dist/components-mWBRE6E4.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.js +1 -1
- package/dist/{generators-D_Cz5DDw.cjs → generators-CJ4ziuNk.cjs} +66 -90
- package/dist/generators-CJ4ziuNk.cjs.map +1 -0
- package/dist/{generators-CSinCSzM.js → generators-D2blgLOY.js} +67 -91
- package/dist/generators-D2blgLOY.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +6 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +68 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +69 -19
- package/dist/index.js.map +1 -1
- package/dist/{types-BoANASs2.d.ts → types-DcBaH0gA.d.ts} +13 -16
- package/package.json +10 -18
- package/dist/components-BuLagnaM.js.map +0 -1
- package/dist/components-CD7ZoS3B.cjs.map +0 -1
- package/dist/generators-CSinCSzM.js.map +0 -1
- package/dist/generators-D_Cz5DDw.cjs.map +0 -1
- package/extension.yaml +0 -199
- package/src/components/Mutation.tsx +0 -155
- package/src/components/MutationKey.tsx +0 -25
- package/src/components/Query.tsx +0 -128
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -74
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -2
- package/src/generators/mutationGenerator.tsx +0 -162
- package/src/generators/queryGenerator.tsx +0 -174
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -154
- package/src/resolvers/resolverSwr.ts +0 -56
- package/src/types.ts +0 -197
- package/src/utils.ts +0 -10
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function, Type } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import type { PluginSwr } from '../types.ts'
|
|
7
|
-
import { buildRequestConfigType, getComments, resolveErrorNames } from '../utils.ts'
|
|
8
|
-
|
|
9
|
-
type Props = {
|
|
10
|
-
name: string
|
|
11
|
-
clientName: string
|
|
12
|
-
mutationKeyName: string
|
|
13
|
-
mutationKeyTypeName: string
|
|
14
|
-
mutationArgTypeName: string
|
|
15
|
-
node: ast.OperationNode
|
|
16
|
-
tsResolver: ResolverTs
|
|
17
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
18
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
19
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
20
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
24
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
25
|
-
const keysPrinter = functionPrinter({ mode: 'keys' })
|
|
26
|
-
|
|
27
|
-
function createMutationArgParams(
|
|
28
|
-
node: ast.OperationNode,
|
|
29
|
-
options: {
|
|
30
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
31
|
-
resolver: ResolverTs
|
|
32
|
-
},
|
|
33
|
-
): ast.FunctionParametersNode {
|
|
34
|
-
return ast.createOperationParams(node, {
|
|
35
|
-
paramsType: 'inline',
|
|
36
|
-
pathParamsType: 'inline',
|
|
37
|
-
paramsCasing: options.paramsCasing,
|
|
38
|
-
resolver: options.resolver,
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function buildMutationParamsNode(
|
|
43
|
-
node: ast.OperationNode,
|
|
44
|
-
options: {
|
|
45
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
46
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
47
|
-
mutationKeyTypeName: string
|
|
48
|
-
mutationArgTypeName: string
|
|
49
|
-
resolver: ResolverTs
|
|
50
|
-
},
|
|
51
|
-
): ast.FunctionParametersNode {
|
|
52
|
-
const { dataReturnType, mutationKeyTypeName, mutationArgTypeName, resolver } = options
|
|
53
|
-
const responseName = resolver.resolveResponseName(node)
|
|
54
|
-
const errorNames = resolveErrorNames(node, resolver)
|
|
55
|
-
|
|
56
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
57
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
58
|
-
|
|
59
|
-
return ast.createFunctionParameters({
|
|
60
|
-
params: [
|
|
61
|
-
ast.createFunctionParameter({
|
|
62
|
-
name: 'options',
|
|
63
|
-
type: ast.createParamsType({
|
|
64
|
-
variant: 'reference',
|
|
65
|
-
name: `{
|
|
66
|
-
mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
|
|
67
|
-
client?: ${buildRequestConfigType(node, resolver)},
|
|
68
|
-
shouldFetch?: boolean,
|
|
69
|
-
}`,
|
|
70
|
-
}),
|
|
71
|
-
default: '{}',
|
|
72
|
-
}),
|
|
73
|
-
],
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function Mutation({
|
|
78
|
-
name,
|
|
79
|
-
clientName,
|
|
80
|
-
mutationKeyName,
|
|
81
|
-
mutationKeyTypeName,
|
|
82
|
-
mutationArgTypeName,
|
|
83
|
-
paramsCasing,
|
|
84
|
-
paramsType,
|
|
85
|
-
pathParamsType,
|
|
86
|
-
dataReturnType,
|
|
87
|
-
node,
|
|
88
|
-
tsResolver,
|
|
89
|
-
}: Props): KubbReactNode {
|
|
90
|
-
const responseName = tsResolver.resolveResponseName(node)
|
|
91
|
-
const errorNames = resolveErrorNames(node, tsResolver)
|
|
92
|
-
|
|
93
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
94
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
95
|
-
|
|
96
|
-
const mutationArgParamsNode = createMutationArgParams(node, { paramsCasing, resolver: tsResolver })
|
|
97
|
-
const hasMutationParams = mutationArgParamsNode.params.length > 0
|
|
98
|
-
const argTypeBody = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
99
|
-
const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
100
|
-
|
|
101
|
-
const generics = [TData, TError, `${mutationKeyTypeName} | null`, mutationArgTypeName]
|
|
102
|
-
|
|
103
|
-
const paramsNode = buildMutationParamsNode(node, {
|
|
104
|
-
paramsCasing,
|
|
105
|
-
dataReturnType,
|
|
106
|
-
mutationKeyTypeName,
|
|
107
|
-
mutationArgTypeName,
|
|
108
|
-
resolver: tsResolver,
|
|
109
|
-
})
|
|
110
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
111
|
-
|
|
112
|
-
const clientCallParamsNode = ast.createOperationParams(node, {
|
|
113
|
-
paramsType,
|
|
114
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
115
|
-
paramsCasing,
|
|
116
|
-
resolver: tsResolver,
|
|
117
|
-
extraParams: [
|
|
118
|
-
ast.createFunctionParameter({
|
|
119
|
-
name: 'config',
|
|
120
|
-
type: ast.createParamsType({
|
|
121
|
-
variant: 'reference',
|
|
122
|
-
name: buildRequestConfigType(node, tsResolver),
|
|
123
|
-
}),
|
|
124
|
-
default: '{}',
|
|
125
|
-
}),
|
|
126
|
-
],
|
|
127
|
-
})
|
|
128
|
-
const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
|
|
129
|
-
|
|
130
|
-
return (
|
|
131
|
-
<>
|
|
132
|
-
<File.Source name={mutationArgTypeName} isExportable isIndexable isTypeOnly>
|
|
133
|
-
<Type name={mutationArgTypeName} export>
|
|
134
|
-
{hasMutationParams ? `{ ${argTypeBody} }` : 'never'}
|
|
135
|
-
</Type>
|
|
136
|
-
</File.Source>
|
|
137
|
-
<File.Source name={name} isExportable isIndexable>
|
|
138
|
-
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
|
|
139
|
-
{`
|
|
140
|
-
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
|
|
141
|
-
const mutationKey = ${mutationKeyName}()
|
|
142
|
-
|
|
143
|
-
return useSWRMutation<${generics.join(', ')}>(
|
|
144
|
-
shouldFetch ? mutationKey : null,
|
|
145
|
-
async (_url${hasMutationParams ? `, { arg: { ${argKeysStr} } }` : ''}) => {
|
|
146
|
-
return ${clientName}(${clientCallStr})
|
|
147
|
-
},
|
|
148
|
-
mutationOptions
|
|
149
|
-
)
|
|
150
|
-
`}
|
|
151
|
-
</Function>
|
|
152
|
-
</File.Source>
|
|
153
|
-
</>
|
|
154
|
-
)
|
|
155
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Transformer } from '@internals/tanstack-query'
|
|
2
|
-
import { MutationKey as SharedMutationKey } from '@internals/tanstack-query'
|
|
3
|
-
import type { ast } from '@kubb/core'
|
|
4
|
-
import { File, Type } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
|
|
7
|
-
type Props = {
|
|
8
|
-
name: string
|
|
9
|
-
typeName: string
|
|
10
|
-
node: ast.OperationNode
|
|
11
|
-
paramsCasing: 'camelcase' | undefined
|
|
12
|
-
pathParamsType: 'object' | 'inline'
|
|
13
|
-
transformer: Transformer | undefined
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function MutationKey({ name, typeName, node, paramsCasing, pathParamsType, transformer }: Props): KubbReactNode {
|
|
17
|
-
return (
|
|
18
|
-
<>
|
|
19
|
-
<SharedMutationKey name={name} node={node} paramsCasing={paramsCasing} pathParamsType={pathParamsType} transformer={transformer} />
|
|
20
|
-
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
21
|
-
<Type export name={typeName}>{`ReturnType<typeof ${name}>`}</Type>
|
|
22
|
-
</File.Source>
|
|
23
|
-
</>
|
|
24
|
-
)
|
|
25
|
-
}
|
package/src/components/Query.tsx
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
|
|
7
|
-
import type { PluginSwr } from '../types.ts'
|
|
8
|
-
import { buildQueryKeyParams, getComments, resolveErrorNames } from '../utils.ts'
|
|
9
|
-
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
10
|
-
|
|
11
|
-
type Props = {
|
|
12
|
-
name: string
|
|
13
|
-
queryOptionsName: string
|
|
14
|
-
queryKeyName: string
|
|
15
|
-
queryKeyTypeName: string
|
|
16
|
-
node: ast.OperationNode
|
|
17
|
-
tsResolver: ResolverTs
|
|
18
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
19
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
20
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
21
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
25
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
26
|
-
|
|
27
|
-
function buildQueryParamsNode(
|
|
28
|
-
node: ast.OperationNode,
|
|
29
|
-
options: {
|
|
30
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
31
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
32
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
33
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
34
|
-
resolver: ResolverTs
|
|
35
|
-
},
|
|
36
|
-
): ast.FunctionParametersNode {
|
|
37
|
-
const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
|
|
38
|
-
const responseName = resolver.resolveResponseName(node)
|
|
39
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
40
|
-
const errorNames = resolveErrorNames(node, resolver)
|
|
41
|
-
|
|
42
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
43
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
44
|
-
|
|
45
|
-
const optionsParam = ast.createFunctionParameter({
|
|
46
|
-
name: 'options',
|
|
47
|
-
type: ast.createParamsType({
|
|
48
|
-
variant: 'reference',
|
|
49
|
-
name: `{
|
|
50
|
-
query?: SWRConfiguration<${[TData, TError].join(', ')}>,
|
|
51
|
-
client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
52
|
-
shouldFetch?: boolean,
|
|
53
|
-
immutable?: boolean
|
|
54
|
-
}`,
|
|
55
|
-
}),
|
|
56
|
-
default: '{}',
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
return ast.createOperationParams(node, {
|
|
60
|
-
paramsType,
|
|
61
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
62
|
-
paramsCasing,
|
|
63
|
-
resolver,
|
|
64
|
-
extraParams: [optionsParam],
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function Query({
|
|
69
|
-
name,
|
|
70
|
-
queryKeyTypeName,
|
|
71
|
-
queryOptionsName,
|
|
72
|
-
queryKeyName,
|
|
73
|
-
paramsType,
|
|
74
|
-
paramsCasing,
|
|
75
|
-
pathParamsType,
|
|
76
|
-
dataReturnType,
|
|
77
|
-
node,
|
|
78
|
-
tsResolver,
|
|
79
|
-
}: Props): KubbReactNode {
|
|
80
|
-
const responseName = tsResolver.resolveResponseName(node)
|
|
81
|
-
const errorNames = resolveErrorNames(node, tsResolver)
|
|
82
|
-
|
|
83
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
84
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
85
|
-
const generics = [TData, TError, `${queryKeyTypeName} | null`]
|
|
86
|
-
|
|
87
|
-
const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
88
|
-
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
89
|
-
const enabledNames = getEnabledParamNames(queryKeyParamsNode)
|
|
90
|
-
|
|
91
|
-
const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
92
|
-
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
|
|
93
|
-
|
|
94
|
-
const paramsNode = markParamsOptional(
|
|
95
|
-
buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver }),
|
|
96
|
-
enabledNames,
|
|
97
|
-
)
|
|
98
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
99
|
-
|
|
100
|
-
// SWR has no `enabled` option; fold the param-presence check into the null-key gate
|
|
101
|
-
// so passing `undefined` for a required param disables the request (mirrors React Query #60).
|
|
102
|
-
const shouldFetchExpr = enabledNames.length ? `shouldFetch && !!(${enabledNames.join(' && ')})` : 'shouldFetch'
|
|
103
|
-
|
|
104
|
-
return (
|
|
105
|
-
<File.Source name={name} isExportable isIndexable>
|
|
106
|
-
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
|
|
107
|
-
{`
|
|
108
|
-
const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
|
|
109
|
-
|
|
110
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
111
|
-
|
|
112
|
-
return useSWR<${generics.join(', ')}>(
|
|
113
|
-
${shouldFetchExpr} ? queryKey : null,
|
|
114
|
-
{
|
|
115
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),
|
|
116
|
-
...(immutable ? {
|
|
117
|
-
revalidateIfStale: false,
|
|
118
|
-
revalidateOnFocus: false,
|
|
119
|
-
revalidateOnReconnect: false
|
|
120
|
-
} : { }),
|
|
121
|
-
...queryOptions,
|
|
122
|
-
}
|
|
123
|
-
)
|
|
124
|
-
`}
|
|
125
|
-
</Function>
|
|
126
|
-
</File.Source>
|
|
127
|
-
)
|
|
128
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryKey } from '@internals/tanstack-query'
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import { getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
|
|
7
|
-
import type { PluginSwr } from '../types.ts'
|
|
8
|
-
import { buildQueryKeyParams } from '../utils.ts'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
name: string
|
|
12
|
-
clientName: string
|
|
13
|
-
node: ast.OperationNode
|
|
14
|
-
tsResolver: ResolverTs
|
|
15
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
16
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
17
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
21
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
22
|
-
|
|
23
|
-
export function getQueryOptionsParams(
|
|
24
|
-
node: ast.OperationNode,
|
|
25
|
-
options: {
|
|
26
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
27
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
28
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
29
|
-
resolver: ResolverTs
|
|
30
|
-
},
|
|
31
|
-
): ast.FunctionParametersNode {
|
|
32
|
-
const { paramsType, paramsCasing, pathParamsType, resolver } = options
|
|
33
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
34
|
-
|
|
35
|
-
return ast.createOperationParams(node, {
|
|
36
|
-
paramsType,
|
|
37
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
38
|
-
paramsCasing,
|
|
39
|
-
resolver,
|
|
40
|
-
extraParams: [
|
|
41
|
-
ast.createFunctionParameter({
|
|
42
|
-
name: 'config',
|
|
43
|
-
type: ast.createParamsType({
|
|
44
|
-
variant: 'reference',
|
|
45
|
-
name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
|
|
46
|
-
}),
|
|
47
|
-
default: '{}',
|
|
48
|
-
}),
|
|
49
|
-
],
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function QueryOptions({ name, clientName, node, tsResolver, paramsCasing, paramsType, pathParamsType }: Props): KubbReactNode {
|
|
54
|
-
const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
55
|
-
const enabledNames = getEnabledParamNames(queryKeyParamsNode)
|
|
56
|
-
|
|
57
|
-
const paramsNode = markParamsOptional(getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver }), enabledNames)
|
|
58
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
59
|
-
const clientCallStr = injectNonNullAssertions(callPrinter.print(paramsNode) ?? '', enabledNames)
|
|
60
|
-
|
|
61
|
-
return (
|
|
62
|
-
<File.Source name={name} isExportable isIndexable>
|
|
63
|
-
<Function name={name} export params={paramsSignature}>
|
|
64
|
-
{`
|
|
65
|
-
return {
|
|
66
|
-
fetcher: async () => {
|
|
67
|
-
return ${clientName}(${clientCallStr})
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
`}
|
|
71
|
-
</Function>
|
|
72
|
-
</File.Source>
|
|
73
|
-
)
|
|
74
|
-
}
|
package/src/components/index.ts
DELETED
package/src/generators/index.ts
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { resolveOperationTypeNames } from '@internals/shared'
|
|
3
|
-
import { resolveZodSchemaNames } from '@internals/tanstack-query'
|
|
4
|
-
import { ast, defineGenerator } from '@kubb/core'
|
|
5
|
-
import { Client, pluginClientName } from '@kubb/plugin-client'
|
|
6
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
|
-
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
|
-
import { File, jsxRenderer } from '@kubb/renderer-jsx'
|
|
9
|
-
import { Mutation, MutationKey } from '../components'
|
|
10
|
-
import type { PluginSwr } from '../types'
|
|
11
|
-
|
|
12
|
-
export const mutationGenerator = defineGenerator<PluginSwr>({
|
|
13
|
-
name: 'swr-mutation',
|
|
14
|
-
renderer: jsxRenderer,
|
|
15
|
-
operation(node, ctx) {
|
|
16
|
-
if (!ast.isHttpOperationNode(node)) return null
|
|
17
|
-
const { config, driver, resolver, root } = ctx
|
|
18
|
-
const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
|
|
19
|
-
|
|
20
|
-
const pluginTs = driver.getPlugin(pluginTsName)
|
|
21
|
-
if (!pluginTs) return null
|
|
22
|
-
const tsResolver = driver.getResolver(pluginTsName)
|
|
23
|
-
|
|
24
|
-
const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
|
|
25
|
-
const queryMethods = new Set(query ? query.methods : [])
|
|
26
|
-
const isMutation =
|
|
27
|
-
mutation !== false &&
|
|
28
|
-
!isQuery &&
|
|
29
|
-
(mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
|
|
30
|
-
|
|
31
|
-
if (!isMutation) return null
|
|
32
|
-
|
|
33
|
-
const importPath = mutation ? mutation.importPath : 'swr/mutation'
|
|
34
|
-
|
|
35
|
-
const mutationHookName = resolver.resolveMutationName(node)
|
|
36
|
-
const mutationKeyName = resolver.resolveMutationKeyName(node)
|
|
37
|
-
const mutationKeyTypeName = resolver.resolveMutationKeyTypeName(node)
|
|
38
|
-
const mutationArgTypeName = resolver.resolveMutationArgTypeName(node)
|
|
39
|
-
const clientName = resolver.resolveClientName(node)
|
|
40
|
-
|
|
41
|
-
const meta = {
|
|
42
|
-
file: resolver.resolveFile({ name: mutationHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
|
|
43
|
-
fileTs: tsResolver.resolveFile(
|
|
44
|
-
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
45
|
-
{ root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group },
|
|
46
|
-
),
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
|
|
50
|
-
|
|
51
|
-
const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) : undefined
|
|
52
|
-
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
|
|
53
|
-
const fileZod = zodResolver
|
|
54
|
-
? zodResolver.resolveFile(
|
|
55
|
-
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
56
|
-
{ root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
|
|
57
|
-
)
|
|
58
|
-
: undefined
|
|
59
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
|
|
60
|
-
|
|
61
|
-
const clientPlugin = driver.getPlugin(pluginClientName)
|
|
62
|
-
const hasClientPlugin = clientPlugin?.name === pluginClientName
|
|
63
|
-
const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
|
|
64
|
-
const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : undefined
|
|
65
|
-
|
|
66
|
-
const clientFile = shouldUseClientPlugin
|
|
67
|
-
? clientResolver?.resolveFile(
|
|
68
|
-
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
69
|
-
{
|
|
70
|
-
root,
|
|
71
|
-
output: clientPlugin?.options?.output ?? output,
|
|
72
|
-
group: clientPlugin?.options?.group,
|
|
73
|
-
},
|
|
74
|
-
)
|
|
75
|
-
: undefined
|
|
76
|
-
|
|
77
|
-
const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
|
|
78
|
-
|
|
79
|
-
return (
|
|
80
|
-
<File
|
|
81
|
-
baseName={meta.file.baseName}
|
|
82
|
-
path={meta.file.path}
|
|
83
|
-
meta={meta.file.meta}
|
|
84
|
-
banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
|
|
85
|
-
footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
|
|
86
|
-
>
|
|
87
|
-
{fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
|
|
88
|
-
{clientOptions.importPath ? (
|
|
89
|
-
<>
|
|
90
|
-
{!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
|
|
91
|
-
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
|
|
92
|
-
{clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
|
|
93
|
-
</>
|
|
94
|
-
) : (
|
|
95
|
-
<>
|
|
96
|
-
{!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
|
|
97
|
-
<File.Import
|
|
98
|
-
name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
|
|
99
|
-
root={meta.file.path}
|
|
100
|
-
path={path.resolve(root, '.kubb/client.ts')}
|
|
101
|
-
isTypeOnly
|
|
102
|
-
/>
|
|
103
|
-
{clientOptions.dataReturnType === 'full' && (
|
|
104
|
-
<File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
|
|
105
|
-
)}
|
|
106
|
-
</>
|
|
107
|
-
)}
|
|
108
|
-
{shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
|
|
109
|
-
{!shouldUseClientPlugin && node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') && (
|
|
110
|
-
<File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />
|
|
111
|
-
)}
|
|
112
|
-
{meta.fileTs && importedTypeNames.length > 0 && (
|
|
113
|
-
<File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
|
|
114
|
-
)}
|
|
115
|
-
|
|
116
|
-
<MutationKey
|
|
117
|
-
name={mutationKeyName}
|
|
118
|
-
typeName={mutationKeyTypeName}
|
|
119
|
-
node={node}
|
|
120
|
-
pathParamsType={pathParamsType}
|
|
121
|
-
paramsCasing={paramsCasing}
|
|
122
|
-
transformer={ctx.options.mutationKey}
|
|
123
|
-
/>
|
|
124
|
-
|
|
125
|
-
{!shouldUseClientPlugin && (
|
|
126
|
-
<Client
|
|
127
|
-
name={resolvedClientName}
|
|
128
|
-
baseURL={clientOptions.baseURL}
|
|
129
|
-
dataReturnType={clientOptions.dataReturnType || 'data'}
|
|
130
|
-
paramsCasing={clientOptions.paramsCasing || paramsCasing}
|
|
131
|
-
paramsType={paramsType}
|
|
132
|
-
pathParamsType={pathParamsType}
|
|
133
|
-
parser={parser}
|
|
134
|
-
node={node}
|
|
135
|
-
tsResolver={tsResolver}
|
|
136
|
-
zodResolver={zodResolver}
|
|
137
|
-
/>
|
|
138
|
-
)}
|
|
139
|
-
|
|
140
|
-
{mutation && (
|
|
141
|
-
<>
|
|
142
|
-
<File.Import name={'useSWRMutation'} path={importPath} />
|
|
143
|
-
<File.Import name={['SWRMutationConfiguration']} path={importPath} isTypeOnly />
|
|
144
|
-
<Mutation
|
|
145
|
-
name={mutationHookName}
|
|
146
|
-
clientName={resolvedClientName}
|
|
147
|
-
mutationKeyName={mutationKeyName}
|
|
148
|
-
mutationKeyTypeName={mutationKeyTypeName}
|
|
149
|
-
mutationArgTypeName={mutationArgTypeName}
|
|
150
|
-
node={node}
|
|
151
|
-
tsResolver={tsResolver}
|
|
152
|
-
dataReturnType={clientOptions.dataReturnType || 'data'}
|
|
153
|
-
paramsCasing={paramsCasing}
|
|
154
|
-
paramsType={paramsType}
|
|
155
|
-
pathParamsType={pathParamsType}
|
|
156
|
-
/>
|
|
157
|
-
</>
|
|
158
|
-
)}
|
|
159
|
-
</File>
|
|
160
|
-
)
|
|
161
|
-
},
|
|
162
|
-
})
|