@kubb/plugin-vue-query 5.0.0-beta.42 → 5.0.0-beta.64

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 (43) hide show
  1. package/dist/{components-Bxe1EuWf.js → components-CAlEf7Oh.js} +273 -388
  2. package/dist/components-CAlEf7Oh.js.map +1 -0
  3. package/dist/{components-BwFPMwK7.cjs → components-CfU59l8V.cjs} +274 -389
  4. package/dist/components-CfU59l8V.cjs.map +1 -0
  5. package/dist/components.cjs +1 -1
  6. package/dist/components.d.ts +1 -1
  7. package/dist/components.js +1 -1
  8. package/dist/{generators-BSN6A0ED.cjs → generators-DpMLVmyi.cjs} +91 -137
  9. package/dist/generators-DpMLVmyi.cjs.map +1 -0
  10. package/dist/{generators-D9TUvYRN.js → generators-fWBjs0CN.js} +93 -139
  11. package/dist/generators-fWBjs0CN.js.map +1 -0
  12. package/dist/generators.cjs +1 -1
  13. package/dist/generators.d.ts +1 -1
  14. package/dist/generators.js +1 -1
  15. package/dist/index.cjs +41 -18
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.js +42 -19
  19. package/dist/index.js.map +1 -1
  20. package/dist/{types-HHnnlEhK.d.ts → types-C6a_58nb.d.ts} +12 -16
  21. package/package.json +10 -18
  22. package/dist/components-BwFPMwK7.cjs.map +0 -1
  23. package/dist/components-Bxe1EuWf.js.map +0 -1
  24. package/dist/generators-BSN6A0ED.cjs.map +0 -1
  25. package/dist/generators-D9TUvYRN.js.map +0 -1
  26. package/extension.yaml +0 -1248
  27. package/src/components/InfiniteQuery.tsx +0 -127
  28. package/src/components/InfiniteQueryOptions.tsx +0 -194
  29. package/src/components/Mutation.tsx +0 -150
  30. package/src/components/MutationKey.tsx +0 -1
  31. package/src/components/Query.tsx +0 -126
  32. package/src/components/QueryKey.tsx +0 -52
  33. package/src/components/QueryOptions.tsx +0 -137
  34. package/src/components/index.ts +0 -7
  35. package/src/generators/index.ts +0 -3
  36. package/src/generators/infiniteQueryGenerator.tsx +0 -200
  37. package/src/generators/mutationGenerator.tsx +0 -158
  38. package/src/generators/queryGenerator.tsx +0 -184
  39. package/src/index.ts +0 -2
  40. package/src/plugin.ts +0 -183
  41. package/src/resolvers/resolverVueQuery.ts +0 -76
  42. package/src/types.ts +0 -272
  43. package/src/utils.ts +0 -56
