@kubb/plugin-vue-query 5.0.0-alpha.9 → 5.0.0-beta.4
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/extension.yaml +793 -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,179 +1,157 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
+
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
6
|
import type { PluginVueQuery } from '../types.ts'
|
|
7
|
+
import { resolveErrorNames } from '../utils.ts'
|
|
8
8
|
import { QueryKey } from './QueryKey.tsx'
|
|
9
9
|
|
|
10
10
|
type Props = {
|
|
11
11
|
name: string
|
|
12
12
|
clientName: string
|
|
13
13
|
queryKeyName: string
|
|
14
|
-
|
|
14
|
+
node: ast.OperationNode
|
|
15
|
+
tsResolver: ResolverTs
|
|
15
16
|
paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
|
|
16
17
|
paramsType: PluginVueQuery['resolvedOptions']['paramsType']
|
|
17
18
|
pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
|
|
18
19
|
dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
23
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
24
|
+
|
|
25
|
+
export function getQueryOptionsParams(
|
|
26
|
+
node: ast.OperationNode,
|
|
27
|
+
options: {
|
|
28
|
+
paramsType: PluginVueQuery['resolvedOptions']['paramsType']
|
|
29
|
+
paramsCasing: PluginVueQuery['resolvedOptions']['paramsCasing']
|
|
30
|
+
pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
|
|
31
|
+
resolver: ResolverTs
|
|
32
|
+
},
|
|
33
|
+
): ast.FunctionParametersNode {
|
|
34
|
+
const { paramsType, paramsCasing, pathParamsType, resolver } = options
|
|
35
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
},
|
|
37
|
+
const baseParams = ast.createOperationParams(node, {
|
|
38
|
+
paramsType,
|
|
39
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
40
|
+
paramsCasing,
|
|
41
|
+
resolver,
|
|
42
|
+
extraParams: [
|
|
43
|
+
ast.createFunctionParameter({
|
|
44
|
+
name: 'config',
|
|
45
|
+
type: ast.createParamsType({
|
|
46
|
+
variant: 'reference',
|
|
47
|
+
name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
|
|
48
|
+
}),
|
|
49
|
+
default: '{}',
|
|
40
50
|
}),
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
51
|
+
],
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
return wrapOperationParamsWithMaybeRef(baseParams)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function wrapOperationParamsWithMaybeRef(paramsNode: ast.FunctionParametersNode): ast.FunctionParametersNode {
|
|
58
|
+
const wrappedParams = paramsNode.params.map((param) => {
|
|
59
|
+
if ('kind' in param && (param as ast.ParameterGroupNode).kind === 'ParameterGroup') {
|
|
60
|
+
const group = param as ast.ParameterGroupNode
|
|
61
|
+
return {
|
|
62
|
+
...group,
|
|
63
|
+
properties: group.properties.map((p) => ({
|
|
64
|
+
...p,
|
|
65
|
+
type: p.type ? ast.createParamsType({ variant: 'reference', name: `MaybeRefOrGetter<${printType(p.type)}>` }) : p.type,
|
|
66
|
+
})),
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const fp = param as ast.FunctionParameterNode
|
|
70
|
+
// Don't wrap 'config' param — it's not reactive
|
|
71
|
+
if (fp.name === 'config') return fp
|
|
72
|
+
return {
|
|
73
|
+
...fp,
|
|
74
|
+
type: fp.type ? ast.createParamsType({ variant: 'reference', name: `MaybeRefOrGetter<${printType(fp.type)}>` }) : fp.type,
|
|
59
75
|
}
|
|
76
|
+
})
|
|
77
|
+
return ast.createFunctionParameters({ params: wrappedParams })
|
|
78
|
+
}
|
|
60
79
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
config: {
|
|
71
|
-
type: typeSchemas.request?.name
|
|
72
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
73
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
74
|
-
default: '{}',
|
|
75
|
-
},
|
|
80
|
+
function printType(typeNode: ast.ParamsTypeNode | undefined): string {
|
|
81
|
+
if (!typeNode) return 'unknown'
|
|
82
|
+
if (typeNode.variant === 'reference') return typeNode.name
|
|
83
|
+
if (typeNode.variant === 'member') return `${typeNode.base}['${typeNode.key}']`
|
|
84
|
+
if (typeNode.variant === 'struct') {
|
|
85
|
+
const parts = typeNode.properties.map((p) => {
|
|
86
|
+
const typeStr = printType(p.type)
|
|
87
|
+
const key = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(p.name) ? p.name : JSON.stringify(p.name)
|
|
88
|
+
return p.optional ? `${key}?: ${typeStr}` : `${key}: ${typeStr}`
|
|
76
89
|
})
|
|
90
|
+
return `{ ${parts.join('; ')} }`
|
|
77
91
|
}
|
|
92
|
+
return 'unknown'
|
|
93
|
+
}
|
|
78
94
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
...item,
|
|
88
|
-
type: `MaybeRefOrGetter<${item.type}>`,
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
}),
|
|
92
|
-
default: getDefaultValue(typeSchemas.pathParams?.schema),
|
|
93
|
-
},
|
|
94
|
-
data: typeSchemas.request?.name
|
|
95
|
-
? {
|
|
96
|
-
type: `MaybeRefOrGetter<${typeSchemas.request?.name}>`,
|
|
97
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
98
|
-
}
|
|
99
|
-
: undefined,
|
|
100
|
-
params: typeSchemas.queryParams?.name
|
|
101
|
-
? {
|
|
102
|
-
type: `MaybeRefOrGetter<${typeSchemas.queryParams?.name}>`,
|
|
103
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
95
|
+
export function buildEnabledCheck(paramsNode: ast.FunctionParametersNode): string {
|
|
96
|
+
const required: string[] = []
|
|
97
|
+
for (const param of paramsNode.params) {
|
|
98
|
+
if ('kind' in param && (param as ast.ParameterGroupNode).kind === 'ParameterGroup') {
|
|
99
|
+
const group = param as ast.ParameterGroupNode
|
|
100
|
+
for (const child of group.properties) {
|
|
101
|
+
if (!child.optional && child.default === undefined) {
|
|
102
|
+
required.push(child.name)
|
|
104
103
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
115
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
116
|
-
default: '{}',
|
|
117
|
-
},
|
|
118
|
-
})
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
const fp = param as ast.FunctionParameterNode
|
|
107
|
+
if (!fp.optional && fp.default === undefined) {
|
|
108
|
+
required.push(fp.name)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return required.join(' && ')
|
|
119
113
|
}
|
|
120
114
|
|
|
121
115
|
export function QueryOptions({
|
|
122
116
|
name,
|
|
123
117
|
clientName,
|
|
124
118
|
dataReturnType,
|
|
125
|
-
|
|
119
|
+
node,
|
|
120
|
+
tsResolver,
|
|
126
121
|
paramsCasing,
|
|
127
122
|
paramsType,
|
|
128
123
|
pathParamsType,
|
|
129
124
|
queryKeyName,
|
|
130
|
-
}: Props):
|
|
131
|
-
const
|
|
132
|
-
const
|
|
125
|
+
}: Props): KubbReactNode {
|
|
126
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
127
|
+
const errorNames = resolveErrorNames(node, tsResolver)
|
|
133
128
|
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
paramsType,
|
|
137
|
-
paramsCasing,
|
|
138
|
-
typeSchemas,
|
|
139
|
-
pathParamsType,
|
|
140
|
-
isConfigurable: true,
|
|
141
|
-
})
|
|
142
|
-
const queryKeyParams = QueryKey.getParams({
|
|
143
|
-
pathParamsType,
|
|
144
|
-
typeSchemas,
|
|
145
|
-
paramsCasing,
|
|
146
|
-
})
|
|
129
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
130
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
147
131
|
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
132
|
+
const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
133
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
134
|
+
const rawParamsCall = callPrinter.print(paramsNode) ?? ''
|
|
135
|
+
|
|
136
|
+
// Transform: wrap non-config params with toValue(), add signal to config
|
|
137
|
+
const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
|
|
138
|
+
|
|
139
|
+
const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
140
|
+
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
156
141
|
|
|
157
|
-
const
|
|
142
|
+
const enabledSource = buildEnabledCheck(queryKeyParamsNode)
|
|
143
|
+
const enabledText = enabledSource ? `enabled: () => !!(${enabledSource}),` : ''
|
|
158
144
|
|
|
159
145
|
return (
|
|
160
146
|
<File.Source name={name} isExportable isIndexable>
|
|
161
|
-
<Function name={name} export params={
|
|
147
|
+
<Function name={name} export params={paramsSignature}>
|
|
162
148
|
{`
|
|
163
|
-
const queryKey = ${queryKeyName}(${
|
|
164
|
-
return queryOptions<${TData}, ${TError}, ${TData}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return ${clientName}(${
|
|
169
|
-
transformName(name) {
|
|
170
|
-
if (name === 'config') {
|
|
171
|
-
return '{ ...config, signal: config.signal ?? signal }'
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return `toValue(${name})`
|
|
175
|
-
},
|
|
176
|
-
})})
|
|
149
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
150
|
+
return queryOptions<${TData}, ${TError}, ${TData}>({
|
|
151
|
+
${enabledText}
|
|
152
|
+
queryKey,
|
|
153
|
+
queryFn: async ({ signal }) => {
|
|
154
|
+
return ${clientName}(${addToValueCalls(clientCallStr)})
|
|
177
155
|
},
|
|
178
156
|
})
|
|
179
157
|
`}
|
|
@@ -182,4 +160,34 @@ export function QueryOptions({
|
|
|
182
160
|
)
|
|
183
161
|
}
|
|
184
162
|
|
|
185
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Wraps parameter names with `toValue()` in the client call string,
|
|
165
|
+
* except for 'config'-related params (which are already plain objects).
|
|
166
|
+
*
|
|
167
|
+
* Handles both inline params (`petId, config`) and object shorthand
|
|
168
|
+
* params (`{ petId }, config`) by expanding to `{ petId: toValue(petId) }`.
|
|
169
|
+
*/
|
|
170
|
+
function addToValueCalls(callStr: string): string {
|
|
171
|
+
// Step 1: Transform shorthand object params like { petId } → { petId: toValue(petId) }
|
|
172
|
+
let result = callStr.replace(/\{\s*([\w,\s]+)\s*\}(?=\s*,)/g, (match, inner: string) => {
|
|
173
|
+
// Only transform simple shorthand (no colons, no spread)
|
|
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(${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(${name})`
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
return result
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
QueryOptions.getParams = getQueryOptionsParams
|