@kubb/plugin-react-query 5.0.0-alpha.9 → 5.0.0-beta.4

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.
Files changed (53) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +1 -3
  3. package/dist/components-DTGLu4UV.js +1451 -0
  4. package/dist/components-DTGLu4UV.js.map +1 -0
  5. package/dist/components-dAKJEn9b.cjs +1571 -0
  6. package/dist/components-dAKJEn9b.cjs.map +1 -0
  7. package/dist/components.cjs +1 -1
  8. package/dist/components.d.ts +105 -161
  9. package/dist/components.js +1 -1
  10. package/dist/generators-CWEQsdO9.cjs +1502 -0
  11. package/dist/generators-CWEQsdO9.cjs.map +1 -0
  12. package/dist/generators-C_fbcjpG.js +1460 -0
  13. package/dist/generators-C_fbcjpG.js.map +1 -0
  14. package/dist/generators.cjs +1 -1
  15. package/dist/generators.d.ts +9 -505
  16. package/dist/generators.js +1 -1
  17. package/dist/index.cjs +114 -126
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +110 -126
  21. package/dist/index.js.map +1 -1
  22. package/dist/{types-D5S7Ny9r.d.ts → types-DfaFRSBf.d.ts} +100 -86
  23. package/extension.yaml +938 -0
  24. package/package.json +59 -62
  25. package/src/components/InfiniteQuery.tsx +75 -139
  26. package/src/components/InfiniteQueryOptions.tsx +62 -164
  27. package/src/components/Mutation.tsx +58 -113
  28. package/src/components/MutationOptions.tsx +61 -80
  29. package/src/components/Query.tsx +67 -140
  30. package/src/components/QueryOptions.tsx +75 -135
  31. package/src/components/SuspenseInfiniteQuery.tsx +75 -139
  32. package/src/components/SuspenseInfiniteQueryOptions.tsx +62 -164
  33. package/src/components/SuspenseQuery.tsx +67 -150
  34. package/src/generators/customHookOptionsFileGenerator.tsx +33 -45
  35. package/src/generators/hookOptionsGenerator.tsx +115 -175
  36. package/src/generators/infiniteQueryGenerator.tsx +183 -176
  37. package/src/generators/mutationGenerator.tsx +127 -138
  38. package/src/generators/queryGenerator.tsx +141 -141
  39. package/src/generators/suspenseInfiniteQueryGenerator.tsx +175 -155
  40. package/src/generators/suspenseQueryGenerator.tsx +149 -148
  41. package/src/index.ts +1 -1
  42. package/src/plugin.ts +133 -183
  43. package/src/resolvers/resolverReactQuery.ts +22 -0
  44. package/src/types.ts +67 -45
  45. package/src/utils.ts +40 -0
  46. package/dist/components-BHQT9ZLc.cjs +0 -1634
  47. package/dist/components-BHQT9ZLc.cjs.map +0 -1
  48. package/dist/components-CpyHYGOw.js +0 -1520
  49. package/dist/components-CpyHYGOw.js.map +0 -1
  50. package/dist/generators-DP07m3rH.cjs +0 -1469
  51. package/dist/generators-DP07m3rH.cjs.map +0 -1
  52. package/dist/generators-DkQwKTc2.js +0 -1427
  53. package/dist/generators-DkQwKTc2.js.map +0 -1
@@ -1,18 +1,20 @@
1
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'
2
+ import type { ast } from '@kubb/core'
3
+ import type { ResolverTs } from '@kubb/plugin-ts'
4
+ import { functionPrinter } from '@kubb/plugin-ts'
5
+ import { File, Function } from '@kubb/renderer-jsx'
6
+ import type { KubbReactNode } from '@kubb/renderer-jsx/types'
8
7
  import type { Infinite, PluginReactQuery } from '../types.ts'
8
+ import { resolveErrorNames } from '../utils.ts'
9
9
  import { QueryKey } from './QueryKey.tsx'
10
+ import { buildEnabledCheck, getQueryOptionsParams } from './QueryOptions.tsx'
10
11
 
11
12
  type Props = {
12
13
  name: string
13
14
  clientName: string
14
15
  queryKeyName: string
15
- typeSchemas: OperationSchemas
16
+ node: ast.OperationNode
17
+ tsResolver: ResolverTs
16
18
  paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
17
19
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
18
20
  pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
@@ -24,94 +26,8 @@ type Props = {
24
26
  queryParam: Infinite['queryParam']
25
27
  }
26
28
 
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
- }
29
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
30
+ const callPrinter = functionPrinter({ mode: 'call' })
115
31
 