@@ -1,127 +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 { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
8
- import { 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
- initialPageParam: Infinite['initialPageParam']
24
- queryParam?: Infinite['queryParam']
25
- }
26
-
27
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
28
- const callPrinter = functionPrinter({ mode: 'call' })
29
-
30
- function buildInfiniteQueryParamsNode(
31
- node: ast.OperationNode,
32
- options: {
33
- paramsType: PluginVueQuery['resolvedOptions']['paramsType']
34
- paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
35
- pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
36
- dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
37
- resolver: ResolverTs
38
- },
39
- ): ast.FunctionParametersNode {
40
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
41
- const successNames = resolveSuccessNames(node, resolver)
42
- const responseName = successNames.length > 0 ? successNames.join(' | ') : resolver.resolveResponseName(node)
43
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null
44
- const errorNames = resolveErrorNames(node, resolver)
45
-
46
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
47
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
48
-
49
- const optionsParam = ast.createFunctionParameter({
50
- name: 'options',
51
- type: ast.createParamsType({
52
- variant: 'reference',
53
- name: `{
54
- query?: Partial<UseInfiniteQueryOptions<${[TData, TError, 'TQueryData', 'TQueryKey', 'TQueryData'].join(', ')}>> & { client?: QueryClient },
55
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
56
- }`,
57
- }),
58
- default: '{}',
59
- })
60
-
61
- const baseParams = ast.createOperationParams(node, {
62
- paramsType,
63
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
64
- paramsCasing,
65
- resolver,
66
- extraParams: [optionsParam],
67
- })
68
-
69
- return wrapWithMaybeRefOrGetter(baseParams, (name) => name === 'options')
70
- }
71
-
72
- export function InfiniteQuery({
73
- name,
74
- queryKeyTypeName,
75
- queryOptionsName,
76
- queryKeyName,
77
- paramsType,
78
- paramsCasing,
79
- pathParamsType,
80
- dataReturnType,
81
- node,
82
- tsResolver,
83
- }: Props): KubbReactNode {
84
- const successNames = resolveSuccessNames(node, tsResolver)
85
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
86
- const errorNames = resolveErrorNames(node, tsResolver)
87
-
88
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
89
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
90
- const returnType = `UseInfiniteQueryReturnType<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
91
- const generics = [`TData = InfiniteData<${TData}>`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
92
-
93
- const queryKeyParamsNode = buildQueryKeyParamsNode(node, { pathParamsType, paramsCasing, resolver: tsResolver })
94
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
95
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
96
-
97
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
98
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
99
-
100
- const paramsNode = markParamsOptional(
101
- buildInfiniteQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver }),
102
- enabledNames,
103
- )
104
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
105
-
106
- return (
107
- <File.Source name={name} isExportable isIndexable>
108
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
109
- {`
110
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
111
- const { client: queryClient, ...resolvedOptions } = queryConfig
112
- const queryKey = (resolvedOptions && 'queryKey' in resolvedOptions ? toValue(resolvedOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParamsCall})
113
-
114
- const query = useInfiniteQuery({
115
- ...${queryOptionsName}(${queryOptionsParamsCall}),
116
- ...resolvedOptions,
117
- queryKey
118
- } as unknown as UseInfiniteQueryOptions<${TData}, ${TError}, ${TData}, TQueryKey, ${TData}>, toValue(queryClient)) as ${returnType}
119
-
120
- query.queryKey = queryKey as TQueryKey
121
-
122
- return query
123
- `}
124
- </Function>
125
- </File.Source>
126
- )
127
- }
@@ -1,194 +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, 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 : `ResponseConfig<${responseName}>`
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
- ? `
126
- params = {
127
- ...(params ?? {}),
128
- ['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
129
- } as ${queryParamsTypeName}`
130
- : ''
131
-
132
- if (infiniteOverrideParams) {
133
- return (
134
- <File.Source name={name} isExportable isIndexable>
135
- <Function name={name} export params={paramsSignature}>
136
- {`
137
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
138
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, QueryKey, ${pageParamType}>({
139
- ${enabledText}
140
- queryKey,
141
- queryFn: async ({ signal, pageParam }) => {
142
- ${infiniteOverrideParams}
143
- return ${clientName}(${addToValueCalls(clientCallStr, enabledNames)})
144
- },
145
- ${queryOptionsArr.join(',\n')}
146
- })
147
- `}
148
- </Function>
149
- </File.Source>
150
- )
151
- }
152
-
153
- return (
154
- <File.Source name={name} isExportable isIndexable>
155
- <Function name={name} export params={paramsSignature}>
156
- {`
157
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
158
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, QueryKey, ${pageParamType}>({
159
- ${enabledText}
160
- queryKey,
161
- queryFn: async ({ signal }) => {
162
- return ${clientName}(${addToValueCalls(clientCallStr, enabledNames)})
163
- },
164
- ${queryOptionsArr.join(',\n')}
165
- })
166
- `}
167
- </Function>
168
- </File.Source>
169
- )
170
- }
171
-
172
- function addToValueCalls(callStr: string, enabledNames: ReadonlyArray<string> = []): string {
173
- const optional = new Set(enabledNames)
174
- // Step 1: Transform shorthand object params like { petId } → { petId: toValue(petId) }
175
- // Params that drive the `enabled` guard are optional, so assert non-null: toValue(petId!)
176
- let result = callStr.replace(/\{\s*([\w,\s]+)\s*\}(?=\s*,)/g, (match, inner: string) => {
177
- if (inner.includes(':') || inner.includes('...')) return match
178
- const keys = inner
179
- .split(',')
180
- .map((k: string) => k.trim())
181
- .filter(Boolean)
182
- const wrapped = keys.map((k: string) => `${k}: toValue(${optional.has(k) ? `${k}!` : k})`).join(', ')
183
- return `{ ${wrapped} }`
184
- })
185
-
186
- // Step 2: Handle standalone identifiers like `data, params`
187
- result = result.replace(/(?<![{.:?])\b(\w+)\b(?=\s*,)/g, (match, name: string) => {
188
- if (name === 'config' || name === 'signal' || name === 'undefined') return match
189
- if (match.includes('toValue(')) return match
190
- return `toValue(${optional.has(name) ? `${name}!` : name})`
191
- })
192
-
193
- return result
194
- }
@@ -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, 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 : `ResponseConfig<${responseName}>`
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 : `ResponseConfig<${responseName}>`
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 { 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 : `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<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 : `ResponseConfig<${responseName}>`
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
- }