@kubb/plugin-vue-query 5.0.0-beta.56 → 5.0.0-beta.73

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