116
32
  export function InfiniteQueryOptions({
117
33
  name,
@@ -120,16 +36,20 @@ export function InfiniteQueryOptions({
120
36
  cursorParam,
121
37
  nextParam,
122
38
  previousParam,
123
- typeSchemas,
39
+ node,
40
+ tsResolver,
124
41
  paramsCasing,
125
42
  paramsType,
126
43
  dataReturnType,
127
44
  pathParamsType,
128
45
  queryParam,
129
46
  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'}>`
47
+ }: Props): KubbReactNode {
48
+ const responseName = tsResolver.resolveResponseName(node)
49
+ const queryFnDataType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
50
+ const errorNames = resolveErrorNames(node, tsResolver)
51
+ const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
52
+
133
53
  const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
134
54
  const fallbackPageParamType =
135
55
  typeof initialPageParam === 'number'
@@ -144,36 +64,37 @@ export function InfiniteQueryOptions({
144
64
  : typeof initialPageParam === 'boolean'
145
65
  ? 'boolean'
146
66
  : 'unknown'
147
- const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : undefined
67
+
68
+ const rawQueryParams = node.parameters.filter((p) => p.in === 'query')
69
+ const queryParamsTypeName =
70
+ rawQueryParams.length > 0
71
+ ? (() => {
72
+ const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
73
+ const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
74
+ return groupName !== individualName ? groupName : undefined
75
+ })()
76
+ : undefined
77
+
78
+ const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
148
79
  const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
149
80
 
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
- })
81
+ const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
82
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
83
+ const rawParamsCall = callPrinter.print(paramsNode) ?? ''
84
+ const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
85
+
86
+ const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
87
+ const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
88
+
89
+ const enabledSource = buildEnabledCheck(queryKeyParamsNode)
90
+ const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : ''
168
91
 
169
- // Determine if we should use the new nextParam/previousParam or fall back to legacy cursorParam behavior
170
92
  const hasNewParams = nextParam !== undefined || previousParam !== undefined
171
93
 
172
94
  let getNextPageParamExpr: string | undefined
173
95
  let getPreviousPageParamExpr: string | undefined
174
96
 
175
97
  if (hasNewParams) {
176
- // Use the new nextParam and previousParam
177
98
  if (nextParam) {
178
99
  const accessor = getNestedAccessor(nextParam, 'lastPage')
179
100
  if (accessor) {
@@ -187,11 +108,9 @@ export function InfiniteQueryOptions({
187
108
  }
188
109
  }
189
110
  } else if (cursorParam) {
190
- // Legacy behavior: use cursorParam for both next and previous
191
111
  getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`
192
112
  getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`
193
113
  } else {
194
- // Fallback behavior: page-based pagination
195
114
  if (dataReturnType === 'full') {
196
115
  getNextPageParamExpr =
197
116
  'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
@@ -202,56 +121,35 @@ export function InfiniteQueryOptions({
202
121
  getPreviousPageParamExpr = 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
203
122
  }
204
123
 
205
- const queryOptions = [
124
+ const queryOptionsArr = [
206
125
  `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
207
126
  getNextPageParamExpr,
208
127
  getPreviousPageParamExpr,
209
128
  ].filter(Boolean)
210
129
 
211
130
  const infiniteOverrideParams =
212
- queryParam && typeSchemas.queryParams?.name
131
+ queryParam && queryParamsTypeName
213
132
  ? `
214
133
  params = {
215
134
  ...(params ?? {}),
216
- ['${queryParam}']: pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}'],
217
- } as ${typeSchemas.queryParams?.name}`
135
+ ['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
136
+ } as ${queryParamsTypeName}`
218
137
  : ''
219
138
 
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
139
  if (infiniteOverrideParams) {
234
140
  return (
235
141
  <File.Source name={name} isExportable isIndexable>
236
- <Function name={name} export params={params.toConstructor()}>
142
+ <Function name={name} export params={paramsSignature}>
237
143
  {`
238
- const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
144
+ const queryKey = ${queryKeyName}(${queryKeyParamsCall})
239
145
  return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
240
146
  ${enabledText}
241
147
  queryKey,
242
148
  queryFn: async ({ signal, pageParam }) => {
243
149
  ${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
- })})
150
+ return ${clientName}(${clientCallStr})
253
151
  },
254
- ${queryOptions.join(',\n')}
152
+ ${queryOptionsArr.join(',\n')}
255
153
  })
256
154
  `}
257
155
  </Function>
@@ -261,24 +159,16 @@ export function InfiniteQueryOptions({
261
159
 
262
160
  return (
263
161
  <File.Source name={name} isExportable isIndexable>
264
- <Function name={name} export params={params.toConstructor()}>
162
+ <Function name={name} export params={paramsSignature}>
265
163
  {`
266
- const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
164
+ const queryKey = ${queryKeyName}(${queryKeyParamsCall})
267
165
  return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
268
166
  ${enabledText}
269
167
  queryKey,
270
168
  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
- })})
169
+ return ${clientName}(${clientCallStr})
280
170
  },
281
- ${queryOptions.join(',\n')}
171
+ ${queryOptionsArr.join(',\n')}
282
172
  })
283
173
  `}
284
174
  </Function>
@@ -286,4 +176,12 @@ export function InfiniteQueryOptions({
286
176
  )
287
177
  }
288
178
 
289
- InfiniteQueryOptions.getParams = getParams
179
+ InfiniteQueryOptions.getParams = (
180
+ node: ast.OperationNode,
181
+ options: {
182
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
183
+ paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
184
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
185
+ resolver: ResolverTs
186
+ },
187
+ ) => getQueryOptionsParams(node, options)
@@ -1,150 +1,93 @@
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'
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
6
  import type { PluginReactQuery } from '../types.ts'
7
- import { MutationKey } from './MutationKey.tsx'
7
+ import { buildMutationArgParams, getComments, resolveErrorNames } from '../utils.ts'
8
8
  import { MutationOptions } from './MutationOptions.tsx'
9
9
 
10
10
  type Props = {
11
- /**
12
- * Name of the function
13
- */
14
11
  name: string
15
12
  typeName: string
16
13
  mutationOptionsName: string
17
14
  mutationKeyName: string
18
- typeSchemas: OperationSchemas
19
- operation: Operation
15
+ node: ast.OperationNode
16
+ tsResolver: ResolverTs
20
17
  dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
21
18
  paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
22
19
  pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
23
20
  customOptions: PluginReactQuery['resolvedOptions']['customOptions']
24
21
  }
25
22
 
26
- type GetParamsProps = {
27
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
28
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
29
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
30
- typeSchemas: OperationSchemas
31
- }
23
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
24
+ const callPrinter = functionPrinter({ mode: 'call' })
32
25
 
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 })
26
+ function getParams(
27
+ node: ast.OperationNode,
28
+ options: {
29
+ paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
30
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
31
+ resolver: ResolverTs
32
+ },
33
+ ): ast.FunctionParametersNode {
34
+ const { paramsCasing, dataReturnType, resolver } = options
35
+ const responseName = resolver.resolveResponseName(node)
36
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
37
+ const errorNames = resolveErrorNames(node, resolver)
36
38
 
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'}>`
39
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
40
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
41
+
42
+ const mutationArgParamsNode = buildMutationArgParams(node, { paramsCasing, resolver })
43
+ const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
60
44
  const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
61
45
 
62
- return FunctionParams.factory({
63
- options: {
64
- type: `
65
- {
46
+ return ast.createFunctionParameters({
47
+ params: [
48
+ ast.createFunctionParameter({
49
+ name: 'options',
50
+ type: ast.createParamsType({
51
+ variant: 'reference',
52
+ name: `{
66
53
  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
- },
54
+ client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
55
+ }`,
56
+ }),
57
+ default: '{}',
58
+ }),
59
+ ],
72
60
  })
73
61
  }
74
62
 
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
- })
63
+ export function Mutation({ name, mutationOptionsName, paramsCasing, dataReturnType, node, tsResolver, mutationKeyName, customOptions }: Props): KubbReactNode {
64
+ const responseName = tsResolver.resolveResponseName(node)
65
+ const errorNames = resolveErrorNames(node, tsResolver)
97
66
 
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
- })
67
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
68
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
119
69
 
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'}>`
70
+ const mutationArgParamsNode = buildMutationArgParams(node, { paramsCasing, resolver: tsResolver })
71
+ const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
125
72
  const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
126
73
  const returnType = `UseMutationResult<${generics}>`
127
74
 
128
- const mutationOptions = `${mutationOptionsName}(${mutationOptionsParams.toCall()})`
75
+ const mutationOptionsConfigNode = MutationOptions.getParams(node, tsResolver)
76
+ const mutationOptionsParamsCall = callPrinter.print(mutationOptionsConfigNode) ?? ''
77
+
78
+ const paramsNode = getParams(node, { paramsCasing, dataReturnType, resolver: tsResolver })
79
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
129
80
 
130
81
  return (
131
82
  <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
- >
83
+ <Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }} generics={['TContext']}>
141
84
  {`
142
85
  const { mutation = {}, client: config = {} } = options ?? {}
143
86
  const { client: queryClient, ...mutationOptions } = mutation;
144
- const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
87
+ const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}()
145
88
 
146
- const baseOptions = ${mutationOptions} as UseMutationOptions<${generics}>
147
- ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' }) as UseMutationOptions<${generics}>` : ''}
89
+ const baseOptions = ${mutationOptionsName}(${mutationOptionsParamsCall}) as UseMutationOptions<${generics}>
90
+ ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' }) as UseMutationOptions<${generics}>` : ''}
148
91
 
149
92
  return useMutation<${generics}>({
150
93
  ...baseOptions,${customOptions ? '\n...customOptions,' : ''}
@@ -156,3 +99,5 @@ export function Mutation({
156
99
  </File.Source>
157
100
  )
158
101
  }
102
+
103
+ Mutation.getParams = getParams