@kubb/plugin-swr 5.0.0-alpha.34 → 5.0.0-alpha.35
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/dist/components-BJSzUg7M.cjs +955 -0
- package/dist/components-BJSzUg7M.cjs.map +1 -0
- package/dist/components-JQ2KRFCa.js +877 -0
- package/dist/components-JQ2KRFCa.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +77 -37
- package/dist/components.js +1 -1
- package/dist/generators-17ulS9mu.cjs +537 -0
- package/dist/generators-17ulS9mu.cjs.map +1 -0
- package/dist/generators-Cl7nr-FB.js +526 -0
- package/dist/generators-Cl7nr-FB.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -4
- package/dist/generators.js +1 -1
- package/dist/index.cjs +132 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +22 -2
- package/dist/index.js +132 -110
- package/dist/index.js.map +1 -1
- package/dist/{types-BVDtH9S7.d.ts → types-FA5mH9Ch.d.ts} +46 -90
- package/package.json +7 -11
- package/src/components/Mutation.tsx +165 -170
- package/src/components/MutationKey.tsx +50 -1
- package/src/components/Query.tsx +122 -126
- package/src/components/QueryKey.tsx +65 -1
- package/src/components/QueryOptions.tsx +38 -93
- package/src/generators/mutationGenerator.tsx +194 -117
- package/src/generators/queryGenerator.tsx +205 -139
- package/src/plugin.ts +117 -152
- package/src/resolvers/resolverSwr.ts +26 -0
- package/src/resolvers/resolverSwrLegacy.ts +17 -0
- package/src/types.ts +55 -18
- package/src/utils.ts +209 -0
- package/dist/components-DaCTPplv.js +0 -756
- package/dist/components-DaCTPplv.js.map +0 -1
- package/dist/components-Qs8_faOt.cjs +0 -834
- package/dist/components-Qs8_faOt.cjs.map +0 -1
- package/dist/generators-0YayIrse.js +0 -400
- package/dist/generators-0YayIrse.js.map +0 -1
- package/dist/generators-Bd4rCa3l.cjs +0 -411
- package/dist/generators-Bd4rCa3l.cjs.map +0 -1
package/src/utils.ts
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
3
|
+
import type { PluginSwr } from './types.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Apply the user-provided `transformers.name` function (if any) to a resolved name.
|
|
7
|
+
* Mirrors the old `createPlugin` `resolveName` lifecycle that applied transformers
|
|
8
|
+
* after resolving the full name (base + suffix).
|
|
9
|
+
*/
|
|
10
|
+
export function transformName(name: string, type: string, transformers?: PluginSwr['resolvedOptions']['transformers']): string {
|
|
11
|
+
return transformers?.name?.(name, type) || name
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Build JSDoc comment lines from an OperationNode.
|
|
16
|
+
*/
|
|
17
|
+
export function getComments(node: ast.OperationNode): Array<string> {
|
|
18
|
+
return [
|
|
19
|
+
node.description && `@description ${node.description}`,
|
|
20
|
+
node.summary && `@summary ${node.summary}`,
|
|
21
|
+
node.deprecated && '@deprecated',
|
|
22
|
+
`{@link ${node.path.replaceAll('{', ':').replaceAll('}', '')}}`,
|
|
23
|
+
].filter((x): x is string => Boolean(x))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolve error type names from operation responses.
|
|
28
|
+
*/
|
|
29
|
+
export function resolveErrorNames(node: ast.OperationNode, tsResolver: PluginTs['resolver']): string[] {
|
|
30
|
+
return node.responses
|
|
31
|
+
.filter((r) => {
|
|
32
|
+
const code = Number.parseInt(r.statusCode, 10)
|
|
33
|
+
return code >= 400 || r.statusCode === 'default'
|
|
34
|
+
})
|
|
35
|
+
.map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolve all status code type names from operation responses (for imports).
|
|
40
|
+
*/
|
|
41
|
+
export function resolveStatusCodeNames(node: ast.OperationNode, tsResolver: PluginTs['resolver']): string[] {
|
|
42
|
+
return node.responses.map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Resolve the type for a single path parameter.
|
|
47
|
+
*
|
|
48
|
+
* - When the resolver's group name differs from the individual param name
|
|
49
|
+
* (e.g. kubbV4) → `GroupName['paramName']` (member access).
|
|
50
|
+
* - When they match (v5 default) → `TypeName` (direct reference).
|
|
51
|
+
*/
|
|
52
|
+
export function resolvePathParamType(node: ast.OperationNode, param: ast.ParameterNode, resolver: PluginTs['resolver']): ast.ParamsTypeNode {
|
|
53
|
+
const individualName = resolver.resolveParamName(node, param)
|
|
54
|
+
const groupName = resolver.resolvePathParamsName(node, param)
|
|
55
|
+
|
|
56
|
+
if (groupName !== individualName) {
|
|
57
|
+
return ast.createParamsType({ variant: 'member', base: groupName, key: param.name })
|
|
58
|
+
}
|
|
59
|
+
return ast.createParamsType({ variant: 'reference', name: individualName })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type QueryGroupResult = { type: ast.ParamsTypeNode; optional: boolean } | undefined
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Derive a query-params group type from the resolver.
|
|
66
|
+
* Returns `undefined` when no query params exist or when the group name
|
|
67
|
+
* equals the individual param name (no real group).
|
|
68
|
+
*/
|
|
69
|
+
export function resolveQueryGroupType(node: ast.OperationNode, params: ast.ParameterNode[], resolver: PluginTs['resolver']): QueryGroupResult {
|
|
70
|
+
if (!params.length) return undefined
|
|
71
|
+
const firstParam = params[0]!
|
|
72
|
+
const groupName = resolver.resolveQueryParamsName(node, firstParam)
|
|
73
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) return undefined
|
|
74
|
+
return { type: ast.createParamsType({ variant: 'reference', name: groupName }), optional: params.every((p) => !p.required) }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Derive a header-params group type from the resolver.
|
|
79
|
+
*/
|
|
80
|
+
export function resolveHeaderGroupType(node: ast.OperationNode, params: ast.ParameterNode[], resolver: PluginTs['resolver']): QueryGroupResult {
|
|
81
|
+
if (!params.length) return undefined
|
|
82
|
+
const firstParam = params[0]!
|
|
83
|
+
const groupName = resolver.resolveHeaderParamsName(node, firstParam)
|
|
84
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) return undefined
|
|
85
|
+
return { type: ast.createParamsType({ variant: 'reference', name: groupName }), optional: params.every((p) => !p.required) }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Build a single `FunctionParameterNode` for a query or header group.
|
|
90
|
+
*/
|
|
91
|
+
export function buildGroupParam(
|
|
92
|
+
name: string,
|
|
93
|
+
node: ast.OperationNode,
|
|
94
|
+
params: ast.ParameterNode[],
|
|
95
|
+
groupType: QueryGroupResult,
|
|
96
|
+
resolver: PluginTs['resolver'],
|
|
97
|
+
): ast.FunctionParameterNode[] {
|
|
98
|
+
if (groupType) {
|
|
99
|
+
return [ast.createFunctionParameter({ name, type: groupType.type, optional: groupType.optional })]
|
|
100
|
+
}
|
|
101
|
+
if (params.length) {
|
|
102
|
+
const structProps = params.map((p) => ({
|
|
103
|
+
name: p.name,
|
|
104
|
+
type: ast.createParamsType({ variant: 'reference', name: resolver.resolveParamName(node, p) }),
|
|
105
|
+
optional: !p.required,
|
|
106
|
+
}))
|
|
107
|
+
return [
|
|
108
|
+
ast.createFunctionParameter({
|
|
109
|
+
name,
|
|
110
|
+
type: ast.createParamsType({ variant: 'struct', properties: structProps }),
|
|
111
|
+
optional: params.every((p) => !p.required),
|
|
112
|
+
}),
|
|
113
|
+
]
|
|
114
|
+
}
|
|
115
|
+
return []
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Build QueryKey params: pathParams + data + queryParams (NO headers, NO config).
|
|
120
|
+
*/
|
|
121
|
+
export function buildQueryKeyParams(
|
|
122
|
+
node: ast.OperationNode,
|
|
123
|
+
options: {
|
|
124
|
+
pathParamsType: 'object' | 'inline'
|
|
125
|
+
paramsCasing: 'camelcase' | undefined
|
|
126
|
+
resolver: PluginTs['resolver']
|
|
127
|
+
},
|
|
128
|
+
): ast.FunctionParametersNode {
|
|
129
|
+
const { pathParamsType, paramsCasing, resolver } = options
|
|
130
|
+
|
|
131
|
+
const casedParams = ast.caseParams(node.parameters, paramsCasing)
|
|
132
|
+
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
133
|
+
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
134
|
+
|
|
135
|
+
const queryGroupType = resolveQueryGroupType(node, queryParams, resolver)
|
|
136
|
+
|
|
137
|
+
const bodyType = node.requestBody?.schema ? ast.createParamsType({ variant: 'reference', name: resolver.resolveDataName(node) }) : undefined
|
|
138
|
+
const bodyRequired = node.requestBody?.required ?? false
|
|
139
|
+
|
|
140
|
+
const params: Array<ast.FunctionParameterNode | ast.ParameterGroupNode> = []
|
|
141
|
+
|
|
142
|
+
// Path params
|
|
143
|
+
if (pathParams.length) {
|
|
144
|
+
const pathChildren = pathParams.map((p) =>
|
|
145
|
+
ast.createFunctionParameter({ name: p.name, type: resolvePathParamType(node, p, resolver), optional: !p.required }),
|
|
146
|
+
)
|
|
147
|
+
params.push({
|
|
148
|
+
kind: 'ParameterGroup',
|
|
149
|
+
properties: pathChildren,
|
|
150
|
+
inline: pathParamsType === 'inline',
|
|
151
|
+
default: pathChildren.every((c) => c.optional) ? '{}' : undefined,
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Request body
|
|
156
|
+
if (bodyType) {
|
|
157
|
+
params.push(ast.createFunctionParameter({ name: 'data', type: bodyType, optional: !bodyRequired }))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Query params
|
|
161
|
+
params.push(...buildGroupParam('params', node, queryParams, queryGroupType, resolver))
|
|
162
|
+
|
|
163
|
+
return ast.createFunctionParameters({ params })
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Build mutation arg params for paramsToTrigger mode.
|
|
168
|
+
* Contains pathParams + data + queryParams + headers (all flattened, for type alias).
|
|
169
|
+
*/
|
|
170
|
+
export function buildMutationArgParams(
|
|
171
|
+
node: ast.OperationNode,
|
|
172
|
+
options: {
|
|
173
|
+
paramsCasing: 'camelcase' | undefined
|
|
174
|
+
resolver: PluginTs['resolver']
|
|
175
|
+
},
|
|
176
|
+
): ast.FunctionParametersNode {
|
|
177
|
+
const { paramsCasing, resolver } = options
|
|
178
|
+
|
|
179
|
+
const casedParams = ast.caseParams(node.parameters, paramsCasing)
|
|
180
|
+
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
181
|
+
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
182
|
+
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
183
|
+
|
|
184
|
+
const queryGroupType = resolveQueryGroupType(node, queryParams, resolver)
|
|
185
|
+
const headerGroupType = resolveHeaderGroupType(node, headerParams, resolver)
|
|
186
|
+
|
|
187
|
+
const bodyType = node.requestBody?.schema ? ast.createParamsType({ variant: 'reference', name: resolver.resolveDataName(node) }) : undefined
|
|
188
|
+
const bodyRequired = node.requestBody?.required ?? false
|
|
189
|
+
|
|
190
|
+
const params: Array<ast.FunctionParameterNode | ast.ParameterGroupNode> = []
|
|
191
|
+
|
|
192
|
+
// Path params (individual entries)
|
|
193
|
+
for (const p of pathParams) {
|
|
194
|
+
params.push(ast.createFunctionParameter({ name: p.name, type: resolvePathParamType(node, p, resolver), optional: !p.required }))
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Request body
|
|
198
|
+
if (bodyType) {
|
|
199
|
+
params.push(ast.createFunctionParameter({ name: 'data', type: bodyType, optional: !bodyRequired }))
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Query params
|
|
203
|
+
params.push(...buildGroupParam('params', node, queryParams, queryGroupType, resolver))
|
|
204
|
+
|
|
205
|
+
// Header params
|
|
206
|
+
params.push(...buildGroupParam('headers', node, headerParams, headerGroupType, resolver))
|
|
207
|
+
|
|
208
|
+
return ast.createFunctionParameters({ params })
|
|
209
|
+
}
|