@kubb/plugin-vue-query 5.0.0-beta.10 → 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.
Files changed (41) hide show
  1. package/README.md +22 -26
  2. package/dist/index.cjs +1629 -147
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +358 -6
  5. package/dist/index.js +1600 -145
  6. package/dist/index.js.map +1 -1
  7. package/package.json +9 -32
  8. package/dist/components-B6jAb2as.js +0 -1150
  9. package/dist/components-B6jAb2as.js.map +0 -1
  10. package/dist/components-X0P-3W2G.cjs +0 -1264
  11. package/dist/components-X0P-3W2G.cjs.map +0 -1
  12. package/dist/components.cjs +0 -9
  13. package/dist/components.d.ts +0 -187
  14. package/dist/components.js +0 -2
  15. package/dist/generators-B33PbKzu.js +0 -681
  16. package/dist/generators-B33PbKzu.js.map +0 -1
  17. package/dist/generators-BE3KD0IR.cjs +0 -698
  18. package/dist/generators-BE3KD0IR.cjs.map +0 -1
  19. package/dist/generators.cjs +0 -5
  20. package/dist/generators.d.ts +0 -15
  21. package/dist/generators.js +0 -2
  22. package/dist/types-Bkm7bWT3.d.ts +0 -243
  23. package/extension.yaml +0 -732
  24. package/src/components/InfiniteQuery.tsx +0 -120
  25. package/src/components/InfiniteQueryOptions.tsx +0 -201
  26. package/src/components/Mutation.tsx +0 -148
  27. package/src/components/MutationKey.tsx +0 -1
  28. package/src/components/Query.tsx +0 -119
  29. package/src/components/QueryKey.tsx +0 -51
  30. package/src/components/QueryOptions.tsx +0 -134
  31. package/src/components/index.ts +0 -7
  32. package/src/generators/index.ts +0 -3
  33. package/src/generators/infiniteQueryGenerator.tsx +0 -194
  34. package/src/generators/mutationGenerator.tsx +0 -153
  35. package/src/generators/queryGenerator.tsx +0 -179
  36. package/src/index.ts +0 -2
  37. package/src/plugin.ts +0 -167
  38. package/src/resolvers/resolverVueQuery.ts +0 -65
  39. package/src/types.ts +0 -245
  40. package/src/utils.ts +0 -49
  41. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,120 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { Infinite, PluginVueQuery } from '../types.ts'
