@kubb/plugin-react-query 5.0.0-alpha.9 → 5.0.0-beta.100
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/LICENSE +17 -10
- package/README.md +38 -93
- package/dist/index.cjs +2317 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +494 -6
- package/dist/index.js +2283 -143
- package/dist/index.js.map +1 -1
- package/package.json +47 -75
- package/dist/components-BHQT9ZLc.cjs +0 -1634
- package/dist/components-BHQT9ZLc.cjs.map +0 -1
- package/dist/components-CpyHYGOw.js +0 -1520
- package/dist/components-CpyHYGOw.js.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -398
- package/dist/components.js +0 -2
- package/dist/generators-DP07m3rH.cjs +0 -1469
- package/dist/generators-DP07m3rH.cjs.map +0 -1
- package/dist/generators-DkQwKTc2.js +0 -1427
- package/dist/generators-DkQwKTc2.js.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -523
- package/dist/generators.js +0 -2
- package/dist/types-D5S7Ny9r.d.ts +0 -270
- package/src/components/InfiniteQuery.tsx +0 -220
- package/src/components/InfiniteQueryOptions.tsx +0 -289
- package/src/components/Mutation.tsx +0 -158
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -122
- package/src/components/Query.tsx +0 -194
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -181
- package/src/components/SuspenseInfiniteQuery.tsx +0 -220
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -289
- package/src/components/SuspenseQuery.tsx +0 -204
- package/src/components/index.ts +0 -11
- package/src/generators/customHookOptionsFileGenerator.tsx +0 -98
- package/src/generators/hookOptionsGenerator.tsx +0 -196
- package/src/generators/index.ts +0 -7
- package/src/generators/infiniteQueryGenerator.tsx +0 -214
- package/src/generators/mutationGenerator.tsx +0 -195
- package/src/generators/queryGenerator.tsx +0 -194
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -202
- package/src/generators/suspenseQueryGenerator.tsx +0 -207
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -239
- package/src/types.ts +0 -205
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
import { getNestedAccessor } from '@internals/utils'
|
|
2
|
-
import { isAllOptional, isOptional } from '@kubb/oas'
|
|
3
|
-
import { Client } from '@kubb/plugin-client/components'
|
|
4
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
5
|
-
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
7
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
8
|
-
import type { Infinite, PluginReactQuery } from '../types.ts'
|
|
9
|
-
import { QueryKey } from './QueryKey.tsx'
|
|
10
|
-
|
|
11
|
-
type Props = {
|
|
12
|
-
name: string
|
|
13
|
-
clientName: string
|
|
14
|
-
queryKeyName: string
|
|
15
|
-
typeSchemas: OperationSchemas
|
|
16
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
17
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
18
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
19
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
20
|
-
initialPageParam: Infinite['initialPageParam']
|
|
21
|
-
cursorParam: Infinite['cursorParam']
|
|
22
|
-
nextParam: Infinite['nextParam']
|
|
23
|
-
previousParam: Infinite['previousParam']
|
|
24
|
-
queryParam: Infinite['queryParam']
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type GetParamsProps = {
|
|
28
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
29
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
30
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
31
|
-
typeSchemas: OperationSchemas
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) {
|
|
35
|
-
if (paramsType === 'object') {
|
|
36
|
-
const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
|
|
37
|
-
|
|
38
|
-
const children = {
|
|
39
|
-
...pathParams,
|
|
40
|
-
data: typeSchemas.request?.name
|
|
41
|
-
? {
|
|
42
|
-
type: typeSchemas.request?.name,
|
|
43
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
44
|
-
}
|
|
45
|
-
: undefined,
|
|
46
|
-
params: typeSchemas.queryParams?.name
|
|
47
|
-
? {
|
|
48
|
-
type: typeSchemas.queryParams?.name,
|
|
49
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
50
|
-
}
|
|
51
|
-
: undefined,
|
|
52
|
-
headers: typeSchemas.headerParams?.name
|
|
53
|
-
? {
|
|
54
|
-
type: typeSchemas.headerParams?.name,
|
|
55
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
56
|
-
}
|
|
57
|
-
: undefined,
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Check if all children are optional or undefined
|
|
61
|
-
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
|
|
62
|
-
|
|
63
|
-
return FunctionParams.factory({
|
|
64
|
-
data: {
|
|
65
|
-
mode: 'object',
|
|
66
|
-
children,
|
|
67
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
68
|
-
},
|
|
69
|
-
config: {
|
|
70
|
-
type: typeSchemas.request?.name
|
|
71
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
72
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
73
|
-
default: '{}',
|
|
74
|
-
},
|
|
75
|
-
})
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return FunctionParams.factory({
|
|
79
|
-
pathParams: typeSchemas.pathParams?.name
|
|
80
|
-
? {
|
|
81
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
82
|
-
children: getPathParams(typeSchemas.pathParams, {
|
|
83
|
-
typed: true,
|
|
84
|
-
casing: paramsCasing,
|
|
85
|
-
}),
|
|
86
|
-
default: isAllOptional(typeSchemas.pathParams?.schema) ? '{}' : undefined,
|
|
87
|
-
}
|
|
88
|
-
: undefined,
|
|
89
|
-
data: typeSchemas.request?.name
|
|
90
|
-
? {
|
|
91
|
-
type: typeSchemas.request?.name,
|
|
92
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
93
|
-
}
|
|
94
|
-
: undefined,
|
|
95
|
-
params: typeSchemas.queryParams?.name
|
|
96
|
-
? {
|
|
97
|
-
type: typeSchemas.queryParams?.name,
|
|
98
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
99
|
-
}
|
|
100
|
-
: undefined,
|
|
101
|
-
headers: typeSchemas.headerParams?.name
|
|
102
|
-
? {
|
|
103
|
-
type: typeSchemas.headerParams?.name,
|
|
104
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
105
|
-
}
|
|
106
|
-
: undefined,
|
|
107
|
-
config: {
|
|
108
|
-
type: typeSchemas.request?.name
|
|
109
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
110
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
111
|
-
default: '{}',
|
|
112
|
-
},
|
|
113
|
-
})
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function InfiniteQueryOptions({
|
|
117
|
-
name,
|
|
118
|
-
clientName,
|
|
119
|
-
initialPageParam,
|
|
120
|
-
cursorParam,
|
|
121
|
-
nextParam,
|
|
122
|
-
previousParam,
|
|
123
|
-
typeSchemas,
|
|
124
|
-
paramsCasing,
|
|
125
|
-
paramsType,
|
|
126
|
-
dataReturnType,
|
|
127
|
-
pathParamsType,
|
|
128
|
-
queryParam,
|
|
129
|
-
queryKeyName,
|
|
130
|
-
}: Props): FabricReactNode {
|
|
131
|
-
const queryFnDataType = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
132
|
-
const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
133
|
-
const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
|
|
134
|
-
const fallbackPageParamType =
|
|
135
|
-
typeof initialPageParam === 'number'
|
|
136
|
-
? 'number'
|
|
137
|
-
: typeof initialPageParam === 'string'
|
|
138
|
-
? initialPageParam.includes(' as ')
|
|
139
|
-
? (() => {
|
|
140
|
-
const parts = initialPageParam.split(' as ')
|
|
141
|
-
return parts[parts.length - 1] ?? 'unknown'
|
|
142
|
-
})()
|
|
143
|
-
: 'string'
|
|
144
|
-
: typeof initialPageParam === 'boolean'
|
|
145
|
-
? 'boolean'
|
|
146
|
-
: 'unknown'
|
|
147
|
-
const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : undefined
|
|
148
|
-
const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
|
|
149
|
-
|
|
150
|
-
const params = getParams({
|
|
151
|
-
paramsType,
|
|
152
|
-
paramsCasing,
|
|
153
|
-
pathParamsType,
|
|
154
|
-
typeSchemas,
|
|
155
|
-
})
|
|
156
|
-
const clientParams = Client.getParams({
|
|
157
|
-
paramsCasing,
|
|
158
|
-
typeSchemas,
|
|
159
|
-
paramsType,
|
|
160
|
-
pathParamsType,
|
|
161
|
-
isConfigurable: true,
|
|
162
|
-
})
|
|
163
|
-
const queryKeyParams = QueryKey.getParams({
|
|
164
|
-
pathParamsType,
|
|
165
|
-
typeSchemas,
|
|
166
|
-
paramsCasing,
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
// Determine if we should use the new nextParam/previousParam or fall back to legacy cursorParam behavior
|
|
170
|
-
const hasNewParams = nextParam !== undefined || previousParam !== undefined
|
|
171
|
-
|
|
172
|
-
let getNextPageParamExpr: string | undefined
|
|
173
|
-
let getPreviousPageParamExpr: string | undefined
|
|
174
|
-
|
|
175
|
-
if (hasNewParams) {
|
|
176
|
-
// Use the new nextParam and previousParam
|
|
177
|
-
if (nextParam) {
|
|
178
|
-
const accessor = getNestedAccessor(nextParam, 'lastPage')
|
|
179
|
-
if (accessor) {
|
|
180
|
-
getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
if (previousParam) {
|
|
184
|
-
const accessor = getNestedAccessor(previousParam, 'firstPage')
|
|
185
|
-
if (accessor) {
|
|
186
|
-
getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} else if (cursorParam) {
|
|
190
|
-
// Legacy behavior: use cursorParam for both next and previous
|
|
191
|
-
getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`
|
|
192
|
-
getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`
|
|
193
|
-
} else {
|
|
194
|
-
// Fallback behavior: page-based pagination
|
|
195
|
-
if (dataReturnType === 'full') {
|
|
196
|
-
getNextPageParamExpr =
|
|
197
|
-
'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
|
|
198
|
-
} else {
|
|
199
|
-
getNextPageParamExpr =
|
|
200
|
-
'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
|
|
201
|
-
}
|
|
202
|
-
getPreviousPageParamExpr = 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const queryOptions = [
|
|
206
|
-
`initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
|
|
207
|
-
getNextPageParamExpr,
|
|
208
|
-
getPreviousPageParamExpr,
|
|
209
|
-
].filter(Boolean)
|
|
210
|
-
|
|
211
|
-
const infiniteOverrideParams =
|
|
212
|
-
queryParam && typeSchemas.queryParams?.name
|
|
213
|
-
? `
|
|
214
|
-
params = {
|
|
215
|
-
...(params ?? {}),
|
|
216
|
-
['${queryParam}']: pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}'],
|
|
217
|
-
} as ${typeSchemas.queryParams?.name}`
|
|
218
|
-
: ''
|
|
219
|
-
|
|
220
|
-
// Only add enabled check for required (non-optional) parameters
|
|
221
|
-
// Optional parameters with defaults should not prevent query execution
|
|
222
|
-
const enabled = Object.entries(queryKeyParams.flatParams)
|
|
223
|
-
.map(([key, item]) => {
|
|
224
|
-
// Only include if the parameter exists and is NOT optional
|
|
225
|
-
// This ensures we only check required parameters
|
|
226
|
-
return item && !item.optional && !item.default ? key : undefined
|
|
227
|
-
})
|
|
228
|
-
.filter(Boolean)
|
|
229
|
-
.join('&& ')
|
|
230
|
-
|
|
231
|
-
const enabledText = enabled ? `enabled: !!(${enabled}),` : ''
|
|
232
|
-
|
|
233
|
-
if (infiniteOverrideParams) {
|
|
234
|
-
return (
|
|
235
|
-
<File.Source name={name} isExportable isIndexable>
|
|
236
|
-
<Function name={name} export params={params.toConstructor()}>
|
|
237
|
-
{`
|
|
238
|
-
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
|
|
239
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
240
|
-
${enabledText}
|
|
241
|
-
queryKey,
|
|
242
|
-
queryFn: async ({ signal, pageParam }) => {
|
|
243
|
-
${infiniteOverrideParams}
|
|
244
|
-
return ${clientName}(${clientParams.toCall({
|
|
245
|
-
transformName(name) {
|
|
246
|
-
if (name === 'config') {
|
|
247
|
-
return '{ ...config, signal: config.signal ?? signal }'
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
return name
|
|
251
|
-
},
|
|
252
|
-
})})
|
|
253
|
-
},
|
|
254
|
-
${queryOptions.join(',\n')}
|
|
255
|
-
})
|
|
256
|
-
`}
|
|
257
|
-
</Function>
|
|
258
|
-
</File.Source>
|
|
259
|
-
)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
return (
|
|
263
|
-
<File.Source name={name} isExportable isIndexable>
|
|
264
|
-
<Function name={name} export params={params.toConstructor()}>
|
|
265
|
-
{`
|
|
266
|
-
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
|
|
267
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
268
|
-
${enabledText}
|
|
269
|
-
queryKey,
|
|
270
|
-
queryFn: async ({ signal }) => {
|
|
271
|
-
return ${clientName}(${clientParams.toCall({
|
|
272
|
-
transformName(name) {
|
|
273
|
-
if (name === 'config') {
|
|
274
|
-
return '{ ...config, signal: config.signal ?? signal }'
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
return name
|
|
278
|
-
},
|
|
279
|
-
})})
|
|
280
|
-
},
|
|
281
|
-
${queryOptions.join(',\n')}
|
|
282
|
-
})
|
|
283
|
-
`}
|
|
284
|
-
</Function>
|
|
285
|
-
</File.Source>
|
|
286
|
-
)
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
InfiniteQueryOptions.getParams = getParams
|
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import { isOptional, type Operation } from '@kubb/oas'
|
|
2
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
3
|
-
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
4
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
5
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
|
-
import type { PluginReactQuery } from '../types.ts'
|
|
7
|
-
import { MutationKey } from './MutationKey.tsx'
|
|
8
|
-
import { MutationOptions } from './MutationOptions.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
|
-
name: string
|
|
15
|
-
typeName: string
|
|
16
|
-
mutationOptionsName: string
|
|
17
|
-
mutationKeyName: string
|
|
18
|
-
typeSchemas: OperationSchemas
|
|
19
|
-
operation: Operation
|
|
20
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
21
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
22
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
23
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
type GetParamsProps = {
|
|
27
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
28
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
29
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
30
|
-
typeSchemas: OperationSchemas
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function getParams({ paramsCasing, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
34
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
35
|
-
const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
|
|
36
|
-
|
|
37
|
-
const mutationParams = FunctionParams.factory({
|
|
38
|
-
...pathParams,
|
|
39
|
-
data: typeSchemas.request?.name
|
|
40
|
-
? {
|
|
41
|
-
type: typeSchemas.request?.name,
|
|
42
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
43
|
-
}
|
|
44
|
-
: undefined,
|
|
45
|
-
params: typeSchemas.queryParams?.name
|
|
46
|
-
? {
|
|
47
|
-
type: typeSchemas.queryParams?.name,
|
|
48
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
49
|
-
}
|
|
50
|
-
: undefined,
|
|
51
|
-
headers: typeSchemas.headerParams?.name
|
|
52
|
-
? {
|
|
53
|
-
type: typeSchemas.headerParams?.name,
|
|
54
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
55
|
-
}
|
|
56
|
-
: undefined,
|
|
57
|
-
})
|
|
58
|
-
const TRequest = mutationParams.toConstructor()
|
|
59
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
60
|
-
const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
|
|
61
|
-
|
|
62
|
-
return FunctionParams.factory({
|
|
63
|
-
options: {
|
|
64
|
-
type: `
|
|
65
|
-
{
|
|
66
|
-
mutation?: UseMutationOptions<${generics}> & { client?: QueryClient },
|
|
67
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
68
|
-
}
|
|
69
|
-
`,
|
|
70
|
-
default: '{}',
|
|
71
|
-
},
|
|
72
|
-
})
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function Mutation({
|
|
76
|
-
name,
|
|
77
|
-
mutationOptionsName,
|
|
78
|
-
paramsCasing,
|
|
79
|
-
pathParamsType,
|
|
80
|
-
dataReturnType,
|
|
81
|
-
typeSchemas,
|
|
82
|
-
operation,
|
|
83
|
-
mutationKeyName,
|
|
84
|
-
customOptions,
|
|
85
|
-
}: Props): FabricReactNode {
|
|
86
|
-
const mutationKeyParams = MutationKey.getParams({
|
|
87
|
-
pathParamsType,
|
|
88
|
-
typeSchemas,
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
const params = getParams({
|
|
92
|
-
paramsCasing,
|
|
93
|
-
pathParamsType,
|
|
94
|
-
dataReturnType,
|
|
95
|
-
typeSchemas,
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
const mutationParams = FunctionParams.factory({
|
|
99
|
-
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
100
|
-
data: typeSchemas.request?.name
|
|
101
|
-
? {
|
|
102
|
-
type: typeSchemas.request?.name,
|
|
103
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
104
|
-
}
|
|
105
|
-
: undefined,
|
|
106
|
-
params: typeSchemas.queryParams?.name
|
|
107
|
-
? {
|
|
108
|
-
type: typeSchemas.queryParams?.name,
|
|
109
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
110
|
-
}
|
|
111
|
-
: undefined,
|
|
112
|
-
headers: typeSchemas.headerParams?.name
|
|
113
|
-
? {
|
|
114
|
-
type: typeSchemas.headerParams?.name,
|
|
115
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
116
|
-
}
|
|
117
|
-
: undefined,
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
const mutationOptionsParams = MutationOptions.getParams({ typeSchemas })
|
|
121
|
-
|
|
122
|
-
const TRequest = mutationParams.toConstructor()
|
|
123
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
124
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
125
|
-
const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
|
|
126
|
-
const returnType = `UseMutationResult<${generics}>`
|
|
127
|
-
|
|
128
|
-
const mutationOptions = `${mutationOptionsName}(${mutationOptionsParams.toCall()})`
|
|
129
|
-
|
|
130
|
-
return (
|
|
131
|
-
<File.Source name={name} isExportable isIndexable>
|
|
132
|
-
<Function
|
|
133
|
-
name={name}
|
|
134
|
-
export
|
|
135
|
-
params={params.toConstructor()}
|
|
136
|
-
JSDoc={{
|
|
137
|
-
comments: getComments(operation),
|
|
138
|
-
}}
|
|
139
|
-
generics={['TContext']}
|
|
140
|
-
>
|
|
141
|
-
{`
|
|
142
|
-
const { mutation = {}, client: config = {} } = options ?? {}
|
|
143
|
-
const { client: queryClient, ...mutationOptions } = mutation;
|
|
144
|
-
const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
|
|
145
|
-
|
|
146
|
-
const baseOptions = ${mutationOptions} as UseMutationOptions<${generics}>
|
|
147
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' }) as UseMutationOptions<${generics}>` : ''}
|
|
148
|
-
|
|
149
|
-
return useMutation<${generics}>({
|
|
150
|
-
...baseOptions,${customOptions ? '\n...customOptions,' : ''}
|
|
151
|
-
mutationKey,
|
|
152
|
-
...mutationOptions,
|
|
153
|
-
}, queryClient) as ${returnType}
|
|
154
|
-
`}
|
|
155
|
-
</Function>
|
|
156
|
-
</File.Source>
|
|
157
|
-
)
|
|
158
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { MutationKey } from '@internals/tanstack-query'
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { isOptional } from '@kubb/oas'
|
|
2
|
-
import { Client } from '@kubb/plugin-client/components'
|
|
3
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
4
|
-
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
5
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
6
|
-
import type { FabricReactNode, Params } from '@kubb/react-fabric/types'
|
|
7
|
-
import type { PluginReactQuery } from '../types.ts'
|
|
8
|
-
import { MutationKey } from './MutationKey.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
name: string
|
|
12
|
-
clientName: string
|
|
13
|
-
mutationKeyName: string
|
|
14
|
-
typeSchemas: OperationSchemas
|
|
15
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
16
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
17
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
18
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type GetParamsProps = {
|
|
22
|
-
typeSchemas: OperationSchemas
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function getParams({ typeSchemas }: GetParamsProps) {
|
|
26
|
-
return FunctionParams.factory({
|
|
27
|
-
config: {
|
|
28
|
-
type: typeSchemas.request?.name
|
|
29
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
30
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
31
|
-
default: '{}',
|
|
32
|
-
},
|
|
33
|
-
})
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function MutationOptions({
|
|
37
|
-
name,
|
|
38
|
-
clientName,
|
|
39
|
-
dataReturnType,
|
|
40
|
-
typeSchemas,
|
|
41
|
-
paramsCasing,
|
|
42
|
-
paramsType,
|
|
43
|
-
pathParamsType,
|
|
44
|
-
mutationKeyName,
|
|
45
|
-
}: Props): FabricReactNode {
|
|
46
|
-
const params = getParams({ typeSchemas })
|
|
47
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
48
|
-
const TError = typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'
|
|
49
|
-
|
|
50
|
-
const clientParams = Client.getParams({
|
|
51
|
-
typeSchemas,
|
|
52
|
-
paramsCasing,
|
|
53
|
-
paramsType,
|
|
54
|
-
pathParamsType,
|
|
55
|
-
isConfigurable: true,
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
const mutationKeyParams = MutationKey.getParams({
|
|
59
|
-
pathParamsType,
|
|
60
|
-
typeSchemas,
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
const mutationParams = FunctionParams.factory({
|
|
64
|
-
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
65
|
-
data: typeSchemas.request?.name
|
|
66
|
-
? {
|
|
67
|
-
type: typeSchemas.request?.name,
|
|
68
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
69
|
-
}
|
|
70
|
-
: undefined,
|
|
71
|
-
params: typeSchemas.queryParams?.name
|
|
72
|
-
? {
|
|
73
|
-
type: typeSchemas.queryParams?.name,
|
|
74
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
75
|
-
}
|
|
76
|
-
: undefined,
|
|
77
|
-
headers: typeSchemas.headerParams?.name
|
|
78
|
-
? {
|
|
79
|
-
type: typeSchemas.headerParams?.name,
|
|
80
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
81
|
-
}
|
|
82
|
-
: undefined,
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
const dataParams = FunctionParams.factory({
|
|
86
|
-
data: {
|
|
87
|
-
// No use of pathParams because useMutation can only take one argument in object form,
|
|
88
|
-
// see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
|
|
89
|
-
mode: 'object',
|
|
90
|
-
children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => {
|
|
91
|
-
if (value) {
|
|
92
|
-
acc[key] = {
|
|
93
|
-
...value,
|
|
94
|
-
type: undefined,
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return acc
|
|
99
|
-
}, {} as Params),
|
|
100
|
-
},
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
const TRequest = mutationParams.toConstructor()
|
|
104
|
-
|
|
105
|
-
return (
|
|
106
|
-
<File.Source name={name} isExportable isIndexable>
|
|
107
|
-
<Function name={name} export params={params.toConstructor()} generics={['TContext = unknown']}>
|
|
108
|
-
{`
|
|
109
|
-
const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
|
|
110
|
-
return mutationOptions<${TData}, ResponseErrorConfig<${TError}>, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
|
|
111
|
-
mutationKey,
|
|
112
|
-
mutationFn: async(${dataParams.toConstructor()}) => {
|
|
113
|
-
return ${clientName}(${clientParams.toCall()})
|
|
114
|
-
},
|
|
115
|
-
})
|
|
116
|
-
`}
|
|
117
|
-
</Function>
|
|
118
|
-
</File.Source>
|
|
119
|
-
)
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
MutationOptions.getParams = getParams
|