@kubb/plugin-client 5.0.0-alpha.3 → 5.0.0-alpha.30
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/clients/axios.d.ts +2 -2
- package/dist/index.cjs +1893 -74
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +480 -4
- package/dist/index.js +1885 -77
- package/dist/index.js.map +1 -1
- package/package.json +10 -25
- package/src/components/ClassClient.tsx +42 -138
- package/src/components/Client.tsx +85 -124
- package/src/components/ClientLegacy.tsx +501 -0
- package/src/components/Operations.tsx +8 -8
- package/src/components/StaticClassClient.tsx +41 -135
- package/src/components/Url.tsx +37 -46
- package/src/generators/classClientGenerator.tsx +129 -152
- package/src/generators/clientGenerator.tsx +93 -82
- package/src/generators/groupedClientGenerator.tsx +48 -51
- package/src/generators/operationsGenerator.tsx +9 -17
- package/src/generators/staticClassClientGenerator.tsx +163 -168
- package/src/index.ts +11 -1
- package/src/plugin.ts +115 -108
- package/src/presets.ts +25 -0
- package/src/resolvers/resolverClient.ts +26 -0
- package/src/resolvers/resolverClientLegacy.ts +26 -0
- package/src/types.ts +105 -40
- package/src/utils.ts +148 -0
- package/dist/StaticClassClient-By-aMAe4.cjs +0 -677
- package/dist/StaticClassClient-By-aMAe4.cjs.map +0 -1
- package/dist/StaticClassClient-CCn9g9eF.js +0 -636
- package/dist/StaticClassClient-CCn9g9eF.js.map +0 -1
- package/dist/components.cjs +0 -7
- package/dist/components.d.ts +0 -216
- package/dist/components.js +0 -2
- package/dist/generators-C2jT7XCH.js +0 -723
- package/dist/generators-C2jT7XCH.js.map +0 -1
- package/dist/generators-qkDW17Hf.cjs +0 -753
- package/dist/generators-qkDW17Hf.cjs.map +0 -1
- package/dist/generators.cjs +0 -7
- package/dist/generators.d.ts +0 -512
- package/dist/generators.js +0 -2
- package/dist/types-CdM4DK1M.d.ts +0 -169
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -5
package/src/utils.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { URLPath } from '@internals/utils'
|
|
2
|
+
import type { OperationNode, ParameterNode } from '@kubb/ast/types'
|
|
3
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
4
|
+
import type { PluginZod } from '@kubb/plugin-zod'
|
|
5
|
+
import { FunctionParams } from '@kubb/react-fabric'
|
|
6
|
+
import type { PluginClient } from './types.ts'
|
|
7
|
+
|
|
8
|
+
export function getComments(node: OperationNode): Array<string> {
|
|
9
|
+
return [
|
|
10
|
+
node.description && `@description ${node.description}`,
|
|
11
|
+
node.summary && `@summary ${node.summary}`,
|
|
12
|
+
node.path && `{@link ${new URLPath(node.path).URL}}`,
|
|
13
|
+
node.deprecated && '@deprecated',
|
|
14
|
+
]
|
|
15
|
+
.filter((x): x is string => Boolean(x))
|
|
16
|
+
.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim()))
|
|
17
|
+
.filter((x): x is string => Boolean(x))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function buildParamsMapping(originalParams: Array<ParameterNode>, casedParams: Array<ParameterNode>): Record<string, string> | undefined {
|
|
21
|
+
const mapping: Record<string, string> = {}
|
|
22
|
+
let hasChanged = false
|
|
23
|
+
originalParams.forEach((param, i) => {
|
|
24
|
+
const casedName = casedParams[i]?.name ?? param.name
|
|
25
|
+
mapping[param.name] = casedName
|
|
26
|
+
if (param.name !== casedName) {
|
|
27
|
+
hasChanged = true
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
return hasChanged ? mapping : undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function buildHeaders(contentType: string, hasHeaderParams: boolean): Array<string> {
|
|
34
|
+
return [
|
|
35
|
+
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined,
|
|
36
|
+
hasHeaderParams ? '...headers' : undefined,
|
|
37
|
+
].filter(Boolean) as Array<string>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function buildGenerics(node: OperationNode, tsResolver: PluginTs['resolver']): Array<string> {
|
|
41
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
42
|
+
const requestName = node.requestBody?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
43
|
+
const errorNames = node.responses.filter((r) => Number.parseInt(r.statusCode, 10) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
44
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
45
|
+
return [responseName, TError, requestName || 'unknown'].filter(Boolean)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildClassClientParams({
|
|
49
|
+
node,
|
|
50
|
+
path,
|
|
51
|
+
baseURL,
|
|
52
|
+
tsResolver,
|
|
53
|
+
isFormData,
|
|
54
|
+
headers,
|
|
55
|
+
}: {
|
|
56
|
+
node: OperationNode
|
|
57
|
+
path: URLPath
|
|
58
|
+
baseURL: string | undefined
|
|
59
|
+
tsResolver: PluginTs['resolver']
|
|
60
|
+
isFormData: boolean
|
|
61
|
+
headers: Array<string>
|
|
62
|
+
}) {
|
|
63
|
+
const queryParamsName =
|
|
64
|
+
node.parameters.filter((p) => p.in === 'query').length > 0
|
|
65
|
+
? tsResolver.resolveQueryParamsName(node, node.parameters.filter((p) => p.in === 'query')[0]!)
|
|
66
|
+
: undefined
|
|
67
|
+
const requestName = node.requestBody?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
68
|
+
|
|
69
|
+
return FunctionParams.factory({
|
|
70
|
+
config: {
|
|
71
|
+
mode: 'object',
|
|
72
|
+
children: {
|
|
73
|
+
requestConfig: {
|
|
74
|
+
mode: 'inlineSpread',
|
|
75
|
+
},
|
|
76
|
+
method: {
|
|
77
|
+
value: JSON.stringify(node.method.toUpperCase()),
|
|
78
|
+
},
|
|
79
|
+
url: {
|
|
80
|
+
value: path.template,
|
|
81
|
+
},
|
|
82
|
+
baseURL: baseURL
|
|
83
|
+
? {
|
|
84
|
+
value: JSON.stringify(baseURL),
|
|
85
|
+
}
|
|
86
|
+
: undefined,
|
|
87
|
+
params: queryParamsName ? {} : undefined,
|
|
88
|
+
data: requestName
|
|
89
|
+
? {
|
|
90
|
+
value: isFormData ? 'formData as FormData' : 'requestData',
|
|
91
|
+
}
|
|
92
|
+
: undefined,
|
|
93
|
+
headers: headers.length
|
|
94
|
+
? {
|
|
95
|
+
value: `{ ${headers.join(', ')}, ...requestConfig.headers }`,
|
|
96
|
+
}
|
|
97
|
+
: undefined,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function buildRequestDataLine({
|
|
104
|
+
parser,
|
|
105
|
+
node,
|
|
106
|
+
zodResolver,
|
|
107
|
+
}: {
|
|
108
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
109
|
+
node: OperationNode
|
|
110
|
+
zodResolver?: PluginZod['resolver']
|
|
111
|
+
}): string {
|
|
112
|
+
const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.schema ? zodResolver.resolveDataName?.(node) : undefined
|
|
113
|
+
if (parser === 'zod' && zodRequestName) {
|
|
114
|
+
return `const requestData = ${zodRequestName}.parse(data)`
|
|
115
|
+
}
|
|
116
|
+
if (node.requestBody?.schema) {
|
|
117
|
+
return 'const requestData = data'
|
|
118
|
+
}
|
|
119
|
+
return ''
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function buildFormDataLine(isFormData: boolean, hasRequest: boolean): string {
|
|
123
|
+
return isFormData && hasRequest ? 'const formData = buildFormData(requestData)' : ''
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function buildReturnStatement({
|
|
127
|
+
dataReturnType,
|
|
128
|
+
parser,
|
|
129
|
+
node,
|
|
130
|
+
zodResolver,
|
|
131
|
+
}: {
|
|
132
|
+
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
133
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
134
|
+
node: OperationNode
|
|
135
|
+
zodResolver?: PluginZod['resolver']
|
|
136
|
+
}): string {
|
|
137
|
+
const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : undefined
|
|
138
|
+
if (dataReturnType === 'full' && parser === 'zod' && zodResponseName) {
|
|
139
|
+
return `return {...res, data: ${zodResponseName}.parse(res.data)}`
|
|
140
|
+
}
|
|
141
|
+
if (dataReturnType === 'data' && parser === 'zod' && zodResponseName) {
|
|
142
|
+
return `return ${zodResponseName}.parse(res.data)`
|
|
143
|
+
}
|
|
144
|
+
if (dataReturnType === 'full' && parser === 'client') {
|
|
145
|
+
return 'return res'
|
|
146
|
+
}
|
|
147
|
+
return 'return res.data'
|
|
148
|
+
}
|