7
- import { getComments, resolveErrorNames, wrapWithMaybeRefOrGetter } from '../utils.ts'
8
- import { buildQueryKeyParamsNode } from './QueryKey.tsx'
9
- import { getQueryOptionsParams } from './QueryOptions.tsx'
10
-
11
- type Props = {
12
- name: string
13
- queryOptionsName: string
14
- queryKeyName: string
15
- queryKeyTypeName: string
16
- node: ast.OperationNode
17
- tsResolver: ResolverTs
18
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
19
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
20
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
21
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
22
- initialPageParam: Infinite['initialPageParam']
23
- queryParam?: Infinite['queryParam']
24
- }
25
-
26
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
27
- const callPrinter = functionPrinter({ mode: 'call' })
28
-
29
- function buildInfiniteQueryParamsNode(
30
- node: ast.OperationNode,
31
- options: {
32
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
33
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
34
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
35
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
36
- resolver: ResolverTs
37
- },
38
- ): ast.FunctionParametersNode {
39
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
40
- const responseName = resolver.resolveResponseName(node)
41
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
42
- const errorNames = resolveErrorNames(node, resolver)
43
-
44
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
45
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
46
-
47
- const optionsParam = ast.createFunctionParameter({
48
- name: 'options',
49
- type: ast.createParamsType({
50
- variant: 'reference',
51
- name: `{
52
- query?: Partial<UseInfiniteQueryOptions<${[TData, TError, 'TQueryData', 'TQueryKey', 'TQueryData'].join(', ')}>> & { client?: QueryClient },
53
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
54
- }`,
55
- }),
56
- default: '{}',
57
- })
58
-
59
- const baseParams = ast.createOperationParams(node, {
60
- paramsType,
61
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
62
- paramsCasing,
63
- resolver,
64
- extraParams: [optionsParam],
65
- })
66
-
67
- return wrapWithMaybeRefOrGetter(baseParams, (name) => name === 'options')
68
- }
69
-
70
- export function InfiniteQuery({
71
- name,
72
- queryKeyTypeName,
73
- queryOptionsName,
74
- queryKeyName,
75
- paramsType,
76
- paramsCasing,
77
- pathParamsType,
78
- dataReturnType,
79
- node,
80
- tsResolver,
81
- }: Props): KubbReactNode {
82
- const responseName = tsResolver.resolveResponseName(node)
83
- const errorNames = resolveErrorNames(node, tsResolver)
84
-
85
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
86
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
87
- const returnType = `UseInfiniteQueryReturnType<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
88
- const generics = [`TData = InfiniteData<${TData}>`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
89
-
90
- const queryKeyParamsNode = buildQueryKeyParamsNode(node, { pathParamsType, paramsCasing, resolver: tsResolver })
91
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
92
-
93
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
94
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
95
-
96
- const paramsNode = buildInfiniteQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
97
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
98
-
99
- return (
100
- <File.Source name={name} isExportable isIndexable>
101
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
102
- {`
103
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
104
- const { client: queryClient, ...resolvedOptions } = queryConfig
105
- const queryKey = (resolvedOptions && 'queryKey' in resolvedOptions ? toValue(resolvedOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParamsCall})
106
-
107
- const query = useInfiniteQuery({
108
- ...${queryOptionsName}(${queryOptionsParamsCall}),
109
- ...resolvedOptions,
110
- queryKey
111
- } as unknown as UseInfiniteQueryOptions<${TData}, ${TError}, ${TData}, TQueryKey, ${TData}>, toValue(queryClient)) as ${returnType}
112
-
113
- query.queryKey = queryKey as TQueryKey
114
-
115
- return query
116
- `}
117
- </Function>
118
- </File.Source>
119
- )
120
- }
@@ -1,201 +0,0 @@
1
- import { getOperationParameters } from '@internals/shared'
2
- import { getNestedAccessor } from '@internals/utils'
3
- import type { ast } from '@kubb/core'
4
- import type { ResolverTs } from '@kubb/plugin-ts'
5
- import { functionPrinter } from '@kubb/plugin-ts'
6
- import { File, Function } from '@kubb/renderer-jsx'
7
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
8
- import type { Infinite, PluginVueQuery } from '../types.ts'
9
- import { resolveErrorNames } from '../utils.ts'
10
- import { buildQueryKeyParamsNode } from './QueryKey.tsx'
11
- import { buildEnabledCheck } from '@internals/tanstack-query'
12
- import { getQueryOptionsParams } from './QueryOptions.tsx'
13
-
14
- type Props = {
15
- name: string
16
- clientName: string
17
- queryKeyName: string
18
- node: ast.OperationNode
19
- tsResolver: ResolverTs
20
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
21
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
22
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
23
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
24
- initialPageParam: Infinite['initialPageParam']
25
- cursorParam: Infinite['cursorParam']
26
- nextParam: Infinite['nextParam']
27
- previousParam: Infinite['previousParam']
28
- queryParam: Infinite['queryParam']
29
- }
30
-
31
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
32
- const callPrinter = functionPrinter({ mode: 'call' })
33
-
34
- export function InfiniteQueryOptions({
35
- name,
36
- clientName,
37
- initialPageParam,
38
- cursorParam,
39
- nextParam,
40
- previousParam,
41
- node,
42
- tsResolver,
43
- paramsCasing,
44
- paramsType,
45
- dataReturnType,
46
- pathParamsType,
47
- queryParam,
48
- queryKeyName,
49
- }: Props): KubbReactNode {
50
- const responseName = tsResolver.resolveResponseName(node)
51
- const queryFnDataType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
52
- const errorNames = resolveErrorNames(node, tsResolver)
53
- const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
54
-
55
- const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
56
- const fallbackPageParamType =
57
- typeof initialPageParam === 'number'
58
- ? 'number'
59
- : typeof initialPageParam === 'string'
60
- ? initialPageParam.includes(' as ')
61
- ? (() => {
62
- const parts = initialPageParam.split(' as ')
63
- return parts[parts.length - 1] ?? 'unknown'
64
- })()
65
- : 'string'
66
- : typeof initialPageParam === 'boolean'
67
- ? 'boolean'
68
- : 'unknown'
69
-
70
- const rawQueryParams = getOperationParameters(node).query
71
- const queryParamsTypeName =
72
- rawQueryParams.length > 0
73
- ? (() => {
74
- const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
75
- const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
76
- return groupName !== individualName ? groupName : undefined
77
- })()
78
- : undefined
79
-
80
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
81
- const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
82
-
83
- const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
84
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
85
- const rawParamsCall = callPrinter.print(paramsNode) ?? ''
86
- const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
87
-
88
- const queryKeyParamsNode = buildQueryKeyParamsNode(node, { pathParamsType, paramsCasing, resolver: tsResolver })
89
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
90
-
91
- const enabledSource = buildEnabledCheck(queryKeyParamsNode)
92
- const enabledText = enabledSource ? `enabled: () => !!(${enabledSource}),` : ''
93
-
94
- const hasNewParams = nextParam !== undefined || previousParam !== undefined
95
-
96
- let getNextPageParamExpr: string | undefined
97
- let getPreviousPageParamExpr: string | undefined
98
-
99
- if (hasNewParams) {
100
- if (nextParam) {
101
- const accessor = getNestedAccessor(nextParam, 'lastPage')
102
- if (accessor) {
103
- getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`
104
- }
105
- }
106
- if (previousParam) {
107
- const accessor = getNestedAccessor(previousParam, 'firstPage')
108
- if (accessor) {
109
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`
110
- }
111
- }
112
- } else if (cursorParam) {
113
- getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`
114
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`
115
- } else {
116
- if (dataReturnType === 'full') {
117
- getNextPageParamExpr =
118
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
119
- } else {
120
- getNextPageParamExpr =
121
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
122
- }
123
- getPreviousPageParamExpr = 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
124
- }
125
-
126
- const queryOptionsArr = [
127
- `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
128
- getNextPageParamExpr,
129
- getPreviousPageParamExpr,
130
- ].filter(Boolean)
131
-
132
- const infiniteOverrideParams =
133
- queryParam && queryParamsTypeName
134
- ? `
135
- params = {
136
- ...(params ?? {}),
137
- ['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
138
- } as ${queryParamsTypeName}`
139
- : ''
140
-
141
- if (infiniteOverrideParams) {
142
- return (
143
- <File.Source name={name} isExportable isIndexable>
144
- <Function name={name} export params={paramsSignature}>
145
- {`
146
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
147
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, QueryKey, ${pageParamType}>({
148
- ${enabledText}
149
- queryKey,
150
- queryFn: async ({ signal, pageParam }) => {
151
- ${infiniteOverrideParams}
152
- return ${clientName}(${addToValueCalls(clientCallStr)})
153
- },
154
- ${queryOptionsArr.join(',\n')}
155
- })
156
- `}
157
- </Function>
158
- </File.Source>
159
- )
160
- }
161
-
162
- return (
163
- <File.Source name={name} isExportable isIndexable>
164
- <Function name={name} export params={paramsSignature}>
165
- {`
166
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
167
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, QueryKey, ${pageParamType}>({
168
- ${enabledText}
169
- queryKey,
170
- queryFn: async ({ signal }) => {
171
- return ${clientName}(${addToValueCalls(clientCallStr)})
172
- },
173
- ${queryOptionsArr.join(',\n')}
174
- })
175
- `}
176
- </Function>
177
- </File.Source>
178
- )
179
- }
180
-
181
- function addToValueCalls(callStr: string): string {
182
- // Step 1: Transform shorthand object params like { petId } → { petId: toValue(petId) }
183
- let result = callStr.replace(/\{\s*([\w,\s]+)\s*\}(?=\s*,)/g, (match, inner: string) => {
184
- if (inner.includes(':') || inner.includes('...')) return match
185
- const keys = inner
186
- .split(',')
187
- .map((k: string) => k.trim())
188
- .filter(Boolean)
189
- const wrapped = keys.map((k: string) => `${k}: toValue(${k})`).join(', ')
190
- return `{ ${wrapped} }`
191
- })
192
-
193
- // Step 2: Handle standalone identifiers like `data, params`
194
- result = result.replace(/(?<![{.:?])\b(\w+)\b(?=\s*,)/g, (match, name: string) => {
195
- if (name === 'config' || name === 'signal' || name === 'undefined') return match
196
- if (match.includes('toValue(')) return match
197
- return `toValue(${name})`
198
- })
199
-
200
- return result
201
- }
@@ -1,148 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { PluginVueQuery } from '../types.ts'
7
- import { buildRequestConfigType, getComments, resolveErrorNames, wrapWithMaybeRefOrGetter } from '../utils.ts'
8
-
9
- type Props = {
10
- name: string
11
- typeName: string
12
- clientName: string
13
- mutationKeyName: string
14
- node: ast.OperationNode
15
- tsResolver: ResolverTs
16
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
17
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
18
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
19
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
20
- }
21
-
22
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
23
- const callPrinter = functionPrinter({ mode: 'call' })
24
- const keysPrinter = functionPrinter({ mode: 'keys' })
25
-
26
- function createMutationArgParams(
27
- node: ast.OperationNode,
28
- options: {
29
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
30
- resolver: ResolverTs
31
- },
32
- ): ast.FunctionParametersNode {
33
- return ast.createOperationParams(node, {
34
- paramsType: 'inline',
35
- pathParamsType: 'inline',
36
- paramsCasing: options.paramsCasing,
37
- resolver: options.resolver,
38
- })
39
- }
40
-
41
- function buildMutationParamsNode(
42
- node: ast.OperationNode,
43
- options: {
44
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
45
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
46
- resolver: ResolverTs
47
- },
48
- ): ast.FunctionParametersNode {
49
- const { paramsCasing, dataReturnType, resolver } = options
50
- const responseName = resolver.resolveResponseName(node)
51
- const errorNames = resolveErrorNames(node, resolver)
52
-
53
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
54
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
55
-
56
- const mutationArgParamsNode = createMutationArgParams(node, { paramsCasing, resolver })
57
-
58
- const wrappedParamsNode = wrapWithMaybeRefOrGetter(mutationArgParamsNode)
59
- const TRequestWrapped = wrappedParamsNode.params.length > 0 ? (declarationPrinter.print(wrappedParamsNode) ?? '') : ''
60
-
61
- return ast.createFunctionParameters({
62
- params: [
63
- ast.createFunctionParameter({
64
- name: 'options',
65
- type: ast.createParamsType({
66
- variant: 'reference',
67
- name: `{
68
- mutation?: MutationObserverOptions<${[TData, TError, TRequestWrapped ? `{${TRequestWrapped}}` : 'void', 'TContext'].join(', ')}> & { client?: QueryClient },
69
- client?: ${buildRequestConfigType(node, resolver)},
70
- }`,
71
- }),
72
- default: '{}',
73
- }),
74
- ],
75
- })
76
- }
77
-
78
- export function Mutation({
79
- name,
80
- clientName,
81
- paramsCasing,
82
- paramsType,
83
- pathParamsType,
84
- dataReturnType,
85
- node,
86
- tsResolver,
87
- mutationKeyName,
88
- }: Props): KubbReactNode {
89
- const responseName = tsResolver.resolveResponseName(node)
90
- const errorNames = resolveErrorNames(node, tsResolver)
91
-
92
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
93
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
94
-
95
- const mutationArgParamsNode = createMutationArgParams(node, {
96
- paramsCasing,
97
- resolver: tsResolver,
98
- })
99
- const hasMutationParams = mutationArgParamsNode.params.length > 0
100
- const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
101
- const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
102
-
103
- const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
104
-
105
- const mutationKeyParamsNode = ast.createFunctionParameters({ params: [] })
106
- const mutationKeyParamsCall = callPrinter.print(mutationKeyParamsNode) ?? ''
107
-
108
- const clientCallParamsNode = ast.createOperationParams(node, {
109
- paramsType,
110
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
111
- paramsCasing,
112
- resolver: tsResolver,
113
- extraParams: [
114
- ast.createFunctionParameter({
115
- name: 'config',
116
- type: ast.createParamsType({
117
- variant: 'reference',
118
- name: buildRequestConfigType(node, tsResolver),
119
- }),
120
- default: '{}',
121
- }),
122
- ],
123
- })
124
- const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
125
-
126
- const paramsNode = buildMutationParamsNode(node, { paramsCasing, dataReturnType, resolver: tsResolver })
127
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
128
-
129
- return (
130
- <File.Source name={name} isExportable isIndexable>
131
- <Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }} generics={['TContext']}>
132
- {`
133
- const { mutation = {}, client: config = {} } = options ?? {}
134
- const { client: queryClient, ...mutationOptions } = mutation;
135
- const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParamsCall})
136
-
137
- return useMutation<${generics}>({
138
- mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : ''}) => {
139
- return ${clientName}(${clientCallStr})
140
- },
141
- mutationKey,
142
- ...mutationOptions
143
- }, queryClient)
144
- `}
145
- </Function>
146
- </File.Source>
147
- )
148
- }
@@ -1 +0,0 @@
1
- export { MutationKey } from '@internals/tanstack-query'
@@ -1,119 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { PluginVueQuery } from '../types.ts'
7
- import { getComments, resolveErrorNames, wrapWithMaybeRefOrGetter } from '../utils.ts'
8
- import { buildQueryKeyParamsNode } from './QueryKey.tsx'
9
- import { getQueryOptionsParams } from './QueryOptions.tsx'
10
-
11
- type Props = {
12
- name: string
13
- queryOptionsName: string
14
- queryKeyName: string
15
- queryKeyTypeName: string
16
- node: ast.OperationNode
17
- tsResolver: ResolverTs
18
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
19
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
20
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
21
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
22
- }
23
-
24
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
25
- const callPrinter = functionPrinter({ mode: 'call' })
26
-
27
- function buildQueryParamsNode(
28
- node: ast.OperationNode,
29
- options: {
30
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
31
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
32
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
33
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
34
- resolver: ResolverTs
35
- },
36
- ): ast.FunctionParametersNode {
37
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
38
- const responseName = resolver.resolveResponseName(node)
39
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
40
- const errorNames = resolveErrorNames(node, resolver)
41
-
42
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
43
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
44
-
45
- const optionsParam = ast.createFunctionParameter({
46
- name: 'options',
47
- type: ast.createParamsType({
48
- variant: 'reference',
49
- name: `{
50
- query?: Partial<UseQueryOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
51
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
52
- }`,
53
- }),
54
- default: '{}',
55
- })
56
-
57
- // Vue-query wraps operation params with MaybeRefOrGetter
58
- const baseParams = ast.createOperationParams(node, {
59
- paramsType,
60
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
61
- paramsCasing,
62
- resolver,
63
- extraParams: [optionsParam],
64
- })
65
-
66
- return wrapWithMaybeRefOrGetter(baseParams, (name) => name === 'options')
67
- }
68
-
69
- export function Query({
70
- name,
71
- queryKeyTypeName,
72
- queryOptionsName,
73
- queryKeyName,
74
- paramsType,
75
- paramsCasing,
76
- pathParamsType,
77
- dataReturnType,
78
- node,
79
- tsResolver,
80
- }: Props): KubbReactNode {
81
- const responseName = tsResolver.resolveResponseName(node)
82
- const errorNames = resolveErrorNames(node, tsResolver)
83
-
84
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
85
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
86
- const returnType = `UseQueryReturnType<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
87
- const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
88
-
89
- const queryKeyParamsNode = buildQueryKeyParamsNode(node, { pathParamsType, paramsCasing, resolver: tsResolver })
90
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
91
-
92
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
93
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
94
-
95
- const paramsNode = buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
96
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
97
-
98
- return (
99
- <File.Source name={name} isExportable isIndexable>
100
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
101
- {`
102
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
103
- const { client: queryClient, ...resolvedOptions } = queryConfig
104
- const queryKey = (resolvedOptions && 'queryKey' in resolvedOptions ? toValue(resolvedOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParamsCall})
105
-
106
- const query = useQuery({
107
- ...${queryOptionsName}(${queryOptionsParamsCall}),
108
- ...resolvedOptions,
109
- queryKey
110
- } as unknown as UseQueryOptions<${TData}, ${TError}, TData, ${TData}, TQueryKey>, toValue(queryClient)) as ${returnType}
111
-
112
- query.queryKey = queryKey as TQueryKey
113
-
114
- return query
115
- `}
116
- </Function>
117
- </File.Source>
118
- )
119
- }
@@ -1,51 +0,0 @@
1
- import type { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function, Type } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import { queryKeyTransformer } from '@internals/tanstack-query'
7
- import type { Transformer } from '../types.ts'
8
- import { buildQueryKeyParams, wrapWithMaybeRefOrGetter } from '../utils.ts'
9
-
10
- type Props = {
11
- name: string
12
- typeName: string
13
- node: ast.OperationNode
14
- tsResolver: ResolverTs
15
- paramsCasing: 'camelcase' | undefined
16
- pathParamsType: 'object' | 'inline'
17
- transformer: Transformer | undefined
18
- }
19
-
20
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
21
-
22
- export function buildQueryKeyParamsNode(
23
- node: ast.OperationNode,
24
- options: { pathParamsType: 'object' | 'inline'; paramsCasing: 'camelcase' | undefined; resolver: ResolverTs },
25
- ): ast.FunctionParametersNode {
26
- return wrapWithMaybeRefOrGetter(buildQueryKeyParams(node, options))
27
- }
28
-
29
- export function QueryKey({ name, node, tsResolver, paramsCasing, pathParamsType, typeName, transformer = queryKeyTransformer }: Props): KubbReactNode {
30
- const paramsNode = buildQueryKeyParamsNode(node, { pathParamsType, paramsCasing, resolver: tsResolver })
31
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
32
- const keys = transformer({
33
- node,
34
- casing: paramsCasing,
35
- })
36
-
37
- return (
38
- <>
39
- <File.Source name={name} isExportable isIndexable>
40
- <Function.Arrow name={name} export params={paramsSignature} singleLine>
41
- {`[${keys.join(', ')}] as const`}
42
- </Function.Arrow>
43
- </File.Source>
44
- <File.Source name={typeName} isExportable isIndexable isTypeOnly>
45
- <Type name={typeName} export>
46
- {`ReturnType<typeof ${name}>`}
47
- </Type>
48
- </File.Source>
49
- </>
50
- )
51
- }