@kubb/plugin-client 5.0.0-alpha.8 → 5.0.0-beta.10
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 +27 -7
- package/dist/clients/axios.cjs +10 -3
- package/dist/clients/axios.cjs.map +1 -1
- package/dist/clients/axios.d.ts +5 -4
- package/dist/clients/axios.js +9 -2
- package/dist/clients/axios.js.map +1 -1
- package/dist/clients/fetch.cjs +5 -2
- package/dist/clients/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.ts +3 -2
- package/dist/clients/fetch.js +5 -2
- package/dist/clients/fetch.js.map +1 -1
- package/dist/index.cjs +1818 -97
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +330 -4
- package/dist/index.js +1804 -95
- package/dist/index.js.map +1 -1
- package/dist/templates/clients/axios.source.cjs +1 -1
- package/dist/templates/clients/axios.source.js +1 -1
- package/dist/templates/clients/fetch.source.cjs +1 -1
- package/dist/templates/clients/fetch.source.js +1 -1
- package/extension.yaml +776 -0
- package/package.json +62 -85
- package/src/clients/axios.ts +19 -4
- package/src/clients/fetch.ts +10 -2
- package/src/components/ClassClient.tsx +46 -145
- package/src/components/Client.tsx +109 -139
- package/src/components/Operations.tsx +10 -10
- package/src/components/StaticClassClient.tsx +46 -142
- package/src/components/Url.tsx +38 -50
- package/src/components/WrapperClient.tsx +11 -7
- package/src/functionParams.ts +118 -0
- package/src/generators/classClientGenerator.tsx +143 -172
- package/src/generators/clientGenerator.tsx +83 -81
- package/src/generators/groupedClientGenerator.tsx +50 -52
- package/src/generators/operationsGenerator.tsx +11 -18
- package/src/generators/staticClassClientGenerator.tsx +172 -184
- package/src/index.ts +9 -2
- package/src/plugin.ts +115 -145
- package/src/resolvers/resolverClient.ts +38 -0
- package/src/types.ts +128 -44
- package/src/utils.ts +156 -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-BYUJaeZP.js +0 -723
- package/dist/generators-BYUJaeZP.js.map +0 -1
- package/dist/generators-DTxD9FDY.cjs +0 -753
- package/dist/generators-DTxD9FDY.cjs.map +0 -1
- package/dist/generators.cjs +0 -7
- package/dist/generators.d.ts +0 -488
- package/dist/generators.js +0 -2
- package/dist/types-DBQdg-BV.d.ts +0 -169
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -5
- package/templates/clients/axios.ts +0 -70
- package/templates/clients/fetch.ts +0 -93
- package/templates/config.ts +0 -43
package/src/utils.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { getOperationParameters } from '@internals/shared'
|
|
2
|
+
import type { URLPath } from '@internals/utils'
|
|
3
|
+
import type { ast } from '@kubb/core'
|
|
4
|
+
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
5
|
+
import type { ResolverZod } from '@kubb/plugin-zod'
|
|
6
|
+
import { createFunctionParams } from './functionParams.ts'
|
|
7
|
+
import type { PluginClient } from './types.ts'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Builds HTTP headers array for a client request.
|
|
11
|
+
* Includes Content-Type (if not default) and spreads header parameters if present.
|
|
12
|
+
*/
|
|
13
|
+
export function buildHeaders(contentType: string, hasHeaderParams: boolean): Array<string> {
|
|
14
|
+
return [
|
|
15
|
+
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined,
|
|
16
|
+
hasHeaderParams ? '...headers' : undefined,
|
|
17
|
+
].filter(Boolean) as Array<string>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Builds TypeScript generic parameters for a client method.
|
|
22
|
+
* Includes response type, error type, and optional request type.
|
|
23
|
+
*/
|
|
24
|
+
export function buildGenerics(node: ast.OperationNode, tsResolver: ResolverTs): Array<string> {
|
|
25
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
26
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
27
|
+
const errorNames = node.responses.filter((r) => Number.parseInt(r.statusCode, 10) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
28
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
29
|
+
return [responseName, TError, requestName || 'unknown'].filter(Boolean)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds the parameters object for a class-based client method.
|
|
34
|
+
* Includes URL, method, base URL, headers, and request/response data.
|
|
35
|
+
*/
|
|
36
|
+
export function buildClassClientParams({
|
|
37
|
+
node,
|
|
38
|
+
path,
|
|
39
|
+
baseURL,
|
|
40
|
+
tsResolver,
|
|
41
|
+
isFormData,
|
|
42
|
+
isMultipleContentTypes,
|
|
43
|
+
hasFormData,
|
|
44
|
+
headers,
|
|
45
|
+
}: {
|
|
46
|
+
node: ast.OperationNode
|
|
47
|
+
path: URLPath
|
|
48
|
+
baseURL: string | undefined
|
|
49
|
+
tsResolver: ResolverTs
|
|
50
|
+
isFormData: boolean
|
|
51
|
+
isMultipleContentTypes: boolean
|
|
52
|
+
hasFormData: boolean
|
|
53
|
+
headers: Array<string>
|
|
54
|
+
}) {
|
|
55
|
+
const { query: queryParams } = getOperationParameters(node)
|
|
56
|
+
const queryParamsName = queryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, queryParams[0]!) : undefined
|
|
57
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
58
|
+
|
|
59
|
+
return createFunctionParams({
|
|
60
|
+
config: {
|
|
61
|
+
mode: 'object',
|
|
62
|
+
children: {
|
|
63
|
+
requestConfig: {
|
|
64
|
+
mode: 'inlineSpread',
|
|
65
|
+
},
|
|
66
|
+
method: {
|
|
67
|
+
value: JSON.stringify(node.method.toUpperCase()),
|
|
68
|
+
},
|
|
69
|
+
url: {
|
|
70
|
+
value: path.template,
|
|
71
|
+
},
|
|
72
|
+
baseURL: baseURL
|
|
73
|
+
? {
|
|
74
|
+
value: JSON.stringify(baseURL),
|
|
75
|
+
}
|
|
76
|
+
: undefined,
|
|
77
|
+
params: queryParamsName ? {} : undefined,
|
|
78
|
+
data: requestName
|
|
79
|
+
? {
|
|
80
|
+
value:
|
|
81
|
+
isMultipleContentTypes && hasFormData
|
|
82
|
+
? "contentType === 'multipart/form-data' ? formData as FormData : requestData"
|
|
83
|
+
: isFormData
|
|
84
|
+
? 'formData as FormData'
|
|
85
|
+
: 'requestData',
|
|
86
|
+
}
|
|
87
|
+
: undefined,
|
|
88
|
+
contentType: isMultipleContentTypes ? {} : undefined,
|
|
89
|
+
headers: headers.length
|
|
90
|
+
? {
|
|
91
|
+
value: `{ ${headers.join(', ')}, ...requestConfig.headers }`,
|
|
92
|
+
}
|
|
93
|
+
: undefined,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Builds the request data parsing line for client methods.
|
|
101
|
+
* Applies Zod validation if configured, otherwise uses data directly.
|
|
102
|
+
*/
|
|
103
|
+
export function buildRequestDataLine({
|
|
104
|
+
parser,
|
|
105
|
+
node,
|
|
106
|
+
zodResolver,
|
|
107
|
+
}: {
|
|
108
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
109
|
+
node: ast.OperationNode
|
|
110
|
+
zodResolver?: ResolverZod
|
|
111
|
+
}): string {
|
|
112
|
+
const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : undefined
|
|
113
|
+
if (parser === 'zod' && zodRequestName) {
|
|
114
|
+
return `const requestData = ${zodRequestName}.parse(data)`
|
|
115
|
+
}
|
|
116
|
+
if (node.requestBody?.content?.[0]?.schema) {
|
|
117
|
+
return 'const requestData = data'
|
|
118
|
+
}
|
|
119
|
+
return ''
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Builds the form data conversion line for file upload requests.
|
|
124
|
+
* Returns empty string if not applicable.
|
|
125
|
+
*/
|
|
126
|
+
export function buildFormDataLine(isFormData: boolean, hasRequest: boolean): string {
|
|
127
|
+
return isFormData && hasRequest ? 'const formData = buildFormData(requestData)' : ''
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Builds the return statement for a client method.
|
|
132
|
+
* Applies Zod validation to response data if configured, otherwise returns raw response.
|
|
133
|
+
*/
|
|
134
|
+
export function buildReturnStatement({
|
|
135
|
+
dataReturnType,
|
|
136
|
+
parser,
|
|
137
|
+
node,
|
|
138
|
+
zodResolver,
|
|
139
|
+
}: {
|
|
140
|
+
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
141
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
142
|
+
node: ast.OperationNode
|
|
143
|
+
zodResolver?: ResolverZod
|
|
144
|
+
}): string {
|
|
145
|
+
const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : undefined
|
|
146
|
+
if (dataReturnType === 'full' && parser === 'zod' && zodResponseName) {
|
|
147
|
+
return `return {...res, data: ${zodResponseName}.parse(res.data)}`
|
|
148
|
+
}
|
|
149
|
+
if (dataReturnType === 'data' && parser === 'zod' && zodResponseName) {
|
|
150
|
+
return `return ${zodResponseName}.parse(res.data)`
|
|
151
|
+
}
|
|
152
|
+
if (dataReturnType === 'full' && parser === 'client') {
|
|
153
|
+
return 'return res'
|
|
154
|
+
}
|
|
155
|
+
return 'return res.data'
|
|
156
|
+
}
|