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

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 (52) 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/package.json +59 -62
  24. package/src/components/InfiniteQuery.tsx +75 -139
  25. package/src/components/InfiniteQueryOptions.tsx +62 -164
  26. package/src/components/Mutation.tsx +58 -113
  27. package/src/components/MutationOptions.tsx +61 -80
  28. package/src/components/Query.tsx +67 -140
  29. package/src/components/QueryOptions.tsx +75 -135
  30. package/src/components/SuspenseInfiniteQuery.tsx +75 -139
  31. package/src/components/SuspenseInfiniteQueryOptions.tsx +62 -164
  32. package/src/components/SuspenseQuery.tsx +67 -150
  33. package/src/generators/customHookOptionsFileGenerator.tsx +33 -45
  34. package/src/generators/hookOptionsGenerator.tsx +115 -175
  35. package/src/generators/infiniteQueryGenerator.tsx +183 -176
  36. package/src/generators/mutationGenerator.tsx +127 -138
  37. package/src/generators/queryGenerator.tsx +141 -141
  38. package/src/generators/suspenseInfiniteQueryGenerator.tsx +175 -155
  39. package/src/generators/suspenseQueryGenerator.tsx +149 -148
  40. package/src/index.ts +1 -1
  41. package/src/plugin.ts +133 -183
  42. package/src/resolvers/resolverReactQuery.ts +22 -0
  43. package/src/types.ts +67 -45
  44. package/src/utils.ts +40 -0
  45. package/dist/components-BHQT9ZLc.cjs +0 -1634
  46. package/dist/components-BHQT9ZLc.cjs.map +0 -1
  47. package/dist/components-CpyHYGOw.js +0 -1520
  48. package/dist/components-CpyHYGOw.js.map +0 -1
  49. package/dist/generators-DP07m3rH.cjs +0 -1469
  50. package/dist/generators-DP07m3rH.cjs.map +0 -1
  51. package/dist/generators-DkQwKTc2.js +0 -1427
  52. package/dist/generators-DkQwKTc2.js.map +0 -1
@@ -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
@@ -1,35 +1,40 @@
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'
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'
7
6
  import type { PluginReactQuery } from '../types.ts'
8
- import { MutationKey } from './MutationKey.tsx'
7
+ import { buildMutationArgParams, resolveErrorNames } from '../utils.ts'
9
8
 
10
9
  type Props = {
11
10
  name: string
12
11
  clientName: string
13
12
  mutationKeyName: string
14
- typeSchemas: OperationSchemas
13
+ node: ast.OperationNode
14
+ tsResolver: ResolverTs
15
15
  paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
16
16
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
17
17
  pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
18
18
  dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
19
19
  }
20
20
 
21
- type GetParamsProps = {
22
- typeSchemas: OperationSchemas
23
- }
21
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
22
+ const callPrinter = functionPrinter({ mode: 'call' })
23
+ const keysPrinter = functionPrinter({ mode: 'keys' })
24
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
- },
25
+ function getConfigParam(node: ast.OperationNode, resolver: ResolverTs): ast.FunctionParametersNode {
26
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
27
+ return ast.createFunctionParameters({
28
+ params: [
29
+ ast.createFunctionParameter({
30
+ name: 'config',
31
+ type: ast.createParamsType({
32
+ variant: 'reference',
33
+ name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
34
+ }),
35
+ default: '{}',
36
+ }),
37
+ ],
33
38
  })
34
39
  }
35
40
 
@@ -37,80 +42,56 @@ export function MutationOptions({
37
42
  name,
38
43
  clientName,
39
44
  dataReturnType,
40
- typeSchemas,
45
+ node,
46
+ tsResolver,
41
47
  paramsCasing,
42
48
  paramsType,
43
49
  pathParamsType,
44
50
  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
- })
51
+ }: Props): KubbReactNode {
52
+ const responseName = tsResolver.resolveResponseName(node)
53
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
54
+ const errorNames = resolveErrorNames(node, tsResolver)
55
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
57
56
 
58
- const mutationKeyParams = MutationKey.getParams({
59
- pathParamsType,
60
- typeSchemas,
61
- })
57
+ const configParamsNode = getConfigParam(node, tsResolver)
58
+ const paramsSignature = declarationPrinter.print(configParamsNode) ?? ''
62
59
 
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
- })
60
+ const mutationArgParamsNode = buildMutationArgParams(node, { paramsCasing, resolver: tsResolver })
61
+ const hasMutationParams = mutationArgParamsNode.params.length > 0
84
62
 
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
- }
63
+ const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
64
+ const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
97
65
 
