@kubb/plugin-fetch 5.0.0-beta.99 → 5.0.0
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 +333 -359
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +332 -358
- package/dist/index.js.map +1 -1
- package/package.json +7 -10
- package/src/generators/clientGenerator.tsx +4 -101
package/dist/index.js
CHANGED
|
@@ -1,11 +1,217 @@
|
|
|
1
1
|
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
+
import { Resolver, Url, ast, createResolver, defineGenerator, definePlugin, macroSimplifyUnion } from "kubb/kit";
|
|
2
3
|
import path from "node:path";
|
|
3
|
-
import { Resolver, ast, createResolver, defineGenerator, definePlugin } from "kubb/kit";
|
|
4
4
|
import { createFunctionParameter, createFunctionParameters, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
5
5
|
import { File, Function, jsxRenderer } from "kubb/jsx";
|
|
6
6
|
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
7
7
|
import { pluginZodName } from "@kubb/plugin-zod";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
|
+
//#region ../../internals/shared/src/params.ts
|
|
10
|
+
/**
|
|
11
|
+
* Drops parameters that share the same name, keeping the first.
|
|
12
|
+
*
|
|
13
|
+
* A malformed spec can declare the same parameter name twice within one `in` location. Both would
|
|
14
|
+
* resolve to the same output property, so emitting both would yield an object type with a duplicate
|
|
15
|
+
* member, which TypeScript rejects. This is a defensive guard against that case, not a casing guard:
|
|
16
|
+
* parameter names flow through unchanged, so no two distinct names ever collide here anymore.
|
|
17
|
+
*/
|
|
18
|
+
function dedupeParams(params) {
|
|
19
|
+
const seen = /* @__PURE__ */ new Set();
|
|
20
|
+
return params.filter((param) => {
|
|
21
|
+
if (seen.has(param.name)) return false;
|
|
22
|
+
seen.add(param.name);
|
|
23
|
+
return true;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region ../../internals/shared/src/operation.ts
|
|
28
|
+
/**
|
|
29
|
+
* Builds the `ResolverFileParams` every operation generator passes to
|
|
30
|
+
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
31
|
+
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
32
|
+
* that was repeated at dozens of call sites across the client and query plugins.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* resolver.file(operationFileEntry(node, node.operationId), { root, output, group })
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function operationFileEntry(node, name, extname = ".ts") {
|
|
40
|
+
return {
|
|
41
|
+
name,
|
|
42
|
+
extname,
|
|
43
|
+
tag: node.tags[0] ?? "default",
|
|
44
|
+
path: node.path
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Resolves a dependency plugin's generated file for `node.operationId`, cached in `cache` (the
|
|
49
|
+
* current node's `ctx.cache`) under the resolver's own plugin name. Several dependents reading the
|
|
50
|
+
* same dependency for the same operation in one pass (a query plugin's several hook generators, the
|
|
51
|
+
* MCP handler, ...) share one computed name and path instead of each calling `resolver.file` again.
|
|
52
|
+
*
|
|
53
|
+
* @example Cache `plugin-ts`'s file for the current operation
|
|
54
|
+
* ```ts
|
|
55
|
+
* const fileTs = resolveDependencyOperationFile({ cache: ctx.cache, node, resolver: tsResolver, root, output })
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function resolveDependencyOperationFile(options) {
|
|
59
|
+
const { cache, node, resolver, root, output, group } = options;
|
|
60
|
+
return cache.ensureItem(`${resolver.pluginName}:operationFile`, () => resolver.file({
|
|
61
|
+
...operationFileEntry(node, node.operationId),
|
|
62
|
+
root,
|
|
63
|
+
output,
|
|
64
|
+
group: group ?? void 0
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
function getOperationLink(node, link) {
|
|
68
|
+
if (!link) return null;
|
|
69
|
+
if (typeof link === "function") return link(node) ?? null;
|
|
70
|
+
return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
|
|
74
|
+
* are present and the union, default, and form-data flags the client uses to pick one.
|
|
75
|
+
*/
|
|
76
|
+
function buildContentTypeInfo(contentTypes) {
|
|
77
|
+
const isMultipleContentTypes = contentTypes.length > 1;
|
|
78
|
+
return {
|
|
79
|
+
contentTypes,
|
|
80
|
+
isMultipleContentTypes,
|
|
81
|
+
contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
|
|
82
|
+
defaultContentType: contentTypes[0] ?? "application/json",
|
|
83
|
+
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function getContentTypeInfo(node) {
|
|
87
|
+
return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The request-body counterpart for the primary success response: the content types it documents and
|
|
91
|
+
* whether several are present, so the client can let a caller pick which one to accept.
|
|
92
|
+
*/
|
|
93
|
+
function getResponseContentTypeInfo(node) {
|
|
94
|
+
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Reads the single base content type of an operation's primary success response, lowercased and
|
|
98
|
+
* stripped of any `; charset=...` suffix. Returns `undefined` when the response declares zero or
|
|
99
|
+
* more than one content type, since neither case has a single type to act on.
|
|
100
|
+
*/
|
|
101
|
+
function getPrimarySuccessContentType(node) {
|
|
102
|
+
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
103
|
+
if (contentTypes.length !== 1) return void 0;
|
|
104
|
+
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Whether an operation streams its primary success response as Server-Sent Events
|
|
108
|
+
* (`text/event-stream`). The client generator uses this to return a typed event stream instead of a
|
|
109
|
+
* one-shot `RequestResult`.
|
|
110
|
+
*/
|
|
111
|
+
function isEventStream(node) {
|
|
112
|
+
return getPrimarySuccessContentType(node) === "text/event-stream";
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Derives the default `responseType` for an operation from its primary success response.
|
|
116
|
+
*
|
|
117
|
+
* Returns a value only when that response declares a single non-JSON content type. `text/event-stream`
|
|
118
|
+
* and other binary types (`application/octet-stream`, `application/pdf`, `image/*`, `audio/*`,
|
|
119
|
+
* `video/*`) map to a stream or `'blob'`, and other `text/*` maps to `'text'`. Otherwise `undefined`,
|
|
120
|
+
* leaving the runtime client's `Content-Type` auto-detection in charge.
|
|
121
|
+
*/
|
|
122
|
+
function getResponseType(node) {
|
|
123
|
+
const baseType = getPrimarySuccessContentType(node);
|
|
124
|
+
if (!baseType) return void 0;
|
|
125
|
+
if (baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json") return void 0;
|
|
126
|
+
if (baseType === "text/event-stream") return "stream";
|
|
127
|
+
if (baseType.startsWith("text/")) return "text";
|
|
128
|
+
if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Which of the grouped request options an operation carries.
|
|
132
|
+
*/
|
|
133
|
+
function getRequestGroups(node) {
|
|
134
|
+
const { path, query, header } = getOperationParameters(node);
|
|
135
|
+
return {
|
|
136
|
+
path: path.length > 0,
|
|
137
|
+
query: query.length > 0,
|
|
138
|
+
body: Boolean(node.requestBody?.content?.[0]?.schema),
|
|
139
|
+
headers: header.length > 0
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Resolves which grouped request options an operation carries together with whether each group
|
|
144
|
+
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
145
|
+
* required, matching the generated `RequestConfig` type.
|
|
146
|
+
*/
|
|
147
|
+
function getRequestGroupOptionality(node) {
|
|
148
|
+
const groups = getRequestGroups(node);
|
|
149
|
+
const { path, query, header } = getOperationParameters(node);
|
|
150
|
+
const hasRequiredPath = path.some((param) => param.required);
|
|
151
|
+
const hasRequiredQuery = query.some((param) => param.required);
|
|
152
|
+
const hasRequiredHeader = header.some((param) => param.required);
|
|
153
|
+
return {
|
|
154
|
+
groups,
|
|
155
|
+
hasRequiredPath,
|
|
156
|
+
hasRequiredQuery,
|
|
157
|
+
hasRequiredHeader,
|
|
158
|
+
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function buildOperationComments(node, options = {}) {
|
|
162
|
+
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
163
|
+
const linkComment = getOperationLink(node, link);
|
|
164
|
+
const filteredComments = (linkPosition === "beforeDeprecated" ? [
|
|
165
|
+
node.description && `@description ${node.description}`,
|
|
166
|
+
node.summary && `@summary ${node.summary}`,
|
|
167
|
+
linkComment,
|
|
168
|
+
node.deprecated && "@deprecated"
|
|
169
|
+
] : [
|
|
170
|
+
node.description && `@description ${node.description}`,
|
|
171
|
+
node.summary && `@summary ${node.summary}`,
|
|
172
|
+
node.deprecated && "@deprecated",
|
|
173
|
+
linkComment
|
|
174
|
+
]).filter((comment) => Boolean(comment));
|
|
175
|
+
if (!splitLines) return filteredComments;
|
|
176
|
+
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
177
|
+
}
|
|
178
|
+
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
179
|
+
/**
|
|
180
|
+
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
181
|
+
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
182
|
+
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
183
|
+
* same parameters once per plugin.
|
|
184
|
+
*/
|
|
185
|
+
function getOperationParameters(node) {
|
|
186
|
+
const cached = operationParameterGroupsByNode.get(node);
|
|
187
|
+
if (cached) return cached;
|
|
188
|
+
const groups = {
|
|
189
|
+
path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
|
|
190
|
+
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
191
|
+
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
192
|
+
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
193
|
+
};
|
|
194
|
+
operationParameterGroupsByNode.set(node, groups);
|
|
195
|
+
return groups;
|
|
196
|
+
}
|
|
197
|
+
function getStatusCodeNumber(statusCode) {
|
|
198
|
+
const code = Number(statusCode);
|
|
199
|
+
return Number.isNaN(code) ? null : code;
|
|
200
|
+
}
|
|
201
|
+
function isSuccessStatusCode(statusCode) {
|
|
202
|
+
const code = getStatusCodeNumber(statusCode);
|
|
203
|
+
return code !== null && code >= 200 && code < 300;
|
|
204
|
+
}
|
|
205
|
+
function getSuccessResponses(responses) {
|
|
206
|
+
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
207
|
+
}
|
|
208
|
+
function getOperationSuccessResponses(node) {
|
|
209
|
+
return getSuccessResponses(node.responses);
|
|
210
|
+
}
|
|
211
|
+
function getPrimarySuccessResponse(node) {
|
|
212
|
+
return getOperationSuccessResponses(node)[0] ?? null;
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
9
215
|
//#region ../../internals/utils/src/casing.ts
|
|
10
216
|
/**
|
|
11
217
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -201,252 +407,6 @@ function buildJSDoc(comments, options = {}) {
|
|
|
201
407
|
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
|
|
202
408
|
}
|
|
203
409
|
//#endregion
|
|
204
|
-
//#region ../../internals/utils/src/url.ts
|
|
205
|
-
/**
|
|
206
|
-
* Keeps the OpenAPI parameter name as-is when it is already a valid JS identifier, and
|
|
207
|
-
* camelCases it only enough to become one otherwise (for example a hyphenated path segment).
|
|
208
|
-
*/
|
|
209
|
-
function transformParam(raw) {
|
|
210
|
-
return isValidVarName(raw) ? raw : camelCase(raw);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Helpers for OpenAPI/Swagger paths.
|
|
214
|
-
*/
|
|
215
|
-
var Url = class Url {
|
|
216
|
-
/**
|
|
217
|
-
* Converts an OpenAPI/Swagger path to Express-style colon syntax.
|
|
218
|
-
*
|
|
219
|
-
* @example
|
|
220
|
-
* Url.toPath('/pet/{petId}') // '/pet/:petId'
|
|
221
|
-
*/
|
|
222
|
-
static toPath(path) {
|
|
223
|
-
return path.replace(/\{([^}]+)\}/g, ":$1");
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Rewrites OpenAPI placeholder names while keeping the `{...}` braces, so the generated `url`
|
|
227
|
-
* literal aligns with the grouped `path` request option that the runtime client interpolates by
|
|
228
|
-
* key.
|
|
229
|
-
*
|
|
230
|
-
* @example
|
|
231
|
-
* Url.toSafeTemplate('/user/{monetary-account-id}') // '/user/{monetaryAccountId}'
|
|
232
|
-
*/
|
|
233
|
-
static toSafeTemplate(path) {
|
|
234
|
-
return path.replace(/\{([^}]+)\}/g, (_, name) => `{${transformParam(name)}}`);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Converts an OpenAPI/Swagger path to a TypeScript template literal string.
|
|
238
|
-
* `prefix` is prepended inside the literal, and `replacer` transforms each parameter name.
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* Url.toTemplateString('/pet/{petId}') // '`/pet/${petId}`'
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* Url.toTemplateString('/pet/{petId}', { prefix: 'https://api' }) // '`https://api/pet/${petId}`'
|
|
245
|
-
*/
|
|
246
|
-
static toTemplateString(path, { prefix, replacer } = {}) {
|
|
247
|
-
const result = path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
248
|
-
if (i % 2 === 0) return part;
|
|
249
|
-
const param = transformParam(part);
|
|
250
|
-
return `\${${replacer ? replacer(param) : param}}`;
|
|
251
|
-
}).join("");
|
|
252
|
-
return `\`${prefix ?? ""}${result}\``;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Converts an OpenAPI/Swagger path to a template literal that reads each parameter off the
|
|
256
|
-
* grouped `path` request option, e.g. `/pet/{petId}` becomes `` `/pet/${path.petId}` ``. Parameter
|
|
257
|
-
* names match the generated `path` type, and `prefix` is prepended inside the literal. Shared by
|
|
258
|
-
* the client and cypress generators that pass a grouped `path` object.
|
|
259
|
-
*
|
|
260
|
-
* @example
|
|
261
|
-
* Url.toGroupedTemplateString('/pet/{petId}') // '`/pet/${path.petId}`'
|
|
262
|
-
*/
|
|
263
|
-
static toGroupedTemplateString(path, { prefix } = {}) {
|
|
264
|
-
return Url.toTemplateString(path, {
|
|
265
|
-
prefix,
|
|
266
|
-
replacer: (name) => `path.${name}`
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
//#endregion
|
|
271
|
-
//#region ../../internals/shared/src/params.ts
|
|
272
|
-
/**
|
|
273
|
-
* Drops parameters that share the same name, keeping the first.
|
|
274
|
-
*
|
|
275
|
-
* A malformed spec can declare the same parameter name twice within one `in` location. Both would
|
|
276
|
-
* resolve to the same output property, so emitting both would yield an object type with a duplicate
|
|
277
|
-
* member, which TypeScript rejects. This is a defensive guard against that case, not a casing guard:
|
|
278
|
-
* parameter names flow through unchanged, so no two distinct names ever collide here anymore.
|
|
279
|
-
*/
|
|
280
|
-
function dedupeParams(params) {
|
|
281
|
-
const seen = /* @__PURE__ */ new Set();
|
|
282
|
-
return params.filter((param) => {
|
|
283
|
-
if (seen.has(param.name)) return false;
|
|
284
|
-
seen.add(param.name);
|
|
285
|
-
return true;
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
//#endregion
|
|
289
|
-
//#region ../../internals/shared/src/operation.ts
|
|
290
|
-
/**
|
|
291
|
-
* Builds the `ResolverFileParams` every operation generator passes to
|
|
292
|
-
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
293
|
-
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
294
|
-
* that was repeated at dozens of call sites across the client and query plugins.
|
|
295
|
-
*
|
|
296
|
-
* @example
|
|
297
|
-
* ```ts
|
|
298
|
-
* resolver.file(operationFileEntry(node, node.operationId), { root, output, group })
|
|
299
|
-
* ```
|
|
300
|
-
*/
|
|
301
|
-
function operationFileEntry(node, name, extname = ".ts") {
|
|
302
|
-
return {
|
|
303
|
-
name,
|
|
304
|
-
extname,
|
|
305
|
-
tag: node.tags[0] ?? "default",
|
|
306
|
-
path: node.path
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
function getOperationLink(node, link) {
|
|
310
|
-
if (!link) return null;
|
|
311
|
-
if (typeof link === "function") return link(node) ?? null;
|
|
312
|
-
if (link === "urlPath") return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
|
|
313
|
-
return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
|
|
317
|
-
* are present and the union, default, and form-data flags the client uses to pick one.
|
|
318
|
-
*/
|
|
319
|
-
function buildContentTypeInfo(contentTypes) {
|
|
320
|
-
const isMultipleContentTypes = contentTypes.length > 1;
|
|
321
|
-
return {
|
|
322
|
-
contentTypes,
|
|
323
|
-
isMultipleContentTypes,
|
|
324
|
-
contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
|
|
325
|
-
defaultContentType: contentTypes[0] ?? "application/json",
|
|
326
|
-
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
function getContentTypeInfo(node) {
|
|
330
|
-
return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
|
|
331
|
-
}
|
|
332
|
-
/**
|
|
333
|
-
* The request-body counterpart for the primary success response: the content types it documents and
|
|
334
|
-
* whether several are present, so the client can let a caller pick which one to accept.
|
|
335
|
-
*/
|
|
336
|
-
function getResponseContentTypeInfo(node) {
|
|
337
|
-
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Reads the single base content type of an operation's primary success response, lowercased and
|
|
341
|
-
* stripped of any `; charset=...` suffix. Returns `undefined` when the response declares zero or
|
|
342
|
-
* more than one content type, since neither case has a single type to act on.
|
|
343
|
-
*/
|
|
344
|
-
function getPrimarySuccessContentType(node) {
|
|
345
|
-
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
346
|
-
if (contentTypes.length !== 1) return void 0;
|
|
347
|
-
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Whether an operation streams its primary success response as Server-Sent Events
|
|
351
|
-
* (`text/event-stream`). The client generator uses this to return a typed event stream instead of a
|
|
352
|
-
* one-shot `RequestResult`.
|
|
353
|
-
*/
|
|
354
|
-
function isEventStream(node) {
|
|
355
|
-
return getPrimarySuccessContentType(node) === "text/event-stream";
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* Derives the default `responseType` for an operation from its primary success response.
|
|
359
|
-
*
|
|
360
|
-
* Returns a value only when that response declares a single non-JSON content type. `text/event-stream`
|
|
361
|
-
* and other binary types (`application/octet-stream`, `application/pdf`, `image/*`, `audio/*`,
|
|
362
|
-
* `video/*`) map to a stream or `'blob'`, and other `text/*` maps to `'text'`. Otherwise `undefined`,
|
|
363
|
-
* leaving the runtime client's `Content-Type` auto-detection in charge.
|
|
364
|
-
*/
|
|
365
|
-
function getResponseType(node) {
|
|
366
|
-
const baseType = getPrimarySuccessContentType(node);
|
|
367
|
-
if (!baseType) return void 0;
|
|
368
|
-
if (baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json") return void 0;
|
|
369
|
-
if (baseType === "text/event-stream") return "stream";
|
|
370
|
-
if (baseType.startsWith("text/")) return "text";
|
|
371
|
-
if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Which of the grouped request options an operation carries.
|
|
375
|
-
*/
|
|
376
|
-
function getRequestGroups(node) {
|
|
377
|
-
const { path, query, header } = getOperationParameters(node);
|
|
378
|
-
return {
|
|
379
|
-
path: path.length > 0,
|
|
380
|
-
query: query.length > 0,
|
|
381
|
-
body: Boolean(node.requestBody?.content?.[0]?.schema),
|
|
382
|
-
headers: header.length > 0
|
|
383
|
-
};
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Resolves which grouped request options an operation carries together with whether each group
|
|
387
|
-
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
388
|
-
* required, matching the generated `RequestConfig` type.
|
|
389
|
-
*/
|
|
390
|
-
function getRequestGroupOptionality(node) {
|
|
391
|
-
const groups = getRequestGroups(node);
|
|
392
|
-
const { path, query, header } = getOperationParameters(node);
|
|
393
|
-
const hasRequiredPath = path.some((param) => param.required);
|
|
394
|
-
const hasRequiredQuery = query.some((param) => param.required);
|
|
395
|
-
const hasRequiredHeader = header.some((param) => param.required);
|
|
396
|
-
return {
|
|
397
|
-
groups,
|
|
398
|
-
hasRequiredPath,
|
|
399
|
-
hasRequiredQuery,
|
|
400
|
-
hasRequiredHeader,
|
|
401
|
-
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
|
|
402
|
-
};
|
|
403
|
-
}
|
|
404
|
-
function buildOperationComments(node, options = {}) {
|
|
405
|
-
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
406
|
-
const linkComment = getOperationLink(node, link);
|
|
407
|
-
const filteredComments = (linkPosition === "beforeDeprecated" ? [
|
|
408
|
-
node.description && `@description ${node.description}`,
|
|
409
|
-
node.summary && `@summary ${node.summary}`,
|
|
410
|
-
linkComment,
|
|
411
|
-
node.deprecated && "@deprecated"
|
|
412
|
-
] : [
|
|
413
|
-
node.description && `@description ${node.description}`,
|
|
414
|
-
node.summary && `@summary ${node.summary}`,
|
|
415
|
-
node.deprecated && "@deprecated",
|
|
416
|
-
linkComment
|
|
417
|
-
]).filter((comment) => Boolean(comment));
|
|
418
|
-
if (!splitLines) return filteredComments;
|
|
419
|
-
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
420
|
-
}
|
|
421
|
-
function getOperationParameters(node) {
|
|
422
|
-
return {
|
|
423
|
-
path: dedupeParams(node.parameters.filter((param) => param.in === "path").map((param) => isValidVarName(param.name) ? param : {
|
|
424
|
-
...param,
|
|
425
|
-
name: camelCase(param.name)
|
|
426
|
-
})),
|
|
427
|
-
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
428
|
-
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
429
|
-
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
430
|
-
};
|
|
431
|
-
}
|
|
432
|
-
function getStatusCodeNumber(statusCode) {
|
|
433
|
-
const code = Number(statusCode);
|
|
434
|
-
return Number.isNaN(code) ? null : code;
|
|
435
|
-
}
|
|
436
|
-
function isSuccessStatusCode(statusCode) {
|
|
437
|
-
const code = getStatusCodeNumber(statusCode);
|
|
438
|
-
return code !== null && code >= 200 && code < 300;
|
|
439
|
-
}
|
|
440
|
-
function getSuccessResponses(responses) {
|
|
441
|
-
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
442
|
-
}
|
|
443
|
-
function getOperationSuccessResponses(node) {
|
|
444
|
-
return getSuccessResponses(node.responses);
|
|
445
|
-
}
|
|
446
|
-
function getPrimarySuccessResponse(node) {
|
|
447
|
-
return getOperationSuccessResponses(node)[0] ?? null;
|
|
448
|
-
}
|
|
449
|
-
//#endregion
|
|
450
410
|
//#region ../../internals/shared/src/group.ts
|
|
451
411
|
/**
|
|
452
412
|
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
@@ -709,7 +669,7 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
|
709
669
|
const securityLiteral = buildSecurityMetadata({ security });
|
|
710
670
|
return `{ ${[
|
|
711
671
|
`method: '${node.method.toUpperCase()}'`,
|
|
712
|
-
`url: '${
|
|
672
|
+
`url: '${node.path}'`,
|
|
713
673
|
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
714
674
|
validatorLiteral,
|
|
715
675
|
"...config"
|
|
@@ -838,7 +798,7 @@ function Operation({ name, node, tsResolver, zodResolver, validator, security, i
|
|
|
838
798
|
const validatorLiteral = validatorEntries.length ? `validator: { ${validatorEntries.join(", ")} }` : null;
|
|
839
799
|
const callConfig = `{ ${[
|
|
840
800
|
`method: '${node.method.toUpperCase()}'`,
|
|
841
|
-
`url: '${
|
|
801
|
+
`url: '${node.path}'`,
|
|
842
802
|
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
843
803
|
stylesLiteral ? `styles: ${stylesLiteral}` : null,
|
|
844
804
|
validatorLiteral,
|
|
@@ -908,6 +868,125 @@ function SdkClient({ name, isExportable = true, isIndexable = true, operations,
|
|
|
908
868
|
});
|
|
909
869
|
}
|
|
910
870
|
//#endregion
|
|
871
|
+
//#region ../../internals/client/src/generators/clientGenerator.tsx
|
|
872
|
+
/**
|
|
873
|
+
* Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`,
|
|
874
|
+
* `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared
|
|
875
|
+
* `Operation` component: a grouped `<Name>Request` type and a function that forwards a single
|
|
876
|
+
* `options` object to the bundled `client` and returns the `RequestResult`. Only the generator
|
|
877
|
+
* `name` differs between plugins; every other resolution, import, and rendering step is identical.
|
|
878
|
+
*/
|
|
879
|
+
function createClientGenerator(name) {
|
|
880
|
+
return defineGenerator({
|
|
881
|
+
name,
|
|
882
|
+
renderer: jsxRenderer,
|
|
883
|
+
operation(node, ctx) {
|
|
884
|
+
if (!ast.isHttpOperationNode(node)) return null;
|
|
885
|
+
const { config, driver, resolver, root } = ctx;
|
|
886
|
+
const { output, validator, group } = ctx.options;
|
|
887
|
+
const pluginTs = driver.getPlugin(pluginTsName);
|
|
888
|
+
if (!pluginTs) return null;
|
|
889
|
+
const tsResolver = driver.getResolver(pluginTsName);
|
|
890
|
+
const pluginZod = resolveResponseValidator(validator) === "zod" || resolveRequestValidator(validator) === "zod" ? driver.getPlugin(pluginZodName) : null;
|
|
891
|
+
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null;
|
|
892
|
+
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
893
|
+
const importedTypeNames = [tsResolver.response.options(node), tsResolver.response.responses(node)];
|
|
894
|
+
const importedZodNames = zodResolver ? [
|
|
895
|
+
resolveResponseValidator(validator) === "zod" ? zodResolver.response.response?.(node) : null,
|
|
896
|
+
resolveResponseValidator(validator) === "zod" ? buildZodErrorParse(node, zodResolver)?.expression ?? null : null,
|
|
897
|
+
resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body?.(node) : null
|
|
898
|
+
].filter((name) => Boolean(name)) : [];
|
|
899
|
+
const meta = {
|
|
900
|
+
name: resolver.name(node.operationId),
|
|
901
|
+
file: resolver.file({
|
|
902
|
+
...operationFileEntry(node, node.operationId),
|
|
903
|
+
root,
|
|
904
|
+
output,
|
|
905
|
+
group: group ?? void 0
|
|
906
|
+
}),
|
|
907
|
+
fileTs: resolveDependencyOperationFile({
|
|
908
|
+
cache: ctx.cache,
|
|
909
|
+
node,
|
|
910
|
+
resolver: tsResolver,
|
|
911
|
+
root,
|
|
912
|
+
output: pluginTs.options?.output ?? output,
|
|
913
|
+
group: pluginTs.options?.group
|
|
914
|
+
}),
|
|
915
|
+
fileZod: zodResolver && pluginZod?.options ? zodResolver.file({
|
|
916
|
+
...operationFileEntry(node, node.operationId),
|
|
917
|
+
root,
|
|
918
|
+
output: pluginZod.options.output ?? output,
|
|
919
|
+
group: pluginZod.options?.group ?? void 0
|
|
920
|
+
}) : null
|
|
921
|
+
};
|
|
922
|
+
const security = getOperationSecurity({
|
|
923
|
+
document: ctx.adapter.document,
|
|
924
|
+
method: node.method,
|
|
925
|
+
path: node.path
|
|
926
|
+
});
|
|
927
|
+
const clientPath = path.resolve(root, ".kubb/client.ts");
|
|
928
|
+
const eventStream = isEventStream(node);
|
|
929
|
+
return /* @__PURE__ */ jsxs(File, {
|
|
930
|
+
baseName: meta.file.baseName,
|
|
931
|
+
path: meta.file.path,
|
|
932
|
+
meta: meta.file.meta,
|
|
933
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
934
|
+
output,
|
|
935
|
+
config,
|
|
936
|
+
file: {
|
|
937
|
+
path: meta.file.path,
|
|
938
|
+
baseName: meta.file.baseName
|
|
939
|
+
}
|
|
940
|
+
}),
|
|
941
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
942
|
+
output,
|
|
943
|
+
config,
|
|
944
|
+
file: {
|
|
945
|
+
path: meta.file.path,
|
|
946
|
+
baseName: meta.file.baseName
|
|
947
|
+
}
|
|
948
|
+
}),
|
|
949
|
+
children: [
|
|
950
|
+
/* @__PURE__ */ jsx(File.Import, {
|
|
951
|
+
name: eventStream ? ["client", "toEventStream"] : ["client"],
|
|
952
|
+
root: meta.file.path,
|
|
953
|
+
path: clientPath
|
|
954
|
+
}),
|
|
955
|
+
/* @__PURE__ */ jsx(File.Import, {
|
|
956
|
+
name: eventStream ? [
|
|
957
|
+
"Options",
|
|
958
|
+
"EventStreamResult",
|
|
959
|
+
"SuccessOf"
|
|
960
|
+
] : ["Options", "RequestResult"],
|
|
961
|
+
root: meta.file.path,
|
|
962
|
+
path: clientPath,
|
|
963
|
+
isTypeOnly: true
|
|
964
|
+
}),
|
|
965
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
966
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
967
|
+
root: meta.file.path,
|
|
968
|
+
path: meta.fileTs.path,
|
|
969
|
+
isTypeOnly: true
|
|
970
|
+
}),
|
|
971
|
+
meta.fileZod && importedZodNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
972
|
+
name: importedZodNames,
|
|
973
|
+
root: meta.file.path,
|
|
974
|
+
path: meta.fileZod.path
|
|
975
|
+
}),
|
|
976
|
+
/* @__PURE__ */ jsx(Operation, {
|
|
977
|
+
name: meta.name,
|
|
978
|
+
node,
|
|
979
|
+
tsResolver,
|
|
980
|
+
zodResolver,
|
|
981
|
+
validator,
|
|
982
|
+
security
|
|
983
|
+
})
|
|
984
|
+
]
|
|
985
|
+
});
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
//#endregion
|
|
911
990
|
//#region ../../internals/client/src/components/SdkFacade.tsx
|
|
912
991
|
/**
|
|
913
992
|
* Renders a composed root SDK class that instantiates every tag client from one shared config, so
|
|
@@ -1177,7 +1256,7 @@ function createSdkGenerator() {
|
|
|
1177
1256
|
* drops union members a broader scalar already covers, keeping the generated response and error
|
|
1178
1257
|
* unions tidy. A plugin wires them with `ctx.setMacros([...defaultMacros, ...userMacros])`.
|
|
1179
1258
|
*/
|
|
1180
|
-
const defaultMacros = [
|
|
1259
|
+
const defaultMacros = [macroSimplifyUnion];
|
|
1181
1260
|
//#endregion
|
|
1182
1261
|
//#region ../../internals/client/src/resolver.ts
|
|
1183
1262
|
/**
|
|
@@ -1206,115 +1285,10 @@ const resolverClient = createResolver({
|
|
|
1206
1285
|
//#region src/generators/clientGenerator.tsx
|
|
1207
1286
|
/**
|
|
1208
1287
|
* Built-in operation generator for `@kubb/plugin-fetch`. Emits one async function per OpenAPI
|
|
1209
|
-
* operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
|
|
1210
|
-
* forwards a single `options` object to the bundled `client` and returns the `RequestResult`.
|
|
1288
|
+
* operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
|
|
1289
|
+
* that forwards a single `options` object to the bundled `client` and returns the `RequestResult`.
|
|
1211
1290
|
*/
|
|
1212
|
-
const clientGenerator =
|
|
1213
|
-
name: "fetch",
|
|
1214
|
-
renderer: jsxRenderer,
|
|
1215
|
-
operation(node, ctx) {
|
|
1216
|
-
if (!ast.isHttpOperationNode(node)) return null;
|
|
1217
|
-
const { config, driver, resolver, root } = ctx;
|
|
1218
|
-
const { output, validator, group } = ctx.options;
|
|
1219
|
-
const pluginTs = driver.getPlugin(pluginTsName);
|
|
1220
|
-
if (!pluginTs) return null;
|
|
1221
|
-
const tsResolver = driver.getResolver(pluginTsName);
|
|
1222
|
-
const pluginZod = resolveResponseValidator(validator) === "zod" || resolveRequestValidator(validator) === "zod" ? driver.getPlugin(pluginZodName) : null;
|
|
1223
|
-
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null;
|
|
1224
|
-
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
1225
|
-
const importedTypeNames = [tsResolver.response.options(node), tsResolver.response.responses(node)];
|
|
1226
|
-
const importedZodNames = zodResolver ? [
|
|
1227
|
-
resolveResponseValidator(validator) === "zod" ? zodResolver.response.response?.(node) : null,
|
|
1228
|
-
resolveResponseValidator(validator) === "zod" ? buildZodErrorParse(node, zodResolver)?.expression ?? null : null,
|
|
1229
|
-
resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body?.(node) : null
|
|
1230
|
-
].filter((name) => Boolean(name)) : [];
|
|
1231
|
-
const meta = {
|
|
1232
|
-
name: resolver.name(node.operationId),
|
|
1233
|
-
file: resolver.file({
|
|
1234
|
-
...operationFileEntry(node, node.operationId),
|
|
1235
|
-
root,
|
|
1236
|
-
output,
|
|
1237
|
-
group: group ?? void 0
|
|
1238
|
-
}),
|
|
1239
|
-
fileTs: tsResolver.file({
|
|
1240
|
-
...operationFileEntry(node, node.operationId),
|
|
1241
|
-
root,
|
|
1242
|
-
output: pluginTs.options?.output ?? output,
|
|
1243
|
-
group: pluginTs.options?.group ?? void 0
|
|
1244
|
-
}),
|
|
1245
|
-
fileZod: zodResolver && pluginZod?.options ? zodResolver.file({
|
|
1246
|
-
...operationFileEntry(node, node.operationId),
|
|
1247
|
-
root,
|
|
1248
|
-
output: pluginZod.options.output ?? output,
|
|
1249
|
-
group: pluginZod.options?.group ?? void 0
|
|
1250
|
-
}) : null
|
|
1251
|
-
};
|
|
1252
|
-
const security = getOperationSecurity({
|
|
1253
|
-
document: ctx.adapter.document,
|
|
1254
|
-
method: node.method,
|
|
1255
|
-
path: node.path
|
|
1256
|
-
});
|
|
1257
|
-
const clientPath = path.resolve(root, ".kubb/client.ts");
|
|
1258
|
-
const eventStream = isEventStream(node);
|
|
1259
|
-
return /* @__PURE__ */ jsxs(File, {
|
|
1260
|
-
baseName: meta.file.baseName,
|
|
1261
|
-
path: meta.file.path,
|
|
1262
|
-
meta: meta.file.meta,
|
|
1263
|
-
banner: resolver.default.banner(ctx.meta, {
|
|
1264
|
-
output,
|
|
1265
|
-
config,
|
|
1266
|
-
file: {
|
|
1267
|
-
path: meta.file.path,
|
|
1268
|
-
baseName: meta.file.baseName
|
|
1269
|
-
}
|
|
1270
|
-
}),
|
|
1271
|
-
footer: resolver.default.footer(ctx.meta, {
|
|
1272
|
-
output,
|
|
1273
|
-
config,
|
|
1274
|
-
file: {
|
|
1275
|
-
path: meta.file.path,
|
|
1276
|
-
baseName: meta.file.baseName
|
|
1277
|
-
}
|
|
1278
|
-
}),
|
|
1279
|
-
children: [
|
|
1280
|
-
/* @__PURE__ */ jsx(File.Import, {
|
|
1281
|
-
name: eventStream ? ["client", "toEventStream"] : ["client"],
|
|
1282
|
-
root: meta.file.path,
|
|
1283
|
-
path: clientPath
|
|
1284
|
-
}),
|
|
1285
|
-
/* @__PURE__ */ jsx(File.Import, {
|
|
1286
|
-
name: eventStream ? [
|
|
1287
|
-
"Options",
|
|
1288
|
-
"EventStreamResult",
|
|
1289
|
-
"SuccessOf"
|
|
1290
|
-
] : ["Options", "RequestResult"],
|
|
1291
|
-
root: meta.file.path,
|
|
1292
|
-
path: clientPath,
|
|
1293
|
-
isTypeOnly: true
|
|
1294
|
-
}),
|
|
1295
|
-
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
1296
|
-
name: Array.from(new Set(importedTypeNames)),
|
|
1297
|
-
root: meta.file.path,
|
|
1298
|
-
path: meta.fileTs.path,
|
|
1299
|
-
isTypeOnly: true
|
|
1300
|
-
}),
|
|
1301
|
-
meta.fileZod && importedZodNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
1302
|
-
name: importedZodNames,
|
|
1303
|
-
root: meta.file.path,
|
|
1304
|
-
path: meta.fileZod.path
|
|
1305
|
-
}),
|
|
1306
|
-
/* @__PURE__ */ jsx(Operation, {
|
|
1307
|
-
name: meta.name,
|
|
1308
|
-
node,
|
|
1309
|
-
tsResolver,
|
|
1310
|
-
zodResolver,
|
|
1311
|
-
validator,
|
|
1312
|
-
security
|
|
1313
|
-
})
|
|
1314
|
-
]
|
|
1315
|
-
});
|
|
1316
|
-
}
|
|
1317
|
-
});
|
|
1291
|
+
const clientGenerator = createClientGenerator("fetch");
|
|
1318
1292
|
//#endregion
|
|
1319
1293
|
//#region src/templates.ts
|
|
1320
1294
|
/** Absolute path to the fetch client template, copied into `.kubb/client.ts`. */
|