@kubb/plugin-client 5.0.0-beta.64 → 5.7.0-canary.20260930T095256
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/README.md +8 -61
- package/dist/index.cjs +753 -1749
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +168 -347
- package/dist/index.js +729 -1734
- package/dist/index.js.map +1 -1
- package/package.json +12 -64
- package/src/generators/clientGenerator.tsx +9 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +71 -0
- package/src/types.ts +54 -0
- package/dist/chunk-Bx3C2hgW.cjs +0 -38
- package/dist/clients/axios.cjs +0 -68
- package/dist/clients/axios.cjs.map +0 -1
- package/dist/clients/axios.d.ts +0 -49
- package/dist/clients/axios.js +0 -58
- package/dist/clients/axios.js.map +0 -1
- package/dist/clients/fetch.cjs +0 -128
- package/dist/clients/fetch.cjs.map +0 -1
- package/dist/clients/fetch.d.ts +0 -50
- package/dist/clients/fetch.js +0 -120
- package/dist/clients/fetch.js.map +0 -1
- package/dist/templates/clients/axios.source.cjs +0 -7
- package/dist/templates/clients/axios.source.cjs.map +0 -1
- package/dist/templates/clients/axios.source.d.ts +0 -7
- package/dist/templates/clients/axios.source.js +0 -6
- package/dist/templates/clients/axios.source.js.map +0 -1
- package/dist/templates/clients/fetch.source.cjs +0 -7
- package/dist/templates/clients/fetch.source.cjs.map +0 -1
- package/dist/templates/clients/fetch.source.d.ts +0 -7
- package/dist/templates/clients/fetch.source.js +0 -6
- package/dist/templates/clients/fetch.source.js.map +0 -1
- package/dist/templates/config.source.cjs +0 -7
- package/dist/templates/config.source.cjs.map +0 -1
- package/dist/templates/config.source.d.ts +0 -7
- package/dist/templates/config.source.js +0 -6
- package/dist/templates/config.source.js.map +0 -1
- /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -2,19 +2,241 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
var __create = Object.create;
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
27
|
+
let kubb_kit = require("kubb/kit");
|
|
9
28
|
let node_path = require("node:path");
|
|
10
|
-
|
|
11
|
-
node_path = require_chunk.__toESM(node_path);
|
|
12
|
-
let _kubb_core = require("@kubb/core");
|
|
13
|
-
let _kubb_ast_utils = require("@kubb/ast/utils");
|
|
29
|
+
node_path = __toESM(node_path, 1);
|
|
14
30
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
15
|
-
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
16
|
-
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
17
31
|
let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
32
|
+
let kubb_jsx = require("kubb/jsx");
|
|
33
|
+
let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
|
|
34
|
+
//#region ../../internals/shared/src/params.ts
|
|
35
|
+
/**
|
|
36
|
+
* Drops parameters that share the same name, keeping the first.
|
|
37
|
+
*
|
|
38
|
+
* A malformed spec can declare the same parameter name twice within one `in` location. Both would
|
|
39
|
+
* resolve to the same output property, so emitting both would yield an object type with a duplicate
|
|
40
|
+
* member, which TypeScript rejects. This is a defensive guard against that case, not a casing guard:
|
|
41
|
+
* parameter names flow through unchanged, so no two distinct names ever collide here anymore.
|
|
42
|
+
*/
|
|
43
|
+
function dedupeParams(params) {
|
|
44
|
+
const seen = /* @__PURE__ */ new Set();
|
|
45
|
+
return params.filter((param) => {
|
|
46
|
+
if (seen.has(param.name)) return false;
|
|
47
|
+
seen.add(param.name);
|
|
48
|
+
return true;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region ../../internals/shared/src/operation.ts
|
|
53
|
+
/**
|
|
54
|
+
* Builds the `ResolverFileParams` every operation generator passes to
|
|
55
|
+
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
56
|
+
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
57
|
+
* that was repeated at dozens of call sites across the client and query plugins.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* resolver.file(operationFileEntry(node, node.operationId), { root, output, group })
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function operationFileEntry(node, name, extname = ".ts") {
|
|
65
|
+
return {
|
|
66
|
+
name,
|
|
67
|
+
extname,
|
|
68
|
+
tag: node.tags[0] ?? "default",
|
|
69
|
+
path: node.path
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolves a dependency plugin's generated file for `node.operationId`, cached in `cache` (the
|
|
74
|
+
* current node's `ctx.cache`) under the resolver's own plugin name. Several dependents reading the
|
|
75
|
+
* same dependency for the same operation in one pass (a query plugin's several hook generators, the
|
|
76
|
+
* MCP handler, ...) share one computed name and path instead of each calling `resolver.file` again.
|
|
77
|
+
*
|
|
78
|
+
* @example Cache `plugin-ts`'s file for the current operation
|
|
79
|
+
* ```ts
|
|
80
|
+
* const fileTs = resolveDependencyOperationFile({ cache: ctx.cache, node, resolver: tsResolver, root, output })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
function resolveDependencyOperationFile(options) {
|
|
84
|
+
const { cache, node, resolver, root, output, group } = options;
|
|
85
|
+
return cache.ensureItem(`${resolver.pluginName}:operationFile`, () => resolver.file({
|
|
86
|
+
...operationFileEntry(node, node.operationId),
|
|
87
|
+
root,
|
|
88
|
+
output,
|
|
89
|
+
group: group ?? void 0
|
|
90
|
+
}));
|
|
91
|
+
}
|
|
92
|
+
function getOperationLink(node, link) {
|
|
93
|
+
if (!link) return null;
|
|
94
|
+
if (typeof link === "function") return link(node) ?? null;
|
|
95
|
+
return node.path ? `{@link ${kubb_kit.Url.toPath(node.path)}}` : null;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
|
|
99
|
+
* are present and the union, default, and form-data flags the client uses to pick one.
|
|
100
|
+
*/
|
|
101
|
+
function buildContentTypeInfo(contentTypes) {
|
|
102
|
+
const isMultipleContentTypes = contentTypes.length > 1;
|
|
103
|
+
return {
|
|
104
|
+
contentTypes,
|
|
105
|
+
isMultipleContentTypes,
|
|
106
|
+
contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
|
|
107
|
+
defaultContentType: contentTypes[0] ?? "application/json",
|
|
108
|
+
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function getContentTypeInfo(node) {
|
|
112
|
+
return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The request-body counterpart for the primary success response: the content types it documents and
|
|
116
|
+
* whether several are present, so the client can let a caller pick which one to accept.
|
|
117
|
+
*/
|
|
118
|
+
function getResponseContentTypeInfo(node) {
|
|
119
|
+
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Reads the single base content type of an operation's primary success response, lowercased and
|
|
123
|
+
* stripped of any `; charset=...` suffix. Returns `undefined` when the response declares zero or
|
|
124
|
+
* more than one content type, since neither case has a single type to act on.
|
|
125
|
+
*/
|
|
126
|
+
function getPrimarySuccessContentType(node) {
|
|
127
|
+
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
128
|
+
if (contentTypes.length !== 1) return void 0;
|
|
129
|
+
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Whether an operation streams its primary success response as Server-Sent Events
|
|
133
|
+
* (`text/event-stream`). The client generator uses this to return a typed event stream instead of a
|
|
134
|
+
* one-shot `RequestResult`.
|
|
135
|
+
*/
|
|
136
|
+
function isEventStream(node) {
|
|
137
|
+
return getPrimarySuccessContentType(node) === "text/event-stream";
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Derives the default `responseType` for an operation from its primary success response.
|
|
141
|
+
*
|
|
142
|
+
* Returns a value only when that response declares a single non-JSON content type. `text/event-stream`
|
|
143
|
+
* and other binary types (`application/octet-stream`, `application/pdf`, `image/*`, `audio/*`,
|
|
144
|
+
* `video/*`) map to a stream or `'blob'`, and other `text/*` maps to `'text'`. Otherwise `undefined`,
|
|
145
|
+
* leaving the runtime client's `Content-Type` auto-detection in charge.
|
|
146
|
+
*/
|
|
147
|
+
function getResponseType(node) {
|
|
148
|
+
const baseType = getPrimarySuccessContentType(node);
|
|
149
|
+
if (!baseType) return void 0;
|
|
150
|
+
if (baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json") return void 0;
|
|
151
|
+
if (baseType === "text/event-stream") return "stream";
|
|
152
|
+
if (baseType.startsWith("text/")) return "text";
|
|
153
|
+
if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Which of the grouped request options an operation carries.
|
|
157
|
+
*/
|
|
158
|
+
function getRequestGroups(node) {
|
|
159
|
+
const { path, query, header } = getOperationParameters(node);
|
|
160
|
+
return {
|
|
161
|
+
path: path.length > 0,
|
|
162
|
+
query: query.length > 0,
|
|
163
|
+
body: Boolean(node.requestBody?.content?.[0]?.schema),
|
|
164
|
+
headers: header.length > 0
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Resolves which grouped request options an operation carries together with whether each group
|
|
169
|
+
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
170
|
+
* required, matching the generated `RequestConfig` type.
|
|
171
|
+
*/
|
|
172
|
+
function getRequestGroupOptionality(node) {
|
|
173
|
+
const groups = getRequestGroups(node);
|
|
174
|
+
const { path, query, header } = getOperationParameters(node);
|
|
175
|
+
const hasRequiredPath = path.some((param) => param.required);
|
|
176
|
+
const hasRequiredQuery = query.some((param) => param.required);
|
|
177
|
+
const hasRequiredHeader = header.some((param) => param.required);
|
|
178
|
+
return {
|
|
179
|
+
groups,
|
|
180
|
+
hasRequiredPath,
|
|
181
|
+
hasRequiredQuery,
|
|
182
|
+
hasRequiredHeader,
|
|
183
|
+
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function buildOperationComments(node, options = {}) {
|
|
187
|
+
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
188
|
+
const linkComment = getOperationLink(node, link);
|
|
189
|
+
const filteredComments = (linkPosition === "beforeDeprecated" ? [
|
|
190
|
+
node.description && `@description ${node.description}`,
|
|
191
|
+
node.summary && `@summary ${node.summary}`,
|
|
192
|
+
linkComment,
|
|
193
|
+
node.deprecated && "@deprecated"
|
|
194
|
+
] : [
|
|
195
|
+
node.description && `@description ${node.description}`,
|
|
196
|
+
node.summary && `@summary ${node.summary}`,
|
|
197
|
+
node.deprecated && "@deprecated",
|
|
198
|
+
linkComment
|
|
199
|
+
]).filter((comment) => Boolean(comment));
|
|
200
|
+
if (!splitLines) return filteredComments;
|
|
201
|
+
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
202
|
+
}
|
|
203
|
+
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
204
|
+
/**
|
|
205
|
+
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
206
|
+
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
207
|
+
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
208
|
+
* same parameters once per plugin.
|
|
209
|
+
*/
|
|
210
|
+
function getOperationParameters(node) {
|
|
211
|
+
const cached = operationParameterGroupsByNode.get(node);
|
|
212
|
+
if (cached) return cached;
|
|
213
|
+
const groups = {
|
|
214
|
+
path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
|
|
215
|
+
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
216
|
+
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
217
|
+
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
218
|
+
};
|
|
219
|
+
operationParameterGroupsByNode.set(node, groups);
|
|
220
|
+
return groups;
|
|
221
|
+
}
|
|
222
|
+
function getStatusCodeNumber(statusCode) {
|
|
223
|
+
const code = Number(statusCode);
|
|
224
|
+
return Number.isNaN(code) ? null : code;
|
|
225
|
+
}
|
|
226
|
+
function isSuccessStatusCode(statusCode) {
|
|
227
|
+
const code = getStatusCodeNumber(statusCode);
|
|
228
|
+
return code !== null && code >= 200 && code < 300;
|
|
229
|
+
}
|
|
230
|
+
function getSuccessResponses(responses) {
|
|
231
|
+
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
232
|
+
}
|
|
233
|
+
function getOperationSuccessResponses(node) {
|
|
234
|
+
return getSuccessResponses(node.responses);
|
|
235
|
+
}
|
|
236
|
+
function getPrimarySuccessResponse(node) {
|
|
237
|
+
return getOperationSuccessResponses(node)[0] ?? null;
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
18
240
|
//#region ../../internals/utils/src/casing.ts
|
|
19
241
|
/**
|
|
20
242
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -54,37 +276,12 @@ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
|
|
|
54
276
|
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
55
277
|
}
|
|
56
278
|
//#endregion
|
|
57
|
-
//#region ../../internals/utils/src/fs.ts
|
|
58
|
-
/**
|
|
59
|
-
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
60
|
-
* (so version numbers embedded in operationIds like `v2025.0` stay intact), camelCases
|
|
61
|
-
* every earlier segment, applies `caseLast` to the final segment, and joins with `/`.
|
|
62
|
-
*
|
|
63
|
-
* Empty segments are dropped before joining. They arise when the name starts with a dot
|
|
64
|
-
* followed by a letter (e.g. `..Schema` splits into `['..', 'Schema']` and `'..'` cases to
|
|
65
|
-
* an empty string). Without this a leading `/` would form, which `path.resolve` reads as an
|
|
66
|
-
* absolute path, letting generated files escape the configured output directory.
|
|
67
|
-
*
|
|
68
|
-
* @example Nested path from a dotted name
|
|
69
|
-
* `toFilePath('pet.petId') // 'pet/petId'`
|
|
70
|
-
*
|
|
71
|
-
* @example PascalCase the final segment
|
|
72
|
-
* `toFilePath('pet.Pet', pascalCase) // 'pet/Pet'`
|
|
73
|
-
*
|
|
74
|
-
* @example Suffix applied to the final segment only
|
|
75
|
-
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
76
|
-
*/
|
|
77
|
-
function toFilePath(name, caseLast = camelCase) {
|
|
78
|
-
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
79
|
-
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
80
|
-
}
|
|
81
|
-
//#endregion
|
|
82
279
|
//#region ../../internals/utils/src/reserved.ts
|
|
83
280
|
/**
|
|
84
281
|
* JavaScript and Java reserved words.
|
|
85
282
|
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
86
283
|
*/
|
|
87
|
-
const reservedWords = new Set([
|
|
284
|
+
const reservedWords = /* @__PURE__ */ new Set([
|
|
88
285
|
"abstract",
|
|
89
286
|
"arguments",
|
|
90
287
|
"boolean",
|
|
@@ -179,7 +376,7 @@ const reservedWords = new Set([
|
|
|
179
376
|
*/
|
|
180
377
|
function isValidVarName(name) {
|
|
181
378
|
if (!name || reservedWords.has(name)) return false;
|
|
182
|
-
return
|
|
379
|
+
return isIdentifier(name);
|
|
183
380
|
}
|
|
184
381
|
/**
|
|
185
382
|
* Returns `name` when it's a syntactically valid JavaScript variable name,
|
|
@@ -201,252 +398,21 @@ function ensureValidVarName(name) {
|
|
|
201
398
|
if (!name || isValidVarName(name)) return name;
|
|
202
399
|
return `_${name}`;
|
|
203
400
|
}
|
|
204
|
-
//#endregion
|
|
205
|
-
//#region ../../internals/utils/src/url.ts
|
|
206
|
-
function transformParam(raw, casing) {
|
|
207
|
-
const param = isValidVarName(raw) ? raw : camelCase(raw);
|
|
208
|
-
return casing === "camelcase" ? camelCase(param) : param;
|
|
209
|
-
}
|
|
210
|
-
function toParamsObject(path, { replacer, casing } = {}) {
|
|
211
|
-
const params = {};
|
|
212
|
-
for (const match of path.matchAll(/\{([^}]+)\}/g)) {
|
|
213
|
-
const param = transformParam(match[1], casing);
|
|
214
|
-
const key = replacer ? replacer(param) : param;
|
|
215
|
-
params[key] = key;
|
|
216
|
-
}
|
|
217
|
-
return Object.keys(params).length > 0 ? params : null;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Helpers for OpenAPI/Swagger paths, plus a thin wrapper over the native `URL`.
|
|
221
|
-
*/
|
|
222
|
-
var Url$1 = class Url$1 {
|
|
223
|
-
static {
|
|
224
|
-
require_chunk.__name(this, "Url");
|
|
225
|
-
}
|
|
226
|
-
/**
|
|
227
|
-
* Reports whether `url` is a parseable absolute URL. Delegates to the native `URL.canParse`.
|
|
228
|
-
*
|
|
229
|
-
* @example
|
|
230
|
-
* Url.canParse('https://petstore.swagger.io/v2') // true
|
|
231
|
-
* Url.canParse('/pet/{petId}') // false
|
|
232
|
-
*/
|
|
233
|
-
static canParse(url, base) {
|
|
234
|
-
return URL.canParse(url, base);
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
* Converts an OpenAPI/Swagger path to Express-style colon syntax.
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* Url.toPath('/pet/{petId}') // '/pet/:petId'
|
|
241
|
-
*/
|
|
242
|
-
static toPath(path) {
|
|
243
|
-
return path.replace(/\{([^}]+)\}/g, ":$1");
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Converts an OpenAPI/Swagger path to a TypeScript template literal string.
|
|
247
|
-
* `prefix` is prepended inside the literal, `replacer` transforms each parameter name,
|
|
248
|
-
* and `casing` controls parameter identifier casing.
|
|
249
|
-
*
|
|
250
|
-
* @example
|
|
251
|
-
* Url.toTemplateString('/pet/{petId}') // '`/pet/${petId}`'
|
|
252
|
-
*
|
|
253
|
-
* @example
|
|
254
|
-
* Url.toTemplateString('/pet/{petId}', { prefix: 'https://api' }) // '`https://api/pet/${petId}`'
|
|
255
|
-
*/
|
|
256
|
-
static toTemplateString(path, { prefix, replacer, casing } = {}) {
|
|
257
|
-
const result = path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
258
|
-
if (i % 2 === 0) return part;
|
|
259
|
-
const param = transformParam(part, casing);
|
|
260
|
-
return `\${${replacer ? replacer(param) : param}}`;
|
|
261
|
-
}).join("");
|
|
262
|
-
return `\`${prefix ?? ""}${result}\``;
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Returns the path and its extracted params as a structured `URLObject`, or as a stringified
|
|
266
|
-
* expression when `stringify` is set.
|
|
267
|
-
*
|
|
268
|
-
* @example
|
|
269
|
-
* Url.toObject('/pet/{petId}')
|
|
270
|
-
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
271
|
-
*/
|
|
272
|
-
static toObject(path, { type = "path", replacer, stringify, casing } = {}) {
|
|
273
|
-
const object = {
|
|
274
|
-
url: type === "path" ? Url$1.toPath(path) : Url$1.toTemplateString(path, {
|
|
275
|
-
replacer,
|
|
276
|
-
casing
|
|
277
|
-
}),
|
|
278
|
-
params: toParamsObject(path, {
|
|
279
|
-
replacer,
|
|
280
|
-
casing
|
|
281
|
-
})
|
|
282
|
-
};
|
|
283
|
-
if (stringify) {
|
|
284
|
-
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
285
|
-
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
286
|
-
return `{ url: '${object.url}' }`;
|
|
287
|
-
}
|
|
288
|
-
return object;
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
//#endregion
|
|
292
|
-
//#region ../../internals/shared/src/operation.ts
|
|
293
401
|
/**
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
402
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
403
|
+
*
|
|
404
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
405
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
406
|
+
* deciding whether an object key needs quoting.
|
|
298
407
|
*
|
|
299
408
|
* @example
|
|
300
409
|
* ```ts
|
|
301
|
-
*
|
|
410
|
+
* isIdentifier('name') // true
|
|
411
|
+
* isIdentifier('x-total')// false
|
|
302
412
|
* ```
|
|
303
413
|
*/
|
|
304
|
-
function
|
|
305
|
-
return
|
|
306
|
-
name,
|
|
307
|
-
extname,
|
|
308
|
-
tag: node.tags[0] ?? "default",
|
|
309
|
-
path: node.path
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
function getOperationLink(node, link) {
|
|
313
|
-
if (!link) return null;
|
|
314
|
-
if (typeof link === "function") return link(node) ?? null;
|
|
315
|
-
if (link === "urlPath") return node.path ? `{@link ${Url$1.toPath(node.path)}}` : null;
|
|
316
|
-
return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
|
|
317
|
-
}
|
|
318
|
-
function getContentTypeInfo(node) {
|
|
319
|
-
const contentTypes = node.requestBody?.content?.map((e) => e.contentType) ?? [];
|
|
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
|
-
/**
|
|
330
|
-
* Derives the default `responseType` for an operation from its primary success response.
|
|
331
|
-
*
|
|
332
|
-
* Returns a value only when that response declares a single non-JSON content type — a binary type
|
|
333
|
-
* (`application/octet-stream`, `application/pdf`, `image/*`, `audio/*`, `video/*`) maps to `'blob'`
|
|
334
|
-
* and other `text/*` maps to `'text'`. Otherwise `undefined`, leaving the runtime client's
|
|
335
|
-
* `Content-Type` auto-detection in charge.
|
|
336
|
-
*/
|
|
337
|
-
function getResponseType(node) {
|
|
338
|
-
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
339
|
-
if (contentTypes.length !== 1) return void 0;
|
|
340
|
-
const baseType = contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
341
|
-
if (baseType === "application/json" || baseType.endsWith("+json") || baseType === "text/json") return void 0;
|
|
342
|
-
if (baseType.startsWith("text/")) return "text";
|
|
343
|
-
if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
|
|
344
|
-
}
|
|
345
|
-
function buildRequestConfigType(node, resolver) {
|
|
346
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null;
|
|
347
|
-
const { isMultipleContentTypes, contentTypeUnion } = getContentTypeInfo(node);
|
|
348
|
-
return `${requestName ? `Partial<RequestConfig<${requestName}>>` : "Partial<RequestConfig>"} & { ${["client?: Client", isMultipleContentTypes ? `contentType?: ${contentTypeUnion}` : null].filter(Boolean).join("; ")} }`;
|
|
349
|
-
}
|
|
350
|
-
function buildOperationComments(node, options = {}) {
|
|
351
|
-
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
352
|
-
const linkComment = getOperationLink(node, link);
|
|
353
|
-
const filteredComments = (linkPosition === "beforeDeprecated" ? [
|
|
354
|
-
node.description && `@description ${node.description}`,
|
|
355
|
-
node.summary && `@summary ${node.summary}`,
|
|
356
|
-
linkComment,
|
|
357
|
-
node.deprecated && "@deprecated"
|
|
358
|
-
] : [
|
|
359
|
-
node.description && `@description ${node.description}`,
|
|
360
|
-
node.summary && `@summary ${node.summary}`,
|
|
361
|
-
node.deprecated && "@deprecated",
|
|
362
|
-
linkComment
|
|
363
|
-
]).filter((comment) => Boolean(comment));
|
|
364
|
-
if (!splitLines) return filteredComments;
|
|
365
|
-
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
366
|
-
}
|
|
367
|
-
function getOperationParameters(node, options = {}) {
|
|
368
|
-
const params = (0, _kubb_ast_utils.caseParams)(node.parameters, options.paramsCasing);
|
|
369
|
-
return {
|
|
370
|
-
path: params.filter((param) => param.in === "path"),
|
|
371
|
-
query: params.filter((param) => param.in === "query"),
|
|
372
|
-
header: params.filter((param) => param.in === "header"),
|
|
373
|
-
cookie: params.filter((param) => param.in === "cookie")
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
function getStatusCodeNumber(statusCode) {
|
|
377
|
-
const code = Number(statusCode);
|
|
378
|
-
return Number.isNaN(code) ? null : code;
|
|
379
|
-
}
|
|
380
|
-
function isSuccessStatusCode(statusCode) {
|
|
381
|
-
const code = getStatusCodeNumber(statusCode);
|
|
382
|
-
return code !== null && code >= 200 && code < 300;
|
|
383
|
-
}
|
|
384
|
-
function isErrorStatusCode(statusCode) {
|
|
385
|
-
const code = getStatusCodeNumber(statusCode);
|
|
386
|
-
return code !== null && code >= 400;
|
|
387
|
-
}
|
|
388
|
-
function getSuccessResponses(responses) {
|
|
389
|
-
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
390
|
-
}
|
|
391
|
-
function getOperationSuccessResponses(node) {
|
|
392
|
-
return getSuccessResponses(node.responses);
|
|
393
|
-
}
|
|
394
|
-
function getPrimarySuccessResponse(node) {
|
|
395
|
-
return getOperationSuccessResponses(node)[0] ?? null;
|
|
396
|
-
}
|
|
397
|
-
function resolveErrorNames(node, resolver) {
|
|
398
|
-
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
399
|
-
}
|
|
400
|
-
function resolveSuccessNames(node, resolver) {
|
|
401
|
-
return node.responses.filter((response) => isSuccessStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
402
|
-
}
|
|
403
|
-
function resolveStatusCodeNames(node, resolver) {
|
|
404
|
-
return node.responses.map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Builds the discriminated union type string for `dataReturnType: 'full'` return shapes.
|
|
408
|
-
* Each member is `{ status: N; data: StatusNType; statusText: string }`.
|
|
409
|
-
*/
|
|
410
|
-
function buildStatusUnionType(node, resolver) {
|
|
411
|
-
const members = node.responses.map((r) => {
|
|
412
|
-
const typeName = resolver.resolveResponseStatusName(node, r.statusCode);
|
|
413
|
-
const statusCode = Number.parseInt(r.statusCode, 10);
|
|
414
|
-
return `{ status: ${Number.isNaN(statusCode) ? "number" : String(statusCode)}; data: ${typeName}; statusText: string }`;
|
|
415
|
-
});
|
|
416
|
-
if (members.length === 1) return members[0];
|
|
417
|
-
return `(${members.join(" | ")})`;
|
|
418
|
-
}
|
|
419
|
-
const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
|
|
420
|
-
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
421
|
-
const cacheKey = `${node.operationId}\0${options.paramsCasing ?? ""}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${(options.exclude ?? []).join(",")}`;
|
|
422
|
-
let byResolver = typeNamesByResolver.get(resolver);
|
|
423
|
-
if (byResolver) {
|
|
424
|
-
const cached = byResolver.get(cacheKey);
|
|
425
|
-
if (cached) return cached;
|
|
426
|
-
} else {
|
|
427
|
-
byResolver = /* @__PURE__ */ new Map();
|
|
428
|
-
typeNamesByResolver.set(resolver, byResolver);
|
|
429
|
-
}
|
|
430
|
-
const { path, query, header } = getOperationParameters(node, { paramsCasing: options.paramsCasing });
|
|
431
|
-
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
432
|
-
const exclude = new Set(options.exclude ?? []);
|
|
433
|
-
const paramNames = [
|
|
434
|
-
...path.map((param) => resolver.resolvePathParamsName(node, param)),
|
|
435
|
-
...query.map((param) => resolver.resolveQueryParamsName(node, param)),
|
|
436
|
-
...header.map((param) => resolver.resolveHeaderParamsName(node, param))
|
|
437
|
-
];
|
|
438
|
-
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null, resolver.resolveResponseName(node)];
|
|
439
|
-
const result = (options.order === "body-response-first" ? [
|
|
440
|
-
...bodyAndResponseNames,
|
|
441
|
-
...paramNames,
|
|
442
|
-
...responseStatusNames
|
|
443
|
-
] : [
|
|
444
|
-
...paramNames,
|
|
445
|
-
...bodyAndResponseNames,
|
|
446
|
-
...responseStatusNames
|
|
447
|
-
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
448
|
-
byResolver.set(cacheKey, result);
|
|
449
|
-
return result;
|
|
414
|
+
function isIdentifier(name) {
|
|
415
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
450
416
|
}
|
|
451
417
|
//#endregion
|
|
452
418
|
//#region ../../internals/shared/src/group.ts
|
|
@@ -480,1617 +446,655 @@ function createGroupConfig(group) {
|
|
|
480
446
|
};
|
|
481
447
|
}
|
|
482
448
|
//#endregion
|
|
483
|
-
//#region ../../internals/
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
449
|
+
//#region ../../internals/client/src/builders/generics.ts
|
|
450
|
+
/**
|
|
451
|
+
* Builds the `RequestResult` generic arguments for one operation: the per-status responses record
|
|
452
|
+
* plus the per-call `ThrowOnError` flag. `SuccessOf` / `ErrorOf` split the record inside the
|
|
453
|
+
* runtime, so this only names the record and threads `ThrowOnError`.
|
|
454
|
+
*
|
|
455
|
+
* @example
|
|
456
|
+
* `buildRequestResultGenerics({ node, types }) // 'AddPetResponses, ThrowOnError'`
|
|
457
|
+
*/
|
|
458
|
+
function buildRequestResultGenerics({ node, types }) {
|
|
459
|
+
return `${types.response.responses(node)}, ThrowOnError`;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Builds the full return type an operation's function signature uses: `Unwrappable<RequestResult>`
|
|
463
|
+
* for the default `returnType: 'full'`, already a promise so no further wrapping is needed, a plain
|
|
464
|
+
* `Promise<RequestResult>` for the internal `'plain'` shape, or a
|
|
465
|
+
* `Promise` of the runtime's `UnwrappedResult` when `returnType: 'data'` narrows a resolved call
|
|
466
|
+
* down to the bare success body.
|
|
467
|
+
*
|
|
468
|
+
* @example
|
|
469
|
+
* `buildResultType({ node, types, returnType: 'data' }) // 'Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>'`
|
|
470
|
+
*/
|
|
471
|
+
function buildResultType({ node, types, returnType }) {
|
|
472
|
+
const generics = buildRequestResultGenerics({
|
|
473
|
+
node,
|
|
474
|
+
types
|
|
491
475
|
});
|
|
492
|
-
|
|
476
|
+
if (returnType === "data") return `Promise<UnwrappedResult<${generics}>>`;
|
|
477
|
+
if (returnType === "plain") return `Promise<RequestResult<${generics}>>`;
|
|
478
|
+
return `Unwrappable<RequestResult<${generics}>>`;
|
|
493
479
|
}
|
|
494
480
|
//#endregion
|
|
495
|
-
//#region src/
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
481
|
+
//#region ../../internals/client/src/builders/returnStatement.ts
|
|
482
|
+
/**
|
|
483
|
+
* Builds the return statement of a generated operation function. With the default
|
|
484
|
+
* `returnType: 'full'` the runtime call already resolves to `{ data, error, request, response }`.
|
|
485
|
+
* The generated code casts that result to the operation's `RequestResult`, then wraps it with
|
|
486
|
+
* `withUnwrap`, so the caller can `await` it directly or call `.unwrap()` for the bare success
|
|
487
|
+
* body. With `returnType: 'data'` it instead routes the call through the runtime's `unwrapResult`,
|
|
488
|
+
* which narrows the resolved value down to the bare success body the same way `RequestResult`
|
|
489
|
+
* already does, using the plugin default when the call leaves `throwOnError` unset.
|
|
490
|
+
* With `returnType: 'plain'` it returns the runtime call as is, cast to `RequestResult`, with no helper.
|
|
491
|
+
*
|
|
492
|
+
* Cast first, wrap second, for the `'full'` path. That order keeps `withUnwrap`'s generic inferred
|
|
493
|
+
* as `RequestResult` instead of the runtime's own internal result type. Casting an `Unwrappable<A>`
|
|
494
|
+
* straight to `Unwrappable<B>` does not work: the `.then` overload stays pinned to `A`, and `as`
|
|
495
|
+
* rejects that two-generic swap even where `A` and `B` on their own would satisfy it.
|
|
496
|
+
*
|
|
497
|
+
* @example
|
|
498
|
+
* `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>)`
|
|
499
|
+
* @example
|
|
500
|
+
* `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError ?? false) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
|
|
501
|
+
* @example
|
|
502
|
+
* `return request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>`
|
|
503
|
+
*/
|
|
504
|
+
function buildReturnStatement({ node, types, callConfig, returnType, throwOnErrorDefault }) {
|
|
505
|
+
if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
|
|
506
|
+
node,
|
|
507
|
+
types,
|
|
508
|
+
returnType
|
|
509
|
+
})}`;
|
|
510
|
+
if (returnType === "plain") return `return request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
|
|
511
|
+
node,
|
|
512
|
+
types
|
|
513
|
+
})}>>`;
|
|
514
|
+
return `return withUnwrap(request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
|
|
515
|
+
node,
|
|
516
|
+
types
|
|
517
|
+
})}>>)`;
|
|
500
518
|
}
|
|
501
|
-
|
|
502
|
-
|
|
519
|
+
//#endregion
|
|
520
|
+
//#region ../../internals/client/src/builders/security.ts
|
|
521
|
+
function serializeAuth(auth) {
|
|
522
|
+
const parts = [`type: '${auth.type}'`];
|
|
523
|
+
if (auth.scheme) parts.push(`scheme: '${auth.scheme}'`);
|
|
524
|
+
if (auth.name) parts.push(`name: '${auth.name}'`);
|
|
525
|
+
if (auth.in) parts.push(`in: '${auth.in}'`);
|
|
526
|
+
return `{ ${parts.join(", ")} }`;
|
|
503
527
|
}
|
|
504
528
|
/**
|
|
505
|
-
*
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
* value-only call entries.
|
|
529
|
+
* Maps an OpenAPI security scheme to the inline `Auth` object, or `null` when the runtime cannot
|
|
530
|
+
* place it (an unresolved `$ref`, or an `apiKey` without a name or outside `header` / `query` /
|
|
531
|
+
* `cookie`). `http` schemes other than `basic` are treated as bearer.
|
|
509
532
|
*/
|
|
510
|
-
function
|
|
511
|
-
return
|
|
512
|
-
|
|
513
|
-
name
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
}
|
|
519
|
-
function createDeclarationLeaf(name, spec) {
|
|
520
|
-
if (spec.default !== void 0) return _kubb_core.ast.factory.createFunctionParameter({
|
|
521
|
-
name,
|
|
522
|
-
type: spec.type,
|
|
523
|
-
default: spec.default,
|
|
524
|
-
rest: spec.mode === "inlineSpread"
|
|
525
|
-
});
|
|
526
|
-
return _kubb_core.ast.factory.createFunctionParameter({
|
|
527
|
-
name,
|
|
528
|
-
type: spec.type,
|
|
529
|
-
optional: !!spec.optional,
|
|
530
|
-
rest: spec.mode === "inlineSpread"
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
function createDeclarationParam(name, spec) {
|
|
534
|
-
if (isGroup(spec)) {
|
|
535
|
-
const entries = groupEntries(spec);
|
|
536
|
-
return createGroupParam(entries.map(([childName]) => ({ name: childName })), entries.filter(([, child]) => child.type).map(([childName, child]) => ({
|
|
537
|
-
name: childName,
|
|
538
|
-
type: child.type,
|
|
539
|
-
optional: !!child.optional || child.default !== void 0
|
|
540
|
-
})), spec.default);
|
|
533
|
+
function resolveSecurityScheme(scheme) {
|
|
534
|
+
if (!scheme || "$ref" in scheme) return null;
|
|
535
|
+
if (scheme.type === "apiKey") {
|
|
536
|
+
if (!scheme.name || scheme.in !== "header" && scheme.in !== "query" && scheme.in !== "cookie") return null;
|
|
537
|
+
return {
|
|
538
|
+
type: "apiKey",
|
|
539
|
+
name: scheme.name,
|
|
540
|
+
in: scheme.in
|
|
541
|
+
};
|
|
541
542
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
if (isGroup(spec)) return createGroupParam(groupEntries(spec).map(([childName, child]) => ({ name: child.mode === "inlineSpread" ? spec.mode === "inlineSpread" ? child.value ?? childName : `...${child.value ?? childName}` : child.value ? `${childName}: ${child.value}` : childName })), []);
|
|
546
|
-
return _kubb_core.ast.factory.createFunctionParameter({
|
|
547
|
-
name: spec.value ?? name,
|
|
548
|
-
rest: spec.mode === "inlineSpread"
|
|
549
|
-
});
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Creates function parameter builders for generating function signatures and calls.
|
|
553
|
-
* Returns utilities to output constructor signatures (`toConstructor()`) or call expressions (`toCall()`).
|
|
554
|
-
*/
|
|
555
|
-
function createFunctionParams(params) {
|
|
556
|
-
const entries = Object.entries(params).filter(([, spec]) => spec != null);
|
|
557
|
-
return {
|
|
558
|
-
toConstructor() {
|
|
559
|
-
return declarationPrinter$4.print(_kubb_core.ast.factory.createFunctionParameters({ params: entries.map(([name, spec]) => createDeclarationParam(name, spec)) })) ?? "";
|
|
560
|
-
},
|
|
561
|
-
toCall() {
|
|
562
|
-
return callPrinter.print(_kubb_core.ast.factory.createFunctionParameters({ params: entries.map(([name, spec]) => createCallParam(name, spec)) })) ?? "";
|
|
563
|
-
}
|
|
543
|
+
if (scheme.type === "http") return {
|
|
544
|
+
type: "http",
|
|
545
|
+
scheme: scheme.scheme?.toLowerCase() === "basic" ? "basic" : "bearer"
|
|
564
546
|
};
|
|
547
|
+
if (scheme.type === "oauth2") return { type: "oauth2" };
|
|
548
|
+
if (scheme.type === "openIdConnect") return { type: "openIdConnect" };
|
|
549
|
+
return null;
|
|
565
550
|
}
|
|
566
|
-
//#endregion
|
|
567
|
-
//#region src/utils.ts
|
|
568
551
|
/**
|
|
569
|
-
*
|
|
552
|
+
* Derives the per-operation security metadata from the OpenAPI document. The operation's own
|
|
553
|
+
* `security` overrides the global `security` (an explicit empty array disables auth), and every
|
|
554
|
+
* referenced scheme is resolved from `components.securitySchemes` into a flat, de-duplicated list of
|
|
555
|
+
* `Auth` objects the runtime walks in order.
|
|
556
|
+
*
|
|
557
|
+
* @example
|
|
558
|
+
* `getOperationSecurity({ document, method: 'POST', path: '/pet' })`
|
|
559
|
+
* `// [{ type: 'http', scheme: 'bearer' }]`
|
|
570
560
|
*/
|
|
571
|
-
function
|
|
572
|
-
if (!
|
|
573
|
-
|
|
574
|
-
|
|
561
|
+
function getOperationSecurity({ document, method, path }) {
|
|
562
|
+
if (!document) return void 0;
|
|
563
|
+
const requirements = (document.paths?.[path]?.[method.toLowerCase()])?.security ?? document.security;
|
|
564
|
+
if (!requirements?.length) return void 0;
|
|
565
|
+
const definitions = document.components?.securitySchemes ?? {};
|
|
566
|
+
const security = [];
|
|
567
|
+
const seen = /* @__PURE__ */ new Set();
|
|
568
|
+
for (const requirement of requirements) for (const schemeName of Object.keys(requirement)) {
|
|
569
|
+
if (seen.has(schemeName)) continue;
|
|
570
|
+
seen.add(schemeName);
|
|
571
|
+
const auth = resolveSecurityScheme(definitions[schemeName]);
|
|
572
|
+
if (auth) security.push(auth);
|
|
573
|
+
}
|
|
574
|
+
return security.length ? security : void 0;
|
|
575
575
|
}
|
|
576
576
|
/**
|
|
577
|
-
*
|
|
578
|
-
* The
|
|
577
|
+
* Serializes the per-operation security into the literal emitted on each generated call's `security`
|
|
578
|
+
* field. The runtime `resolveAuth` helper walks it, calling the configured `auth` resolver per entry.
|
|
579
|
+
*
|
|
580
|
+
* @example
|
|
581
|
+
* `buildSecurityMetadata({ security: [{ type: 'http', scheme: 'bearer' }] }) // "[{ type: 'http', scheme: 'bearer' }]"`
|
|
579
582
|
*/
|
|
580
|
-
function
|
|
581
|
-
if (!
|
|
582
|
-
|
|
583
|
-
return parser.request ?? null;
|
|
583
|
+
function buildSecurityMetadata({ security }) {
|
|
584
|
+
if (!security?.length) return null;
|
|
585
|
+
return `[${security.map(serializeAuth).join(", ")}]`;
|
|
584
586
|
}
|
|
587
|
+
//#endregion
|
|
588
|
+
//#region ../../internals/client/src/builders/signature.ts
|
|
589
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
585
590
|
/**
|
|
586
|
-
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
591
|
+
* Builds the grouped-options signature for one operation: a single `options` object whose `TData`
|
|
592
|
+
* is the `<Name>Options` type, and a `RequestResult` return type keyed to the per-status responses
|
|
593
|
+
* record. There are no positional arguments.
|
|
594
|
+
*
|
|
595
|
+
* The generated file imports `<Name>Options` and `<Name>Responses` and uses them directly, so no
|
|
596
|
+
* per-operation input type has to be emitted. Both names come from `types`, which is `plugin-ts` or
|
|
597
|
+
* `plugin-zod`'s inferred types (see `resolveOperationTypes`).
|
|
589
598
|
*/
|
|
590
|
-
function
|
|
591
|
-
|
|
592
|
-
|
|
599
|
+
function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDefault }) {
|
|
600
|
+
const optionsName = types.response.options(node);
|
|
601
|
+
const { isOptional } = getRequestGroupOptionality(node);
|
|
602
|
+
return {
|
|
603
|
+
paramsSignature: declarationPrinter.print((0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
604
|
+
name: "options",
|
|
605
|
+
type: `Options<${optionsName}, ThrowOnError>`,
|
|
606
|
+
...isOptional ? { default: "{}" } : {}
|
|
607
|
+
})] })) ?? "",
|
|
608
|
+
returnType: buildResultType({
|
|
609
|
+
node,
|
|
610
|
+
types,
|
|
611
|
+
returnType
|
|
612
|
+
}),
|
|
613
|
+
generics: [`ThrowOnError extends boolean = ${throwOnErrorDefault}`]
|
|
614
|
+
};
|
|
593
615
|
}
|
|
616
|
+
//#endregion
|
|
617
|
+
//#region ../../internals/client/src/builders/validatorOptions.ts
|
|
594
618
|
/**
|
|
595
|
-
* Returns `'zod'` when
|
|
596
|
-
* `
|
|
619
|
+
* Returns `'zod'` when request body parsing is enabled, `null` otherwise. The string shorthand
|
|
620
|
+
* `'zod'` validates the response only, so it does not enable request parsing.
|
|
597
621
|
*/
|
|
598
|
-
function
|
|
599
|
-
if (!
|
|
600
|
-
|
|
601
|
-
return parser.response ?? null;
|
|
622
|
+
function resolveRequestValidator(validator) {
|
|
623
|
+
if (!validator || validator === "zod") return null;
|
|
624
|
+
return validator.request ?? null;
|
|
602
625
|
}
|
|
603
626
|
/**
|
|
604
|
-
*
|
|
605
|
-
*
|
|
627
|
+
* Returns `'zod'` when response parsing is enabled, `null` otherwise. The string shorthand `'zod'`
|
|
628
|
+
* maps to response parsing.
|
|
606
629
|
*/
|
|
607
|
-
function
|
|
608
|
-
|
|
630
|
+
function resolveResponseValidator(validator) {
|
|
631
|
+
if (!validator) return null;
|
|
632
|
+
if (validator === "zod") return "zod";
|
|
633
|
+
return validator.response ?? null;
|
|
609
634
|
}
|
|
610
635
|
/**
|
|
611
|
-
*
|
|
612
|
-
*
|
|
613
|
-
*
|
|
614
|
-
* When `dataReturnType` is `'full'`, the response generic widens to a union of all documented
|
|
615
|
-
* status types. When `parser` is `'zod'` and a request body schema exists, the request type
|
|
616
|
-
* uses `z.output<typeof schema>` to reflect post-transform types (e.g. date coercion).
|
|
636
|
+
* Resolves the zod expression a generated client validates a success response with. Only success
|
|
637
|
+
* (2xx) bodies reach the parse under the throw-on-error contract, so the success-only
|
|
638
|
+
* `<operation>ResponseSchema` is used; error bodies are never zod-parsed.
|
|
617
639
|
*/
|
|
618
|
-
function
|
|
619
|
-
const
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
625
|
-
const zodRequestName = options.parser === "zod" && options.zodResolver && node.requestBody?.content?.[0]?.schema ? options.zodResolver.resolveDataName?.(node) ?? null : null;
|
|
626
|
-
return [
|
|
627
|
-
responseName,
|
|
628
|
-
TError,
|
|
629
|
-
zodRequestName ? `z.output<typeof ${zodRequestName}>` : requestName || "unknown"
|
|
630
|
-
].filter(Boolean);
|
|
640
|
+
function buildZodResponseParse(node, zodResolver) {
|
|
641
|
+
const name = zodResolver.response.response(node);
|
|
642
|
+
return name ? {
|
|
643
|
+
expression: name,
|
|
644
|
+
importNames: [name]
|
|
645
|
+
} : null;
|
|
631
646
|
}
|
|
632
647
|
/**
|
|
633
|
-
*
|
|
634
|
-
*
|
|
648
|
+
* Resolves the zod expression a generated client validates an error body with on the non-throw path.
|
|
649
|
+
* Uses the error-only `<operation>ErrorSchema` (the union of non-2xx statuses); returns `null` when the
|
|
650
|
+
* operation documents no error responses with a schema.
|
|
635
651
|
*/
|
|
636
|
-
function
|
|
637
|
-
|
|
638
|
-
const
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
children: {
|
|
644
|
-
requestConfig: { mode: "inlineSpread" },
|
|
645
|
-
method: { value: JSON.stringify(_kubb_core.ast.isHttpOperationNode(node) ? node.method.toUpperCase() : "") },
|
|
646
|
-
url: { value: url },
|
|
647
|
-
baseURL: baseURL ? { value: JSON.stringify(baseURL) } : null,
|
|
648
|
-
params: queryParamsName ? zodQueryParamsName ? { value: "requestParams" } : {} : null,
|
|
649
|
-
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } : null,
|
|
650
|
-
contentType: isMultipleContentTypes ? {} : null,
|
|
651
|
-
responseType: responseType ? { value: JSON.stringify(responseType) } : null,
|
|
652
|
-
headers: headers.length ? { value: `{ ${headers.join(", ")}, ...requestConfig.headers }` } : null
|
|
653
|
-
}
|
|
654
|
-
} });
|
|
652
|
+
function buildZodErrorParse(node, zodResolver) {
|
|
653
|
+
if (!node.responses.some((res) => !isSuccessStatusCode(res.statusCode) && res.content?.some((entry) => entry.schema))) return null;
|
|
654
|
+
const name = zodResolver.response.error?.(node);
|
|
655
|
+
return name ? {
|
|
656
|
+
expression: name,
|
|
657
|
+
importNames: [name]
|
|
658
|
+
} : null;
|
|
655
659
|
}
|
|
660
|
+
//#endregion
|
|
661
|
+
//#region ../../internals/client/src/builders/validator.ts
|
|
656
662
|
/**
|
|
657
|
-
* Builds the
|
|
658
|
-
*
|
|
663
|
+
* Builds the validator-hook references for one operation. Request validation runs before the send;
|
|
664
|
+
* response validation runs on the success body only. Returns `null` references when the matching
|
|
665
|
+
* direction is disabled or the schema is absent.
|
|
659
666
|
*/
|
|
660
|
-
function
|
|
661
|
-
const
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
667
|
+
function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
668
|
+
const importedZodNames = [];
|
|
669
|
+
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
670
|
+
const zodRequestName = zodResolver && resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body(node) : null;
|
|
671
|
+
const request = zodRequestName ?? null;
|
|
672
|
+
if (zodRequestName) importedZodNames.push(zodRequestName);
|
|
673
|
+
const responseParse = zodResolver && resolveResponseValidator(validator) === "zod" ? buildZodResponseParse(node, zodResolver) : null;
|
|
674
|
+
const response = responseParse ? responseParse.expression : null;
|
|
675
|
+
if (responseParse) importedZodNames.push(...responseParse.importNames);
|
|
676
|
+
const errorParse = zodResolver && resolveResponseValidator(validator) === "zod" ? buildZodErrorParse(node, zodResolver) : null;
|
|
677
|
+
const error = errorParse ? errorParse.expression : null;
|
|
678
|
+
if (errorParse) importedZodNames.push(...errorParse.importNames);
|
|
679
|
+
return {
|
|
680
|
+
request,
|
|
681
|
+
response,
|
|
682
|
+
error,
|
|
683
|
+
importedZodNames
|
|
684
|
+
};
|
|
666
685
|
}
|
|
686
|
+
//#endregion
|
|
687
|
+
//#region ../../internals/client/src/builders/styles.ts
|
|
667
688
|
/**
|
|
668
|
-
*
|
|
669
|
-
*
|
|
670
|
-
*
|
|
689
|
+
* Renders a parameter name as an object-literal key, quoted when it is not a bare identifier.
|
|
690
|
+
* Path keys are camelCased to match the URL template placeholders. Query, header, and cookie keys
|
|
691
|
+
* keep the spec name, matching the remapped keys the runtime serializes.
|
|
671
692
|
*/
|
|
672
|
-
function
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
if (queryParams.length === 0) return "";
|
|
676
|
-
const zodQueryParamsName = zodResolver.resolveQueryParamsName?.(node, queryParams[0]);
|
|
677
|
-
if (!zodQueryParamsName) return "";
|
|
678
|
-
return `const requestParams = ${zodQueryParamsName}.parse(params)`;
|
|
693
|
+
function toKey(name, location) {
|
|
694
|
+
const key = location === "path" ? camelCase(name) : name;
|
|
695
|
+
return isValidVarName(key) ? key : JSON.stringify(key);
|
|
679
696
|
}
|
|
680
697
|
/**
|
|
681
|
-
*
|
|
682
|
-
*
|
|
698
|
+
* Serializes one parameter's metadata into a `{ style, explode }` literal, or `null` when the
|
|
699
|
+
* parameter carries neither. Path and query carry the serialization `style`; header and cookie use a
|
|
700
|
+
* fixed style (`simple` and `form`), so only `explode` is emitted for them.
|
|
683
701
|
*/
|
|
684
|
-
function
|
|
685
|
-
|
|
702
|
+
function serializeParameter(parameter) {
|
|
703
|
+
const parts = [];
|
|
704
|
+
if ((parameter.in === "path" || parameter.in === "query") && parameter.style) parts.push(`style: '${parameter.style}'`);
|
|
705
|
+
if (parameter.explode !== void 0) parts.push(`explode: ${parameter.explode}`);
|
|
706
|
+
return parts.length > 0 ? `{ ${parts.join(", ")} }` : null;
|
|
686
707
|
}
|
|
687
708
|
/**
|
|
688
|
-
* Builds the
|
|
689
|
-
*
|
|
690
|
-
*
|
|
709
|
+
* Builds the per-operation `styles` literal from the operation's parameters, grouped by location.
|
|
710
|
+
* Path entries are keyed by the camelCased name to match the URL template placeholders; query,
|
|
711
|
+
* header, and cookie entries keep the spec name to match the keys the runtime serializes.
|
|
712
|
+
* Only parameters whose source defines `style` or `explode` are emitted, so calls without
|
|
713
|
+
* serialization metadata keep the runtime defaults and existing output is unchanged. Returns `null`
|
|
714
|
+
* when no parameter carries metadata.
|
|
715
|
+
*
|
|
716
|
+
* @example
|
|
717
|
+
* ```ts
|
|
718
|
+
* // a path param with { style: 'matrix', explode: true } and a query param with { explode: false }
|
|
719
|
+
* buildStyles({ node }) // "{ path: { id: { style: 'matrix', explode: true } }, query: { tags: { explode: false } } }"
|
|
720
|
+
* ```
|
|
691
721
|
*/
|
|
692
|
-
function
|
|
693
|
-
|
|
694
|
-
const
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
722
|
+
function buildStyles({ node }) {
|
|
723
|
+
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
724
|
+
const groups = {
|
|
725
|
+
path: [],
|
|
726
|
+
query: [],
|
|
727
|
+
header: [],
|
|
728
|
+
cookie: []
|
|
729
|
+
};
|
|
730
|
+
for (const parameter of node.parameters) {
|
|
731
|
+
const literal = serializeParameter(parameter);
|
|
732
|
+
if (!literal) continue;
|
|
733
|
+
groups[parameter.in].push(`${toKey(parameter.name, parameter.in)}: ${literal}`);
|
|
699
734
|
}
|
|
700
|
-
|
|
701
|
-
|
|
735
|
+
const locations = Object.keys(groups).filter((location) => groups[location].length > 0);
|
|
736
|
+
if (locations.length === 0) return null;
|
|
737
|
+
return `{ ${locations.map((location) => `${location}: { ${groups[location].join(", ")} }`).join(", ")} }`;
|
|
702
738
|
}
|
|
703
739
|
//#endregion
|
|
704
|
-
//#region src/components/
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
paramsCasing,
|
|
715
|
-
resolver: tsResolver
|
|
716
|
-
});
|
|
717
|
-
}
|
|
718
|
-
function Url({ name, isExportable = true, isIndexable = true, baseURL, paramsType, paramsCasing, pathParamsType, node, tsResolver }) {
|
|
719
|
-
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
720
|
-
const paramsNode = buildUrlParamsNode({
|
|
721
|
-
paramsType,
|
|
722
|
-
paramsCasing,
|
|
723
|
-
pathParamsType,
|
|
740
|
+
//#region ../../internals/client/src/components/Operation.tsx
|
|
741
|
+
/**
|
|
742
|
+
* Renders one client operation: the grouped `<Name>Request` type and the function that forwards a
|
|
743
|
+
* single `options` object to the resolved client and returns the `Unwrappable<RequestResult>`. The
|
|
744
|
+
* type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
|
|
745
|
+
* the source.
|
|
746
|
+
*/
|
|
747
|
+
function Operation({ name, node, types, zodResolver, validator, returnType, throwOnErrorDefault, security, isExportable = true, isIndexable = true }) {
|
|
748
|
+
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
749
|
+
const signature = buildGroupedOptionsSignature({
|
|
724
750
|
node,
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
const { path: originalPathParams } = getOperationParameters(node);
|
|
729
|
-
const { path: casedPathParams } = getOperationParameters(node, { paramsCasing });
|
|
730
|
-
const pathParamsMapping = paramsCasing ? buildParamsMapping(originalPathParams, casedPathParams) : null;
|
|
731
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
732
|
-
name,
|
|
733
|
-
isExportable,
|
|
734
|
-
isIndexable,
|
|
735
|
-
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.Function, {
|
|
736
|
-
name,
|
|
737
|
-
export: isExportable,
|
|
738
|
-
params: paramsSignature,
|
|
739
|
-
children: [
|
|
740
|
-
pathParamsMapping && Object.entries(pathParamsMapping).filter(([originalName, camelCaseName]) => isValidVarName(originalName) && originalName !== camelCaseName).map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`).join("\n"),
|
|
741
|
-
pathParamsMapping && Object.keys(pathParamsMapping).length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}),
|
|
742
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Const, {
|
|
743
|
-
name: "res",
|
|
744
|
-
children: `{ method: '${node.method.toUpperCase()}', url: ${Url$1.toTemplateString(node.path, { prefix: baseURL })} as const }`
|
|
745
|
-
}),
|
|
746
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}),
|
|
747
|
-
"return res"
|
|
748
|
-
]
|
|
749
|
-
})
|
|
751
|
+
types,
|
|
752
|
+
returnType,
|
|
753
|
+
throwOnErrorDefault
|
|
750
754
|
});
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
function buildClientParamsNode({ paramsType, paramsCasing, pathParamsType, node, tsResolver, isConfigurable }) {
|
|
756
|
-
return (0, _kubb_ast_utils.createOperationParams)(node, {
|
|
757
|
-
paramsType,
|
|
758
|
-
pathParamsType: paramsType === "object" ? "object" : pathParamsType === "object" ? "object" : "inline",
|
|
759
|
-
paramsCasing,
|
|
760
|
-
resolver: tsResolver,
|
|
761
|
-
extraParams: [...isConfigurable ? [_kubb_core.ast.factory.createFunctionParameter({
|
|
762
|
-
name: "config",
|
|
763
|
-
type: buildRequestConfigType(node, tsResolver),
|
|
764
|
-
default: "{}"
|
|
765
|
-
})] : []]
|
|
755
|
+
const validators = buildValidatorHooks({
|
|
756
|
+
node,
|
|
757
|
+
validator,
|
|
758
|
+
zodResolver
|
|
766
759
|
});
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
const
|
|
771
|
-
const
|
|
760
|
+
const securityLiteral = buildSecurityMetadata({ security });
|
|
761
|
+
const stylesLiteral = buildStyles({ node });
|
|
762
|
+
const { defaultContentType } = getContentTypeInfo(node);
|
|
763
|
+
const bakedRequestContentType = Boolean(node.requestBody?.content?.[0]?.schema) && defaultContentType !== "application/json" ? defaultContentType : null;
|
|
764
|
+
const mergeContentType = Boolean(bakedRequestContentType) && getResponseContentTypeInfo(node).isMultipleContentTypes;
|
|
765
|
+
const contentTypeLiteral = !bakedRequestContentType ? null : mergeContentType ? `contentType: { request: '${bakedRequestContentType}', ...(typeof contentType === 'string' ? { request: contentType } : contentType) }` : `contentType: { request: '${bakedRequestContentType}' }`;
|
|
766
|
+
const eventStream = isEventStream(node);
|
|
772
767
|
const responseType = getResponseType(node);
|
|
773
|
-
const
|
|
774
|
-
const
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null;
|
|
779
|
-
const successNames = resolveSuccessNames(node, tsResolver);
|
|
780
|
-
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
781
|
-
const queryParamsName = originalQueryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, originalQueryParams[0]) : null;
|
|
782
|
-
const headerParamsName = originalHeaderParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, originalHeaderParams[0]) : null;
|
|
783
|
-
const requestParser = resolveRequestParser(parser);
|
|
784
|
-
const responseParser = resolveResponseParser(parser);
|
|
785
|
-
const zodResponseName = zodResolver && responseParser === "zod" ? zodResolver.resolveResponseName?.(node) : null;
|
|
786
|
-
const zodRequestName = zodResolver && requestParser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null;
|
|
787
|
-
const queryParamsParser = resolveQueryParamsParser(parser);
|
|
788
|
-
const zodQueryParamsName = zodResolver && queryParamsParser === "zod" && originalQueryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, originalQueryParams[0]) : null;
|
|
789
|
-
const errorNames = node.responses.filter((r) => {
|
|
790
|
-
return Number.parseInt(r.statusCode, 10) >= 400;
|
|
791
|
-
}).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode));
|
|
792
|
-
const headers = [!isMultipleContentTypes && contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : null, headerParamsName ? headerParamsMapping ? "...mappedHeaders" : "...headers" : null].filter(Boolean);
|
|
793
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
794
|
-
const allStatusNames = node.responses.map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode));
|
|
795
|
-
const generics = [
|
|
796
|
-
dataReturnType === "full" ? allStatusNames.length > 0 ? allStatusNames.join(" | ") : responseName : responseName,
|
|
797
|
-
TError,
|
|
798
|
-
parser === "zod" && zodRequestName ? `z.output<typeof ${zodRequestName}>` : requestName || "unknown"
|
|
768
|
+
const responseTypeLiteral = responseType ? `responseType: '${responseType}'` : null;
|
|
769
|
+
const validatorEntries = [
|
|
770
|
+
validators.request ? `request: ${validators.request}` : null,
|
|
771
|
+
validators.response ? `response: ${validators.response}` : null,
|
|
772
|
+
validators.error ? `error: ${validators.error}` : null
|
|
799
773
|
].filter(Boolean);
|
|
800
|
-
const
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
774
|
+
const validatorLiteral = validatorEntries.length ? `validator: { ${validatorEntries.join(", ")} }` : null;
|
|
775
|
+
const callConfig = `{ ${[
|
|
776
|
+
`method: '${node.method.toUpperCase()}'`,
|
|
777
|
+
`url: '${node.path}'`,
|
|
778
|
+
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
779
|
+
stylesLiteral ? `styles: ${stylesLiteral}` : null,
|
|
780
|
+
validatorLiteral,
|
|
781
|
+
contentTypeLiteral,
|
|
782
|
+
responseTypeLiteral,
|
|
783
|
+
"...config",
|
|
784
|
+
`throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
|
|
785
|
+
].filter(Boolean).join(", ")} }`;
|
|
786
|
+
const eventType = `SuccessOf<${types.response.responses(node)}>`;
|
|
787
|
+
const functionReturnType = eventStream ? `Promise<EventStreamResult<${eventType}>>` : signature.returnType;
|
|
788
|
+
const returnStatement = eventStream ? `return toEventStream<${eventType}>(request(${callConfig}))` : buildReturnStatement({
|
|
804
789
|
node,
|
|
805
|
-
|
|
806
|
-
|
|
790
|
+
types,
|
|
791
|
+
callConfig,
|
|
792
|
+
returnType,
|
|
793
|
+
throwOnErrorDefault
|
|
807
794
|
});
|
|
808
|
-
|
|
809
|
-
const urlParamsNode = buildUrlParamsNode({
|
|
810
|
-
paramsType,
|
|
811
|
-
paramsCasing,
|
|
812
|
-
pathParamsType,
|
|
813
|
-
node,
|
|
814
|
-
tsResolver
|
|
815
|
-
});
|
|
816
|
-
const urlParamsCall = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" }).print(urlParamsNode) ?? "";
|
|
817
|
-
const clientParams = createFunctionParams({ config: {
|
|
818
|
-
mode: "object",
|
|
819
|
-
children: {
|
|
820
|
-
method: { value: (0, _kubb_ast_utils.stringify)(node.method.toUpperCase()) },
|
|
821
|
-
url: { value: urlName ? `${urlName}(${urlParamsCall}).url.toString()` : Url$1.toTemplateString(node.path) },
|
|
822
|
-
baseURL: baseURL && !urlName ? { value: `\`${baseURL}\`` } : null,
|
|
823
|
-
params: queryParamsName ? zodQueryParamsName ? { value: "requestParams" } : queryParamsMapping ? { value: "mappedParams" } : {} : null,
|
|
824
|
-
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } : null,
|
|
825
|
-
contentType: isConfigurable && isMultipleContentTypes ? {} : null,
|
|
826
|
-
responseType: responseType ? { value: (0, _kubb_ast_utils.stringify)(responseType) } : null,
|
|
827
|
-
requestConfig: isConfigurable ? { mode: "inlineSpread" } : null,
|
|
828
|
-
headers: headers.length ? { value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }` } : null
|
|
829
|
-
}
|
|
830
|
-
} });
|
|
831
|
-
const statusUnionType = dataReturnType === "full" ? buildStatusUnionType(node, tsResolver) : null;
|
|
832
|
-
const childrenElement = children ? children : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
833
|
-
dataReturnType === "full" && responseParser === "zod" && zodResponseName && statusUnionType && `return {...res, data: ${zodResponseName}.parse(res.data)} as ${statusUnionType}`,
|
|
834
|
-
dataReturnType === "full" && responseParser !== "zod" && statusUnionType && `return res as ${statusUnionType}`,
|
|
835
|
-
dataReturnType === "data" && responseParser === "zod" && zodResponseName && `return ${zodResponseName}.parse(res.data)`,
|
|
836
|
-
dataReturnType === "data" && responseParser !== "zod" && "return res.data"
|
|
837
|
-
] });
|
|
838
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
795
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
839
796
|
name,
|
|
840
797
|
isExportable,
|
|
841
798
|
isIndexable,
|
|
842
|
-
children: /* @__PURE__ */ (0,
|
|
799
|
+
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.Function, {
|
|
843
800
|
name,
|
|
844
|
-
async: true,
|
|
845
801
|
export: isExportable,
|
|
846
|
-
|
|
802
|
+
generics: signature.generics,
|
|
803
|
+
params: signature.paramsSignature,
|
|
804
|
+
returnType: functionReturnType,
|
|
847
805
|
JSDoc: { comments: buildOperationComments(node, {
|
|
848
806
|
link: "urlPath",
|
|
849
807
|
linkPosition: "beforeDeprecated",
|
|
850
808
|
splitLines: true
|
|
851
809
|
}) },
|
|
852
|
-
returnType,
|
|
853
810
|
children: [
|
|
854
|
-
|
|
855
|
-
/* @__PURE__ */ (0,
|
|
856
|
-
|
|
857
|
-
pathParamsMapping && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}),
|
|
858
|
-
queryParamsMapping && queryParamsName && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [`const mappedParams = params ? { ${Object.entries(queryParamsMapping).map(([originalName, camelCaseName]) => `"${originalName}": params.${camelCaseName}`).join(", ")} } : undefined`, /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {})] }),
|
|
859
|
-
headerParamsMapping && headerParamsName && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [`const mappedHeaders = headers ? { ${Object.entries(headerParamsMapping).map(([originalName, camelCaseName]) => `"${originalName}": headers.${camelCaseName}`).join(", ")} } : undefined`, /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {})] }),
|
|
860
|
-
requestParser === "zod" && zodRequestName ? `const requestData = ${zodRequestName}.parse(data)` : requestName && "const requestData = data",
|
|
861
|
-
zodQueryParamsName && `const requestParams = ${zodQueryParamsName}.parse(params)`,
|
|
862
|
-
(isFormData || isMultipleContentTypes && hasFormData) && requestName && "const formData = buildFormData(requestData)",
|
|
863
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}),
|
|
864
|
-
isConfigurable ? `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})` : `const res = await client<${generics.join(", ")}>(${clientParams.toCall()})`,
|
|
865
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)("br", {}),
|
|
866
|
-
childrenElement
|
|
811
|
+
mergeContentType ? "const { client: request = client, contentType, ...config } = options" : "const { client: request = client, ...config } = options",
|
|
812
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)("br", {}),
|
|
813
|
+
returnStatement
|
|
867
814
|
]
|
|
868
815
|
})
|
|
869
|
-
})] });
|
|
870
|
-
}
|
|
871
|
-
//#endregion
|
|
872
|
-
//#region src/components/ClassClient.tsx
|
|
873
|
-
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
874
|
-
function generateMethod$1({ node, name, tsResolver, zodResolver, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType }) {
|
|
875
|
-
if (!_kubb_core.ast.isHttpOperationNode(node)) return "";
|
|
876
|
-
const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node);
|
|
877
|
-
const isFormData = !isMultipleContentTypes && contentType === "multipart/form-data";
|
|
878
|
-
const { header: headerParams } = getOperationParameters(node);
|
|
879
|
-
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) : null;
|
|
880
|
-
const headers = isMultipleContentTypes ? headerParamsName ? ["...headers"] : [] : buildHeaders(contentType, !!headerParamsName);
|
|
881
|
-
const generics = buildGenerics(node, tsResolver, {
|
|
882
|
-
dataReturnType,
|
|
883
|
-
zodResolver,
|
|
884
|
-
parser
|
|
885
|
-
});
|
|
886
|
-
const paramsNode = buildClientParamsNode({
|
|
887
|
-
paramsType,
|
|
888
|
-
paramsCasing,
|
|
889
|
-
pathParamsType,
|
|
890
|
-
node,
|
|
891
|
-
tsResolver,
|
|
892
|
-
isConfigurable: true
|
|
893
|
-
});
|
|
894
|
-
const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
|
|
895
|
-
const { query: queryParams } = getOperationParameters(node);
|
|
896
|
-
const zodQueryParamsName = zodResolver && resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null;
|
|
897
|
-
const clientParams = buildClassClientParams({
|
|
898
|
-
node,
|
|
899
|
-
url: Url$1.toTemplateString(node.path, { casing: paramsCasing }),
|
|
900
|
-
baseURL,
|
|
901
|
-
tsResolver,
|
|
902
|
-
isFormData,
|
|
903
|
-
isMultipleContentTypes,
|
|
904
|
-
hasFormData,
|
|
905
|
-
headers,
|
|
906
|
-
zodQueryParamsName
|
|
907
|
-
});
|
|
908
|
-
const jsdoc = (0, _kubb_ast_utils.buildJSDoc)(buildOperationComments(node, {
|
|
909
|
-
link: "urlPath",
|
|
910
|
-
linkPosition: "beforeDeprecated",
|
|
911
|
-
splitLines: true
|
|
912
|
-
}));
|
|
913
|
-
const requestDataLine = buildRequestDataLine({
|
|
914
|
-
parser,
|
|
915
|
-
node,
|
|
916
|
-
zodResolver
|
|
917
|
-
});
|
|
918
|
-
const queryParamsLine = buildQueryParamsLine({
|
|
919
|
-
parser,
|
|
920
|
-
node,
|
|
921
|
-
zodResolver
|
|
922
|
-
});
|
|
923
|
-
const formDataLine = buildFormDataLine(isFormData || isMultipleContentTypes && hasFormData, !!node.requestBody?.content?.[0]?.schema);
|
|
924
|
-
const returnStatement = buildReturnStatement({
|
|
925
|
-
dataReturnType,
|
|
926
|
-
parser,
|
|
927
|
-
node,
|
|
928
|
-
zodResolver,
|
|
929
|
-
tsResolver
|
|
930
|
-
});
|
|
931
|
-
return `${jsdoc}async ${name}(${paramsSignature}) {\n${[
|
|
932
|
-
`const { client: request = client, ${isMultipleContentTypes ? `contentType = ${(0, _kubb_ast_utils.stringify)(contentType)}, ` : ""}...requestConfig } = mergeConfig(this.#config, config)`,
|
|
933
|
-
"",
|
|
934
|
-
requestDataLine,
|
|
935
|
-
queryParamsLine,
|
|
936
|
-
formDataLine,
|
|
937
|
-
`const res = await request<${generics.join(", ")}>(${clientParams.toCall()})`,
|
|
938
|
-
returnStatement
|
|
939
|
-
].filter(Boolean).map((line) => ` ${line}`).join("\n")}\n }`;
|
|
940
|
-
}
|
|
941
|
-
require_chunk.__name(generateMethod$1, "generateMethod");
|
|
942
|
-
function ClassClient({ name, isExportable = true, isIndexable = true, operations, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType, children }) {
|
|
943
|
-
const classCode = `export class ${name} {
|
|
944
|
-
#config: Partial<RequestConfig> & { client?: Client }
|
|
945
|
-
|
|
946
|
-
constructor(config: Partial<RequestConfig> & { client?: Client } = {}) {
|
|
947
|
-
this.#config = config
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
${operations.map(({ node, name: methodName, tsResolver, zodResolver }) => generateMethod$1({
|
|
951
|
-
node,
|
|
952
|
-
name: methodName,
|
|
953
|
-
tsResolver,
|
|
954
|
-
zodResolver,
|
|
955
|
-
baseURL,
|
|
956
|
-
dataReturnType,
|
|
957
|
-
parser,
|
|
958
|
-
paramsType,
|
|
959
|
-
paramsCasing,
|
|
960
|
-
pathParamsType
|
|
961
|
-
})).join("\n\n")}
|
|
962
|
-
}`;
|
|
963
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File.Source, {
|
|
964
|
-
name,
|
|
965
|
-
isExportable,
|
|
966
|
-
isIndexable,
|
|
967
|
-
children: [classCode, children]
|
|
968
816
|
});
|
|
969
817
|
}
|
|
970
818
|
//#endregion
|
|
971
|
-
//#region src/
|
|
972
|
-
function WrapperClient({ name, controllers, isExportable = true, isIndexable = true }) {
|
|
973
|
-
const classCode = `export class ${name} {
|
|
974
|
-
${controllers.map(({ className, propertyName }) => ` readonly ${propertyName}: ${className}`).join("\n")}
|
|
975
|
-
|
|
976
|
-
constructor(config: Partial<RequestConfig> & { client?: Client } = {}) {
|
|
977
|
-
${controllers.map(({ className, propertyName }) => ` this.${propertyName} = new ${className}(config)`).join("\n")}
|
|
978
|
-
}
|
|
979
|
-
}`;
|
|
980
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
981
|
-
name,
|
|
982
|
-
isExportable,
|
|
983
|
-
isIndexable,
|
|
984
|
-
children: classCode
|
|
985
|
-
});
|
|
986
|
-
}
|
|
987
|
-
//#endregion
|
|
988
|
-
//#region src/generators/classClientGenerator.tsx
|
|
989
|
-
function resolveTypeImportNames$1(node, tsResolver) {
|
|
990
|
-
return resolveOperationTypeNames(node, tsResolver, { order: "body-response-first" });
|
|
991
|
-
}
|
|
992
|
-
require_chunk.__name(resolveTypeImportNames$1, "resolveTypeImportNames");
|
|
993
|
-
function resolveZodImportNames$1(node, zodResolver, parser) {
|
|
994
|
-
const { query: queryParams } = getOperationParameters(node);
|
|
995
|
-
return [
|
|
996
|
-
resolveResponseParser(parser) === "zod" ? zodResolver.resolveResponseName?.(node) : null,
|
|
997
|
-
resolveRequestParser(parser) === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
998
|
-
resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null
|
|
999
|
-
].filter((n) => Boolean(n));
|
|
1000
|
-
}
|
|
1001
|
-
require_chunk.__name(resolveZodImportNames$1, "resolveZodImportNames");
|
|
819
|
+
//#region ../../internals/client/src/resolveOperationTypes.ts
|
|
1002
820
|
/**
|
|
1003
|
-
*
|
|
1004
|
-
*
|
|
1005
|
-
* per operation and a shared constructor for request configuration.
|
|
821
|
+
* Warning a client generator reports when neither plugin can supply the operation types, so the
|
|
822
|
+
* pipeline says why nothing was generated instead of silently emitting no files.
|
|
1006
823
|
*/
|
|
1007
|
-
const
|
|
1008
|
-
name: "classClient",
|
|
1009
|
-
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
1010
|
-
operations(nodes, ctx) {
|
|
1011
|
-
const { config, driver, resolver, root } = ctx;
|
|
1012
|
-
const { output, group, dataReturnType, paramsCasing, paramsType, pathParamsType, parser, importPath, sdk } = ctx.options;
|
|
1013
|
-
const baseURL = ctx.options.baseURL ?? ctx.meta.baseURL;
|
|
1014
|
-
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1015
|
-
if (!pluginTs) return null;
|
|
1016
|
-
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1017
|
-
const tsPluginOptions = pluginTs.options;
|
|
1018
|
-
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1019
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1020
|
-
function buildOperationData(node) {
|
|
1021
|
-
const typeFile = tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1022
|
-
root,
|
|
1023
|
-
output: tsPluginOptions?.output ?? output,
|
|
1024
|
-
group: tsPluginOptions?.group
|
|
1025
|
-
});
|
|
1026
|
-
const zodFile = zodResolver && pluginZod?.options ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1027
|
-
root,
|
|
1028
|
-
output: pluginZod.options?.output ?? output,
|
|
1029
|
-
group: pluginZod.options?.group ?? void 0
|
|
1030
|
-
}) : null;
|
|
1031
|
-
return {
|
|
1032
|
-
node,
|
|
1033
|
-
name: resolver.resolveName(node.operationId),
|
|
1034
|
-
tsResolver,
|
|
1035
|
-
zodResolver,
|
|
1036
|
-
typeFile,
|
|
1037
|
-
zodFile
|
|
1038
|
-
};
|
|
1039
|
-
}
|
|
1040
|
-
const controllers = nodes.reduce((acc, operationNode) => {
|
|
1041
|
-
if (!_kubb_core.ast.isHttpOperationNode(operationNode)) return acc;
|
|
1042
|
-
const tag = operationNode.tags[0];
|
|
1043
|
-
const groupName = tag ? group?.name?.({ group: camelCase(tag) }) ?? resolver.resolveGroupName(tag) : resolver.resolveClassName("Client");
|
|
1044
|
-
if (!tag && !group) {
|
|
1045
|
-
const name = resolver.resolveClassName("ApiClient");
|
|
1046
|
-
const file = resolver.resolveFile({
|
|
1047
|
-
name,
|
|
1048
|
-
extname: ".ts"
|
|
1049
|
-
}, {
|
|
1050
|
-
root,
|
|
1051
|
-
output,
|
|
1052
|
-
group: group ?? void 0
|
|
1053
|
-
});
|
|
1054
|
-
const operationData = buildOperationData(operationNode);
|
|
1055
|
-
const previous = acc.find((item) => item.file.path === file.path);
|
|
1056
|
-
if (previous) previous.operations.push(operationData);
|
|
1057
|
-
else acc.push({
|
|
1058
|
-
name,
|
|
1059
|
-
propertyName: resolver.resolveClientPropertyName(name),
|
|
1060
|
-
file,
|
|
1061
|
-
operations: [operationData]
|
|
1062
|
-
});
|
|
1063
|
-
return acc;
|
|
1064
|
-
}
|
|
1065
|
-
if (tag) {
|
|
1066
|
-
const name = groupName;
|
|
1067
|
-
const file = resolver.resolveFile({
|
|
1068
|
-
name,
|
|
1069
|
-
extname: ".ts",
|
|
1070
|
-
tag
|
|
1071
|
-
}, {
|
|
1072
|
-
root,
|
|
1073
|
-
output,
|
|
1074
|
-
group: group ?? void 0
|
|
1075
|
-
});
|
|
1076
|
-
const operationData = buildOperationData(operationNode);
|
|
1077
|
-
const previous = acc.find((item) => item.file.path === file.path);
|
|
1078
|
-
if (previous) previous.operations.push(operationData);
|
|
1079
|
-
else acc.push({
|
|
1080
|
-
name,
|
|
1081
|
-
propertyName: resolver.resolveClientPropertyName(name),
|
|
1082
|
-
file,
|
|
1083
|
-
operations: [operationData]
|
|
1084
|
-
});
|
|
1085
|
-
}
|
|
1086
|
-
return acc;
|
|
1087
|
-
}, []);
|
|
1088
|
-
function collectTypeImports(ops) {
|
|
1089
|
-
const typeImportsByFile = /* @__PURE__ */ new Map();
|
|
1090
|
-
const typeFilesByPath = /* @__PURE__ */ new Map();
|
|
1091
|
-
ops.forEach((op) => {
|
|
1092
|
-
const names = resolveTypeImportNames$1(op.node, tsResolver);
|
|
1093
|
-
if (!typeImportsByFile.has(op.typeFile.path)) typeImportsByFile.set(op.typeFile.path, /* @__PURE__ */ new Set());
|
|
1094
|
-
const imports = typeImportsByFile.get(op.typeFile.path);
|
|
1095
|
-
names.forEach((n) => {
|
|
1096
|
-
imports.add(n);
|
|
1097
|
-
});
|
|
1098
|
-
typeFilesByPath.set(op.typeFile.path, op.typeFile);
|
|
1099
|
-
});
|
|
1100
|
-
return {
|
|
1101
|
-
typeImportsByFile,
|
|
1102
|
-
typeFilesByPath
|
|
1103
|
-
};
|
|
1104
|
-
}
|
|
1105
|
-
function collectZodImports(ops) {
|
|
1106
|
-
const zodImportsByFile = /* @__PURE__ */ new Map();
|
|
1107
|
-
const zodFilesByPath = /* @__PURE__ */ new Map();
|
|
1108
|
-
ops.forEach((op) => {
|
|
1109
|
-
if (!op.zodFile || !zodResolver) return;
|
|
1110
|
-
const names = resolveZodImportNames$1(op.node, zodResolver, parser);
|
|
1111
|
-
if (!zodImportsByFile.has(op.zodFile.path)) zodImportsByFile.set(op.zodFile.path, /* @__PURE__ */ new Set());
|
|
1112
|
-
const imports = zodImportsByFile.get(op.zodFile.path);
|
|
1113
|
-
names.forEach((n) => {
|
|
1114
|
-
imports.add(n);
|
|
1115
|
-
});
|
|
1116
|
-
zodFilesByPath.set(op.zodFile.path, op.zodFile);
|
|
1117
|
-
});
|
|
1118
|
-
return {
|
|
1119
|
-
zodImportsByFile,
|
|
1120
|
-
zodFilesByPath
|
|
1121
|
-
};
|
|
1122
|
-
}
|
|
1123
|
-
const files = controllers.map(({ name, file, operations: ops }) => {
|
|
1124
|
-
const { typeImportsByFile, typeFilesByPath } = collectTypeImports(ops);
|
|
1125
|
-
const { zodImportsByFile, zodFilesByPath } = isParserEnabled(parser) ? collectZodImports(ops) : {
|
|
1126
|
-
zodImportsByFile: /* @__PURE__ */ new Map(),
|
|
1127
|
-
zodFilesByPath: /* @__PURE__ */ new Map()
|
|
1128
|
-
};
|
|
1129
|
-
const hasFormData = ops.some((op) => op.node.requestBody?.content?.some((e) => e.contentType === "multipart/form-data") ?? false);
|
|
1130
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1131
|
-
baseName: file.baseName,
|
|
1132
|
-
path: file.path,
|
|
1133
|
-
meta: file.meta,
|
|
1134
|
-
banner: resolver.resolveBanner(ctx.meta, {
|
|
1135
|
-
output,
|
|
1136
|
-
config,
|
|
1137
|
-
file: {
|
|
1138
|
-
path: file.path,
|
|
1139
|
-
baseName: file.baseName
|
|
1140
|
-
}
|
|
1141
|
-
}),
|
|
1142
|
-
footer: resolver.resolveFooter(ctx.meta, {
|
|
1143
|
-
output,
|
|
1144
|
-
config,
|
|
1145
|
-
file: {
|
|
1146
|
-
path: file.path,
|
|
1147
|
-
baseName: file.baseName
|
|
1148
|
-
}
|
|
1149
|
-
}),
|
|
1150
|
-
children: [
|
|
1151
|
-
importPath ? /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
1152
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1153
|
-
name: "client",
|
|
1154
|
-
path: importPath
|
|
1155
|
-
}),
|
|
1156
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1157
|
-
name: ["mergeConfig"],
|
|
1158
|
-
path: importPath
|
|
1159
|
-
}),
|
|
1160
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1161
|
-
name: [
|
|
1162
|
-
"Client",
|
|
1163
|
-
"RequestConfig",
|
|
1164
|
-
"ResponseErrorConfig"
|
|
1165
|
-
],
|
|
1166
|
-
path: importPath,
|
|
1167
|
-
isTypeOnly: true
|
|
1168
|
-
})
|
|
1169
|
-
] }) : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
1170
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1171
|
-
name: ["client"],
|
|
1172
|
-
root: file.path,
|
|
1173
|
-
path: node_path.default.resolve(root, ".kubb/client.ts")
|
|
1174
|
-
}),
|
|
1175
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1176
|
-
name: ["mergeConfig"],
|
|
1177
|
-
root: file.path,
|
|
1178
|
-
path: node_path.default.resolve(root, ".kubb/client.ts")
|
|
1179
|
-
}),
|
|
1180
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1181
|
-
name: [
|
|
1182
|
-
"Client",
|
|
1183
|
-
"RequestConfig",
|
|
1184
|
-
"ResponseErrorConfig"
|
|
1185
|
-
],
|
|
1186
|
-
root: file.path,
|
|
1187
|
-
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1188
|
-
isTypeOnly: true
|
|
1189
|
-
})
|
|
1190
|
-
] }),
|
|
1191
|
-
hasFormData && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1192
|
-
name: ["buildFormData"],
|
|
1193
|
-
root: file.path,
|
|
1194
|
-
path: node_path.default.resolve(root, ".kubb/config.ts")
|
|
1195
|
-
}),
|
|
1196
|
-
parser === "zod" && zodResolver && ops.some((op) => op.node.requestBody?.content?.[0]?.schema != null) && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1197
|
-
name: ["z"],
|
|
1198
|
-
path: "zod",
|
|
1199
|
-
isTypeOnly: true
|
|
1200
|
-
}),
|
|
1201
|
-
Array.from(typeImportsByFile.entries()).map(([filePath, importSet]) => {
|
|
1202
|
-
const typeFile = typeFilesByPath.get(filePath);
|
|
1203
|
-
if (!typeFile) return null;
|
|
1204
|
-
const importNames = Array.from(importSet).filter(Boolean);
|
|
1205
|
-
if (importNames.length === 0) return null;
|
|
1206
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1207
|
-
name: importNames,
|
|
1208
|
-
root: file.path,
|
|
1209
|
-
path: typeFile.path,
|
|
1210
|
-
isTypeOnly: true
|
|
1211
|
-
}, filePath);
|
|
1212
|
-
}),
|
|
1213
|
-
isParserEnabled(parser) && Array.from(zodImportsByFile.entries()).map(([filePath, importSet]) => {
|
|
1214
|
-
const zodFile = zodFilesByPath.get(filePath);
|
|
1215
|
-
if (!zodFile) return null;
|
|
1216
|
-
const importNames = Array.from(importSet).filter(Boolean);
|
|
1217
|
-
if (importNames.length === 0) return null;
|
|
1218
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1219
|
-
name: importNames,
|
|
1220
|
-
root: file.path,
|
|
1221
|
-
path: zodFile.path
|
|
1222
|
-
}, filePath);
|
|
1223
|
-
}),
|
|
1224
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(ClassClient, {
|
|
1225
|
-
name,
|
|
1226
|
-
operations: ops,
|
|
1227
|
-
baseURL,
|
|
1228
|
-
dataReturnType,
|
|
1229
|
-
pathParamsType,
|
|
1230
|
-
paramsCasing,
|
|
1231
|
-
paramsType,
|
|
1232
|
-
parser
|
|
1233
|
-
})
|
|
1234
|
-
]
|
|
1235
|
-
}, file.path);
|
|
1236
|
-
});
|
|
1237
|
-
if (sdk) {
|
|
1238
|
-
const sdkFile = resolver.resolveFile({
|
|
1239
|
-
name: sdk.className,
|
|
1240
|
-
extname: ".ts"
|
|
1241
|
-
}, {
|
|
1242
|
-
root,
|
|
1243
|
-
output,
|
|
1244
|
-
group: group ?? void 0
|
|
1245
|
-
});
|
|
1246
|
-
files.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1247
|
-
baseName: sdkFile.baseName,
|
|
1248
|
-
path: sdkFile.path,
|
|
1249
|
-
meta: sdkFile.meta,
|
|
1250
|
-
banner: resolver.resolveBanner(ctx.meta, {
|
|
1251
|
-
output,
|
|
1252
|
-
config,
|
|
1253
|
-
file: {
|
|
1254
|
-
path: sdkFile.path,
|
|
1255
|
-
baseName: sdkFile.baseName
|
|
1256
|
-
}
|
|
1257
|
-
}),
|
|
1258
|
-
footer: resolver.resolveFooter(ctx.meta, {
|
|
1259
|
-
output,
|
|
1260
|
-
config,
|
|
1261
|
-
file: {
|
|
1262
|
-
path: sdkFile.path,
|
|
1263
|
-
baseName: sdkFile.baseName
|
|
1264
|
-
}
|
|
1265
|
-
}),
|
|
1266
|
-
children: [
|
|
1267
|
-
importPath ? /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1268
|
-
name: ["Client", "RequestConfig"],
|
|
1269
|
-
path: importPath,
|
|
1270
|
-
isTypeOnly: true
|
|
1271
|
-
}) : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1272
|
-
name: ["Client", "RequestConfig"],
|
|
1273
|
-
root: sdkFile.path,
|
|
1274
|
-
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1275
|
-
isTypeOnly: true
|
|
1276
|
-
}),
|
|
1277
|
-
controllers.map(({ name, file }) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1278
|
-
name: [name],
|
|
1279
|
-
root: sdkFile.path,
|
|
1280
|
-
path: file.path
|
|
1281
|
-
}, name)),
|
|
1282
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(WrapperClient, {
|
|
1283
|
-
name: sdk.className,
|
|
1284
|
-
controllers: controllers.map(({ name, propertyName }) => ({
|
|
1285
|
-
className: name,
|
|
1286
|
-
propertyName
|
|
1287
|
-
}))
|
|
1288
|
-
})
|
|
1289
|
-
]
|
|
1290
|
-
}, sdkFile.path));
|
|
1291
|
-
}
|
|
1292
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: files });
|
|
1293
|
-
}
|
|
1294
|
-
});
|
|
1295
|
-
//#endregion
|
|
1296
|
-
//#region src/generators/clientGenerator.tsx
|
|
824
|
+
const MISSING_OPERATION_TYPES_WARNING = "Skipped: no operation types available. Add `pluginTs()`, or `pluginZod({ inferred: true })` to type the client from its zod schemas.";
|
|
1297
825
|
/**
|
|
1298
|
-
*
|
|
1299
|
-
*
|
|
1300
|
-
* `
|
|
826
|
+
* Picks the plugin a generated client takes its `Options` and `Responses` types from.
|
|
827
|
+
*
|
|
828
|
+
* `plugin-ts` wins whenever it is in the pipeline, so existing configs keep the types they generate
|
|
829
|
+
* today. Without it, `pluginZod({ inferred: true })` takes over: its inferred options and responses
|
|
830
|
+
* types are built from the same shared schemas as the `plugin-ts` ones, so a client typed off
|
|
831
|
+
* `z.infer` carries what the codecs decode to (a `Temporal` instance, a `Date`) rather than the raw
|
|
832
|
+
* wire type. Returns `null` when neither is available, leaving the caller nothing to type against.
|
|
833
|
+
*
|
|
834
|
+
* @example
|
|
835
|
+
* ```ts
|
|
836
|
+
* const types = resolveOperationTypes(ctx.driver)
|
|
837
|
+
* if (!types) return null
|
|
838
|
+
* types.response.options(node) // 'GetPetByIdOptions', or 'GetPetByIdOptionsSchemaType' from zod
|
|
839
|
+
* ```
|
|
1301
840
|
*/
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
if (!pluginTs) return null;
|
|
1312
|
-
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1313
|
-
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1314
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1315
|
-
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing });
|
|
1316
|
-
const { query: queryParams } = getOperationParameters(node);
|
|
1317
|
-
const importedZodNames = zodResolver ? [
|
|
1318
|
-
resolveResponseParser(parser) === "zod" ? zodResolver.resolveResponseName?.(node) : null,
|
|
1319
|
-
resolveRequestParser(parser) === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
1320
|
-
resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null
|
|
1321
|
-
].filter((name) => Boolean(name)) : [];
|
|
1322
|
-
const zodRequestName = zodResolver && parser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) ?? null : null;
|
|
1323
|
-
const meta = {
|
|
1324
|
-
name: resolver.resolveName(node.operationId),
|
|
1325
|
-
urlName: resolver.resolveUrlName(node),
|
|
1326
|
-
file: resolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1327
|
-
root,
|
|
1328
|
-
output,
|
|
1329
|
-
group: group ?? void 0
|
|
1330
|
-
}),
|
|
1331
|
-
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1332
|
-
root,
|
|
1333
|
-
output: pluginTs.options?.output ?? output,
|
|
1334
|
-
group: pluginTs.options?.group ?? void 0
|
|
1335
|
-
}),
|
|
1336
|
-
fileZod: zodResolver && pluginZod?.options ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1337
|
-
root,
|
|
1338
|
-
output: pluginZod.options.output ?? output,
|
|
1339
|
-
group: pluginZod.options?.group ?? void 0
|
|
1340
|
-
}) : null
|
|
841
|
+
function resolveOperationTypes(driver) {
|
|
842
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
843
|
+
if (pluginTs) {
|
|
844
|
+
const resolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
845
|
+
return {
|
|
846
|
+
response: resolver.response,
|
|
847
|
+
resolver,
|
|
848
|
+
output: pluginTs.options?.output,
|
|
849
|
+
group: pluginTs.options?.group
|
|
1341
850
|
};
|
|
1342
|
-
const hasFormData = node.requestBody?.content?.some((e) => e.contentType === "multipart/form-data") ?? false;
|
|
1343
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1344
|
-
baseName: meta.file.baseName,
|
|
1345
|
-
path: meta.file.path,
|
|
1346
|
-
meta: meta.file.meta,
|
|
1347
|
-
banner: resolver.resolveBanner(ctx.meta, {
|
|
1348
|
-
output,
|
|
1349
|
-
config,
|
|
1350
|
-
file: {
|
|
1351
|
-
path: meta.file.path,
|
|
1352
|
-
baseName: meta.file.baseName
|
|
1353
|
-
}
|
|
1354
|
-
}),
|
|
1355
|
-
footer: resolver.resolveFooter(ctx.meta, {
|
|
1356
|
-
output,
|
|
1357
|
-
config,
|
|
1358
|
-
file: {
|
|
1359
|
-
path: meta.file.path,
|
|
1360
|
-
baseName: meta.file.baseName
|
|
1361
|
-
}
|
|
1362
|
-
}),
|
|
1363
|
-
children: [
|
|
1364
|
-
importPath ? /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1365
|
-
name: "client",
|
|
1366
|
-
path: importPath
|
|
1367
|
-
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1368
|
-
name: [
|
|
1369
|
-
"Client",
|
|
1370
|
-
"RequestConfig",
|
|
1371
|
-
"ResponseErrorConfig"
|
|
1372
|
-
],
|
|
1373
|
-
path: importPath,
|
|
1374
|
-
isTypeOnly: true
|
|
1375
|
-
})] }) : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1376
|
-
name: ["client"],
|
|
1377
|
-
root: meta.file.path,
|
|
1378
|
-
path: node_path.default.resolve(root, ".kubb/client.ts")
|
|
1379
|
-
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1380
|
-
name: [
|
|
1381
|
-
"Client",
|
|
1382
|
-
"RequestConfig",
|
|
1383
|
-
"ResponseErrorConfig"
|
|
1384
|
-
],
|
|
1385
|
-
root: meta.file.path,
|
|
1386
|
-
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1387
|
-
isTypeOnly: true
|
|
1388
|
-
})] }),
|
|
1389
|
-
hasFormData && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1390
|
-
name: ["buildFormData"],
|
|
1391
|
-
root: meta.file.path,
|
|
1392
|
-
path: node_path.default.resolve(root, ".kubb/config.ts")
|
|
1393
|
-
}),
|
|
1394
|
-
zodRequestName && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1395
|
-
name: ["z"],
|
|
1396
|
-
path: "zod",
|
|
1397
|
-
isTypeOnly: true
|
|
1398
|
-
}),
|
|
1399
|
-
meta.fileZod && importedZodNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1400
|
-
name: importedZodNames,
|
|
1401
|
-
root: meta.file.path,
|
|
1402
|
-
path: meta.fileZod.path
|
|
1403
|
-
}),
|
|
1404
|
-
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1405
|
-
name: Array.from(new Set(importedTypeNames)),
|
|
1406
|
-
root: meta.file.path,
|
|
1407
|
-
path: meta.fileTs.path,
|
|
1408
|
-
isTypeOnly: true
|
|
1409
|
-
}),
|
|
1410
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Url, {
|
|
1411
|
-
name: meta.urlName,
|
|
1412
|
-
baseURL,
|
|
1413
|
-
pathParamsType,
|
|
1414
|
-
paramsCasing,
|
|
1415
|
-
paramsType,
|
|
1416
|
-
node,
|
|
1417
|
-
tsResolver,
|
|
1418
|
-
isIndexable: urlType === "export",
|
|
1419
|
-
isExportable: urlType === "export"
|
|
1420
|
-
}),
|
|
1421
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Client, {
|
|
1422
|
-
name: meta.name,
|
|
1423
|
-
urlName: meta.urlName,
|
|
1424
|
-
baseURL,
|
|
1425
|
-
dataReturnType,
|
|
1426
|
-
pathParamsType,
|
|
1427
|
-
paramsCasing,
|
|
1428
|
-
paramsType,
|
|
1429
|
-
node,
|
|
1430
|
-
tsResolver,
|
|
1431
|
-
zodResolver,
|
|
1432
|
-
parser
|
|
1433
|
-
})
|
|
1434
|
-
]
|
|
1435
|
-
});
|
|
1436
851
|
}
|
|
1437
|
-
|
|
852
|
+
const pluginZod = driver.getPlugin(_kubb_plugin_zod.pluginZodName);
|
|
853
|
+
if (!pluginZod?.options?.inferred) return null;
|
|
854
|
+
const resolver = driver.getResolver(_kubb_plugin_zod.pluginZodName);
|
|
855
|
+
return {
|
|
856
|
+
response: {
|
|
857
|
+
options: resolver.response.options,
|
|
858
|
+
responses: (node) => resolver.schema.type(resolver.response.responses(node))
|
|
859
|
+
},
|
|
860
|
+
resolver,
|
|
861
|
+
output: pluginZod.options.output,
|
|
862
|
+
group: pluginZod.options.group
|
|
863
|
+
};
|
|
864
|
+
}
|
|
1438
865
|
//#endregion
|
|
1439
|
-
//#region src/generators/
|
|
866
|
+
//#region ../../internals/client/src/generators/clientGenerator.tsx
|
|
1440
867
|
/**
|
|
1441
|
-
*
|
|
1442
|
-
*
|
|
1443
|
-
* `
|
|
1444
|
-
*
|
|
868
|
+
* Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`,
|
|
869
|
+
* `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared
|
|
870
|
+
* `Operation` component: a grouped `<Name>Request` type and a function that forwards a single
|
|
871
|
+
* `options` object to the bundled `client` and returns the `Unwrappable<RequestResult>`. Only the
|
|
872
|
+
* generator `name` differs between plugins. Every other resolution, import, and rendering step is
|
|
873
|
+
* identical.
|
|
1445
874
|
*/
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
875
|
+
function createClientGenerator(name, resolveClientPath) {
|
|
876
|
+
return (0, kubb_kit.defineGenerator)({
|
|
877
|
+
name,
|
|
878
|
+
renderer: kubb_jsx.jsxRenderer,
|
|
879
|
+
operation(node, ctx) {
|
|
880
|
+
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
881
|
+
const { config, driver, resolver, root } = ctx;
|
|
882
|
+
const { output, validator, returnType, throwOnErrorDefault, group } = ctx.options;
|
|
883
|
+
const types = resolveOperationTypes(driver);
|
|
884
|
+
if (!types) {
|
|
885
|
+
ctx.warn(MISSING_OPERATION_TYPES_WARNING);
|
|
886
|
+
return null;
|
|
887
|
+
}
|
|
888
|
+
const pluginZod = resolveResponseValidator(validator) === "zod" || resolveRequestValidator(validator) === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
889
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
890
|
+
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
891
|
+
const importedTypeNames = [types.response.options(node), types.response.responses(node)];
|
|
892
|
+
const importedZodNames = zodResolver ? [
|
|
893
|
+
resolveResponseValidator(validator) === "zod" ? zodResolver.response.response?.(node) : null,
|
|
894
|
+
resolveResponseValidator(validator) === "zod" ? buildZodErrorParse(node, zodResolver)?.expression ?? null : null,
|
|
895
|
+
resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body?.(node) : null
|
|
896
|
+
].filter((name) => Boolean(name)) : [];
|
|
897
|
+
const meta = {
|
|
898
|
+
name: resolver.name(node.operationId),
|
|
899
|
+
file: resolver.file({
|
|
900
|
+
...operationFileEntry(node, node.operationId),
|
|
1472
901
|
root,
|
|
1473
902
|
output,
|
|
1474
903
|
group: group ?? void 0
|
|
1475
|
-
});
|
|
1476
|
-
const client = {
|
|
1477
|
-
name: resolver.resolveName(operationNode.operationId),
|
|
1478
|
-
file: clientFile
|
|
1479
|
-
};
|
|
1480
|
-
const previous = acc.find((item) => item.file.path === file.path);
|
|
1481
|
-
if (previous) previous.clients.push(client);
|
|
1482
|
-
else acc.push({
|
|
1483
|
-
name,
|
|
1484
|
-
file,
|
|
1485
|
-
clients: [client]
|
|
1486
|
-
});
|
|
1487
|
-
}
|
|
1488
|
-
return acc;
|
|
1489
|
-
}, []).map(({ name, file, clients }) => {
|
|
1490
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1491
|
-
baseName: file.baseName,
|
|
1492
|
-
path: file.path,
|
|
1493
|
-
meta: file.meta,
|
|
1494
|
-
banner: resolver.resolveBanner(ctx.meta, {
|
|
1495
|
-
output,
|
|
1496
|
-
config,
|
|
1497
|
-
file: {
|
|
1498
|
-
path: file.path,
|
|
1499
|
-
baseName: file.baseName,
|
|
1500
|
-
isAggregation: true
|
|
1501
|
-
}
|
|
1502
|
-
}),
|
|
1503
|
-
footer: resolver.resolveFooter(ctx.meta, {
|
|
1504
|
-
output,
|
|
1505
|
-
config,
|
|
1506
|
-
file: {
|
|
1507
|
-
path: file.path,
|
|
1508
|
-
baseName: file.baseName,
|
|
1509
|
-
isAggregation: true
|
|
1510
|
-
}
|
|
1511
904
|
}),
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
}, client.name)), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1517
|
-
name,
|
|
1518
|
-
isExportable: true,
|
|
1519
|
-
isIndexable: true,
|
|
1520
|
-
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1521
|
-
export: true,
|
|
1522
|
-
name,
|
|
1523
|
-
children: `return { ${clients.map((client) => client.name).join(", ")} }`
|
|
1524
|
-
})
|
|
1525
|
-
})]
|
|
1526
|
-
}, file.path);
|
|
1527
|
-
}) });
|
|
1528
|
-
}
|
|
1529
|
-
});
|
|
1530
|
-
//#endregion
|
|
1531
|
-
//#region src/generators/operationsGenerator.ts
|
|
1532
|
-
/**
|
|
1533
|
-
* Generates an `operations.ts` file that re-exports every operation grouped
|
|
1534
|
-
* by HTTP method. Enabled when `pluginClient({ operations: true })`. Useful
|
|
1535
|
-
* for building meta-tooling on top of the generated client (route
|
|
1536
|
-
* registries, API explorers).
|
|
1537
|
-
*/
|
|
1538
|
-
const operationsGenerator = (0, _kubb_core.defineGenerator)({
|
|
1539
|
-
name: "client",
|
|
1540
|
-
operations(nodes, ctx) {
|
|
1541
|
-
const { config, resolver, root } = ctx;
|
|
1542
|
-
const { output, group } = ctx.options;
|
|
1543
|
-
const name = "operations";
|
|
1544
|
-
const file = resolver.resolveFile({
|
|
1545
|
-
name,
|
|
1546
|
-
extname: ".ts"
|
|
1547
|
-
}, {
|
|
1548
|
-
root,
|
|
1549
|
-
output,
|
|
1550
|
-
group: group ?? void 0
|
|
1551
|
-
});
|
|
1552
|
-
const operationsObject = {};
|
|
1553
|
-
for (const node of nodes) {
|
|
1554
|
-
if (!_kubb_core.ast.isHttpOperationNode(node)) continue;
|
|
1555
|
-
operationsObject[node.operationId] = {
|
|
1556
|
-
path: Url$1.toPath(node.path),
|
|
1557
|
-
method: node.method.toLowerCase()
|
|
1558
|
-
};
|
|
1559
|
-
}
|
|
1560
|
-
return [_kubb_core.ast.factory.createFile({
|
|
1561
|
-
baseName: file.baseName,
|
|
1562
|
-
path: file.path,
|
|
1563
|
-
meta: file.meta,
|
|
1564
|
-
banner: resolver.resolveBanner(ctx.meta, {
|
|
1565
|
-
output,
|
|
1566
|
-
config,
|
|
1567
|
-
file: {
|
|
1568
|
-
path: file.path,
|
|
1569
|
-
baseName: file.baseName
|
|
1570
|
-
}
|
|
1571
|
-
}),
|
|
1572
|
-
footer: resolver.resolveFooter(ctx.meta, {
|
|
1573
|
-
output,
|
|
1574
|
-
config,
|
|
1575
|
-
file: {
|
|
1576
|
-
path: file.path,
|
|
1577
|
-
baseName: file.baseName
|
|
1578
|
-
}
|
|
1579
|
-
}),
|
|
1580
|
-
sources: [_kubb_core.ast.factory.createSource({
|
|
1581
|
-
name,
|
|
1582
|
-
isExportable: true,
|
|
1583
|
-
isIndexable: true,
|
|
1584
|
-
nodes: [_kubb_core.ast.factory.createConst({
|
|
1585
|
-
name,
|
|
1586
|
-
export: true,
|
|
1587
|
-
nodes: [_kubb_core.ast.factory.createText(JSON.stringify(operationsObject, void 0, 2))]
|
|
1588
|
-
})]
|
|
1589
|
-
})]
|
|
1590
|
-
})];
|
|
1591
|
-
}
|
|
1592
|
-
});
|
|
1593
|
-
//#endregion
|
|
1594
|
-
//#region src/components/StaticClassClient.tsx
|
|
1595
|
-
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1596
|
-
function generateMethod({ node, name, tsResolver, zodResolver, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType }) {
|
|
1597
|
-
if (!_kubb_core.ast.isHttpOperationNode(node)) return "";
|
|
1598
|
-
const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node);
|
|
1599
|
-
const isFormData = !isMultipleContentTypes && contentType === "multipart/form-data";
|
|
1600
|
-
const { header: headerParams } = getOperationParameters(node);
|
|
1601
|
-
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) : null;
|
|
1602
|
-
const headers = isMultipleContentTypes ? headerParamsName ? ["...headers"] : [] : buildHeaders(contentType, !!headerParamsName);
|
|
1603
|
-
const generics = buildGenerics(node, tsResolver, {
|
|
1604
|
-
dataReturnType,
|
|
1605
|
-
zodResolver,
|
|
1606
|
-
parser
|
|
1607
|
-
});
|
|
1608
|
-
const paramsNode = buildClientParamsNode({
|
|
1609
|
-
paramsType,
|
|
1610
|
-
paramsCasing,
|
|
1611
|
-
pathParamsType,
|
|
1612
|
-
node,
|
|
1613
|
-
tsResolver,
|
|
1614
|
-
isConfigurable: true
|
|
1615
|
-
});
|
|
1616
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
1617
|
-
const { query: queryParams } = getOperationParameters(node);
|
|
1618
|
-
const zodQueryParamsName = zodResolver && resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null;
|
|
1619
|
-
const clientParams = buildClassClientParams({
|
|
1620
|
-
node,
|
|
1621
|
-
url: Url$1.toTemplateString(node.path, { casing: paramsCasing }),
|
|
1622
|
-
baseURL,
|
|
1623
|
-
tsResolver,
|
|
1624
|
-
isFormData,
|
|
1625
|
-
isMultipleContentTypes,
|
|
1626
|
-
hasFormData,
|
|
1627
|
-
headers,
|
|
1628
|
-
zodQueryParamsName
|
|
1629
|
-
});
|
|
1630
|
-
const jsdoc = (0, _kubb_ast_utils.buildJSDoc)(buildOperationComments(node, {
|
|
1631
|
-
link: "urlPath",
|
|
1632
|
-
linkPosition: "beforeDeprecated",
|
|
1633
|
-
splitLines: true
|
|
1634
|
-
}));
|
|
1635
|
-
const requestDataLine = buildRequestDataLine({
|
|
1636
|
-
parser,
|
|
1637
|
-
node,
|
|
1638
|
-
zodResolver
|
|
1639
|
-
});
|
|
1640
|
-
const queryParamsLine = buildQueryParamsLine({
|
|
1641
|
-
parser,
|
|
1642
|
-
node,
|
|
1643
|
-
zodResolver
|
|
1644
|
-
});
|
|
1645
|
-
const formDataLine = buildFormDataLine(isFormData || isMultipleContentTypes && hasFormData, !!node.requestBody?.content?.[0]?.schema);
|
|
1646
|
-
const returnStatement = buildReturnStatement({
|
|
1647
|
-
dataReturnType,
|
|
1648
|
-
parser,
|
|
1649
|
-
node,
|
|
1650
|
-
zodResolver,
|
|
1651
|
-
tsResolver
|
|
1652
|
-
});
|
|
1653
|
-
return `${jsdoc} static async ${name}(${paramsSignature}) {\n${[
|
|
1654
|
-
`const { client: request = client, ${isMultipleContentTypes ? `contentType = ${(0, _kubb_ast_utils.stringify)(contentType)}, ` : ""}...requestConfig } = mergeConfig(this.#config, config)`,
|
|
1655
|
-
"",
|
|
1656
|
-
requestDataLine,
|
|
1657
|
-
queryParamsLine,
|
|
1658
|
-
formDataLine,
|
|
1659
|
-
`const res = await request<${generics.join(", ")}>(${clientParams.toCall()})`,
|
|
1660
|
-
returnStatement
|
|
1661
|
-
].filter(Boolean).map((line) => ` ${line}`).join("\n")}\n }`;
|
|
1662
|
-
}
|
|
1663
|
-
function StaticClassClient({ name, isExportable = true, isIndexable = true, operations, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType, children }) {
|
|
1664
|
-
const classCode = `export class ${name} {\n static #config: Partial<RequestConfig> & { client?: Client } = {}\n\n${operations.map(({ node, name: methodName, tsResolver, zodResolver }) => generateMethod({
|
|
1665
|
-
node,
|
|
1666
|
-
name: methodName,
|
|
1667
|
-
tsResolver,
|
|
1668
|
-
zodResolver,
|
|
1669
|
-
baseURL,
|
|
1670
|
-
dataReturnType,
|
|
1671
|
-
parser,
|
|
1672
|
-
paramsType,
|
|
1673
|
-
paramsCasing,
|
|
1674
|
-
pathParamsType
|
|
1675
|
-
})).join("\n\n")}\n}`;
|
|
1676
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File.Source, {
|
|
1677
|
-
name,
|
|
1678
|
-
isExportable,
|
|
1679
|
-
isIndexable,
|
|
1680
|
-
children: [classCode, children]
|
|
1681
|
-
});
|
|
1682
|
-
}
|
|
1683
|
-
//#endregion
|
|
1684
|
-
//#region src/generators/staticClassClientGenerator.tsx
|
|
1685
|
-
function resolveTypeImportNames(node, tsResolver) {
|
|
1686
|
-
return resolveOperationTypeNames(node, tsResolver, { order: "body-response-first" });
|
|
1687
|
-
}
|
|
1688
|
-
function resolveZodImportNames(node, zodResolver, parser) {
|
|
1689
|
-
const { query: queryParams } = getOperationParameters(node);
|
|
1690
|
-
return [
|
|
1691
|
-
resolveResponseParser(parser) === "zod" ? zodResolver.resolveResponseName?.(node) : null,
|
|
1692
|
-
resolveRequestParser(parser) === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
1693
|
-
resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null
|
|
1694
|
-
].filter((n) => Boolean(n));
|
|
1695
|
-
}
|
|
1696
|
-
/**
|
|
1697
|
-
* Built-in `operations` generator for `@kubb/plugin-client` when
|
|
1698
|
-
* `clientType: 'staticClass'`. Emits one class per tag, with a static method
|
|
1699
|
-
* per operation so callers can use `Pet.getPetById(...)` without
|
|
1700
|
-
* instantiating the class.
|
|
1701
|
-
*/
|
|
1702
|
-
const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
1703
|
-
name: "staticClassClient",
|
|
1704
|
-
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
1705
|
-
operations(nodes, ctx) {
|
|
1706
|
-
const { config, driver, resolver, root } = ctx;
|
|
1707
|
-
const { output, group, dataReturnType, paramsCasing, paramsType, pathParamsType, parser, importPath } = ctx.options;
|
|
1708
|
-
const baseURL = ctx.options.baseURL ?? ctx.meta.baseURL;
|
|
1709
|
-
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1710
|
-
if (!pluginTs) return null;
|
|
1711
|
-
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1712
|
-
const tsPluginOptions = pluginTs.options;
|
|
1713
|
-
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1714
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1715
|
-
function buildOperationData(node) {
|
|
1716
|
-
const typeFile = tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1717
|
-
root,
|
|
1718
|
-
output: tsPluginOptions?.output ?? output,
|
|
1719
|
-
group: tsPluginOptions?.group
|
|
1720
|
-
});
|
|
1721
|
-
const zodFile = zodResolver && pluginZod?.options ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1722
|
-
root,
|
|
1723
|
-
output: pluginZod.options?.output ?? output,
|
|
1724
|
-
group: pluginZod.options?.group ?? void 0
|
|
1725
|
-
}) : null;
|
|
1726
|
-
return {
|
|
1727
|
-
node,
|
|
1728
|
-
name: resolver.resolveName(node.operationId),
|
|
1729
|
-
tsResolver,
|
|
1730
|
-
zodResolver,
|
|
1731
|
-
typeFile,
|
|
1732
|
-
zodFile
|
|
1733
|
-
};
|
|
1734
|
-
}
|
|
1735
|
-
const controllers = nodes.reduce((acc, operationNode) => {
|
|
1736
|
-
if (!_kubb_core.ast.isHttpOperationNode(operationNode)) return acc;
|
|
1737
|
-
const tag = operationNode.tags[0];
|
|
1738
|
-
const groupName = tag ? group?.name?.({ group: camelCase(tag) }) ?? resolver.resolveGroupName(tag) : resolver.resolveClassName("Client");
|
|
1739
|
-
if (!tag && !group) {
|
|
1740
|
-
const name = resolver.resolveClassName("ApiClient");
|
|
1741
|
-
const file = resolver.resolveFile({
|
|
1742
|
-
name,
|
|
1743
|
-
extname: ".ts"
|
|
1744
|
-
}, {
|
|
905
|
+
fileTypes: resolveDependencyOperationFile({
|
|
906
|
+
cache: ctx.cache,
|
|
907
|
+
node,
|
|
908
|
+
resolver: types.resolver,
|
|
1745
909
|
root,
|
|
1746
|
-
output,
|
|
1747
|
-
group: group
|
|
1748
|
-
})
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
if (previous) previous.operations.push(operationData);
|
|
1752
|
-
else acc.push({
|
|
1753
|
-
name,
|
|
1754
|
-
file,
|
|
1755
|
-
operations: [operationData]
|
|
1756
|
-
});
|
|
1757
|
-
return acc;
|
|
1758
|
-
}
|
|
1759
|
-
if (tag) {
|
|
1760
|
-
const name = groupName;
|
|
1761
|
-
const file = resolver.resolveFile({
|
|
1762
|
-
name,
|
|
1763
|
-
extname: ".ts",
|
|
1764
|
-
tag
|
|
1765
|
-
}, {
|
|
910
|
+
output: types.output ?? output,
|
|
911
|
+
group: types.group
|
|
912
|
+
}),
|
|
913
|
+
fileZod: zodResolver && pluginZod?.options ? zodResolver.file({
|
|
914
|
+
...operationFileEntry(node, node.operationId),
|
|
1766
915
|
root,
|
|
1767
|
-
output,
|
|
1768
|
-
group: group ?? void 0
|
|
1769
|
-
})
|
|
1770
|
-
const operationData = buildOperationData(operationNode);
|
|
1771
|
-
const previous = acc.find((item) => item.file.path === file.path);
|
|
1772
|
-
if (previous) previous.operations.push(operationData);
|
|
1773
|
-
else acc.push({
|
|
1774
|
-
name,
|
|
1775
|
-
file,
|
|
1776
|
-
operations: [operationData]
|
|
1777
|
-
});
|
|
1778
|
-
}
|
|
1779
|
-
return acc;
|
|
1780
|
-
}, []);
|
|
1781
|
-
function collectTypeImports(ops) {
|
|
1782
|
-
const typeImportsByFile = /* @__PURE__ */ new Map();
|
|
1783
|
-
const typeFilesByPath = /* @__PURE__ */ new Map();
|
|
1784
|
-
ops.forEach((op) => {
|
|
1785
|
-
const names = resolveTypeImportNames(op.node, tsResolver);
|
|
1786
|
-
if (!typeImportsByFile.has(op.typeFile.path)) typeImportsByFile.set(op.typeFile.path, /* @__PURE__ */ new Set());
|
|
1787
|
-
const imports = typeImportsByFile.get(op.typeFile.path);
|
|
1788
|
-
names.forEach((n) => {
|
|
1789
|
-
imports.add(n);
|
|
1790
|
-
});
|
|
1791
|
-
typeFilesByPath.set(op.typeFile.path, op.typeFile);
|
|
1792
|
-
});
|
|
1793
|
-
return {
|
|
1794
|
-
typeImportsByFile,
|
|
1795
|
-
typeFilesByPath
|
|
916
|
+
output: pluginZod.options.output ?? output,
|
|
917
|
+
group: pluginZod.options?.group ?? void 0
|
|
918
|
+
}) : null
|
|
1796
919
|
};
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
ops.forEach((op) => {
|
|
1802
|
-
if (!op.zodFile || !zodResolver) return;
|
|
1803
|
-
const names = resolveZodImportNames(op.node, zodResolver, parser);
|
|
1804
|
-
if (!zodImportsByFile.has(op.zodFile.path)) zodImportsByFile.set(op.zodFile.path, /* @__PURE__ */ new Set());
|
|
1805
|
-
const imports = zodImportsByFile.get(op.zodFile.path);
|
|
1806
|
-
names.forEach((n) => {
|
|
1807
|
-
imports.add(n);
|
|
1808
|
-
});
|
|
1809
|
-
zodFilesByPath.set(op.zodFile.path, op.zodFile);
|
|
920
|
+
const security = getOperationSecurity({
|
|
921
|
+
document: ctx.adapter.document,
|
|
922
|
+
method: node.method,
|
|
923
|
+
path: node.path
|
|
1810
924
|
});
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
925
|
+
const customClientPath = resolveClientPath?.(ctx.options, root);
|
|
926
|
+
const clientPath = customClientPath ?? node_path.default.resolve(root, ".kubb/client.ts");
|
|
927
|
+
const clientRoot = customClientPath === void 0 ? meta.file.path : void 0;
|
|
928
|
+
const eventStream = isEventStream(node);
|
|
929
|
+
const clientValueImports = (() => {
|
|
930
|
+
if (eventStream) return ["client", "toEventStream"];
|
|
931
|
+
if (returnType === "plain") return ["client"];
|
|
932
|
+
if (returnType === "data") return ["client", "unwrapResult"];
|
|
933
|
+
return ["client", "withUnwrap"];
|
|
934
|
+
})();
|
|
935
|
+
const clientTypeImports = (() => {
|
|
936
|
+
if (eventStream) return [
|
|
937
|
+
"Options",
|
|
938
|
+
"EventStreamResult",
|
|
939
|
+
"SuccessOf"
|
|
940
|
+
];
|
|
941
|
+
if (returnType === "plain") return ["Options", "RequestResult"];
|
|
942
|
+
if (returnType === "data") return ["Options", "UnwrappedResult"];
|
|
943
|
+
return [
|
|
944
|
+
"Options",
|
|
945
|
+
"Unwrappable",
|
|
946
|
+
"RequestResult"
|
|
947
|
+
];
|
|
948
|
+
})();
|
|
949
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
950
|
+
baseName: meta.file.baseName,
|
|
951
|
+
path: meta.file.path,
|
|
952
|
+
meta: meta.file.meta,
|
|
953
|
+
banner: resolver.default.banner(ctx.meta, {
|
|
1828
954
|
output,
|
|
1829
955
|
config,
|
|
1830
956
|
file: {
|
|
1831
|
-
path: file.path,
|
|
1832
|
-
baseName: file.baseName
|
|
957
|
+
path: meta.file.path,
|
|
958
|
+
baseName: meta.file.baseName
|
|
1833
959
|
}
|
|
1834
960
|
}),
|
|
1835
|
-
footer: resolver.
|
|
961
|
+
footer: resolver.default.footer(ctx.meta, {
|
|
1836
962
|
output,
|
|
1837
963
|
config,
|
|
1838
964
|
file: {
|
|
1839
|
-
path: file.path,
|
|
1840
|
-
baseName: file.baseName
|
|
965
|
+
path: meta.file.path,
|
|
966
|
+
baseName: meta.file.baseName
|
|
1841
967
|
}
|
|
1842
968
|
}),
|
|
1843
969
|
children: [
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
}),
|
|
1849
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1850
|
-
name: ["mergeConfig"],
|
|
1851
|
-
path: importPath
|
|
1852
|
-
}),
|
|
1853
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1854
|
-
name: [
|
|
1855
|
-
"Client",
|
|
1856
|
-
"RequestConfig",
|
|
1857
|
-
"ResponseErrorConfig"
|
|
1858
|
-
],
|
|
1859
|
-
path: importPath,
|
|
1860
|
-
isTypeOnly: true
|
|
1861
|
-
})
|
|
1862
|
-
] }) : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
1863
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1864
|
-
name: ["client"],
|
|
1865
|
-
root: file.path,
|
|
1866
|
-
path: node_path.default.resolve(root, ".kubb/client.ts")
|
|
1867
|
-
}),
|
|
1868
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1869
|
-
name: ["mergeConfig"],
|
|
1870
|
-
root: file.path,
|
|
1871
|
-
path: node_path.default.resolve(root, ".kubb/client.ts")
|
|
1872
|
-
}),
|
|
1873
|
-
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1874
|
-
name: [
|
|
1875
|
-
"Client",
|
|
1876
|
-
"RequestConfig",
|
|
1877
|
-
"ResponseErrorConfig"
|
|
1878
|
-
],
|
|
1879
|
-
root: file.path,
|
|
1880
|
-
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1881
|
-
isTypeOnly: true
|
|
1882
|
-
})
|
|
1883
|
-
] }),
|
|
1884
|
-
hasFormData && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1885
|
-
name: ["buildFormData"],
|
|
1886
|
-
root: file.path,
|
|
1887
|
-
path: node_path.default.resolve(root, ".kubb/config.ts")
|
|
970
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
971
|
+
name: clientValueImports,
|
|
972
|
+
root: clientRoot,
|
|
973
|
+
path: clientPath
|
|
1888
974
|
}),
|
|
1889
|
-
|
|
1890
|
-
name:
|
|
1891
|
-
|
|
975
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
976
|
+
name: clientTypeImports,
|
|
977
|
+
root: clientRoot,
|
|
978
|
+
path: clientPath,
|
|
1892
979
|
isTypeOnly: true
|
|
1893
980
|
}),
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1900
|
-
name: importNames,
|
|
1901
|
-
root: file.path,
|
|
1902
|
-
path: typeFile.path,
|
|
1903
|
-
isTypeOnly: true
|
|
1904
|
-
}, filePath);
|
|
981
|
+
meta.fileTypes && importedTypeNames.length > 0 && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
982
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
983
|
+
root: meta.file.path,
|
|
984
|
+
path: meta.fileTypes.path,
|
|
985
|
+
isTypeOnly: true
|
|
1905
986
|
}),
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
if (importNames.length === 0) return null;
|
|
1911
|
-
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1912
|
-
name: importNames,
|
|
1913
|
-
root: file.path,
|
|
1914
|
-
path: zodFile.path
|
|
1915
|
-
}, filePath);
|
|
987
|
+
meta.fileZod && importedZodNames.length > 0 && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
988
|
+
name: importedZodNames,
|
|
989
|
+
root: meta.file.path,
|
|
990
|
+
path: meta.fileZod.path
|
|
1916
991
|
}),
|
|
1917
|
-
/* @__PURE__ */ (0,
|
|
1918
|
-
name,
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
992
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Operation, {
|
|
993
|
+
name: meta.name,
|
|
994
|
+
node,
|
|
995
|
+
types,
|
|
996
|
+
zodResolver,
|
|
997
|
+
validator,
|
|
998
|
+
returnType,
|
|
999
|
+
throwOnErrorDefault,
|
|
1000
|
+
security
|
|
1926
1001
|
})
|
|
1927
1002
|
]
|
|
1928
|
-
}
|
|
1929
|
-
}
|
|
1930
|
-
}
|
|
1931
|
-
}
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
//#endregion
|
|
1008
|
+
//#region ../../internals/client/src/macros.ts
|
|
1009
|
+
/**
|
|
1010
|
+
* Macros the client plugins apply by default, ahead of any user macros. `macroSimplifyUnion`
|
|
1011
|
+
* drops union members a broader scalar already covers, keeping the generated response and error
|
|
1012
|
+
* unions tidy. A plugin wires them with `ctx.setMacros([...defaultMacros, ...userMacros])`.
|
|
1013
|
+
*/
|
|
1014
|
+
const defaultMacros = [kubb_kit.macroSimplifyUnion];
|
|
1932
1015
|
//#endregion
|
|
1933
|
-
//#region src/
|
|
1016
|
+
//#region ../../internals/client/src/resolver.ts
|
|
1934
1017
|
/**
|
|
1935
|
-
* Default resolver
|
|
1936
|
-
*
|
|
1937
|
-
* camelCase; classes and tag groups use PascalCase.
|
|
1018
|
+
* Default resolver shared by the client plugins. Functions and files inherit the built-in camelCase
|
|
1019
|
+
* `name` and `file`; classes and tag groups use PascalCase.
|
|
1938
1020
|
*
|
|
1939
|
-
* @example
|
|
1021
|
+
* @example
|
|
1940
1022
|
* ```ts
|
|
1941
|
-
*
|
|
1942
|
-
*
|
|
1943
|
-
* resolverClient.default('list pets', 'function') // 'listPets'
|
|
1944
|
-
* resolverClient.resolveClassName('pet') // 'Pet'
|
|
1945
|
-
* resolverClient.resolveGroupName('pet') // 'PetClient'
|
|
1946
|
-
* resolverClient.resolveUrlName(operationNode) // 'getShowPetByIdUrl'
|
|
1023
|
+
* resolverClient.name('show pet by id') // 'showPetById'
|
|
1024
|
+
* resolverClient.groupName('pet') // 'PetClient'
|
|
1947
1025
|
* ```
|
|
1948
1026
|
*/
|
|
1949
|
-
const resolverClient = (0,
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
default(name, type) {
|
|
1953
|
-
if (type === "file") return toFilePath(name);
|
|
1954
|
-
return ensureValidVarName(camelCase(name));
|
|
1955
|
-
},
|
|
1956
|
-
resolveName(name) {
|
|
1957
|
-
return this.default(name, "function");
|
|
1958
|
-
},
|
|
1959
|
-
resolvePathName(name, type) {
|
|
1960
|
-
return this.default(name, type);
|
|
1961
|
-
},
|
|
1962
|
-
resolveClassName(name) {
|
|
1027
|
+
const resolverClient = (0, kubb_kit.createResolver)({
|
|
1028
|
+
pluginName: "plugin-contract-client",
|
|
1029
|
+
className(name) {
|
|
1963
1030
|
return ensureValidVarName(pascalCase(name));
|
|
1964
1031
|
},
|
|
1965
|
-
|
|
1032
|
+
groupName(name) {
|
|
1966
1033
|
return ensureValidVarName(pascalCase(`${name} Client`));
|
|
1967
1034
|
},
|
|
1968
|
-
|
|
1035
|
+
propertyName(name) {
|
|
1969
1036
|
return ensureValidVarName(camelCase(name));
|
|
1970
|
-
},
|
|
1971
|
-
resolveUrlName(node) {
|
|
1972
|
-
const name = this.resolveName(node.operationId);
|
|
1973
|
-
return `get${name.charAt(0).toUpperCase()}${name.slice(1)}Url`;
|
|
1974
1037
|
}
|
|
1975
|
-
})
|
|
1038
|
+
});
|
|
1039
|
+
//#endregion
|
|
1040
|
+
//#region src/generators/clientGenerator.tsx
|
|
1041
|
+
/**
|
|
1042
|
+
* Built-in operation generator for `@kubb/plugin-client`. Emits one async function per OpenAPI
|
|
1043
|
+
* operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
|
|
1044
|
+
* that forwards a single `options` object to the user-provided client module and returns the `RequestResult`.
|
|
1045
|
+
*/
|
|
1046
|
+
const clientGenerator = createClientGenerator("client", (options) => options.importPath);
|
|
1976
1047
|
//#endregion
|
|
1977
1048
|
//#region src/plugin.ts
|
|
1978
1049
|
/**
|
|
1979
|
-
* Canonical plugin name for `@kubb/plugin-client`.
|
|
1980
|
-
* cross-plugin dependency references.
|
|
1050
|
+
* Canonical plugin name for `@kubb/plugin-client`.
|
|
1981
1051
|
*/
|
|
1982
1052
|
const pluginClientName = "plugin-client";
|
|
1983
1053
|
/**
|
|
1984
|
-
* Generates
|
|
1985
|
-
*
|
|
1986
|
-
*
|
|
1987
|
-
* your own by setting `importPath`.
|
|
1054
|
+
* Generates typed operations that call a client module owned by your application.
|
|
1055
|
+
* The module at `importPath` must export the runtime helpers and types used by generated operations.
|
|
1056
|
+
* Kubb does not copy or inject a transport implementation.
|
|
1988
1057
|
*
|
|
1989
1058
|
* @example
|
|
1990
1059
|
* ```ts
|
|
1991
|
-
*
|
|
1992
|
-
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1993
|
-
* import { pluginClient } from '@kubb/plugin-client'
|
|
1994
|
-
*
|
|
1995
|
-
* export default defineConfig({
|
|
1996
|
-
* input: { path: './petStore.yaml' },
|
|
1997
|
-
* output: { path: './src/gen' },
|
|
1998
|
-
* plugins: [
|
|
1999
|
-
* pluginTs(),
|
|
2000
|
-
* pluginClient({
|
|
2001
|
-
* output: { path: './clients' },
|
|
2002
|
-
* client: 'fetch',
|
|
2003
|
-
* }),
|
|
2004
|
-
* ],
|
|
2005
|
-
* })
|
|
1060
|
+
* pluginClient({ importPath: '../api/client' })
|
|
2006
1061
|
* ```
|
|
2007
1062
|
*/
|
|
2008
|
-
const pluginClient = (0,
|
|
1063
|
+
const pluginClient = (0, kubb_kit.definePlugin)((options) => {
|
|
2009
1064
|
const { output = {
|
|
2010
1065
|
path: "clients",
|
|
2011
1066
|
barrel: { type: "named" }
|
|
2012
|
-
},
|
|
2013
|
-
const
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
1067
|
+
}, exclude = [], include, override = [], importPath, throwOnErrorDefault = true, validator = false, group, resolver: userResolver } = options;
|
|
1068
|
+
const resolved = {
|
|
1069
|
+
output,
|
|
1070
|
+
exclude,
|
|
1071
|
+
include,
|
|
1072
|
+
override,
|
|
1073
|
+
group: createGroupConfig(group),
|
|
1074
|
+
importPath,
|
|
1075
|
+
baseURL: void 0,
|
|
1076
|
+
throwOnErrorDefault,
|
|
1077
|
+
validator,
|
|
1078
|
+
returnType: "plain",
|
|
1079
|
+
sdk: void 0,
|
|
1080
|
+
resolver: userResolver ? kubb_kit.Resolver.merge(resolverClient, userResolver) : resolverClient
|
|
1081
|
+
};
|
|
2020
1082
|
return {
|
|
2021
1083
|
name: pluginClientName,
|
|
2022
1084
|
options,
|
|
2023
|
-
dependencies: [_kubb_plugin_ts.pluginTsName,
|
|
1085
|
+
dependencies: [_kubb_plugin_ts.pluginTsName, _kubb_plugin_zod.pluginZodName],
|
|
2024
1086
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
ctx.
|
|
2030
|
-
client,
|
|
2031
|
-
clientType,
|
|
2032
|
-
bundle,
|
|
2033
|
-
output,
|
|
2034
|
-
exclude,
|
|
2035
|
-
include,
|
|
2036
|
-
override,
|
|
2037
|
-
group: groupConfig,
|
|
2038
|
-
parser,
|
|
2039
|
-
dataReturnType,
|
|
2040
|
-
importPath: resolvedImportPath,
|
|
2041
|
-
baseURL,
|
|
2042
|
-
paramsType,
|
|
2043
|
-
paramsCasing,
|
|
2044
|
-
pathParamsType,
|
|
2045
|
-
urlType,
|
|
2046
|
-
sdk,
|
|
2047
|
-
resolver
|
|
2048
|
-
});
|
|
2049
|
-
ctx.setResolver(resolver);
|
|
2050
|
-
if (userMacros?.length) ctx.setMacros(userMacros);
|
|
2051
|
-
for (const gen of selectedGenerators) ctx.addGenerator(gen);
|
|
2052
|
-
const root = node_path$1.default.resolve(ctx.config.root, ctx.config.output.path);
|
|
2053
|
-
if (!resolvedImportPath?.startsWith(".")) {
|
|
2054
|
-
const isInlineSource = bundle && !resolvedImportPath;
|
|
2055
|
-
ctx.injectFile({
|
|
2056
|
-
baseName: "client.ts",
|
|
2057
|
-
path: node_path$1.default.resolve(root, ".kubb/client.ts"),
|
|
2058
|
-
sources: [_kubb_core.ast.factory.createSource({
|
|
2059
|
-
name: "client",
|
|
2060
|
-
nodes: isInlineSource ? [_kubb_core.ast.factory.createText(client === "fetch" ? require_templates_clients_fetch_source.source : require_templates_clients_axios_source.source)] : [],
|
|
2061
|
-
isExportable: true,
|
|
2062
|
-
isIndexable: true
|
|
2063
|
-
})],
|
|
2064
|
-
exports: !isInlineSource && resolvedImportPath ? [_kubb_core.ast.factory.createExport({ path: resolvedImportPath })] : []
|
|
2065
|
-
});
|
|
2066
|
-
}
|
|
2067
|
-
ctx.injectFile({
|
|
2068
|
-
baseName: "config.ts",
|
|
2069
|
-
path: node_path$1.default.resolve(root, ".kubb/config.ts"),
|
|
2070
|
-
sources: [_kubb_core.ast.factory.createSource({
|
|
2071
|
-
name: "config",
|
|
2072
|
-
nodes: [_kubb_core.ast.factory.createText(require_templates_config_source.source)],
|
|
2073
|
-
isExportable: false,
|
|
2074
|
-
isIndexable: false
|
|
2075
|
-
})]
|
|
2076
|
-
});
|
|
1087
|
+
if (validator && !ctx.config.plugins?.some((plugin) => plugin.name === _kubb_plugin_zod.pluginZodName)) throw new Error(`${pluginClientName}: \`validator\` needs ${_kubb_plugin_zod.pluginZodName}. Add pluginZod() to the plugins or set \`validator: false\`.`);
|
|
1088
|
+
ctx.setOptions(resolved);
|
|
1089
|
+
ctx.setResolver(resolved.resolver);
|
|
1090
|
+
ctx.setMacros([...defaultMacros, ...options.macros ?? []]);
|
|
1091
|
+
ctx.addGenerator(clientGenerator);
|
|
2077
1092
|
} }
|
|
2078
1093
|
};
|
|
2079
1094
|
});
|
|
2080
1095
|
//#endregion
|
|
2081
|
-
exports.Client = Client;
|
|
2082
|
-
exports.classClientGenerator = classClientGenerator;
|
|
2083
|
-
exports.clientGenerator = clientGenerator;
|
|
2084
1096
|
exports.default = pluginClient;
|
|
2085
|
-
exports.groupedClientGenerator = groupedClientGenerator;
|
|
2086
|
-
exports.isParserEnabled = isParserEnabled;
|
|
2087
|
-
exports.operationsGenerator = operationsGenerator;
|
|
2088
1097
|
exports.pluginClient = pluginClient;
|
|
2089
1098
|
exports.pluginClientName = pluginClientName;
|
|
2090
|
-
exports.resolveQueryParamsParser = resolveQueryParamsParser;
|
|
2091
|
-
exports.resolveRequestParser = resolveRequestParser;
|
|
2092
|
-
exports.resolveResponseParser = resolveResponseParser;
|
|
2093
|
-
exports.resolverClient = resolverClient;
|
|
2094
|
-
exports.staticClassClientGenerator = staticClassClientGenerator;
|
|
2095
1099
|
|
|
2096
1100
|
//# sourceMappingURL=index.cjs.map
|