98
- return acc
99
- }, {} as Params),
100
- },
66
+ const clientCallParamsNode = ast.createOperationParams(node, {
67
+ paramsType,
68
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
69
+ paramsCasing,
70
+ resolver: tsResolver,
71
+ extraParams: [
72
+ ast.createFunctionParameter({
73
+ name: 'config',
74
+ type: ast.createParamsType({
75
+ variant: 'reference',
76
+ name: node.requestBody?.content?.[0]?.schema
77
+ ? `Partial<RequestConfig<${tsResolver.resolveDataName(node)}>> & { client?: Client }`
78
+ : 'Partial<RequestConfig> & { client?: Client }',
79
+ }),
80
+ default: '{}',
81
+ }),
82
+ ],
101
83
  })
102
-
103
- const TRequest = mutationParams.toConstructor()
84
+ const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
104
85
 
105
86
  return (
106
87
  <File.Source name={name} isExportable isIndexable>
107
- <Function name={name} export params={params.toConstructor()} generics={['TContext = unknown']}>
88
+ <Function name={name} export params={paramsSignature} generics={['TContext = unknown']}>
108
89
  {`
109
- const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
110
- return mutationOptions<${TData}, ResponseErrorConfig<${TError}>, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
90
+ const mutationKey = ${mutationKeyName}()
91
+ return mutationOptions<${TData}, ${TError}, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
111
92
  mutationKey,
112
- mutationFn: async(${dataParams.toConstructor()}) => {
113
- return ${clientName}(${clientParams.toCall()})
93
+ mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : '_'}) => {
94
+ return ${clientName}(${clientCallStr})
114
95
  },
115
96
  })
116
97
  `}
@@ -119,4 +100,4 @@ export function MutationOptions({
119
100
  )
120
101
  }
121
102
 
122
- MutationOptions.getParams = getParams
103
+ MutationOptions.getParams = getConfigParam
@@ -1,22 +1,20 @@
1
- import { isAllOptional, 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 { getComments, resolveErrorNames } from '../utils.ts'
7
8
  import { QueryKey } from './QueryKey.tsx'
8
- import { QueryOptions } from './QueryOptions.tsx'
9
+ import { getQueryOptionsParams } from './QueryOptions.tsx'
9
10
 
10
11
  type Props = {
11
- /**
12
- * Name of the function
13
- */
14
12
  name: string
15
13
  queryOptionsName: string
16
14
  queryKeyName: string
17
15
  queryKeyTypeName: string
18
- typeSchemas: OperationSchemas
19
- operation: Operation
16
+ node: ast.OperationNode
17
+ tsResolver: ResolverTs
20
18
  paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
21
19
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
22
20
  pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
@@ -24,99 +22,45 @@ type Props = {
24
22
  customOptions: PluginReactQuery['resolvedOptions']['customOptions']
25
23
  }
26
24
 
27
- type GetParamsProps = {
28
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
29
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
30
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
31
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
32
- typeSchemas: OperationSchemas
33
- }
34
-
35
- function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
36
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
37
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
38
-
39
- if (paramsType === 'object') {
40
- const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
41
-
42
- const children = {
43
- ...pathParams,
44
- data: typeSchemas.request?.name
45
- ? {
46
- type: typeSchemas.request?.name,
47
- optional: isOptional(typeSchemas.request?.schema),
48
- }
49
- : undefined,
50
- params: typeSchemas.queryParams?.name
51
- ? {
52
- type: typeSchemas.queryParams?.name,
53
- optional: isOptional(typeSchemas.queryParams?.schema),
54
- }
55
- : undefined,
56
- headers: typeSchemas.headerParams?.name
57
- ? {
58
- type: typeSchemas.headerParams?.name,
59
- optional: isOptional(typeSchemas.headerParams?.schema),
60
- }
61
- : undefined,
62
- }
63
-
64
- // Check if all children are optional or undefined
65
- const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
66
-
67
- return FunctionParams.factory({
68
- data: {
69
- mode: 'object',
70
- children,
71
- default: allChildrenAreOptional ? '{}' : undefined,
72
- },
73
- options: {
74
- type: `
75
- {
25
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
26
+ const callPrinter = functionPrinter({ mode: 'call' })
27
+
28
+ function getParams(
29
+ node: ast.OperationNode,
30
+ options: {
31
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
32
+ paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
33
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
34
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
35
+ resolver: ResolverTs
36
+ },
37
+ ): ast.FunctionParametersNode {
38
+ const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
39
+ const responseName = resolver.resolveResponseName(node)
40
+ const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
41
+ const errorNames = resolveErrorNames(node, resolver)
42
+
43
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
44
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
45
+
46
+ const optionsParam = ast.createFunctionParameter({
47
+ name: 'options',
48
+ type: ast.createParamsType({
49
+ variant: 'reference',
50
+ name: `{
76
51
  query?: Partial<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
77
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
78
- }
79
- `,
80
- default: '{}',
81
- },
82
- })
83
- }
84
-
85
- return FunctionParams.factory({
86
- pathParams: typeSchemas.pathParams?.name
87
- ? {
88
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
89
- children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
90
- default: isAllOptional(typeSchemas.pathParams?.schema) ? '{}' : undefined,
91
- }
92
- : undefined,
93
- data: typeSchemas.request?.name
94
- ? {
95
- type: typeSchemas.request?.name,
96
- optional: isOptional(typeSchemas.request?.schema),
97
- }
98
- : undefined,
99
- params: typeSchemas.queryParams?.name
100
- ? {
101
- type: typeSchemas.queryParams?.name,
102
- optional: isOptional(typeSchemas.queryParams?.schema),
103
- }
104
- : undefined,
105
- headers: typeSchemas.headerParams?.name
106
- ? {
107
- type: typeSchemas.headerParams?.name,
108
- optional: isOptional(typeSchemas.headerParams?.schema),
109
- }
110
- : undefined,
111
- options: {
112
- type: `
113
- {
114
- query?: Partial<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
115
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
116
- }
117
- `,
118
- default: '{}',
119
- },
52
+ client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
53
+ }`,
54
+ }),
55
+ default: '{}',
56
+ })
57
+
58
+ return ast.createOperationParams(node, {
59
+ paramsType,
60
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
61
+ paramsCasing,
62
+ resolver,
63
+ extraParams: [optionsParam],
120
64
  })
121
65
  }
122
66
 
@@ -129,55 +73,38 @@ export function Query({
129
73
  paramsCasing,
130
74
  pathParamsType,
131
75
  dataReturnType,
132
- typeSchemas,
133
- operation,
76
+ node,
77
+ tsResolver,
134
78
  customOptions,
135
- }: Props): FabricReactNode {
136
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
137
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
138
- const returnType = `UseQueryResult<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
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 returnType = `UseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
139
86
  const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
140
87
 
141
- const queryKeyParams = QueryKey.getParams({
142
- pathParamsType,
143
- typeSchemas,
144
- paramsCasing,
145
- })
146
- const queryOptionsParams = QueryOptions.getParams({
147
- paramsType,
148
- pathParamsType,
149
- typeSchemas,
150
- paramsCasing,
151
- })
152
- const params = getParams({
153
- paramsCasing,
154
- paramsType,
155
- pathParamsType,
156
- dataReturnType,
157
- typeSchemas,
158
- })
88
+ const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
89
+ const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
90
+
91
+ const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
92
+ const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
159
93
 
160
- const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`
94
+ const paramsNode = getParams(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
95
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
161
96
 
162
97
  return (
163
98
  <File.Source name={name} isExportable isIndexable>
164
- <Function
165
- name={name}
166
- export
167
- generics={generics.join(', ')}
168
- params={params.toConstructor()}
169
- JSDoc={{
170
- comments: getComments(operation),
171
- }}
172
- >
99
+ <Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
173
100
  {`
174
101
  const { query: queryConfig = {}, client: config = {} } = options ?? {}
175
102
  const { client: queryClient, ...resolvedOptions } = queryConfig
176
- const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
177
- ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' })` : ''}
103
+ const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})
104
+ ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
178
105
 
179
106
  const query = useQuery({
180
- ...${queryOptions},${customOptions ? '\n...customOptions,' : ''}
107
+ ...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
181
108
  ...resolvedOptions,
182
109
  queryKey,
183
110
  } as unknown as QueryObserverOptions, queryClient) as ${returnType}