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

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 (54) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +34 -85
  3. package/dist/components-Dow6tde8.js +1459 -0
  4. package/dist/components-Dow6tde8.js.map +1 -0
  5. package/dist/components-HwdCDefj.cjs +1603 -0
  6. package/dist/components-HwdCDefj.cjs.map +1 -0
  7. package/dist/components.cjs +1 -1
  8. package/dist/components.d.ts +49 -179
  9. package/dist/components.js +1 -1
  10. package/dist/generators-CcOmnTPa.cjs +1454 -0
  11. package/dist/generators-CcOmnTPa.cjs.map +1 -0
  12. package/dist/generators-yfZr_qfT.js +1412 -0
  13. package/dist/generators-yfZr_qfT.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 +197 -126
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +193 -126
  21. package/dist/index.js.map +1 -1
  22. package/dist/types-DG_OxOym.d.ts +363 -0
  23. package/extension.yaml +911 -0
  24. package/package.json +59 -64
  25. package/src/components/InfiniteQuery.tsx +79 -138
  26. package/src/components/InfiniteQueryOptions.tsx +55 -166
  27. package/src/components/Mutation.tsx +74 -111
  28. package/src/components/MutationOptions.tsx +61 -80
  29. package/src/components/Query.tsx +66 -142
  30. package/src/components/QueryOptions.tsx +56 -138
  31. package/src/components/SuspenseInfiniteQuery.tsx +79 -138
  32. package/src/components/SuspenseInfiniteQueryOptions.tsx +55 -166
  33. package/src/components/SuspenseQuery.tsx +66 -152
  34. package/src/generators/customHookOptionsFileGenerator.tsx +37 -51
  35. package/src/generators/hookOptionsGenerator.tsx +111 -174
  36. package/src/generators/infiniteQueryGenerator.tsx +158 -178
  37. package/src/generators/mutationGenerator.tsx +112 -139
  38. package/src/generators/queryGenerator.tsx +128 -142
  39. package/src/generators/suspenseInfiniteQueryGenerator.tsx +157 -156
  40. package/src/generators/suspenseQueryGenerator.tsx +126 -152
  41. package/src/index.ts +1 -1
  42. package/src/plugin.ts +134 -187
  43. package/src/resolvers/resolverReactQuery.ts +107 -0
  44. package/src/types.ts +172 -49
  45. package/src/utils.ts +10 -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
  54. package/dist/types-D5S7Ny9r.d.ts +0 -270
@@ -1,35 +1,39 @@
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 { buildRequestConfigType, 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
+ export function buildMutationConfigParamsNode(node: ast.OperationNode, resolver: ResolverTs): ast.FunctionParametersNode {
26
+ return ast.createFunctionParameters({
27
+ params: [
28
+ ast.createFunctionParameter({
29
+ name: 'config',
30
+ type: ast.createParamsType({
31
+ variant: 'reference',
32
+ name: buildRequestConfigType(node, resolver),
33
+ }),
34
+ default: '{}',
35
+ }),
36
+ ],
33
37
  })
34
38
  }
35
39
 
@@ -37,80 +41,59 @@ export function MutationOptions({
37
41
  name,
38
42
  clientName,
39
43
  dataReturnType,
40
- typeSchemas,
44
+ node,
45
+ tsResolver,
41
46
  paramsCasing,
42
47
  paramsType,
43
48
  pathParamsType,
44
49
  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
- })
50
+ }: Props): KubbReactNode {
51
+ const responseName = tsResolver.resolveResponseName(node)
52
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
53
+ const errorNames = resolveErrorNames(node, tsResolver)
54
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
57
55
 
58
- const mutationKeyParams = MutationKey.getParams({
59
- pathParamsType,
60
- typeSchemas,
61
- })
56
+ const configParamsNode = buildMutationConfigParamsNode(node, tsResolver)
57
+ const paramsSignature = declarationPrinter.print(configParamsNode) ?? ''
62
58
 
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,
59
+ const mutationArgParamsNode = ast.createOperationParams(node, {
60
+ paramsType: 'inline',
61
+ pathParamsType: 'inline',
62
+ paramsCasing,
63
+ resolver: tsResolver,
83
64
  })
65
+ const hasMutationParams = mutationArgParamsNode.params.length > 0
84
66
 
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
- }
67
+ const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
68
+ const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
97
69
 
98
- return acc
99
- }, {} as Params),
100
- },
70
+ const clientCallParamsNode = ast.createOperationParams(node, {
71
+ paramsType,
72
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
73
+ paramsCasing,
74
+ resolver: tsResolver,
75
+ extraParams: [
76
+ ast.createFunctionParameter({
77
+ name: 'config',
78
+ type: ast.createParamsType({
79
+ variant: 'reference',
80
+ name: buildRequestConfigType(node, tsResolver),
81
+ }),
82
+ default: '{}',
83
+ }),
84
+ ],
101
85
  })
