@kubb/plugin-client 5.0.0-alpha.27 → 5.0.0-alpha.29
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/index.cjs +1911 -62
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +468 -2
- package/dist/index.js +1903 -65
- package/dist/index.js.map +1 -1
- package/package.json +5 -20
- package/src/components/ClassClient.tsx +42 -138
- package/src/components/Client.tsx +85 -125
- 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 +121 -131
- package/src/generators/clientGenerator.tsx +104 -80
- package/src/generators/groupedClientGenerator.tsx +28 -30
- package/src/generators/operationsGenerator.tsx +11 -17
- package/src/generators/staticClassClientGenerator.tsx +115 -121
- package/src/index.ts +11 -1
- package/src/plugin.ts +121 -92
- package/src/presets.ts +25 -0
- package/src/resolvers/resolverClient.ts +26 -0
- package/src/resolvers/resolverClientLegacy.ts +26 -0
- package/src/types.ts +93 -39
- package/src/utils.ts +148 -0
- package/dist/StaticClassClient-D6v3vhZL.js +0 -695
- package/dist/StaticClassClient-D6v3vhZL.js.map +0 -1
- package/dist/StaticClassClient-GyNiWMHA.cjs +0 -736
- package/dist/StaticClassClient-GyNiWMHA.cjs.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-C0t5dIvZ.js +0 -723
- package/dist/generators-C0t5dIvZ.js.map +0 -1
- package/dist/generators-D8A8QE4S.cjs +0 -753
- package/dist/generators-D8A8QE4S.cjs.map +0 -1
- package/dist/generators.cjs +0 -7
- package/dist/generators.d.ts +0 -21
- package/dist/generators.js +0 -2
- package/dist/types-jdcuAELq.d.ts +0 -169
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -5
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
import { buildJSDoc, URLPath } from '@internals/utils'
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import type { OperationNode } from '@kubb/ast/types'
|
|
3
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
4
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
5
|
+
import type { PluginZod } from '@kubb/plugin-zod'
|
|
6
|
+
import { File } from '@kubb/react-fabric'
|
|
6
7
|
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
7
8
|
import type { PluginClient } from '../types.ts'
|
|
9
|
+
import { buildClassClientParams, buildFormDataLine, buildGenerics, buildHeaders, buildRequestDataLine, buildReturnStatement, getComments } from '../utils.ts'
|
|
8
10
|
|
|
9
11
|
import { Client } from './Client.tsx'
|
|
10
12
|
|
|
13
|
+
type OperationData = {
|
|
14
|
+
node: OperationNode
|
|
15
|
+
name: string
|
|
16
|
+
tsResolver: PluginTs['resolver']
|
|
17
|
+
zodResolver?: PluginZod['resolver']
|
|
18
|
+
}
|
|
19
|
+
|
|
11
20
|
type Props = {
|
|
12
|
-
/**
|
|
13
|
-
* Name of the class
|
|
14
|
-
*/
|
|
15
21
|
name: string
|
|
16
22
|
isExportable?: boolean
|
|
17
23
|
isIndexable?: boolean
|
|
18
|
-
operations: Array<
|
|
19
|
-
operation: Operation
|
|
20
|
-
name: string
|
|
21
|
-
typeSchemas: OperationSchemas
|
|
22
|
-
zodSchemas: OperationSchemas | undefined
|
|
23
|
-
}>
|
|
24
|
+
operations: Array<OperationData>
|
|
24
25
|
baseURL: string | undefined
|
|
25
26
|
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
26
27
|
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
@@ -31,10 +32,10 @@ type Props = {
|
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
type GenerateMethodProps = {
|
|
34
|
-
|
|
35
|
+
node: OperationNode
|
|
35
36
|
name: string
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
tsResolver: PluginTs['resolver']
|
|
38
|
+
zodResolver?: PluginZod['resolver']
|
|
38
39
|
baseURL: string | undefined
|
|
39
40
|
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
40
41
|
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
@@ -43,115 +44,13 @@ type GenerateMethodProps = {
|
|
|
43
44
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
return [
|
|
48
|
-
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined,
|
|
49
|
-
hasHeaderParams ? '...headers' : undefined,
|
|
50
|
-
].filter(Boolean) as Array<string>
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function buildGenerics(typeSchemas: OperationSchemas): Array<string> {
|
|
54
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
55
|
-
return [typeSchemas.response.name, TError, typeSchemas.request?.name || 'unknown'].filter(Boolean)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function buildClientParams({
|
|
59
|
-
operation,
|
|
60
|
-
path,
|
|
61
|
-
baseURL,
|
|
62
|
-
typeSchemas,
|
|
63
|
-
isFormData,
|
|
64
|
-
headers,
|
|
65
|
-
}: {
|
|
66
|
-
operation: Operation
|
|
67
|
-
path: URLPath
|
|
68
|
-
baseURL: string | undefined
|
|
69
|
-
typeSchemas: OperationSchemas
|
|
70
|
-
isFormData: boolean
|
|
71
|
-
headers: Array<string>
|
|
72
|
-
}) {
|
|
73
|
-
return FunctionParams.factory({
|
|
74
|
-
config: {
|
|
75
|
-
mode: 'object',
|
|
76
|
-
children: {
|
|
77
|
-
requestConfig: {
|
|
78
|
-
mode: 'inlineSpread',
|
|
79
|
-
},
|
|
80
|
-
method: {
|
|
81
|
-
value: JSON.stringify(operation.method.toUpperCase()),
|
|
82
|
-
},
|
|
83
|
-
url: {
|
|
84
|
-
value: path.template,
|
|
85
|
-
},
|
|
86
|
-
baseURL: baseURL
|
|
87
|
-
? {
|
|
88
|
-
value: JSON.stringify(baseURL),
|
|
89
|
-
}
|
|
90
|
-
: undefined,
|
|
91
|
-
params: typeSchemas.queryParams?.name ? {} : undefined,
|
|
92
|
-
data: typeSchemas.request?.name
|
|
93
|
-
? {
|
|
94
|
-
value: isFormData ? 'formData as FormData' : 'requestData',
|
|
95
|
-
}
|
|
96
|
-
: undefined,
|
|
97
|
-
headers: headers.length
|
|
98
|
-
? {
|
|
99
|
-
value: `{ ${headers.join(', ')}, ...requestConfig.headers }`,
|
|
100
|
-
}
|
|
101
|
-
: undefined,
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
})
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function buildRequestDataLine({
|
|
108
|
-
parser,
|
|
109
|
-
zodSchemas,
|
|
110
|
-
typeSchemas,
|
|
111
|
-
}: {
|
|
112
|
-
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
113
|
-
zodSchemas: OperationSchemas | undefined
|
|
114
|
-
typeSchemas: OperationSchemas
|
|
115
|
-
}): string {
|
|
116
|
-
if (parser === 'zod' && zodSchemas?.request?.name) {
|
|
117
|
-
return `const requestData = ${zodSchemas.request.name}.parse(data)`
|
|
118
|
-
}
|
|
119
|
-
if (typeSchemas?.request?.name) {
|
|
120
|
-
return 'const requestData = data'
|
|
121
|
-
}
|
|
122
|
-
return ''
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
function buildFormDataLine(isFormData: boolean, hasRequest: boolean): string {
|
|
126
|
-
return isFormData && hasRequest ? 'const formData = buildFormData(requestData)' : ''
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function buildReturnStatement({
|
|
130
|
-
dataReturnType,
|
|
131
|
-
parser,
|
|
132
|
-
zodSchemas,
|
|
133
|
-
}: {
|
|
134
|
-
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
135
|
-
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
136
|
-
zodSchemas: OperationSchemas | undefined
|
|
137
|
-
}): string {
|
|
138
|
-
if (dataReturnType === 'full' && parser === 'zod' && zodSchemas) {
|
|
139
|
-
return `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`
|
|
140
|
-
}
|
|
141
|
-
if (dataReturnType === 'data' && parser === 'zod' && zodSchemas) {
|
|
142
|
-
return `return ${zodSchemas.response.name}.parse(res.data)`
|
|
143
|
-
}
|
|
144
|
-
if (dataReturnType === 'full' && parser === 'client') {
|
|
145
|
-
return 'return res'
|
|
146
|
-
}
|
|
147
|
-
return 'return res.data'
|
|
148
|
-
}
|
|
47
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
149
48
|
|
|
150
49
|
function generateMethod({
|
|
151
|
-
|
|
50
|
+
node,
|
|
152
51
|
name,
|
|
153
|
-
|
|
154
|
-
|
|
52
|
+
tsResolver,
|
|
53
|
+
zodResolver,
|
|
155
54
|
baseURL,
|
|
156
55
|
dataReturnType,
|
|
157
56
|
parser,
|
|
@@ -159,18 +58,23 @@ function generateMethod({
|
|
|
159
58
|
paramsCasing,
|
|
160
59
|
pathParamsType,
|
|
161
60
|
}: GenerateMethodProps): string {
|
|
162
|
-
const path = new URLPath(
|
|
163
|
-
const contentType =
|
|
61
|
+
const path = new URLPath(node.path, { casing: paramsCasing })
|
|
62
|
+
const contentType = node.requestBody?.contentType ?? 'application/json'
|
|
164
63
|
const isFormData = contentType === 'multipart/form-data'
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
const
|
|
173
|
-
const
|
|
64
|
+
const headerParamsName =
|
|
65
|
+
node.parameters.filter((p) => p.in === 'header').length > 0
|
|
66
|
+
? tsResolver.resolveHeaderParamsName(node, node.parameters.filter((p) => p.in === 'header')[0]!)
|
|
67
|
+
: undefined
|
|
68
|
+
const headers = buildHeaders(contentType, !!headerParamsName)
|
|
69
|
+
const generics = buildGenerics(node, tsResolver)
|
|
70
|
+
const paramsNode = ClassClient.getParams({ paramsType, paramsCasing, pathParamsType, node, tsResolver, isConfigurable: true })
|
|
71
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
72
|
+
const clientParams = buildClassClientParams({ node, path, baseURL, tsResolver, isFormData, headers })
|
|
73
|
+
const jsdoc = buildJSDoc(getComments(node))
|
|
74
|
+
|
|
75
|
+
const requestDataLine = buildRequestDataLine({ parser, node, zodResolver })
|
|
76
|
+
const formDataLine = buildFormDataLine(isFormData, !!node.requestBody?.schema)
|
|
77
|
+
const returnStatement = buildReturnStatement({ dataReturnType, parser, node, zodResolver })
|
|
174
78
|
|
|
175
79
|
const methodBody = [
|
|
176
80
|
'const { client: request = fetch, ...requestConfig } = mergeConfig(this.#config, config)',
|
|
@@ -184,7 +88,7 @@ function generateMethod({
|
|
|
184
88
|
.map((line) => ` ${line}`)
|
|
185
89
|
.join('\n')
|
|
186
90
|
|
|
187
|
-
return `${jsdoc}async ${name}(${
|
|
91
|
+
return `${jsdoc}async ${name}(${paramsSignature}) {\n${methodBody}\n }`
|
|
188
92
|
}
|
|
189
93
|
|
|
190
94
|
export function ClassClient({
|
|
@@ -200,12 +104,12 @@ export function ClassClient({
|
|
|
200
104
|
pathParamsType,
|
|
201
105
|
children,
|
|
202
106
|
}: Props): FabricReactNode {
|
|
203
|
-
const methods = operations.map(({
|
|
107
|
+
const methods = operations.map(({ node, name: methodName, tsResolver, zodResolver }) =>
|
|
204
108
|
generateMethod({
|
|
205
|
-
|
|
109
|
+
node,
|
|
206
110
|
name: methodName,
|
|
207
|
-
|
|
208
|
-
|
|
111
|
+
tsResolver,
|
|
112
|
+
zodResolver,
|
|
209
113
|
baseURL,
|
|
210
114
|
dataReturnType,
|
|
211
115
|
parser,
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { isValidVarName, URLPath } from '@internals/utils'
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
2
|
+
import { caseParams, createFunctionParameter, createOperationParams, createTypeNode } from '@kubb/ast'
|
|
3
|
+
import type { FunctionParametersNode, OperationNode } from '@kubb/ast/types'
|
|
4
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
5
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
6
|
+
import type { PluginZod } from '@kubb/plugin-zod'
|
|
5
7
|
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
6
8
|
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
7
9
|
import type { PluginClient } from '../types.ts'
|
|
10
|
+
import { buildParamsMapping, getComments } from '../utils.ts'
|
|
8
11
|
import { Url } from './Url.tsx'
|
|
9
12
|
|
|
10
13
|
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
14
|
name: string
|
|
15
15
|
urlName?: string
|
|
16
16
|
isExportable?: boolean
|
|
@@ -24,9 +24,9 @@ type Props = {
|
|
|
24
24
|
paramsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
25
25
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
26
26
|
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
node: OperationNode
|
|
28
|
+
tsResolver: PluginTs['resolver']
|
|
29
|
+
zodResolver?: PluginZod['resolver']
|
|
30
30
|
children?: FabricReactNode
|
|
31
31
|
}
|
|
32
32
|
|
|
@@ -34,96 +34,33 @@ type GetParamsProps = {
|
|
|
34
34
|
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
35
35
|
paramsType: PluginClient['resolvedOptions']['paramsType']
|
|
36
36
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
37
|
-
|
|
37
|
+
node: OperationNode
|
|
38
|
+
tsResolver: PluginTs['resolver']
|
|
38
39
|
isConfigurable: boolean
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
if (paramsType === 'object') {
|
|
43
|
-
const pathParams = getPathParams(typeSchemas.pathParams, {
|
|
44
|
-
typed: true,
|
|
45
|
-
casing: paramsCasing,
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
const children = {
|
|
49
|
-
...pathParams,
|
|
50
|
-
data: typeSchemas.request?.name
|
|
51
|
-
? {
|
|
52
|
-
type: typeSchemas.request?.name,
|
|
53
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
54
|
-
}
|
|
55
|
-
: undefined,
|
|
56
|
-
params: typeSchemas.queryParams?.name
|
|
57
|
-
? {
|
|
58
|
-
type: typeSchemas.queryParams?.name,
|
|
59
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
60
|
-
}
|
|
61
|
-
: undefined,
|
|
62
|
-
headers: typeSchemas.headerParams?.name
|
|
63
|
-
? {
|
|
64
|
-
type: typeSchemas.headerParams?.name,
|
|
65
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
66
|
-
}
|
|
67
|
-
: undefined,
|
|
68
|
-
}
|
|
42
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
69
43
|
|
|
70
|
-
|
|
71
|
-
|
|
44
|
+
function getParams({ paramsType, paramsCasing, pathParamsType, node, tsResolver, isConfigurable }: GetParamsProps): FunctionParametersNode {
|
|
45
|
+
const requestName = node.requestBody?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
72
46
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
: '
|
|
47
|
+
return createOperationParams(node, {
|
|
48
|
+
paramsType,
|
|
49
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
50
|
+
paramsCasing,
|
|
51
|
+
resolver: tsResolver,
|
|
52
|
+
extraParams: isConfigurable
|
|
53
|
+
? [
|
|
54
|
+
createFunctionParameter({
|
|
55
|
+
name: 'config',
|
|
56
|
+
type: createTypeNode({
|
|
57
|
+
variant: 'reference',
|
|
58
|
+
name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
|
|
59
|
+
}),
|
|
84
60
|
default: '{}',
|
|
85
|
-
}
|
|
86
|
-
: undefined,
|
|
87
|
-
})
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return FunctionParams.factory({
|
|
91
|
-
pathParams: typeSchemas.pathParams?.name
|
|
92
|
-
? {
|
|
93
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
94
|
-
children: getPathParams(typeSchemas.pathParams, {
|
|
95
|
-
typed: true,
|
|
96
|
-
casing: paramsCasing,
|
|
97
61
|
}),
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
: undefined,
|
|
101
|
-
data: typeSchemas.request?.name
|
|
102
|
-
? {
|
|
103
|
-
type: typeSchemas.request?.name,
|
|
104
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
105
|
-
}
|
|
106
|
-
: undefined,
|
|
107
|
-
params: typeSchemas.queryParams?.name
|
|
108
|
-
? {
|
|
109
|
-
type: typeSchemas.queryParams?.name,
|
|
110
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
111
|
-
}
|
|
112
|
-
: undefined,
|
|
113
|
-
headers: typeSchemas.headerParams?.name
|
|
114
|
-
? {
|
|
115
|
-
type: typeSchemas.headerParams?.name,
|
|
116
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
117
|
-
}
|
|
118
|
-
: undefined,
|
|
119
|
-
config: isConfigurable
|
|
120
|
-
? {
|
|
121
|
-
type: typeSchemas.request?.name
|
|
122
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
123
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
124
|
-
default: '{}',
|
|
125
|
-
}
|
|
126
|
-
: undefined,
|
|
62
|
+
]
|
|
63
|
+
: [],
|
|
127
64
|
})
|
|
128
65
|
}
|
|
129
66
|
|
|
@@ -132,61 +69,86 @@ export function Client({
|
|
|
132
69
|
isExportable = true,
|
|
133
70
|
isIndexable = true,
|
|
134
71
|
returnType,
|
|
135
|
-
typeSchemas,
|
|
136
72
|
baseURL,
|
|
137
73
|
dataReturnType,
|
|
138
74
|
parser,
|
|
139
|
-
zodSchemas,
|
|
140
75
|
paramsType,
|
|
141
76
|
paramsCasing,
|
|
142
77
|
pathParamsType,
|
|
143
|
-
|
|
78
|
+
node,
|
|
79
|
+
tsResolver,
|
|
80
|
+
zodResolver,
|
|
144
81
|
urlName,
|
|
145
82
|
children,
|
|
146
83
|
isConfigurable = true,
|
|
147
84
|
}: Props): FabricReactNode {
|
|
148
|
-
const path = new URLPath(
|
|
149
|
-
const contentType =
|
|
85
|
+
const path = new URLPath(node.path)
|
|
86
|
+
const contentType = node.requestBody?.contentType ?? 'application/json'
|
|
150
87
|
const isFormData = contentType === 'multipart/form-data'
|
|
151
88
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
const
|
|
89
|
+
const originalPathParams = node.parameters.filter((p) => p.in === 'path')
|
|
90
|
+
const casedPathParams = caseParams(originalPathParams, paramsCasing)
|
|
91
|
+
const originalQueryParams = node.parameters.filter((p) => p.in === 'query')
|
|
92
|
+
const casedQueryParams = caseParams(originalQueryParams, paramsCasing)
|
|
93
|
+
const originalHeaderParams = node.parameters.filter((p) => p.in === 'header')
|
|
94
|
+
const casedHeaderParams = caseParams(originalHeaderParams, paramsCasing)
|
|
95
|
+
|
|
96
|
+
const pathParamsMapping = paramsCasing && !urlName ? buildParamsMapping(originalPathParams, casedPathParams) : undefined
|
|
97
|
+
const queryParamsMapping = paramsCasing ? buildParamsMapping(originalQueryParams, casedQueryParams) : undefined
|
|
98
|
+
const headerParamsMapping = paramsCasing ? buildParamsMapping(originalHeaderParams, casedHeaderParams) : undefined
|
|
99
|
+
|
|
100
|
+
const requestName = node.requestBody?.schema ? tsResolver.resolveDataName(node) : undefined
|
|
101
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
102
|
+
const queryParamsName = originalQueryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, originalQueryParams[0]!) : undefined
|
|
103
|
+
const headerParamsName = originalHeaderParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, originalHeaderParams[0]!) : undefined
|
|
104
|
+
|
|
105
|
+
const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : undefined
|
|
106
|
+
const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.schema ? zodResolver.resolveDataName?.(node) : undefined
|
|
107
|
+
|
|
108
|
+
const errorNames = node.responses
|
|
109
|
+
.filter((r) => {
|
|
110
|
+
const code = Number.parseInt(r.statusCode, 10)
|
|
111
|
+
return code >= 400
|
|
112
|
+
})
|
|
113
|
+
.map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
158
114
|
|
|
159
115
|
const headers = [
|
|
160
116
|
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined,
|
|
161
|
-
|
|
117
|
+
headerParamsName ? (headerParamsMapping ? '...mappedHeaders' : '...headers') : undefined,
|
|
162
118
|
].filter(Boolean)
|
|
163
119
|
|
|
164
|
-
const TError = `ResponseErrorConfig<${
|
|
120
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
165
121
|
|
|
166
|
-
const generics = [
|
|
167
|
-
const
|
|
122
|
+
const generics = [responseName, TError, requestName || 'unknown'].filter(Boolean)
|
|
123
|
+
const paramsNode = getParams({
|
|
168
124
|
paramsType,
|
|
169
125
|
paramsCasing,
|
|
170
126
|
pathParamsType,
|
|
171
|
-
|
|
127
|
+
node,
|
|
128
|
+
tsResolver,
|
|
172
129
|
isConfigurable,
|
|
173
130
|
})
|
|
174
|
-
const
|
|
131
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
132
|
+
|
|
133
|
+
const urlParamsNode = Url.getParams({
|
|
175
134
|
paramsType,
|
|
176
135
|
paramsCasing,
|
|
177
136
|
pathParamsType,
|
|
178
|
-
|
|
137
|
+
node,
|
|
138
|
+
tsResolver,
|
|
179
139
|
})
|
|
140
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
141
|
+
const urlParamsCall = callPrinter.print(urlParamsNode) ?? ''
|
|
180
142
|
|
|
181
143
|
const clientParams = FunctionParams.factory({
|
|
182
144
|
config: {
|
|
183
145
|
mode: 'object',
|
|
184
146
|
children: {
|
|
185
147
|
method: {
|
|
186
|
-
value: JSON.stringify(
|
|
148
|
+
value: JSON.stringify(node.method.toUpperCase()),
|
|
187
149
|
},
|
|
188
150
|
url: {
|
|
189
|
-
value: urlName ? `${urlName}(${
|
|
151
|
+
value: urlName ? `${urlName}(${urlParamsCall}).url.toString()` : path.template,
|
|
190
152
|
},
|
|
191
153
|
baseURL:
|
|
192
154
|
baseURL && !urlName
|
|
@@ -194,8 +156,8 @@ export function Client({
|
|
|
194
156
|
value: `\`${baseURL}\``,
|
|
195
157
|
}
|
|
196
158
|
: undefined,
|
|
197
|
-
params:
|
|
198
|
-
data:
|
|
159
|
+
params: queryParamsName ? (queryParamsMapping ? { value: 'mappedParams' } : {}) : undefined,
|
|
160
|
+
data: requestName
|
|
199
161
|
? {
|
|
200
162
|
value: isFormData ? 'formData as FormData' : 'requestData',
|
|
201
163
|
}
|
|
@@ -218,8 +180,8 @@ export function Client({
|
|
|
218
180
|
children
|
|
219
181
|
) : (
|
|
220
182
|
<>
|
|
221
|
-
{dataReturnType === 'full' && parser === 'zod' &&
|
|
222
|
-
{dataReturnType === 'data' && parser === 'zod' &&
|
|
183
|
+
{dataReturnType === 'full' && parser === 'zod' && zodResponseName && `return {...res, data: ${zodResponseName}.parse(res.data)}`}
|
|
184
|
+
{dataReturnType === 'data' && parser === 'zod' && zodResponseName && `return ${zodResponseName}.parse(res.data)`}
|
|
223
185
|
{dataReturnType === 'full' && parser === 'client' && 'return res'}
|
|
224
186
|
{dataReturnType === 'data' && parser === 'client' && 'return res.data'}
|
|
225
187
|
</>
|
|
@@ -234,9 +196,9 @@ export function Client({
|
|
|
234
196
|
name={name}
|
|
235
197
|
async
|
|
236
198
|
export={isExportable}
|
|
237
|
-
params={
|
|
199
|
+
params={paramsSignature}
|
|
238
200
|
JSDoc={{
|
|
239
|
-
comments: getComments(
|
|
201
|
+
comments: getComments(node),
|
|
240
202
|
}}
|
|
241
203
|
returnType={returnType}
|
|
242
204
|
>
|
|
@@ -245,7 +207,7 @@ export function Client({
|
|
|
245
207
|
<br />
|
|
246
208
|
{pathParamsMapping &&
|
|
247
209
|
Object.entries(pathParamsMapping)
|
|
248
|
-
.filter(([originalName, camelCaseName]) => originalName !== camelCaseName
|
|
210
|
+
.filter(([originalName, camelCaseName]) => isValidVarName(originalName) && originalName !== camelCaseName)
|
|
249
211
|
.map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`)
|
|
250
212
|
.join('\n')}
|
|
251
213
|
{pathParamsMapping && (
|
|
@@ -254,7 +216,7 @@ export function Client({
|
|
|
254
216
|
<br />
|
|
255
217
|
</>
|
|
256
218
|
)}
|
|
257
|
-
{queryParamsMapping &&
|
|
219
|
+
{queryParamsMapping && queryParamsName && (
|
|
258
220
|
<>
|
|
259
221
|
{`const mappedParams = params ? { ${Object.entries(queryParamsMapping)
|
|
260
222
|
.map(([originalName, camelCaseName]) => `"${originalName}": params.${camelCaseName}`)
|
|
@@ -263,7 +225,7 @@ export function Client({
|
|
|
263
225
|
<br />
|
|
264
226
|
</>
|
|
265
227
|
)}
|
|
266
|
-
{headerParamsMapping &&
|
|
228
|
+
{headerParamsMapping && headerParamsName && (
|
|
267
229
|
<>
|
|
268
230
|
{`const mappedHeaders = headers ? { ${Object.entries(headerParamsMapping)
|
|
269
231
|
.map(([originalName, camelCaseName]) => `"${originalName}": headers.${camelCaseName}`)
|
|
@@ -272,11 +234,9 @@ export function Client({
|
|
|
272
234
|
<br />
|
|
273
235
|
</>
|
|
274
236
|
)}
|
|
275
|
-
{parser === 'zod' &&
|
|
276
|
-
? `const requestData = ${zodSchemas.request.name}.parse(data)`
|
|
277
|
-
: typeSchemas?.request?.name && 'const requestData = data'}
|
|
237
|
+
{parser === 'zod' && zodRequestName ? `const requestData = ${zodRequestName}.parse(data)` : requestName && 'const requestData = data'}
|
|
278
238
|
<br />
|
|
279
|
-
{isFormData &&
|
|
239
|
+
{isFormData && requestName && 'const formData = buildFormData(requestData)'}
|
|
280
240
|
<br />
|
|
281
241
|
{isConfigurable
|
|
282
242
|
? `const res = await request<${generics.join(', ')}>(${clientParams.toCall()})`
|