@kubb/plugin-client 5.0.0-beta.15 → 5.0.0-beta.25
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/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.ts +1 -1
- package/dist/clients/fetch.js.map +1 -1
- package/dist/index.cjs +165 -113
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +126 -66
- package/dist/index.js +165 -113
- package/dist/index.js.map +1 -1
- package/dist/templates/clients/fetch.source.cjs +1 -1
- package/dist/templates/clients/fetch.source.js +1 -1
- package/extension.yaml +774 -260
- package/package.json +6 -6
- package/src/clients/fetch.ts +1 -1
- package/src/components/ClassClient.tsx +5 -5
- package/src/components/Client.tsx +18 -18
- package/src/components/StaticClassClient.tsx +5 -5
- package/src/components/Url.tsx +2 -2
- package/src/functionParams.ts +8 -8
- package/src/generators/classClientGenerator.tsx +26 -18
- package/src/generators/clientGenerator.tsx +20 -12
- package/src/generators/groupedClientGenerator.tsx +12 -6
- package/src/generators/operationsGenerator.tsx +10 -4
- package/src/generators/staticClassClientGenerator.tsx +24 -15
- package/src/plugin.ts +24 -11
- package/src/resolvers/resolverClient.ts +10 -4
- package/src/types.ts +66 -53
- package/src/utils.ts +15 -15
|
@@ -15,9 +15,9 @@ type OperationData = {
|
|
|
15
15
|
node: ast.OperationNode
|
|
16
16
|
name: string
|
|
17
17
|
tsResolver: ResolverTs
|
|
18
|
-
zodResolver: ResolverZod |
|
|
18
|
+
zodResolver: ResolverZod | null
|
|
19
19
|
typeFile: ast.FileNode
|
|
20
|
-
zodFile: ast.FileNode |
|
|
20
|
+
zodFile: ast.FileNode | null
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
type Controller = {
|
|
@@ -31,28 +31,34 @@ function resolveTypeImportNames(node: ast.OperationNode, tsResolver: ResolverTs)
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
function resolveZodImportNames(node: ast.OperationNode, zodResolver: ResolverZod): Array<string> {
|
|
34
|
-
const names: Array<string | undefined> = [
|
|
34
|
+
const names: Array<string | null | undefined> = [
|
|
35
35
|
zodResolver.resolveResponseName?.(node),
|
|
36
|
-
node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
36
|
+
node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
37
37
|
]
|
|
38
38
|
return names.filter((n): n is string => Boolean(n))
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* Built-in `operations` generator for `@kubb/plugin-client` when
|
|
43
|
+
* `clientType: 'staticClass'`. Emits one class per tag, with a static method
|
|
44
|
+
* per operation so callers can use `Pet.getPetById(...)` without
|
|
45
|
+
* instantiating the class.
|
|
46
|
+
*/
|
|
41
47
|
export const staticClassClientGenerator = defineGenerator<PluginClient>({
|
|
42
48
|
name: 'staticClassClient',
|
|
43
49
|
renderer: jsxRendererSync,
|
|
44
50
|
operations(nodes, ctx) {
|
|
45
|
-
const { config, driver, resolver, root
|
|
51
|
+
const { config, driver, resolver, root } = ctx
|
|
46
52
|
const { output, group, dataReturnType, paramsCasing, paramsType, pathParamsType, parser, importPath } = ctx.options
|
|
47
|
-
const baseURL = ctx.options.baseURL ??
|
|
53
|
+
const baseURL = ctx.options.baseURL ?? ctx.meta.baseURL
|
|
48
54
|
|
|
49
55
|
const pluginTs = driver.getPlugin(pluginTsName)
|
|
50
56
|
if (!pluginTs) return null
|
|
51
57
|
|
|
52
58
|
const tsResolver = driver.getResolver(pluginTsName)
|
|
53
59
|
const tsPluginOptions = pluginTs.options
|
|
54
|
-
const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) :
|
|
55
|
-
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) :
|
|
60
|
+
const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) : null
|
|
61
|
+
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
|
|
56
62
|
|
|
57
63
|
function buildOperationData(node: ast.OperationNode): OperationData {
|
|
58
64
|
const typeFile = tsResolver.resolveFile(
|
|
@@ -63,9 +69,9 @@ export const staticClassClientGenerator = defineGenerator<PluginClient>({
|
|
|
63
69
|
zodResolver && pluginZod?.options
|
|
64
70
|
? zodResolver.resolveFile(
|
|
65
71
|
{ name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
|
|
66
|
-
{ root, output: pluginZod.options?.output ?? output, group: pluginZod.options?.group },
|
|
72
|
+
{ root, output: pluginZod.options?.output ?? output, group: pluginZod.options?.group ?? undefined },
|
|
67
73
|
)
|
|
68
|
-
:
|
|
74
|
+
: null
|
|
69
75
|
|
|
70
76
|
return {
|
|
71
77
|
node: node,
|
|
@@ -83,7 +89,7 @@ export const staticClassClientGenerator = defineGenerator<PluginClient>({
|
|
|
83
89
|
|
|
84
90
|
if (!tag && !group) {
|
|
85
91
|
const name = resolver.resolveClassName('ApiClient')
|
|
86
|
-
const file = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group })
|
|
92
|
+
const file = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group: group ?? undefined })
|
|
87
93
|
const operationData = buildOperationData(operationNode)
|
|
88
94
|
const previous = acc.find((item) => item.file.path === file.path)
|
|
89
95
|
|
|
@@ -92,9 +98,12 @@ export const staticClassClientGenerator = defineGenerator<PluginClient>({
|
|
|
92
98
|
} else {
|
|
93
99
|
acc.push({ name, file, operations: [operationData] })
|
|
94
100
|
}
|
|
95
|
-
|
|
101
|
+
return acc
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (tag) {
|
|
96
105
|
const name = groupName
|
|
97
|
-
const file = resolver.resolveFile({ name, extname: '.ts', tag }, { root, output, group })
|
|
106
|
+
const file = resolver.resolveFile({ name, extname: '.ts', tag }, { root, output, group: group ?? undefined })
|
|
98
107
|
const operationData = buildOperationData(operationNode)
|
|
99
108
|
const previous = acc.find((item) => item.file.path === file.path)
|
|
100
109
|
|
|
@@ -161,8 +170,8 @@ export const staticClassClientGenerator = defineGenerator<PluginClient>({
|
|
|
161
170
|
baseName={file.baseName}
|
|
162
171
|
path={file.path}
|
|
163
172
|
meta={file.meta}
|
|
164
|
-
banner={resolver.resolveBanner(
|
|
165
|
-
footer={resolver.resolveFooter(
|
|
173
|
+
banner={resolver.resolveBanner(ctx.meta, { output, config })}
|
|
174
|
+
footer={resolver.resolveFooter(ctx.meta, { output, config })}
|
|
166
175
|
>
|
|
167
176
|
{importPath ? (
|
|
168
177
|
<>
|
package/src/plugin.ts
CHANGED
|
@@ -16,20 +16,33 @@ import { source as configSource } from './templates/config.source.ts'
|
|
|
16
16
|
import type { PluginClient } from './types.ts'
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Canonical plugin name for `@kubb/plugin-client
|
|
19
|
+
* Canonical plugin name for `@kubb/plugin-client`. Used for driver lookups and
|
|
20
|
+
* cross-plugin dependency references.
|
|
20
21
|
*/
|
|
21
22
|
export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
|
-
* Generates
|
|
25
|
-
*
|
|
26
|
-
*
|
|
25
|
+
* Generates one HTTP client function per OpenAPI operation. Each function has
|
|
26
|
+
* typed path params, query params, body, and response, so callers use the API
|
|
27
|
+
* like any other typed function. Ships with `axios` and `fetch` runtimes; bring
|
|
28
|
+
* your own by setting `importPath`.
|
|
27
29
|
*
|
|
28
|
-
* @example
|
|
30
|
+
* @example
|
|
29
31
|
* ```ts
|
|
30
|
-
* import
|
|
32
|
+
* import { defineConfig } from 'kubb'
|
|
33
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
34
|
+
* import { pluginClient } from '@kubb/plugin-client'
|
|
35
|
+
*
|
|
31
36
|
* export default defineConfig({
|
|
32
|
-
*
|
|
37
|
+
* input: { path: './petStore.yaml' },
|
|
38
|
+
* output: { path: './src/gen' },
|
|
39
|
+
* plugins: [
|
|
40
|
+
* pluginTs(),
|
|
41
|
+
* pluginClient({
|
|
42
|
+
* output: { path: './clients' },
|
|
43
|
+
* client: 'fetch',
|
|
44
|
+
* }),
|
|
45
|
+
* ],
|
|
33
46
|
* })
|
|
34
47
|
* ```
|
|
35
48
|
*/
|
|
@@ -63,8 +76,8 @@ export const pluginClient = definePlugin<PluginClient>((options) => {
|
|
|
63
76
|
options.generators ??
|
|
64
77
|
[
|
|
65
78
|
clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,
|
|
66
|
-
group && clientType === 'function' ? groupedClientGenerator :
|
|
67
|
-
operations ? operationsGenerator :
|
|
79
|
+
group && clientType === 'function' ? groupedClientGenerator : null,
|
|
80
|
+
operations ? operationsGenerator : null,
|
|
68
81
|
].filter((x): x is NonNullable<typeof x> => Boolean(x))
|
|
69
82
|
|
|
70
83
|
const groupConfig = group
|
|
@@ -79,12 +92,12 @@ export const pluginClient = definePlugin<PluginClient>((options) => {
|
|
|
79
92
|
return `${camelCase(ctx.group)}Controller`
|
|
80
93
|
},
|
|
81
94
|
} satisfies Group)
|
|
82
|
-
:
|
|
95
|
+
: null
|
|
83
96
|
|
|
84
97
|
return {
|
|
85
98
|
name: pluginClientName,
|
|
86
99
|
options,
|
|
87
|
-
dependencies: [pluginTsName, parser === 'zod' ? pluginZodName :
|
|
100
|
+
dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : null].filter((dependency): dependency is string => Boolean(dependency)),
|
|
88
101
|
hooks: {
|
|
89
102
|
'kubb:plugin:setup'(ctx) {
|
|
90
103
|
const resolver = userResolver ? { ...resolverClient, ...userResolver } : resolverClient
|
|
@@ -3,12 +3,18 @@ import { defineResolver } from '@kubb/core'
|
|
|
3
3
|
import type { PluginClient } from '../types.ts'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Default resolver used by `@kubb/plugin-client`. Decides the names and file
|
|
7
|
+
* paths for every generated client function or class. Functions and files use
|
|
8
|
+
* camelCase; classes and tag groups use PascalCase.
|
|
7
9
|
*
|
|
8
|
-
*
|
|
10
|
+
* @example Resolve client function and class names
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { resolverClient } from '@kubb/plugin-client'
|
|
9
13
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
14
|
+
* resolverClient.default('list pets', 'function') // 'listPets'
|
|
15
|
+
* resolverClient.resolveClassName('pet') // 'Pet'
|
|
16
|
+
* resolverClient.resolveUrlName(operationNode) // 'getShowPetByIdUrl'
|
|
17
|
+
* ```
|
|
12
18
|
*/
|
|
13
19
|
export const resolverClient = defineResolver<PluginClient>(() => ({
|
|
14
20
|
name: 'default',
|
package/src/types.ts
CHANGED
|
@@ -46,9 +46,10 @@ export type ResolverClient = Resolver & {
|
|
|
46
46
|
export type ClientImportPath =
|
|
47
47
|
| {
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
50
|
-
* - 'axios'
|
|
51
|
-
* - 'fetch'
|
|
49
|
+
* HTTP client used by the generated code.
|
|
50
|
+
* - `'axios'` — imports from `@kubb/plugin-client/clients/axios`. Requires `axios` at runtime.
|
|
51
|
+
* - `'fetch'` — imports from `@kubb/plugin-client/clients/fetch`. Uses the global `fetch`.
|
|
52
|
+
*
|
|
52
53
|
* @default 'axios'
|
|
53
54
|
*/
|
|
54
55
|
client?: 'axios' | 'fetch'
|
|
@@ -57,9 +58,12 @@ export type ClientImportPath =
|
|
|
57
58
|
| {
|
|
58
59
|
client?: never
|
|
59
60
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
61
|
+
* Path to a custom client module. Generated files import their HTTP runtime from here
|
|
62
|
+
* instead of `@kubb/plugin-client/clients/{client}`. Accepts both relative paths and
|
|
63
|
+
* bare module specifiers; the value is used as-is.
|
|
64
|
+
*
|
|
65
|
+
* @note When combined with a query plugin, the module must export `Client`,
|
|
66
|
+
* `RequestConfig`, and `ResponseErrorConfig` types.
|
|
63
67
|
*/
|
|
64
68
|
importPath: string
|
|
65
69
|
/**
|
|
@@ -81,29 +85,28 @@ export type ClientImportPath =
|
|
|
81
85
|
type ParamsTypeOptions =
|
|
82
86
|
| {
|
|
83
87
|
/**
|
|
84
|
-
*
|
|
88
|
+
* Every operation parameter (path, query, headers, body) is wrapped in a single
|
|
85
89
|
* destructured object argument.
|
|
86
|
-
* - 'object' returns the params and pathParams as an object.
|
|
87
|
-
* @default 'inline'
|
|
88
90
|
*/
|
|
89
91
|
paramsType: 'object'
|
|
90
92
|
/**
|
|
91
93
|
* `pathParamsType` has no effect when `paramsType` is `'object'`.
|
|
92
|
-
* Path params
|
|
94
|
+
* Path params already live inside the single destructured object.
|
|
93
95
|
*/
|
|
94
96
|
pathParamsType?: never
|
|
95
97
|
}
|
|
96
98
|
| {
|
|
97
99
|
/**
|
|
98
|
-
* Each parameter group is emitted as a separate function argument.
|
|
99
|
-
*
|
|
100
|
+
* Each parameter group is emitted as a separate positional function argument.
|
|
101
|
+
*
|
|
100
102
|
* @default 'inline'
|
|
101
103
|
*/
|
|
102
104
|
paramsType?: 'inline'
|
|
103
105
|
/**
|
|
104
|
-
*
|
|
105
|
-
* - 'object' groups
|
|
106
|
-
* - 'inline' emits each path param as its own argument: `petId: string`.
|
|
106
|
+
* How URL path parameters are arranged inside the inline argument list.
|
|
107
|
+
* - `'object'` groups them into one destructured object: `{ petId }: PathParams`.
|
|
108
|
+
* - `'inline'` emits each path param as its own argument: `petId: string`.
|
|
109
|
+
*
|
|
107
110
|
* @default 'inline'
|
|
108
111
|
*/
|
|
109
112
|
pathParamsType?: 'object' | 'inline'
|
|
@@ -111,87 +114,96 @@ type ParamsTypeOptions =
|
|
|
111
114
|
|
|
112
115
|
export type Options = {
|
|
113
116
|
/**
|
|
114
|
-
*
|
|
115
|
-
*
|
|
117
|
+
* Where the generated client files are written and how they are exported.
|
|
118
|
+
*
|
|
119
|
+
* @default { path: 'clients', barrel: { type: 'named' } }
|
|
116
120
|
*/
|
|
117
121
|
output?: Output
|
|
118
122
|
/**
|
|
119
|
-
*
|
|
123
|
+
* Split generated files into subfolders based on the operation's tag.
|
|
120
124
|
*/
|
|
121
125
|
group?: Group
|
|
122
126
|
/**
|
|
123
|
-
*
|
|
127
|
+
* Skip operations matching at least one entry in the list.
|
|
124
128
|
*/
|
|
125
129
|
exclude?: Array<Exclude>
|
|
126
130
|
/**
|
|
127
|
-
*
|
|
131
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
128
132
|
*/
|
|
129
133
|
include?: Array<Include>
|
|
130
134
|
/**
|
|
131
|
-
*
|
|
135
|
+
* Apply a different options object to operations matching a pattern.
|
|
132
136
|
*/
|
|
133
137
|
override?: Array<Override<ResolvedOptions>>
|
|
134
138
|
/**
|
|
135
|
-
*
|
|
139
|
+
* Emit an `operations.ts` file that re-exports every generated function grouped by HTTP method.
|
|
140
|
+
*
|
|
136
141
|
* @default false
|
|
137
142
|
*/
|
|
138
143
|
operations?: boolean
|
|
139
144
|
/**
|
|
140
|
-
*
|
|
141
|
-
* - 'export'
|
|
142
|
-
* - false
|
|
145
|
+
* Whether to also export the URL builder helpers (`get<Operation>Url`).
|
|
146
|
+
* - `'export'` exposes them via the barrel.
|
|
147
|
+
* - `false` keeps them private.
|
|
148
|
+
*
|
|
143
149
|
* @default false
|
|
144
|
-
* @example getGetPetByIdUrl
|
|
145
150
|
*/
|
|
146
151
|
urlType?: 'export' | false
|
|
147
152
|
/**
|
|
148
|
-
*
|
|
153
|
+
* Base URL prepended to every request. When omitted, falls back to the adapter's
|
|
154
|
+
* server URL (typically `servers[0].url`).
|
|
149
155
|
*/
|
|
150
156
|
baseURL?: string
|
|
151
157
|
/**
|
|
152
|
-
*
|
|
153
|
-
* - 'data'
|
|
154
|
-
* - 'full'
|
|
158
|
+
* Shape of the value returned by each generated client function.
|
|
159
|
+
* - `'data'` — only the response body.
|
|
160
|
+
* - `'full'` — the full response config (body, status, headers, request).
|
|
161
|
+
*
|
|
155
162
|
* @default 'data'
|
|
156
163
|
*/
|
|
157
164
|
dataReturnType?: 'data' | 'full'
|
|
158
165
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
*
|
|
166
|
+
* Rename parameter properties in the generated client (path, query, headers).
|
|
167
|
+
* The HTTP request still uses the original spec names; Kubb writes the mapping for you.
|
|
168
|
+
*
|
|
169
|
+
* @note Use the same value on `@kubb/plugin-ts` so types stay compatible.
|
|
162
170
|
*/
|
|
163
171
|
paramsCasing?: 'camelcase'
|
|
164
172
|
/**
|
|
165
|
-
*
|
|
166
|
-
* - 'client'
|
|
167
|
-
* - 'zod'
|
|
173
|
+
* Validator applied to response bodies before they are returned to the caller.
|
|
174
|
+
* - `'client'` — no validation. Trusts the API.
|
|
175
|
+
* - `'zod'` — pipes responses through schemas from `@kubb/plugin-zod`.
|
|
176
|
+
*
|
|
168
177
|
* @default 'client'
|
|
169
178
|
*/
|
|
170
179
|
parser?: 'client' | 'zod'
|
|
171
180
|
/**
|
|
172
|
-
*
|
|
173
|
-
* - 'function'
|
|
174
|
-
* - 'class'
|
|
175
|
-
* - 'staticClass'
|
|
181
|
+
* Shape of the generated client.
|
|
182
|
+
* - `'function'` — one standalone async function per operation.
|
|
183
|
+
* - `'class'` — one class per tag with instance methods.
|
|
184
|
+
* - `'staticClass'` — one class per tag with static methods.
|
|
185
|
+
*
|
|
176
186
|
* @default 'function'
|
|
187
|
+
* @note Only `'function'` is compatible with query plugins.
|
|
177
188
|
*/
|
|
178
189
|
clientType?: 'function' | 'class' | 'staticClass'
|
|
179
190
|
/**
|
|
180
|
-
*
|
|
181
|
-
* When
|
|
191
|
+
* Copy the HTTP client runtime into the generated output so consumers do not need
|
|
192
|
+
* `@kubb/plugin-client` at runtime. When `false`, generated files import from
|
|
193
|
+
* `@kubb/plugin-client/clients/{client}`.
|
|
194
|
+
*
|
|
182
195
|
* @default false
|
|
183
|
-
* In version 5 of Kubb this is by default true
|
|
184
196
|
*/
|
|
185
197
|
bundle?: boolean
|
|
186
198
|
/**
|
|
187
|
-
* Generate
|
|
188
|
-
*
|
|
199
|
+
* Generate a single SDK class composing every tag-based client into one entry point.
|
|
200
|
+
* Automatically enables `clientType: 'class'`.
|
|
201
|
+
*
|
|
189
202
|
* @example
|
|
190
203
|
* ```ts
|
|
191
204
|
* pluginClient({
|
|
192
205
|
* sdk: { className: 'PetStoreSDK' },
|
|
193
206
|
* })
|
|
194
|
-
* // Generates a class with a shared constructor config and one property per tag:
|
|
195
207
|
* // class PetStoreSDK {
|
|
196
208
|
* // readonly petController: petController
|
|
197
209
|
* // readonly storeController: storeController
|
|
@@ -201,22 +213,23 @@ export type Options = {
|
|
|
201
213
|
*/
|
|
202
214
|
sdk?: {
|
|
203
215
|
/**
|
|
204
|
-
* Name of the generated SDK facade class.
|
|
216
|
+
* Name of the generated SDK facade class. Also the file name.
|
|
205
217
|
*/
|
|
206
218
|
className: string
|
|
207
219
|
}
|
|
208
220
|
/**
|
|
209
|
-
* Override
|
|
210
|
-
*
|
|
221
|
+
* Override how names and file paths are built for the generated client.
|
|
222
|
+
* Methods you omit fall back to the default resolver. `this` is bound to the
|
|
223
|
+
* full resolver, so `this.default(name)` delegates to the built-in implementation.
|
|
211
224
|
*/
|
|
212
225
|
resolver?: Partial<ResolverClient> & ThisType<ResolverClient>
|
|
213
226
|
/**
|
|
214
|
-
*
|
|
215
|
-
* Return `null` or `undefined`
|
|
227
|
+
* AST visitor applied to each operation node before code is printed.
|
|
228
|
+
* Return `null` or `undefined` to leave the node unchanged.
|
|
216
229
|
*/
|
|
217
230
|
transformer?: ast.Visitor
|
|
218
231
|
/**
|
|
219
|
-
*
|
|
232
|
+
* Custom generators that run alongside the built-in client generators.
|
|
220
233
|
*/
|
|
221
234
|
generators?: Array<Generator<PluginClient>>
|
|
222
235
|
} & ClientImportPath &
|
|
@@ -227,7 +240,7 @@ type ResolvedOptions = {
|
|
|
227
240
|
exclude: Array<Exclude>
|
|
228
241
|
include: Array<Include> | undefined
|
|
229
242
|
override: Array<Override<ResolvedOptions>>
|
|
230
|
-
group: Group |
|
|
243
|
+
group: Group | null
|
|
231
244
|
client: Options['client']
|
|
232
245
|
clientType: NonNullable<Options['clientType']>
|
|
233
246
|
bundle: NonNullable<Options['bundle']>
|
package/src/utils.ts
CHANGED
|
@@ -12,8 +12,8 @@ import type { PluginClient } from './types.ts'
|
|
|
12
12
|
*/
|
|
13
13
|
export function buildHeaders(contentType: string, hasHeaderParams: boolean): Array<string> {
|
|
14
14
|
return [
|
|
15
|
-
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` :
|
|
16
|
-
hasHeaderParams ? '...headers' :
|
|
15
|
+
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : null,
|
|
16
|
+
hasHeaderParams ? '...headers' : null,
|
|
17
17
|
].filter(Boolean) as Array<string>
|
|
18
18
|
}
|
|
19
19
|
|
|
@@ -23,7 +23,7 @@ export function buildHeaders(contentType: string, hasHeaderParams: boolean): Arr
|
|
|
23
23
|
*/
|
|
24
24
|
export function buildGenerics(node: ast.OperationNode, tsResolver: ResolverTs): Array<string> {
|
|
25
25
|
const responseName = tsResolver.resolveResponseName(node)
|
|
26
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
26
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null
|
|
27
27
|
const errorNames = node.responses.filter((r) => Number.parseInt(r.statusCode, 10) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
|
|
28
28
|
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
29
29
|
return [responseName, TError, requestName || 'unknown'].filter(Boolean)
|
|
@@ -45,7 +45,7 @@ export function buildClassClientParams({
|
|
|
45
45
|
}: {
|
|
46
46
|
node: ast.OperationNode
|
|
47
47
|
path: URLPath
|
|
48
|
-
baseURL: string | undefined
|
|
48
|
+
baseURL: string | null | undefined
|
|
49
49
|
tsResolver: ResolverTs
|
|
50
50
|
isFormData: boolean
|
|
51
51
|
isMultipleContentTypes: boolean
|
|
@@ -53,8 +53,8 @@ export function buildClassClientParams({
|
|
|
53
53
|
headers: Array<string>
|
|
54
54
|
}) {
|
|
55
55
|
const { query: queryParams } = getOperationParameters(node)
|
|
56
|
-
const queryParamsName = queryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, queryParams[0]!) :
|
|
57
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
56
|
+
const queryParamsName = queryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, queryParams[0]!) : null
|
|
57
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null
|
|
58
58
|
|
|
59
59
|
return createFunctionParams({
|
|
60
60
|
config: {
|
|
@@ -73,8 +73,8 @@ export function buildClassClientParams({
|
|
|
73
73
|
? {
|
|
74
74
|
value: JSON.stringify(baseURL),
|
|
75
75
|
}
|
|
76
|
-
:
|
|
77
|
-
params: queryParamsName ? {} :
|
|
76
|
+
: null,
|
|
77
|
+
params: queryParamsName ? {} : null,
|
|
78
78
|
data: requestName
|
|
79
79
|
? {
|
|
80
80
|
value:
|
|
@@ -84,13 +84,13 @@ export function buildClassClientParams({
|
|
|
84
84
|
? 'formData as FormData'
|
|
85
85
|
: 'requestData',
|
|
86
86
|
}
|
|
87
|
-
:
|
|
88
|
-
contentType: isMultipleContentTypes ? {} :
|
|
87
|
+
: null,
|
|
88
|
+
contentType: isMultipleContentTypes ? {} : null,
|
|
89
89
|
headers: headers.length
|
|
90
90
|
? {
|
|
91
91
|
value: `{ ${headers.join(', ')}, ...requestConfig.headers }`,
|
|
92
92
|
}
|
|
93
|
-
:
|
|
93
|
+
: null,
|
|
94
94
|
},
|
|
95
95
|
},
|
|
96
96
|
})
|
|
@@ -107,9 +107,9 @@ export function buildRequestDataLine({
|
|
|
107
107
|
}: {
|
|
108
108
|
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
109
109
|
node: ast.OperationNode
|
|
110
|
-
zodResolver?: ResolverZod
|
|
110
|
+
zodResolver?: ResolverZod | null
|
|
111
111
|
}): string {
|
|
112
|
-
const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
112
|
+
const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null
|
|
113
113
|
if (parser === 'zod' && zodRequestName) {
|
|
114
114
|
return `const requestData = ${zodRequestName}.parse(data)`
|
|
115
115
|
}
|
|
@@ -140,9 +140,9 @@ export function buildReturnStatement({
|
|
|
140
140
|
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
141
141
|
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
142
142
|
node: ast.OperationNode
|
|
143
|
-
zodResolver?: ResolverZod
|
|
143
|
+
zodResolver?: ResolverZod | null
|
|
144
144
|
}): string {
|
|
145
|
-
const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) :
|
|
145
|
+
const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : null
|
|
146
146
|
if (dataReturnType === 'full' && parser === 'zod' && zodResponseName) {
|
|
147
147
|
return `return {...res, data: ${zodResponseName}.parse(res.data)}`
|
|
148
148
|
}
|