102
-
103
- const TRequest = mutationParams.toConstructor()
86
+ const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
104
87
 
105
88
  return (
106
89
  <File.Source name={name} isExportable isIndexable>
107
- <Function name={name} export params={params.toConstructor()} generics={['TContext = unknown']}>
90
+ <Function name={name} export params={paramsSignature} generics={['TContext = unknown']}>
108
91
  {`
109
- const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
110
- return mutationOptions<${TData}, ResponseErrorConfig<${TError}>, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
92
+ const mutationKey = ${mutationKeyName}()
93
+ return mutationOptions<${TData}, ${TError}, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
111
94
  mutationKey,
112
- mutationFn: async(${dataParams.toConstructor()}) => {
113
- return ${clientName}(${clientParams.toCall()})
95
+ mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : '_'}) => {
96
+ return ${clientName}(${clientCallStr})
114
97
  },
115
98
  })
116
99
  `}
@@ -118,5 +101,3 @@ export function MutationOptions({
118
101
  </File.Source>
119
102
  )
120
103
  }
121
-
122
- MutationOptions.getParams = getParams
@@ -1,22 +1,19 @@
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 { QueryKey } from './QueryKey.tsx'
8
- import { QueryOptions } from './QueryOptions.tsx'
7
+ import { buildQueryKeyParams, getComments, resolveErrorNames } from '../utils.ts'
8
+ import { getQueryOptionsParams } from './QueryOptions.tsx'
9
9
 
10
10
  type Props = {
11
- /**
12
- * Name of the function
13
- */
14
11
  name: string
15
12
  queryOptionsName: string
16
13
  queryKeyName: string
17
14
  queryKeyTypeName: string
18
- typeSchemas: OperationSchemas
19
- operation: Operation
15
+ node: ast.OperationNode
16
+ tsResolver: ResolverTs
20
17
  paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
21
18
  paramsType: PluginReactQuery['resolvedOptions']['paramsType']
22
19
  pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
@@ -24,99 +21,45 @@ type Props = {
24
21
  customOptions: PluginReactQuery['resolvedOptions']['customOptions']
25
22
  }
26
23
 
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
- {
24
+ const declarationPrinter = functionPrinter({ mode: 'declaration' })
25
+ const callPrinter = functionPrinter({ mode: 'call' })
26
+
27
+ function buildQueryParamsNode(
28
+ node: ast.OperationNode,
29
+ options: {
30
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
31
+ paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
32
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
33
+ dataReturnType: PluginReactQuery['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: `{
76
50
  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
- }
51
+ client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
52
+ }`,
53
+ }),
54
+ default: '{}',
55
+ })
84
56
 
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
- },
57
+ return ast.createOperationParams(node, {
58
+ paramsType,
59
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
60
+ paramsCasing,
61
+ resolver,
62
+ extraParams: [optionsParam],
120
63
  })
121
64
  }
122
65
 
@@ -129,55 +72,38 @@ export function Query({
129
72
  paramsCasing,
130
73
  pathParamsType,
131
74
  dataReturnType,
132
- typeSchemas,
133
- operation,
75
+ node,
76
+ tsResolver,
134
77
  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 }`
78
+ }: Props): KubbReactNode {
79
+ const responseName = tsResolver.resolveResponseName(node)
80
+ const errorNames = resolveErrorNames(node, tsResolver)
81
+
82
+ const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
83
+ const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
84
+ const returnType = `UseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
139
85
  const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
140
86
 
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
- })
87
+ const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
88
+ const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
89
+
90
+ const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
91
+ const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
159
92
 
160
- const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`
93
+ const paramsNode = buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
94
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
161
95
 
162
96
  return (
163
97
  <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
- >
98
+ <Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
173
99
  {`
174
100
  const { query: queryConfig = {}, client: config = {} } = options ?? {}
175
101
  const { client: queryClient, ...resolvedOptions } = queryConfig
176
- const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
177
- ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' })` : ''}
102
+ const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})
103
+ ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
178
104
 
179
105
  const query = useQuery({
180
- ...${queryOptions},${customOptions ? '\n...customOptions,' : ''}
106
+ ...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
181
107
  ...resolvedOptions,
182
108
  queryKey,
183
109
  } as unknown as QueryObserverOptions, queryClient) as ${returnType}
@@ -190,5 +116,3 @@ export function Query({
190
116
  </File.Source>
191
117
  )
192
118
  }
193
-
194
- Query.getParams = getParams