@kubb/plugin-client 5.0.0-alpha.9 → 5.0.0-beta.15
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 +1830 -97
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +330 -4
- package/dist/index.js +1816 -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 +779 -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 -517
- 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/package.json
CHANGED
|
@@ -1,34 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-client",
|
|
3
|
-
"version": "5.0.0-
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "5.0.0-beta.15",
|
|
4
|
+
"description": "Generate type-safe HTTP clients from your OpenAPI specification. Supports Axios, Fetch, and custom client adapters with full TypeScript inference and zero boilerplate.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
7
|
-
"http-client",
|
|
8
|
-
"sdk-generator",
|
|
9
7
|
"axios",
|
|
8
|
+
"code-generation",
|
|
9
|
+
"codegen",
|
|
10
10
|
"fetch",
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"type-safety",
|
|
14
|
-
"type-safe",
|
|
11
|
+
"http-client",
|
|
12
|
+
"kubb",
|
|
15
13
|
"openapi",
|
|
16
14
|
"swagger",
|
|
17
|
-
"
|
|
18
|
-
"code-generator",
|
|
19
|
-
"codegen",
|
|
20
|
-
"plugins",
|
|
21
|
-
"kubb"
|
|
15
|
+
"typescript"
|
|
22
16
|
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "stijnvanhulle",
|
|
23
19
|
"repository": {
|
|
24
20
|
"type": "git",
|
|
25
|
-
"url": "git+https://github.com/kubb-labs/
|
|
21
|
+
"url": "git+https://github.com/kubb-labs/plugins.git",
|
|
26
22
|
"directory": "packages/plugin-client"
|
|
27
23
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"dist",
|
|
27
|
+
"extension.yaml",
|
|
28
|
+
"*.d.ts",
|
|
29
|
+
"*.d.cts",
|
|
30
|
+
"!/**/**.test.**",
|
|
31
|
+
"!/**/__tests__/**",
|
|
32
|
+
"!/**/__snapshots__/**"
|
|
33
|
+
],
|
|
31
34
|
"type": "module",
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"main": "./dist/index.cjs",
|
|
37
|
+
"module": "./dist/index.js",
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"typesVersions": {
|
|
40
|
+
"*": {
|
|
41
|
+
"clients/axios": [
|
|
42
|
+
"./dist/clients/axios.d.ts"
|
|
43
|
+
],
|
|
44
|
+
"clients/fetch": [
|
|
45
|
+
"./dist/clients/fetch.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"templates/config.source": [
|
|
48
|
+
"./dist/templates/config.source.d.ts"
|
|
49
|
+
],
|
|
50
|
+
"templates/clients/axios.source": [
|
|
51
|
+
"./dist/templates/clients/axios.source.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"templates/clients/fetch.source": [
|
|
54
|
+
"./dist/templates/clients/fetch.source.d.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
32
58
|
"exports": {
|
|
33
59
|
".": {
|
|
34
60
|
"import": "./dist/index.js",
|
|
@@ -42,14 +68,6 @@
|
|
|
42
68
|
"import": "./dist/clients/fetch.js",
|
|
43
69
|
"require": "./dist/clients/fetch.cjs"
|
|
44
70
|
},
|
|
45
|
-
"./components": {
|
|
46
|
-
"import": "./dist/components.js",
|
|
47
|
-
"require": "./dist/components.cjs"
|
|
48
|
-
},
|
|
49
|
-
"./generators": {
|
|
50
|
-
"import": "./dist/generators.js",
|
|
51
|
-
"require": "./dist/generators.cjs"
|
|
52
|
-
},
|
|
53
71
|
"./templates/clients/axios.source": {
|
|
54
72
|
"import": "./dist/templates/clients/axios.source.js",
|
|
55
73
|
"require": "./dist/templates/clients/axios.source.cjs"
|
|
@@ -64,65 +82,23 @@
|
|
|
64
82
|
},
|
|
65
83
|
"./package.json": "./package.json"
|
|
66
84
|
},
|
|
67
|
-
"
|
|
68
|
-
|
|
69
|
-
"
|
|
70
|
-
"components": [
|
|
71
|
-
"./dist/components.d.ts"
|
|
72
|
-
],
|
|
73
|
-
"generators": [
|
|
74
|
-
"./dist/generators.d.ts"
|
|
75
|
-
],
|
|
76
|
-
"clients/axios": [
|
|
77
|
-
"./dist/clients/axios.d.ts"
|
|
78
|
-
],
|
|
79
|
-
"clients/fetch": [
|
|
80
|
-
"./dist/clients/fetch.d.ts"
|
|
81
|
-
],
|
|
82
|
-
"templates/config.source": [
|
|
83
|
-
"./dist/templates/config.source.d.ts"
|
|
84
|
-
],
|
|
85
|
-
"templates/clients/axios.source": [
|
|
86
|
-
"./dist/templates/clients/axios.source.d.ts"
|
|
87
|
-
],
|
|
88
|
-
"templates/clients/fetch.source": [
|
|
89
|
-
"./dist/templates/clients/fetch.source.d.ts"
|
|
90
|
-
]
|
|
91
|
-
}
|
|
85
|
+
"publishConfig": {
|
|
86
|
+
"access": "public",
|
|
87
|
+
"registry": "https://registry.npmjs.org/"
|
|
92
88
|
},
|
|
93
|
-
"files": [
|
|
94
|
-
"src",
|
|
95
|
-
"dist",
|
|
96
|
-
"templates",
|
|
97
|
-
"*.d.ts",
|
|
98
|
-
"*.d.cts",
|
|
99
|
-
"!/**/**.test.**",
|
|
100
|
-
"!/**/__tests__/**",
|
|
101
|
-
"!/**/__snapshots__/**"
|
|
102
|
-
],
|
|
103
|
-
"size-limit": [
|
|
104
|
-
{
|
|
105
|
-
"path": "./dist/*.js",
|
|
106
|
-
"limit": "510 KiB",
|
|
107
|
-
"gzip": true
|
|
108
|
-
}
|
|
109
|
-
],
|
|
110
89
|
"dependencies": {
|
|
111
|
-
"@kubb/
|
|
112
|
-
"@kubb/
|
|
113
|
-
"@kubb/
|
|
114
|
-
"@kubb/
|
|
115
|
-
"@kubb/plugin-oas": "5.0.0-alpha.9",
|
|
116
|
-
"@kubb/plugin-ts": "5.0.0-alpha.9",
|
|
117
|
-
"@kubb/plugin-zod": "5.0.0-alpha.9"
|
|
90
|
+
"@kubb/core": "5.0.0-beta.19",
|
|
91
|
+
"@kubb/renderer-jsx": "5.0.0-beta.19",
|
|
92
|
+
"@kubb/plugin-ts": "5.0.0-beta.15",
|
|
93
|
+
"@kubb/plugin-zod": "5.0.0-beta.15"
|
|
118
94
|
},
|
|
119
95
|
"devDependencies": {
|
|
120
|
-
"axios": "^1.
|
|
96
|
+
"axios": "^1.16.1",
|
|
97
|
+
"@internals/shared": "0.0.0",
|
|
121
98
|
"@internals/utils": "0.0.0"
|
|
122
99
|
},
|
|
123
100
|
"peerDependencies": {
|
|
124
|
-
"@kubb/
|
|
125
|
-
"@kubb/react-fabric": "0.14.0",
|
|
101
|
+
"@kubb/renderer-jsx": "5.0.0-beta.19",
|
|
126
102
|
"axios": "^1.7.2"
|
|
127
103
|
},
|
|
128
104
|
"peerDependenciesMeta": {
|
|
@@ -130,20 +106,21 @@
|
|
|
130
106
|
"optional": true
|
|
131
107
|
}
|
|
132
108
|
},
|
|
109
|
+
"size-limit": [
|
|
110
|
+
{
|
|
111
|
+
"path": "./dist/*.js",
|
|
112
|
+
"limit": "510 KiB",
|
|
113
|
+
"gzip": true
|
|
114
|
+
}
|
|
115
|
+
],
|
|
133
116
|
"engines": {
|
|
134
117
|
"node": ">=22"
|
|
135
118
|
},
|
|
136
|
-
"publishConfig": {
|
|
137
|
-
"access": "public",
|
|
138
|
-
"registry": "https://registry.npmjs.org/"
|
|
139
|
-
},
|
|
140
|
-
"main": "./dist/index.cjs",
|
|
141
|
-
"module": "./dist/index.js",
|
|
142
119
|
"scripts": {
|
|
143
120
|
"build": "tsdown && size-limit",
|
|
144
121
|
"clean": "npx rimraf ./dist",
|
|
145
|
-
"lint": "
|
|
146
|
-
"lint:fix": "
|
|
122
|
+
"lint": "oxlint .",
|
|
123
|
+
"lint:fix": "oxlint --fix .",
|
|
147
124
|
"release": "pnpm publish --no-git-check",
|
|
148
125
|
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
149
126
|
"start": "tsdown --watch",
|
package/src/clients/axios.ts
CHANGED
|
@@ -18,6 +18,7 @@ export type RequestConfig<TData = unknown> = {
|
|
|
18
18
|
validateStatus?: (status: number) => boolean
|
|
19
19
|
headers?: AxiosRequestConfig['headers']
|
|
20
20
|
paramsSerializer?: AxiosRequestConfig['paramsSerializer']
|
|
21
|
+
contentType?: string
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
/**
|
|
@@ -32,7 +33,10 @@ export type ResponseConfig<TData = unknown> = {
|
|
|
32
33
|
|
|
33
34
|
export type ResponseErrorConfig<TError = unknown> = AxiosError<TError>
|
|
34
35
|
|
|
35
|
-
export type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(
|
|
36
|
+
export type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(
|
|
37
|
+
config: RequestConfig<TRequestData>,
|
|
38
|
+
request?: unknown,
|
|
39
|
+
) => Promise<ResponseConfig<TResponseData>>
|
|
36
40
|
|
|
37
41
|
let _config: Partial<RequestConfig> = {
|
|
38
42
|
baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,
|
|
@@ -63,10 +67,21 @@ export const axiosInstance = axios.create(getConfig())
|
|
|
63
67
|
|
|
64
68
|
export const client = async <TResponseData, TError = unknown, TRequestData = unknown>(
|
|
65
69
|
config: RequestConfig<TRequestData>,
|
|
70
|
+
_request?: unknown,
|
|
66
71
|
): Promise<ResponseConfig<TResponseData>> => {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
72
|
+
const requestConfig = mergeConfig(getConfig(), config)
|
|
73
|
+
const { contentType, headers, ...axiosConfig } = requestConfig
|
|
74
|
+
return axiosInstance
|
|
75
|
+
.request<TResponseData, ResponseConfig<TResponseData>>({
|
|
76
|
+
...axiosConfig,
|
|
77
|
+
headers: {
|
|
78
|
+
...(contentType && contentType !== 'multipart/form-data' ? { 'Content-Type': contentType } : {}),
|
|
79
|
+
...headers,
|
|
80
|
+
},
|
|
81
|
+
})
|
|
82
|
+
.catch((e: AxiosError<TError>) => {
|
|
83
|
+
throw e
|
|
84
|
+
})
|
|
70
85
|
}
|
|
71
86
|
|
|
72
87
|
client.getConfig = getConfig
|
package/src/clients/fetch.ts
CHANGED
|
@@ -16,6 +16,7 @@ export type RequestConfig<TData = unknown> = {
|
|
|
16
16
|
signal?: AbortSignal
|
|
17
17
|
headers?: [string, string][] | Record<string, string>
|
|
18
18
|
credentials?: RequestCredentials
|
|
19
|
+
contentType?: string
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
@@ -52,10 +53,14 @@ export const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T
|
|
|
52
53
|
|
|
53
54
|
export type ResponseErrorConfig<TError = unknown> = TError
|
|
54
55
|
|
|
55
|
-
export type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(
|
|
56
|
+
export type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(
|
|
57
|
+
config: RequestConfig<TRequestData>,
|
|
58
|
+
request?: unknown,
|
|
59
|
+
) => Promise<ResponseConfig<TResponseData>>
|
|
56
60
|
|
|
57
61
|
export const client = async <TResponseData, _TError = unknown, RequestData = unknown>(
|
|
58
62
|
paramsConfig: RequestConfig<RequestData>,
|
|
63
|
+
_request?: unknown,
|
|
59
64
|
): Promise<ResponseConfig<TResponseData>> => {
|
|
60
65
|
const normalizedParams = new URLSearchParams()
|
|
61
66
|
|
|
@@ -78,7 +83,10 @@ export const client = async <TResponseData, _TError = unknown, RequestData = unk
|
|
|
78
83
|
method: config.method?.toUpperCase(),
|
|
79
84
|
body: config.data instanceof FormData ? config.data : JSON.stringify(config.data),
|
|
80
85
|
signal: config.signal,
|
|
81
|
-
headers:
|
|
86
|
+
headers: {
|
|
87
|
+
...(config.contentType && config.contentType !== 'multipart/form-data' ? { 'Content-Type': config.contentType } : {}),
|
|
88
|
+
...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),
|
|
89
|
+
},
|
|
82
90
|
})
|
|
83
91
|
|
|
84
92
|
const data = [204, 205, 304].includes(response.status) || !response.body ? {} : await response.json()
|
|
@@ -1,40 +1,41 @@
|
|
|
1
|
+
import { buildOperationComments, getContentTypeInfo, getOperationParameters } from '@internals/shared'
|
|
1
2
|
import { buildJSDoc, URLPath } from '@internals/utils'
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
3
|
+
import type { ast } from '@kubb/core'
|
|
4
|
+
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
5
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
6
|
+
import type { ResolverZod } from '@kubb/plugin-zod'
|
|
7
|
+
import { File } from '@kubb/renderer-jsx'
|
|
8
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
9
|
import type { PluginClient } from '../types.ts'
|
|
10
|
+
import { buildClassClientParams, buildFormDataLine, buildGenerics, buildHeaders, buildRequestDataLine, buildReturnStatement } from '../utils.ts'
|
|
11
|
+
import { buildClientParamsNode } from './Client.tsx'
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
type OperationData = {
|
|
14
|
+
node: ast.OperationNode
|
|
15
|
+
name: string
|
|
16
|
+
tsResolver: ResolverTs
|
|
17
|
+
zodResolver?: ResolverZod
|
|
18
|
+
}
|
|
10
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']
|
|
27
28
|
paramsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
28
29
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
29
30
|
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
30
|
-
children?:
|
|
31
|
+
children?: KubbReactNode
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
type GenerateMethodProps = {
|
|
34
|
-
|
|
35
|
+
node: ast.OperationNode
|
|
35
36
|
name: string
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
tsResolver: ResolverTs
|
|
38
|
+
zodResolver?: ResolverZod
|
|
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,21 +58,24 @@ function generateMethod({
|
|
|
159
58
|
paramsCasing,
|
|
160
59
|
pathParamsType,
|
|
161
60
|
}: GenerateMethodProps): string {
|
|
162
|
-
const path = new URLPath(
|
|
163
|
-
const contentType =
|
|
164
|
-
const isFormData = contentType === 'multipart/form-data'
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
const
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
const
|
|
172
|
-
const
|
|
173
|
-
|
|
61
|
+
const path = new URLPath(node.path, { casing: paramsCasing })
|
|
62
|
+
const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node)
|
|
63
|
+
const isFormData = !isMultipleContentTypes && contentType === 'multipart/form-data'
|
|
64
|
+
const { header: headerParams } = getOperationParameters(node)
|
|
65
|
+
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]!) : undefined
|
|
66
|
+
const headers = isMultipleContentTypes ? (headerParamsName ? ['...headers'] : []) : buildHeaders(contentType, !!headerParamsName)
|
|
67
|
+
const generics = buildGenerics(node, tsResolver)
|
|
68
|
+
const paramsNode = buildClientParamsNode({ paramsType, paramsCasing, pathParamsType, node, tsResolver, isConfigurable: true })
|
|
69
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
70
|
+
const clientParams = buildClassClientParams({ node, path, baseURL, tsResolver, isFormData, isMultipleContentTypes, hasFormData, headers })
|
|
71
|
+
const jsdoc = buildJSDoc(buildOperationComments(node, { link: 'urlPath', linkPosition: 'beforeDeprecated', splitLines: true }))
|
|
72
|
+
|
|
73
|
+
const requestDataLine = buildRequestDataLine({ parser, node, zodResolver })
|
|
74
|
+
const formDataLine = buildFormDataLine(isFormData || (isMultipleContentTypes && hasFormData), !!node.requestBody?.content?.[0]?.schema)
|
|
75
|
+
const returnStatement = buildReturnStatement({ dataReturnType, parser, node, zodResolver })
|
|
174
76
|
|
|
175
77
|
const methodBody = [
|
|
176
|
-
|
|
78
|
+
`const { client: request = fetch, ${isMultipleContentTypes ? `contentType = ${JSON.stringify(contentType)}, ` : ''}...requestConfig } = mergeConfig(this.#config, config)`,
|
|
177
79
|
'',
|
|
178
80
|
requestDataLine,
|
|
179
81
|
formDataLine,
|
|
@@ -184,7 +86,7 @@ function generateMethod({
|
|
|
184
86
|
.map((line) => ` ${line}`)
|
|
185
87
|
.join('\n')
|
|
186
88
|
|
|
187
|
-
return `${jsdoc}async ${name}(${
|
|
89
|
+
return `${jsdoc}async ${name}(${paramsSignature}) {\n${methodBody}\n }`
|
|
188
90
|
}
|
|
189
91
|
|
|
190
92
|
export function ClassClient({
|
|
@@ -199,13 +101,13 @@ export function ClassClient({
|
|
|
199
101
|
paramsCasing,
|
|
200
102
|
pathParamsType,
|
|
201
103
|
children,
|
|
202
|
-
}: Props):
|
|
203
|
-
const methods = operations.map(({
|
|
104
|
+
}: Props): KubbReactNode {
|
|
105
|
+
const methods = operations.map(({ node, name: methodName, tsResolver, zodResolver }) =>
|
|
204
106
|
generateMethod({
|
|
205
|
-
|
|
107
|
+
node,
|
|
206
108
|
name: methodName,
|
|
207
|
-
|
|
208
|
-
|
|
109
|
+
tsResolver,
|
|
110
|
+
zodResolver,
|
|
209
111
|
baseURL,
|
|
210
112
|
dataReturnType,
|
|
211
113
|
parser,
|
|
@@ -232,4 +134,3 @@ ${methods.join('\n\n')}
|
|
|
232
134
|
</File.Source>
|
|
233
135
|
)
|
|
234
136
|
}
|
|
235
|
-
ClassClient.getParams = Client.getParams
|