@kubb/plugin-vue-query 5.0.0-alpha.9 → 5.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +17 -10
- package/README.md +1 -3
- package/dist/components-D1UhYFgY.js +1277 -0
- package/dist/components-D1UhYFgY.js.map +1 -0
- package/dist/components-qfOFRSoM.cjs +1367 -0
- package/dist/components-qfOFRSoM.cjs.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +118 -109
- package/dist/components.js +1 -1
- package/dist/generators-C4gs_P1i.cjs +726 -0
- package/dist/generators-C4gs_P1i.cjs.map +1 -0
- package/dist/generators-CbnIVBgY.js +709 -0
- package/dist/generators-CbnIVBgY.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +5 -501
- package/dist/generators.js +1 -1
- package/dist/index.cjs +106 -121
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +102 -121
- package/dist/index.js.map +1 -1
- package/dist/types-nVDTfuS1.d.ts +194 -0
- package/package.json +61 -64
- package/src/components/InfiniteQuery.tsx +104 -153
- package/src/components/InfiniteQueryOptions.tsx +122 -162
- package/src/components/Mutation.tsx +110 -136
- package/src/components/Query.tsx +102 -151
- package/src/components/QueryKey.tsx +68 -58
- package/src/components/QueryOptions.tsx +147 -139
- package/src/generators/infiniteQueryGenerator.tsx +165 -170
- package/src/generators/mutationGenerator.tsx +117 -124
- package/src/generators/queryGenerator.tsx +138 -136
- package/src/index.ts +1 -1
- package/src/plugin.ts +124 -175
- package/src/resolvers/resolverVueQuery.ts +19 -0
- package/src/types.ts +68 -48
- package/src/utils.ts +37 -0
- package/dist/components-Yjoe78Y7.cjs +0 -1119
- package/dist/components-Yjoe78Y7.cjs.map +0 -1
- package/dist/components-_AMBl0g-.js +0 -1029
- package/dist/components-_AMBl0g-.js.map +0 -1
- package/dist/generators-CR34GjVu.js +0 -661
- package/dist/generators-CR34GjVu.js.map +0 -1
- package/dist/generators-DH8VkK1q.cjs +0 -678
- package/dist/generators-DH8VkK1q.cjs.map +0 -1
- package/dist/types-CgDFUvfZ.d.ts +0 -211
|
@@ -1,84 +1,90 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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 { PluginVueQuery } from '../types.ts'
|
|
7
|
+
import { buildMutationArgParams, getComments, resolveErrorNames } from '../utils.ts'
|
|
8
8
|
import { MutationKey } from './MutationKey.tsx'
|
|
9
9
|
|
|
10
10
|
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
11
|
name: string
|
|
15
12
|
typeName: string
|
|
16
13
|
clientName: string
|
|
17
14
|
mutationKeyName: string
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
node: ast.OperationNode
|
|
16
|
+
tsResolver: ResolverTs
|
|
20
17
|
paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
|
|
21
18
|
paramsType: PluginVueQuery['resolvedOptions']['paramsType']
|
|
22
19
|
dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
20
|
pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
|
|
24
21
|
}
|
|
25
22
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
24
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
25
|
+
const keysPrinter = functionPrinter({ mode: 'keys' })
|
|
26
|
+
|
|
27
|
+
function getParams(
|
|
28
|
+
node: ast.OperationNode,
|
|
29
|
+
options: {
|
|
30
|
+
paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
|
|
31
|
+
dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
|
|
32
|
+
resolver: ResolverTs
|
|
33
|
+
},
|
|
34
|
+
): ast.FunctionParametersNode {
|
|
35
|
+
const { paramsCasing, dataReturnType, resolver } = options
|
|
36
|
+
const responseName = resolver.resolveResponseName(node)
|
|
37
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
38
|
+
const errorNames = resolveErrorNames(node, resolver)
|
|
39
|
+
|
|
40
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
41
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
42
|
+
|
|
43
|
+
const mutationArgParamsNode = buildMutationArgParams(node, { paramsCasing, resolver })
|
|
32
44
|
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
casing: paramsCasing,
|
|
41
|
-
override(item) {
|
|
42
|
-
return {
|
|
43
|
-
...item,
|
|
44
|
-
type: `MaybeRefOrGetter<${item.type}>`,
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
}),
|
|
48
|
-
data: typeSchemas.request?.name
|
|
49
|
-
? {
|
|
50
|
-
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
|
|
51
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
52
|
-
}
|
|
53
|
-
: undefined,
|
|
54
|
-
params: typeSchemas.queryParams?.name
|
|
55
|
-
? {
|
|
56
|
-
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
|
|
57
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
58
|
-
}
|
|
59
|
-
: undefined,
|
|
60
|
-
headers: typeSchemas.headerParams?.name
|
|
61
|
-
? {
|
|
62
|
-
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
|
|
63
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
64
|
-
}
|
|
65
|
-
: undefined,
|
|
45
|
+
// Vue-query uses MutationObserverOptions instead of UseMutationOptions, and wraps params with MaybeRefOrGetter
|
|
46
|
+
const mutationArgWrapped = mutationArgParamsNode.params.map((param) => {
|
|
47
|
+
const fp = param as ast.FunctionParameterNode
|
|
48
|
+
return {
|
|
49
|
+
...fp,
|
|
50
|
+
type: fp.type ? ast.createParamsType({ variant: 'reference', name: `MaybeRefOrGetter<${printType(fp.type)}>` }) : fp.type,
|
|
51
|
+
}
|
|
66
52
|
})
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
{
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
53
|
+
const wrappedParamsNode = ast.createFunctionParameters({ params: mutationArgWrapped })
|
|
54
|
+
const TRequestWrapped = wrappedParamsNode.params.length > 0 ? (declarationPrinter.print(wrappedParamsNode) ?? '') : ''
|
|
55
|
+
|
|
56
|
+
return ast.createFunctionParameters({
|
|
57
|
+
params: [
|
|
58
|
+
ast.createFunctionParameter({
|
|
59
|
+
name: 'options',
|
|
60
|
+
type: ast.createParamsType({
|
|
61
|
+
variant: 'reference',
|
|
62
|
+
name: `{
|
|
63
|
+
mutation?: MutationObserverOptions<${[TData, TError, TRequestWrapped ? `{${TRequestWrapped}}` : 'void', 'TContext'].join(', ')}> & { client?: QueryClient },
|
|
64
|
+
client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
65
|
+
}`,
|
|
66
|
+
}),
|
|
67
|
+
default: '{}',
|
|
68
|
+
}),
|
|
69
|
+
],
|
|
79
70
|
})
|
|
80
71
|
}
|
|
81
72
|
|
|
73
|
+
function printType(typeNode: ast.ParamsTypeNode | undefined): string {
|
|
74
|
+
if (!typeNode) return 'unknown'
|
|
75
|
+
if (typeNode.variant === 'reference') return typeNode.name
|
|
76
|
+
if (typeNode.variant === 'member') return `${typeNode.base}['${typeNode.key}']`
|
|
77
|
+
if (typeNode.variant === 'struct') {
|
|
78
|
+
const parts = typeNode.properties.map((p) => {
|
|
79
|
+
const typeStr = printType(p.type)
|
|
80
|
+
const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p.name) ? p.name : JSON.stringify(p.name)
|
|
81
|
+
return p.optional ? `${key}?: ${typeStr}` : `${key}: ${typeStr}`
|
|
82
|
+
})
|
|
83
|
+
return `{ ${parts.join('; ')} }`
|
|
84
|
+
}
|
|
85
|
+
return 'unknown'
|
|
86
|
+
}
|
|
87
|
+
|
|
82
88
|
export function Mutation({
|
|
83
89
|
name,
|
|
84
90
|
clientName,
|
|
@@ -86,94 +92,60 @@ export function Mutation({
|
|
|
86
92
|
paramsType,
|
|
87
93
|
pathParamsType,
|
|
88
94
|
dataReturnType,
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
node,
|
|
96
|
+
tsResolver,
|
|
91
97
|
mutationKeyName,
|
|
92
|
-
}: Props):
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
typeSchemas,
|
|
96
|
-
})
|
|
98
|
+
}: Props): KubbReactNode {
|
|
99
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
100
|
+
const errorNames = resolveErrorNames(node, tsResolver)
|
|
97
101
|
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
pathParamsType,
|
|
101
|
-
dataReturnType,
|
|
102
|
-
typeSchemas,
|
|
103
|
-
})
|
|
102
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
103
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
104
104
|
|
|
105
|
-
const
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
pathParamsType,
|
|
110
|
-
isConfigurable: true,
|
|
111
|
-
})
|
|
105
|
+
const mutationArgParamsNode = buildMutationArgParams(node, { paramsCasing, resolver: tsResolver })
|
|
106
|
+
const hasMutationParams = mutationArgParamsNode.params.length > 0
|
|
107
|
+
const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
108
|
+
const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
112
109
|
|
|
113
|
-
const
|
|
114
|
-
...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
115
|
-
data: typeSchemas.request?.name
|
|
116
|
-
? {
|
|
117
|
-
type: typeSchemas.request?.name,
|
|
118
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
119
|
-
}
|
|
120
|
-
: undefined,
|
|
121
|
-
params: typeSchemas.queryParams?.name
|
|
122
|
-
? {
|
|
123
|
-
type: typeSchemas.queryParams?.name,
|
|
124
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
125
|
-
}
|
|
126
|
-
: undefined,
|
|
127
|
-
headers: typeSchemas.headerParams?.name
|
|
128
|
-
? {
|
|
129
|
-
type: typeSchemas.headerParams?.name,
|
|
130
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
131
|
-
}
|
|
132
|
-
: undefined,
|
|
133
|
-
})
|
|
134
|
-
const dataParams = FunctionParams.factory({
|
|
135
|
-
data: {
|
|
136
|
-
// No use of pathParams because useMutation can only take one argument in object form,
|
|
137
|
-
// see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
|
|
138
|
-
mode: 'object',
|
|
139
|
-
children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => {
|
|
140
|
-
if (value) {
|
|
141
|
-
acc[key] = {
|
|
142
|
-
...value,
|
|
143
|
-
type: undefined,
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return acc
|
|
148
|
-
}, {} as Params),
|
|
149
|
-
},
|
|
150
|
-
})
|
|
110
|
+
const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
|
|
151
111
|
|
|
152
|
-
const
|
|
153
|
-
const
|
|
154
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
112
|
+
const mutationKeyParamsNode = MutationKey.getParams()
|
|
113
|
+
const mutationKeyParamsCall = callPrinter.print(mutationKeyParamsNode) ?? ''
|
|
155
114
|
|
|
156
|
-
const
|
|
115
|
+
const clientCallParamsNode = ast.createOperationParams(node, {
|
|
116
|
+
paramsType,
|
|
117
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
118
|
+
paramsCasing,
|
|
119
|
+
resolver: tsResolver,
|
|
120
|
+
extraParams: [
|
|
121
|
+
ast.createFunctionParameter({
|
|
122
|
+
name: 'config',
|
|
123
|
+
type: ast.createParamsType({
|
|
124
|
+
variant: 'reference',
|
|
125
|
+
name: node.requestBody?.content?.[0]?.schema
|
|
126
|
+
? `Partial<RequestConfig<${tsResolver.resolveDataName(node)}>> & { client?: Client }`
|
|
127
|
+
: 'Partial<RequestConfig> & { client?: Client }',
|
|
128
|
+
}),
|
|
129
|
+
default: '{}',
|
|
130
|
+
}),
|
|
131
|
+
],
|
|
132
|
+
})
|
|
133
|
+
const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
|
|
134
|
+
|
|
135
|
+
const paramsNode = getParams(node, { paramsCasing, dataReturnType, resolver: tsResolver })
|
|
136
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
157
137
|
|
|
158
138
|
return (
|
|
159
139
|
<File.Source name={name} isExportable isIndexable>
|
|
160
|
-
<Function
|
|
161
|
-
name={name}
|
|
162
|
-
export
|
|
163
|
-
params={params.toConstructor()}
|
|
164
|
-
JSDoc={{
|
|
165
|
-
comments: getComments(operation),
|
|
166
|
-
}}
|
|
167
|
-
generics={['TContext']}
|
|
168
|
-
>
|
|
140
|
+
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }} generics={['TContext']}>
|
|
169
141
|
{`
|
|
170
142
|
const { mutation = {}, client: config = {} } = options ?? {}
|
|
171
143
|
const { client: queryClient, ...mutationOptions } = mutation;
|
|
172
|
-
const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${
|
|
144
|
+
const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParamsCall})
|
|
173
145
|
|
|
174
146
|
return useMutation<${generics}>({
|
|
175
|
-
mutationFn: async(${
|
|
176
|
-
return ${clientName}(${
|
|
147
|
+
mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : ''}) => {
|
|
148
|
+
return ${clientName}(${clientCallStr})
|
|
177
149
|
},
|
|
178
150
|
mutationKey,
|
|
179
151
|
...mutationOptions
|
|
@@ -183,3 +155,5 @@ export function Mutation({
|
|
|
183
155
|
</File.Source>
|
|
184
156
|
)
|
|
185
157
|
}
|
|
158
|
+
|
|
159
|
+
Mutation.getParams = getParams
|
package/src/components/Query.tsx
CHANGED
|
@@ -1,139 +1,107 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
import {
|
|
4
|
-
import { File, Function
|
|
5
|
-
import type {
|
|
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 { PluginVueQuery } from '../types.ts'
|
|
7
|
+
import { getComments, resolveErrorNames } from '../utils.ts'
|
|
7
8
|
import { QueryKey } from './QueryKey.tsx'
|
|
8
|
-
import {
|
|
9
|
+
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
9
10
|
|
|
10
11
|
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
12
|
name: string
|
|
15
13
|
queryOptionsName: string
|
|
16
14
|
queryKeyName: string
|
|
17
15
|
queryKeyTypeName: string
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
node: ast.OperationNode
|
|
17
|
+
tsResolver: ResolverTs
|
|
20
18
|
paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
|
|
21
19
|
paramsType: PluginVueQuery['resolvedOptions']['paramsType']
|
|
22
20
|
pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
|
|
23
21
|
dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
paramsType: PluginVueQuery['resolvedOptions']['paramsType']
|
|
29
|
-
pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
|
|
30
|
-
dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
|
|
31
|
-
typeSchemas: OperationSchemas
|
|
32
|
-
}
|
|
24
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
25
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
33
26
|
|
|
34
|
-
function getParams(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
})
|
|
49
|
-
const children = {
|
|
50
|
-
...pathParams,
|
|
51
|
-
data: typeSchemas.request?.name
|
|
52
|
-
? {
|
|
53
|
-
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
|
|
54
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
55
|
-
}
|
|
56
|
-
: undefined,
|
|
57
|
-
params: typeSchemas.queryParams?.name
|
|
58
|
-
? {
|
|
59
|
-
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
|
|
60
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
61
|
-
}
|
|
62
|
-
: undefined,
|
|
63
|
-
headers: typeSchemas.headerParams?.name
|
|
64
|
-
? {
|
|
65
|
-
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
|
|
66
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
67
|
-
}
|
|
68
|
-
: undefined,
|
|
69
|
-
}
|
|
27
|
+
function getParams(
|
|
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)
|
|
70
41
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return FunctionParams.factory({
|
|
75
|
-
data: {
|
|
76
|
-
mode: 'object',
|
|
77
|
-
children,
|
|
78
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
79
|
-
},
|
|
80
|
-
options: {
|
|
81
|
-
type: `
|
|
82
|
-
{
|
|
83
|
-
query?: Partial<UseQueryOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
|
|
84
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
|
|
85
|
-
}
|
|
86
|
-
`,
|
|
87
|
-
default: '{}',
|
|
88
|
-
},
|
|
89
|
-
})
|
|
90
|
-
}
|
|
42
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
43
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
91
44
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
typed: true,
|
|
98
|
-
casing: paramsCasing,
|
|
99
|
-
override(item) {
|
|
100
|
-
return {
|
|
101
|
-
...item,
|
|
102
|
-
type: `MaybeRefOrGetter<${item.type}>`,
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
}),
|
|
106
|
-
default: getDefaultValue(typeSchemas.pathParams?.schema),
|
|
107
|
-
}
|
|
108
|
-
: undefined,
|
|
109
|
-
data: typeSchemas.request?.name
|
|
110
|
-
? {
|
|
111
|
-
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
|
|
112
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
113
|
-
}
|
|
114
|
-
: undefined,
|
|
115
|
-
params: typeSchemas.queryParams?.name
|
|
116
|
-
? {
|
|
117
|
-
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
|
|
118
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
119
|
-
}
|
|
120
|
-
: undefined,
|
|
121
|
-
headers: typeSchemas.headerParams?.name
|
|
122
|
-
? {
|
|
123
|
-
type: `MaybeRefOrGetter<${typeSchemas.headerParams?.name}>`,
|
|
124
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
125
|
-
}
|
|
126
|
-
: undefined,
|
|
127
|
-
options: {
|
|
128
|
-
type: `
|
|
129
|
-
{
|
|
45
|
+
const optionsParam = ast.createFunctionParameter({
|
|
46
|
+
name: 'options',
|
|
47
|
+
type: ast.createParamsType({
|
|
48
|
+
variant: 'reference',
|
|
49
|
+
name: `{
|
|
130
50
|
query?: Partial<UseQueryOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
|
|
131
|
-
client?: ${
|
|
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 wrapOperationParamsWithMaybeRef(baseParams)
|
|
132
67
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
68
|
+
|
|
69
|
+
function wrapOperationParamsWithMaybeRef(paramsNode: ast.FunctionParametersNode): ast.FunctionParametersNode {
|
|
70
|
+
const wrappedParams = paramsNode.params.map((param) => {
|
|
71
|
+
if ('kind' in param && (param as ast.ParameterGroupNode).kind === 'ParameterGroup') {
|
|
72
|
+
const group = param as ast.ParameterGroupNode
|
|
73
|
+
return {
|
|
74
|
+
...group,
|
|
75
|
+
properties: group.properties.map((p) => ({
|
|
76
|
+
...p,
|
|
77
|
+
type: p.type ? ast.createParamsType({ variant: 'reference', name: `MaybeRefOrGetter<${printType(p.type)}>` }) : p.type,
|
|
78
|
+
})),
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const fp = param as ast.FunctionParameterNode
|
|
82
|
+
// Don't wrap 'options' param — it's not a reactive value
|
|
83
|
+
if (fp.name === 'options') return fp
|
|
84
|
+
return {
|
|
85
|
+
...fp,
|
|
86
|
+
type: fp.type ? ast.createParamsType({ variant: 'reference', name: `MaybeRefOrGetter<${printType(fp.type)}>` }) : fp.type,
|
|
87
|
+
}
|
|
136
88
|
})
|
|
89
|
+
return ast.createFunctionParameters({ params: wrappedParams })
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function printType(typeNode: ast.ParamsTypeNode | undefined): string {
|
|
93
|
+
if (!typeNode) return 'unknown'
|
|
94
|
+
if (typeNode.variant === 'reference') return typeNode.name
|
|
95
|
+
if (typeNode.variant === 'member') return `${typeNode.base}['${typeNode.key}']`
|
|
96
|
+
if (typeNode.variant === 'struct') {
|
|
97
|
+
const parts = typeNode.properties.map((p) => {
|
|
98
|
+
const typeStr = printType(p.type)
|
|
99
|
+
const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p.name) ? p.name : JSON.stringify(p.name)
|
|
100
|
+
return p.optional ? `${key}?: ${typeStr}` : `${key}: ${typeStr}`
|
|
101
|
+
})
|
|
102
|
+
return `{ ${parts.join('; ')} }`
|
|
103
|
+
}
|
|
104
|
+
return 'unknown'
|
|
137
105
|
}
|
|
138
106
|
|
|
139
107
|
export function Query({
|
|
@@ -145,53 +113,36 @@ export function Query({
|
|
|
145
113
|
paramsCasing,
|
|
146
114
|
pathParamsType,
|
|
147
115
|
dataReturnType,
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}: Props):
|
|
151
|
-
const
|
|
152
|
-
const
|
|
116
|
+
node,
|
|
117
|
+
tsResolver,
|
|
118
|
+
}: Props): KubbReactNode {
|
|
119
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
120
|
+
const errorNames = resolveErrorNames(node, tsResolver)
|
|
121
|
+
|
|
122
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
123
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
153
124
|
const returnType = `UseQueryReturnType<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
|
|
154
125
|
const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
|
|
155
126
|
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const queryOptionsParams = QueryOptions.getParams({
|
|
162
|
-
paramsType,
|
|
163
|
-
pathParamsType,
|
|
164
|
-
typeSchemas,
|
|
165
|
-
paramsCasing,
|
|
166
|
-
})
|
|
167
|
-
const params = getParams({
|
|
168
|
-
paramsCasing,
|
|
169
|
-
paramsType,
|
|
170
|
-
pathParamsType,
|
|
171
|
-
dataReturnType,
|
|
172
|
-
typeSchemas,
|
|
173
|
-
})
|
|
127
|
+
const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
128
|
+
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
129
|
+
|
|
130
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
131
|
+
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
|
|
174
132
|
|
|
175
|
-
const
|
|
133
|
+
const paramsNode = getParams(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
|
|
134
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
176
135
|
|
|
177
136
|
return (
|
|
178
137
|
<File.Source name={name} isExportable isIndexable>
|
|
179
|
-
<Function
|
|
180
|
-
name={name}
|
|
181
|
-
export
|
|
182
|
-
generics={generics.join(', ')}
|
|
183
|
-
params={params.toConstructor()}
|
|
184
|
-
JSDoc={{
|
|
185
|
-
comments: getComments(operation),
|
|
186
|
-
}}
|
|
187
|
-
>
|
|
138
|
+
<Function name={name} export generics={generics.join(', ')} params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
|
|
188
139
|
{`
|
|
189
140
|
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
190
141
|
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
191
|
-
const queryKey = (resolvedOptions && 'queryKey' in resolvedOptions ? toValue(resolvedOptions.queryKey) : undefined) ?? ${queryKeyName}(${
|
|
142
|
+
const queryKey = (resolvedOptions && 'queryKey' in resolvedOptions ? toValue(resolvedOptions.queryKey) : undefined) ?? ${queryKeyName}(${queryKeyParamsCall})
|
|
192
143
|
|
|
193
144
|
const query = useQuery({
|
|
194
|
-
...${
|
|
145
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),
|
|
195
146
|
...resolvedOptions,
|
|
196
147
|
queryKey
|
|
197
148
|
} as unknown as UseQueryOptions<${TData}, ${TError}, TData, ${TData}, TQueryKey>, toValue(queryClient)) as ${returnType}
|