@kubb/plugin-swr 5.9.0-canary.20261109T201601 → 5.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +561 -533
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -9
- package/dist/index.d.ts +9 -9
- package/dist/index.js +560 -532
- package/dist/index.js.map +1 -1
- package/docs/guides/calling-operations.md +8 -6
- package/docs/options.md +4 -2
- package/docs/overview.md +4 -2
- package/kubb-extension.json +9 -6
- package/package.json +9 -8
package/dist/index.js
CHANGED
|
@@ -1,9 +1,277 @@
|
|
|
1
|
-
import { Diagnostics, Resolver, Url, addGenerator, ast,
|
|
2
|
-
import path from "node:path";
|
|
1
|
+
import { Diagnostics, Resolver, Url, addGenerator, ast, casing, createResolver, defineExtension, defineGenerator, setMacros, setResolver, useConfig } from "@kubb/kit";
|
|
3
2
|
import { createFunctionParameter, createFunctionParameters, createObjectBindingPattern, createTypeLiteral, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
4
|
-
import
|
|
3
|
+
import path from "node:path";
|
|
5
4
|
import { File, Function, Type, jsxRenderer } from "@kubb/renderer-jsx";
|
|
6
5
|
import { Fragment, jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
|
|
6
|
+
// kubb-extension.json
|
|
7
|
+
var compatibility = {
|
|
8
|
+
"kubb": ">=5.18.4",
|
|
9
|
+
"node": ">=22.22"
|
|
10
|
+
};
|
|
11
|
+
// package.json
|
|
12
|
+
var version = "5.9.1";
|
|
13
|
+
// ../../internals/client/src/resolveClient.ts
|
|
14
|
+
/**
|
|
15
|
+
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
16
|
+
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
17
|
+
*
|
|
18
|
+
* Every client runtime lives in a dedicated client plugin, so a consumer always calls a registered
|
|
19
|
+
* contract client plugin (plugin-fetch, plugin-axios, or plugin-client) and never emits its own
|
|
20
|
+
* inline client:
|
|
21
|
+
*
|
|
22
|
+
* - `contract` — a registered contract client plugin owns the `<op>` functions and the consumer
|
|
23
|
+
* imports and calls them.
|
|
24
|
+
* - `error` — no client plugin is registered, several are registered without a selector, the
|
|
25
|
+
* requested one is missing, or `client` is not a known selector.
|
|
26
|
+
*
|
|
27
|
+
* The `client` string selects explicitly (`'fetch'` / `'axios'` / `'client'`); when it is unset a
|
|
28
|
+
* lone registered contract client plugin is picked up automatically.
|
|
29
|
+
*/
|
|
30
|
+
const pluginFetchName = "plugin-fetch";
|
|
31
|
+
const pluginAxiosName = "plugin-axios";
|
|
32
|
+
const pluginClientName = "plugin-client";
|
|
33
|
+
const selectorToPlugin = {
|
|
34
|
+
fetch: pluginFetchName,
|
|
35
|
+
axios: pluginAxiosName,
|
|
36
|
+
client: pluginClientName
|
|
37
|
+
};
|
|
38
|
+
const contractPlugins = [
|
|
39
|
+
pluginFetchName,
|
|
40
|
+
pluginAxiosName,
|
|
41
|
+
pluginClientName
|
|
42
|
+
];
|
|
43
|
+
/**
|
|
44
|
+
* Applies the `client` resolution rules. See the module comment for the strategy shape.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* resolveClient({ client: 'fetch', pluginNames: ['plugin-ts', 'plugin-fetch'] })
|
|
49
|
+
* // { kind: 'contract', pluginName: 'plugin-fetch' }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* resolveClient({ client: undefined, pluginNames: ['plugin-ts'] })
|
|
55
|
+
* // { kind: 'error', code: 'KUBB_MISSING_DEPENDENCY', message: 'No client plugin is registered.', help: 'Add …' }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function resolveClient(options) {
|
|
59
|
+
const { client, pluginNames } = options;
|
|
60
|
+
const has = (name) => pluginNames.includes(name);
|
|
61
|
+
if (client === "fetch" || client === "axios" || client === "client") {
|
|
62
|
+
const pluginName = selectorToPlugin[client];
|
|
63
|
+
if (!has(pluginName)) return {
|
|
64
|
+
kind: "error",
|
|
65
|
+
code: Diagnostics.code.missingDependency,
|
|
66
|
+
message: `\`client: '${client}'\` is set but \`@kubb/plugin-${client}\` is not registered in \`plugins\`.`,
|
|
67
|
+
help: `Add \`@kubb/plugin-${client}\` to \`plugins\`, or drop \`client\` to use a different client plugin.`
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
kind: "contract",
|
|
71
|
+
pluginName
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (client !== void 0) return {
|
|
75
|
+
kind: "error",
|
|
76
|
+
code: Diagnostics.code.invalidPluginOptions,
|
|
77
|
+
message: `Option \`client\` is ${typeof client === "string" ? `'${client}'` : String(client)}, but it must be one of 'fetch', 'axios', 'client'.`,
|
|
78
|
+
help: "Set `client` to one of those values, or drop it to use the registered client plugin."
|
|
79
|
+
};
|
|
80
|
+
const registered = contractPlugins.filter(has);
|
|
81
|
+
if (registered.length === 1) return {
|
|
82
|
+
kind: "contract",
|
|
83
|
+
pluginName: registered[0]
|
|
84
|
+
};
|
|
85
|
+
if (registered.length > 1) return {
|
|
86
|
+
kind: "error",
|
|
87
|
+
code: Diagnostics.code.invalidPluginOptions,
|
|
88
|
+
message: `Multiple client plugins are registered (${registered.map((name) => `\`@kubb/${name}\``).join(", ")}) and \`client\` is not set.`,
|
|
89
|
+
help: "Set `client: 'fetch' | 'axios' | 'client'` to choose which client the hooks call, or register a single client plugin."
|
|
90
|
+
};
|
|
91
|
+
return {
|
|
92
|
+
kind: "error",
|
|
93
|
+
code: Diagnostics.code.missingDependency,
|
|
94
|
+
message: "No client plugin is registered.",
|
|
95
|
+
help: "Add `@kubb/plugin-fetch`, `@kubb/plugin-axios`, or `@kubb/plugin-client` to `plugins` so the generated code has an HTTP client to call."
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Resolves the contract client during a consumer plugin's setup hook.
|
|
100
|
+
* It extracts the plugin names from the raw `plugins` config and applies {@link resolveClient}.
|
|
101
|
+
* On a misconfiguration it throws a config diagnostic tagged with `plugin`, so every consumer
|
|
102
|
+
* fails fast with the same message.
|
|
103
|
+
*/
|
|
104
|
+
function resolveContractClient(options) {
|
|
105
|
+
const { plugin, client, plugins = [] } = options;
|
|
106
|
+
const resolved = resolveClient({
|
|
107
|
+
client,
|
|
108
|
+
pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
|
|
109
|
+
});
|
|
110
|
+
if (resolved.kind === "error") {
|
|
111
|
+
const { code, message, help } = resolved;
|
|
112
|
+
throw new Diagnostics.Error({
|
|
113
|
+
code,
|
|
114
|
+
severity: "error",
|
|
115
|
+
message,
|
|
116
|
+
help,
|
|
117
|
+
location: { kind: "config" },
|
|
118
|
+
plugin
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return resolved;
|
|
122
|
+
}
|
|
123
|
+
// ../../internals/utils/src/reserved.ts
|
|
124
|
+
/**
|
|
125
|
+
* JavaScript and Java reserved words.
|
|
126
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
127
|
+
*/
|
|
128
|
+
const reservedWords = /* @__PURE__ */ new Set([
|
|
129
|
+
"abstract",
|
|
130
|
+
"arguments",
|
|
131
|
+
"await",
|
|
132
|
+
"boolean",
|
|
133
|
+
"break",
|
|
134
|
+
"byte",
|
|
135
|
+
"case",
|
|
136
|
+
"catch",
|
|
137
|
+
"char",
|
|
138
|
+
"class",
|
|
139
|
+
"const",
|
|
140
|
+
"continue",
|
|
141
|
+
"debugger",
|
|
142
|
+
"default",
|
|
143
|
+
"delete",
|
|
144
|
+
"do",
|
|
145
|
+
"double",
|
|
146
|
+
"else",
|
|
147
|
+
"enum",
|
|
148
|
+
"eval",
|
|
149
|
+
"export",
|
|
150
|
+
"extends",
|
|
151
|
+
"false",
|
|
152
|
+
"final",
|
|
153
|
+
"finally",
|
|
154
|
+
"float",
|
|
155
|
+
"for",
|
|
156
|
+
"function",
|
|
157
|
+
"goto",
|
|
158
|
+
"if",
|
|
159
|
+
"implements",
|
|
160
|
+
"import",
|
|
161
|
+
"in",
|
|
162
|
+
"instanceof",
|
|
163
|
+
"int",
|
|
164
|
+
"interface",
|
|
165
|
+
"let",
|
|
166
|
+
"long",
|
|
167
|
+
"native",
|
|
168
|
+
"new",
|
|
169
|
+
"null",
|
|
170
|
+
"package",
|
|
171
|
+
"private",
|
|
172
|
+
"protected",
|
|
173
|
+
"public",
|
|
174
|
+
"return",
|
|
175
|
+
"short",
|
|
176
|
+
"static",
|
|
177
|
+
"super",
|
|
178
|
+
"switch",
|
|
179
|
+
"synchronized",
|
|
180
|
+
"this",
|
|
181
|
+
"throw",
|
|
182
|
+
"throws",
|
|
183
|
+
"transient",
|
|
184
|
+
"true",
|
|
185
|
+
"try",
|
|
186
|
+
"typeof",
|
|
187
|
+
"var",
|
|
188
|
+
"void",
|
|
189
|
+
"volatile",
|
|
190
|
+
"while",
|
|
191
|
+
"with",
|
|
192
|
+
"yield",
|
|
193
|
+
"Array",
|
|
194
|
+
"Date",
|
|
195
|
+
"hasOwnProperty",
|
|
196
|
+
"Infinity",
|
|
197
|
+
"isFinite",
|
|
198
|
+
"isNaN",
|
|
199
|
+
"isPrototypeOf",
|
|
200
|
+
"length",
|
|
201
|
+
"Math",
|
|
202
|
+
"name",
|
|
203
|
+
"NaN",
|
|
204
|
+
"Number",
|
|
205
|
+
"Object",
|
|
206
|
+
"prototype",
|
|
207
|
+
"String",
|
|
208
|
+
"toString",
|
|
209
|
+
"undefined",
|
|
210
|
+
"valueOf"
|
|
211
|
+
]);
|
|
212
|
+
/**
|
|
213
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```ts
|
|
217
|
+
* isValidVarName('status') // true
|
|
218
|
+
* isValidVarName('class') // false (reserved word)
|
|
219
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
220
|
+
* ```
|
|
221
|
+
*/
|
|
222
|
+
function isValidVarName(name) {
|
|
223
|
+
if (!name || reservedWords.has(name)) return false;
|
|
224
|
+
return isIdentifier(name);
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Returns `name` when it's a syntactically valid JavaScript variable name,
|
|
228
|
+
* otherwise prefixes it with `_` so the result is a valid identifier.
|
|
229
|
+
*
|
|
230
|
+
* Useful for sanitizing OpenAPI schema names or operation IDs that start with
|
|
231
|
+
* a digit (e.g. `409`, `504AccountCancel`) before using them as exported
|
|
232
|
+
* variable, type, or function names.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```ts
|
|
236
|
+
* ensureValidVarName('409') // '_409'
|
|
237
|
+
* ensureValidVarName('504AccountCancel') // '_504AccountCancel'
|
|
238
|
+
* ensureValidVarName('Pet') // 'Pet'
|
|
239
|
+
* ensureValidVarName('class') // '_class'
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
function ensureValidVarName(name) {
|
|
243
|
+
if (!name || isValidVarName(name)) return name;
|
|
244
|
+
return `_${name}`;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
248
|
+
*
|
|
249
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
250
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
251
|
+
* deciding whether an object key needs quoting.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* isIdentifier('name') // true
|
|
256
|
+
* isIdentifier('x-total')// false
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
function isIdentifier(name) {
|
|
260
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
261
|
+
}
|
|
262
|
+
// ../../internals/utils/src/strings.ts
|
|
263
|
+
/**
|
|
264
|
+
* Escapes an `@` at the start of a line, so a multi-line description can't start a new JSDoc tag.
|
|
265
|
+
* Editors render `\@` as `@` in hovers.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* ```ts
|
|
269
|
+
* jsDocEscape('intro\n@example value') // 'intro\n\\@example value'
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
function jsDocEscape(input) {
|
|
273
|
+
return `${input}`.replace(/^(\s*)@/gm, "$1\\@");
|
|
274
|
+
}
|
|
7
275
|
// ../../internals/shared/src/operation.ts
|
|
8
276
|
/**
|
|
9
277
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
@@ -25,29 +293,55 @@ function operationFileEntry(node, name, extname = ".ts") {
|
|
|
25
293
|
};
|
|
26
294
|
}
|
|
27
295
|
/**
|
|
28
|
-
* Resolves a
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
296
|
+
* Resolves the file a schema-emitting plugin (`plugin-ts`, `plugin-zod`, `plugin-faker`) writes
|
|
297
|
+
* `node` to, cached in `ctx.memo` under the resolver's plugin name and the
|
|
298
|
+
* operation. The plugin itself and every plugin that imports from it call this, so they all get
|
|
299
|
+
* the same path.
|
|
32
300
|
*
|
|
33
|
-
*
|
|
301
|
+
* When that path matches the file of a component schema in `ctx.driver.inputNode`, ignoring case, the file name
|
|
302
|
+
* gets an `Operation` suffix: operation `point` goes to `PointOperation.ts` next to schema `Point.ts`.
|
|
303
|
+
* Otherwise the two merge into one file, or overwrite each other on a case-insensitive file system.
|
|
304
|
+
* Only the file name changes. The declarations keep their names.
|
|
305
|
+
*
|
|
306
|
+
* @example Resolve `plugin-ts`'s file for the current operation
|
|
34
307
|
* ```ts
|
|
35
|
-
* const fileTs =
|
|
308
|
+
* const fileTs = resolveOperationFile({ ctx, node, resolver: tsResolver, root, output })
|
|
36
309
|
* ```
|
|
37
310
|
*/
|
|
38
|
-
function
|
|
39
|
-
const {
|
|
40
|
-
|
|
41
|
-
|
|
311
|
+
function resolveOperationFile(options) {
|
|
312
|
+
const { ctx, node, resolver, root, output } = options;
|
|
313
|
+
const { memo } = ctx;
|
|
314
|
+
const location = {
|
|
42
315
|
root,
|
|
43
316
|
output,
|
|
44
|
-
group: group ?? void 0
|
|
45
|
-
}
|
|
317
|
+
group: options.group ?? void 0
|
|
318
|
+
};
|
|
319
|
+
return memo(`${resolver.pluginName}:operationFile:${node.operationId}`, () => {
|
|
320
|
+
const schemaPaths = memo(`${resolver.pluginName}:schemaFilePaths`, () => new Set((ctx.driver.inputNode?.schemas ?? []).flatMap((schema) => schema.name ? [resolver.file({
|
|
321
|
+
name: schema.name,
|
|
322
|
+
extname: ".ts",
|
|
323
|
+
...location
|
|
324
|
+
}).path.toLowerCase()] : [])));
|
|
325
|
+
let file = resolver.file({
|
|
326
|
+
...operationFileEntry(node, node.operationId),
|
|
327
|
+
...location
|
|
328
|
+
});
|
|
329
|
+
for (let name = `${node.operationId}Operation`; schemaPaths.has(file.path.toLowerCase()); name += "Operation") {
|
|
330
|
+
const next = resolver.file({
|
|
331
|
+
...operationFileEntry(node, name),
|
|
332
|
+
...location
|
|
333
|
+
});
|
|
334
|
+
if (next.path === file.path) break;
|
|
335
|
+
file = next;
|
|
336
|
+
}
|
|
337
|
+
return file;
|
|
338
|
+
});
|
|
46
339
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
340
|
+
/**
|
|
341
|
+
* The operation's method and path as plain text, such as `` `GET /pet/{petId}` ``. A path is not a valid `{@link}` target.
|
|
342
|
+
*/
|
|
343
|
+
function getOperationRequestLine(node) {
|
|
344
|
+
return ast.http.isOperation(node) && node.path ? `\`${node.method.toUpperCase()} ${node.path}\`` : null;
|
|
51
345
|
}
|
|
52
346
|
/**
|
|
53
347
|
* Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
|
|
@@ -71,18 +365,7 @@ function getContentTypeInfo(node) {
|
|
|
71
365
|
* whether several are present, so the client can let a caller pick which one to accept.
|
|
72
366
|
*/
|
|
73
367
|
function getResponseContentTypeInfo(node) {
|
|
74
|
-
return buildContentTypeInfo(
|
|
75
|
-
}
|
|
76
|
-
function baseContentType(contentType) {
|
|
77
|
-
return contentType.split(";")[0].trim().toLowerCase();
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Whether a content entry is a sequence of JSON values the adapter marks as a stream, such as `application/jsonl`,
|
|
81
|
-
* `application/x-ndjson`, `application/json-seq`, or any media type with an OpenAPI 3.2 `itemSchema`.
|
|
82
|
-
*/
|
|
83
|
-
function isJsonLines(entry) {
|
|
84
|
-
const baseType = baseContentType(entry.contentType);
|
|
85
|
-
return Boolean(entry.stream) && baseType !== "text/event-stream" && !baseType.startsWith("multipart/");
|
|
368
|
+
return buildContentTypeInfo(ast.http.responses.findPrimary(node)?.content?.map((e) => e.contentType) ?? []);
|
|
86
369
|
}
|
|
87
370
|
/**
|
|
88
371
|
* How an operation streams its primary success response item by item, when that response declares one content type:
|
|
@@ -94,11 +377,19 @@ function isJsonLines(entry) {
|
|
|
94
377
|
* call with its query hooks.
|
|
95
378
|
*/
|
|
96
379
|
function getEventStream(node) {
|
|
97
|
-
const content =
|
|
380
|
+
const content = ast.http.responses.findPrimary(node)?.content ?? [];
|
|
98
381
|
if (content.length !== 1) return null;
|
|
99
382
|
const entry = content[0];
|
|
100
|
-
|
|
101
|
-
|
|
383
|
+
const framing = getStreamFraming(entry);
|
|
384
|
+
if (framing === "events") return entry.stream?.shape ?? "data";
|
|
385
|
+
return framing === "lines" ? "line" : null;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* How a content entry is framed as a stream: `'events'` for Server-Sent Events, `'lines'` for JSON Lines.
|
|
389
|
+
*/
|
|
390
|
+
function getStreamFraming(entry) {
|
|
391
|
+
if (ast.mediaType.getBase(entry) === "text/event-stream") return "events";
|
|
392
|
+
return entry.stream && ast.mediaType.isStream(entry) ? "lines" : void 0;
|
|
102
393
|
}
|
|
103
394
|
/**
|
|
104
395
|
* Whether an operation streams its primary success response, as Server-Sent Events or JSON Lines. The client generator uses
|
|
@@ -108,20 +399,42 @@ function isEventStream(node) {
|
|
|
108
399
|
return getEventStream(node) !== null;
|
|
109
400
|
}
|
|
110
401
|
/**
|
|
402
|
+
* Whether a success response offers a stream media type next to other content types. The client returns that variant's
|
|
403
|
+
* body as a `ReadableStream`, so the stream runtime is registered and a hook's data type includes the stream.
|
|
404
|
+
*/
|
|
405
|
+
function hasStreamVariant(node) {
|
|
406
|
+
return ast.http.responses.filterSuccess(node).some((response) => {
|
|
407
|
+
const variants = (response.content ?? []).filter((entry) => entry.schema);
|
|
408
|
+
return variants.length > 1 && variants.some((entry) => ast.mediaType.isStream(entry));
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
111
412
|
* Builds the `client?:` option type shared by the generated query hooks (`useQuery`,
|
|
112
|
-
* `useInfiniteQuery`, `useSWR`, ...)
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
413
|
+
* `useInfiniteQuery`, `useSWR`, ...): `RequestConfig` minus what the
|
|
414
|
+
* hook fills in, which is every request group and `throwOnError`. Unlike {@link buildRequestConfigType},
|
|
415
|
+
* it never narrows `contentType`: query hooks wrap GET operations, which carry no request body to select
|
|
416
|
+
* a content type for.
|
|
116
417
|
*/
|
|
117
|
-
function buildClientOptionType(
|
|
118
|
-
return `Partial<Omit<RequestConfig,
|
|
418
|
+
function buildClientOptionType(options = {}) {
|
|
419
|
+
return `Partial<Omit<RequestConfig, ${[
|
|
420
|
+
...requestGroupOrder,
|
|
421
|
+
"url",
|
|
422
|
+
...options.omit ?? [],
|
|
423
|
+
"throwOnError"
|
|
424
|
+
].map((key) => `'${key}'`).join(" | ")}>>`;
|
|
119
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* The `client?:` option type of a mutation hook. When the operation has a request content type choice,
|
|
428
|
+
* the mutation variables carry `contentType`, so the config leaves it out. Otherwise the ambiguous side is
|
|
429
|
+
* narrowed to the operation's media types; a single-type side has nothing to pick, so it stays baked in the
|
|
430
|
+
* generated call.
|
|
431
|
+
*/
|
|
120
432
|
function buildRequestConfigType(node) {
|
|
433
|
+
if (hasRequestContentTypeChoice(node)) return buildClientOptionType({ omit: ["contentType"] });
|
|
121
434
|
const request = getContentTypeInfo(node);
|
|
122
435
|
const response = getResponseContentTypeInfo(node);
|
|
123
436
|
const members = [request.isMultipleContentTypes ? `request?: ${request.contentTypeUnion}` : null, response.isMultipleContentTypes ? `response?: ${response.contentTypeUnion}` : null].filter(Boolean);
|
|
124
|
-
const base = buildClientOptionType(
|
|
437
|
+
const base = buildClientOptionType();
|
|
125
438
|
return members.length ? `${base} & { contentType?: { ${members.join("; ")} } }` : base;
|
|
126
439
|
}
|
|
127
440
|
/** The grouped request options, in the order generated signatures and calls list them. */
|
|
@@ -136,11 +449,11 @@ const requestGroupOrder = [
|
|
|
136
449
|
* Which of the grouped request options an operation carries.
|
|
137
450
|
*/
|
|
138
451
|
function getRequestGroups(node) {
|
|
139
|
-
const { path, query, header, cookie } =
|
|
452
|
+
const { path, query, header, cookie } = ast.http.groupParameters(node);
|
|
140
453
|
return {
|
|
141
454
|
path: path.length > 0,
|
|
142
455
|
query: query.length > 0,
|
|
143
|
-
body: Boolean(
|
|
456
|
+
body: Boolean(ast.http.requestBody.getSchema(node)),
|
|
144
457
|
headers: header.length > 0,
|
|
145
458
|
cookies: cookie.length > 0
|
|
146
459
|
};
|
|
@@ -152,7 +465,7 @@ function getRequestGroups(node) {
|
|
|
152
465
|
*/
|
|
153
466
|
function getRequestGroupOptionality(node) {
|
|
154
467
|
const groups = getRequestGroups(node);
|
|
155
|
-
const { path, query, header } =
|
|
468
|
+
const { path, query, header } = ast.http.groupParameters(node);
|
|
156
469
|
const hasRequiredPath = path.some((param) => param.required);
|
|
157
470
|
const hasRequiredQuery = query.some((param) => param.required);
|
|
158
471
|
const hasRequiredHeader = header.some((param) => param.required);
|
|
@@ -165,104 +478,28 @@ function getRequestGroupOptionality(node) {
|
|
|
165
478
|
};
|
|
166
479
|
}
|
|
167
480
|
function buildOperationComments(node, options = {}) {
|
|
168
|
-
const {
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
node.description && `@description ${node.description}`,
|
|
172
|
-
node.summary && `@summary ${node.summary}`,
|
|
173
|
-
linkComment,
|
|
174
|
-
node.deprecated && "@deprecated"
|
|
175
|
-
] : [
|
|
481
|
+
const { splitLines = false } = options;
|
|
482
|
+
const filteredComments = [
|
|
483
|
+
getOperationRequestLine(node),
|
|
176
484
|
node.description && `@description ${node.description}`,
|
|
177
485
|
node.summary && `@summary ${node.summary}`,
|
|
178
486
|
node.deprecated && "@deprecated",
|
|
179
|
-
|
|
180
|
-
]
|
|
487
|
+
node.externalDocs && `@see ${jsDocEscape([node.externalDocs.url, node.externalDocs.description].filter(Boolean).join(" "))}`
|
|
488
|
+
].filter((comment) => Boolean(comment));
|
|
181
489
|
if (!splitLines) return filteredComments;
|
|
182
490
|
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
183
491
|
}
|
|
184
|
-
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
185
|
-
/**
|
|
186
|
-
* An OpenAPI 3.2 `querystring` parameter is the whole query string, so a form-encoded object becomes one query parameter
|
|
187
|
-
* per property, typed and serialized like any other.
|
|
188
|
-
* Another content type, such as JSON, stays one parameter named after it, and the runtime sends its value as the whole query string.
|
|
189
|
-
*/
|
|
190
|
-
function spreadQuerystring(param) {
|
|
191
|
-
const schema = param.schema.type === "ref" ? param.schema.schema : param.schema;
|
|
192
|
-
const formEncoded = !param.contentType || param.contentType.split(";")[0].trim().toLowerCase() === "application/x-www-form-urlencoded";
|
|
193
|
-
if (schema?.type !== "object" || !formEncoded) return [param];
|
|
194
|
-
return schema.properties.map((property) => ast.factory.createParameter({
|
|
195
|
-
name: property.name,
|
|
196
|
-
in: "query",
|
|
197
|
-
schema: property.schema,
|
|
198
|
-
required: param.required && property.required
|
|
199
|
-
}));
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
203
|
-
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
204
|
-
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
205
|
-
* same parameters once per plugin.
|
|
206
|
-
*/
|
|
207
|
-
function getOperationParameters(node) {
|
|
208
|
-
const cached = operationParameterGroupsByNode.get(node);
|
|
209
|
-
if (cached) return cached;
|
|
210
|
-
const dedupe = (params) => {
|
|
211
|
-
const seen = /* @__PURE__ */ new Set();
|
|
212
|
-
return params.filter((param) => {
|
|
213
|
-
if (seen.has(param.name)) return false;
|
|
214
|
-
seen.add(param.name);
|
|
215
|
-
return true;
|
|
216
|
-
});
|
|
217
|
-
};
|
|
218
|
-
const group = (location) => dedupe(node.parameters.filter((param) => param.in === location));
|
|
219
|
-
const groups = {
|
|
220
|
-
path: group("path"),
|
|
221
|
-
query: dedupe([...group("query"), ...group("querystring").flatMap(spreadQuerystring)]),
|
|
222
|
-
header: group("header"),
|
|
223
|
-
cookie: group("cookie")
|
|
224
|
-
};
|
|
225
|
-
operationParameterGroupsByNode.set(node, groups);
|
|
226
|
-
return groups;
|
|
227
|
-
}
|
|
228
492
|
/**
|
|
229
493
|
* Whether the operation declares more than one request media type with a schema, so a caller must pick one on `contentType`.
|
|
230
494
|
*/
|
|
231
495
|
function hasRequestContentTypeChoice(node) {
|
|
232
496
|
return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
|
|
233
497
|
}
|
|
234
|
-
/**
|
|
235
|
-
* Casts a call argument to the client's parameter type; a `'plain'` client is non-generic.
|
|
236
|
-
*/
|
|
237
|
-
function castToClientOptions(callArg, clientName, returnType = "full") {
|
|
238
|
-
return `${callArg} as Parameters<typeof ${clientName}${returnType === "plain" ? "" : "<true>"}>[0]`;
|
|
239
|
-
}
|
|
240
|
-
function getStatusCodeNumber(statusCode) {
|
|
241
|
-
const code = Number(statusCode);
|
|
242
|
-
return Number.isNaN(code) ? null : code;
|
|
243
|
-
}
|
|
244
|
-
function isSuccessStatusCode(statusCode) {
|
|
245
|
-
const code = getStatusCodeNumber(statusCode);
|
|
246
|
-
return code !== null && code >= 200 && code < 300;
|
|
247
|
-
}
|
|
248
|
-
function isErrorStatusCode(statusCode) {
|
|
249
|
-
const code = getStatusCodeNumber(statusCode);
|
|
250
|
-
return code !== null && code >= 400;
|
|
251
|
-
}
|
|
252
|
-
function getSuccessResponses(responses) {
|
|
253
|
-
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
254
|
-
}
|
|
255
|
-
function getOperationSuccessResponses(node) {
|
|
256
|
-
return getSuccessResponses(node.responses);
|
|
257
|
-
}
|
|
258
|
-
function getPrimarySuccessResponse(node) {
|
|
259
|
-
return getOperationSuccessResponses(node)[0] ?? null;
|
|
260
|
-
}
|
|
261
498
|
function resolveErrorNames(node, resolver) {
|
|
262
|
-
return
|
|
499
|
+
return ast.http.responses.filterError(node).map((response) => resolver.response.status(node, response.statusCode));
|
|
263
500
|
}
|
|
264
501
|
function resolveSuccessNames(node, resolver) {
|
|
265
|
-
return
|
|
502
|
+
return ast.http.responses.filterSuccess(node).map((response) => resolver.response.status(node, response.statusCode));
|
|
266
503
|
}
|
|
267
504
|
function resolveStatusCodeNames(node, resolver) {
|
|
268
505
|
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
@@ -274,7 +511,7 @@ function resolveStatusCodeNames(node, resolver) {
|
|
|
274
511
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
275
512
|
const key = `${node.operationId}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${options.includeParams === false ? "noparams" : ""}\0${(options.exclude ?? []).join(",")}`;
|
|
276
513
|
return resolver.memo(`operationTypeNames:${key}`, () => {
|
|
277
|
-
const { path, query, header, cookie } =
|
|
514
|
+
const { path, query, header, cookie } = ast.http.groupParameters(node);
|
|
278
515
|
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
279
516
|
const exclude = new Set(options.exclude ?? []);
|
|
280
517
|
const paramNames = options.includeParams === false ? [] : [
|
|
@@ -283,7 +520,7 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
283
520
|
...header.map((param) => resolver.param.headers(node, param)),
|
|
284
521
|
...cookie.map((param) => resolver.param.cookies(node, param))
|
|
285
522
|
];
|
|
286
|
-
const bodyAndResponseNames = [
|
|
523
|
+
const bodyAndResponseNames = [ast.http.requestBody.getSchema(node) ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
287
524
|
return (options.order === "body-response-first" ? [
|
|
288
525
|
...bodyAndResponseNames,
|
|
289
526
|
...paramNames,
|
|
@@ -295,219 +532,139 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
295
532
|
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
296
533
|
});
|
|
297
534
|
}
|
|
298
|
-
// ../../internals/
|
|
535
|
+
// ../../internals/shared/src/resolver.ts
|
|
299
536
|
/**
|
|
300
|
-
*
|
|
301
|
-
* Unlike `pascalCase` it never re-splits word boundaries or strips characters.
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* `capitalize('getPetById') // 'GetPetById'`
|
|
537
|
+
* The shared top-level `name` of the operation plugins, computed once per operation name and build.
|
|
305
538
|
*/
|
|
306
|
-
function
|
|
307
|
-
return
|
|
539
|
+
function resolveOperationName(name) {
|
|
540
|
+
return this.memo(`operation:${name}`, () => ensureValidVarName(casing.camel(name)));
|
|
308
541
|
}
|
|
309
|
-
// ../../internals/utils/src/reserved.ts
|
|
310
542
|
/**
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
"await",
|
|
318
|
-
"boolean",
|
|
319
|
-
"break",
|
|
320
|
-
"byte",
|
|
321
|
-
"case",
|
|
322
|
-
"catch",
|
|
323
|
-
"char",
|
|
324
|
-
"class",
|
|
325
|
-
"const",
|
|
326
|
-
"continue",
|
|
327
|
-
"debugger",
|
|
328
|
-
"default",
|
|
329
|
-
"delete",
|
|
330
|
-
"do",
|
|
331
|
-
"double",
|
|
332
|
-
"else",
|
|
333
|
-
"enum",
|
|
334
|
-
"eval",
|
|
335
|
-
"export",
|
|
336
|
-
"extends",
|
|
337
|
-
"false",
|
|
338
|
-
"final",
|
|
339
|
-
"finally",
|
|
340
|
-
"float",
|
|
341
|
-
"for",
|
|
342
|
-
"function",
|
|
343
|
-
"goto",
|
|
344
|
-
"if",
|
|
345
|
-
"implements",
|
|
346
|
-
"import",
|
|
347
|
-
"in",
|
|
348
|
-
"instanceof",
|
|
349
|
-
"int",
|
|
350
|
-
"interface",
|
|
351
|
-
"let",
|
|
352
|
-
"long",
|
|
353
|
-
"native",
|
|
354
|
-
"new",
|
|
355
|
-
"null",
|
|
356
|
-
"package",
|
|
357
|
-
"private",
|
|
358
|
-
"protected",
|
|
359
|
-
"public",
|
|
360
|
-
"return",
|
|
361
|
-
"short",
|
|
362
|
-
"static",
|
|
363
|
-
"super",
|
|
364
|
-
"switch",
|
|
365
|
-
"synchronized",
|
|
366
|
-
"this",
|
|
367
|
-
"throw",
|
|
368
|
-
"throws",
|
|
369
|
-
"transient",
|
|
370
|
-
"true",
|
|
371
|
-
"try",
|
|
372
|
-
"typeof",
|
|
373
|
-
"var",
|
|
374
|
-
"void",
|
|
375
|
-
"volatile",
|
|
376
|
-
"while",
|
|
377
|
-
"with",
|
|
378
|
-
"yield",
|
|
379
|
-
"Array",
|
|
380
|
-
"Date",
|
|
381
|
-
"hasOwnProperty",
|
|
382
|
-
"Infinity",
|
|
383
|
-
"isFinite",
|
|
384
|
-
"isNaN",
|
|
385
|
-
"isPrototypeOf",
|
|
386
|
-
"length",
|
|
387
|
-
"Math",
|
|
388
|
-
"name",
|
|
389
|
-
"NaN",
|
|
390
|
-
"Number",
|
|
391
|
-
"Object",
|
|
392
|
-
"prototype",
|
|
393
|
-
"String",
|
|
394
|
-
"toString",
|
|
395
|
-
"undefined",
|
|
396
|
-
"valueOf"
|
|
397
|
-
]);
|
|
398
|
-
/**
|
|
399
|
-
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
543
|
+
* Builds the allocation key for a declaration an operation owns. `scope` names which declaration
|
|
544
|
+
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
545
|
+
* name. Leave `scope` empty for the operation's own declaration.
|
|
546
|
+
*
|
|
547
|
+
* The key starts from the operation's input `pointer`, which macros keep, so a plugin whose macro
|
|
548
|
+
* rewrites the method or path still resolves another plugin's names for the same operation.
|
|
400
549
|
*
|
|
401
550
|
* @example
|
|
402
551
|
* ```ts
|
|
403
|
-
*
|
|
404
|
-
* isValidVarName('class') // false (reserved word)
|
|
405
|
-
* isValidVarName('42foo') // false (starts with digit)
|
|
552
|
+
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
406
553
|
* ```
|
|
407
554
|
*/
|
|
408
|
-
function
|
|
409
|
-
|
|
410
|
-
|
|
555
|
+
function operationKey(node, ...scope) {
|
|
556
|
+
return [
|
|
557
|
+
"operation",
|
|
558
|
+
...node.pointer ? [node.pointer] : [node.method, node.path],
|
|
559
|
+
...scope.map(String)
|
|
560
|
+
];
|
|
561
|
+
}
|
|
562
|
+
// ../../internals/client/src/resolveClientOperation.ts
|
|
563
|
+
function isRelativeImport(importPath) {
|
|
564
|
+
return importPath.startsWith(".");
|
|
565
|
+
}
|
|
566
|
+
function resolveImportPath(options) {
|
|
567
|
+
const { importPath, operationFile } = options;
|
|
568
|
+
return isRelativeImport(importPath) ? path.resolve(path.dirname(operationFile), importPath) : importPath;
|
|
411
569
|
}
|
|
412
570
|
/**
|
|
413
|
-
*
|
|
414
|
-
*
|
|
571
|
+
* Resolves the contract client `<op>` a consumer (query hook, MCP handler) imports, by looking up
|
|
572
|
+
* the registered contract client plugin's resolver and output. Works for any contract client plugin
|
|
573
|
+
* (plugin-fetch, plugin-axios, or plugin-client). Returns `null` when no contract plugin is in play (the inline
|
|
574
|
+
* path), or when the client plugin's `exclude` / `include` leaves this operation out, since it then
|
|
575
|
+
* emits no `<op>` to import. plugin-fetch / plugin-axios inject `.kubb/client.ts` at the global
|
|
576
|
+
* output root, the same path consumers read `RequestConfig` / `ResponseErrorConfig` from.
|
|
577
|
+
* With plugin-client they come from the user's `importPath` module instead.
|
|
415
578
|
*
|
|
416
|
-
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
579
|
+
* The result is cached in `memo` (the build's `ctx.memo`) under the client plugin's name and the operation,
|
|
580
|
+
* so several dependents reading the same client plugin for one operation in a single pass
|
|
581
|
+
* (react-query's query/mutation/infinite generators, vue-query, swr, the MCP handler, ...) share
|
|
582
|
+
* one computed result instead of each re-deriving the name and path.
|
|
419
583
|
*
|
|
420
|
-
*
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
* ensureValidVarName('504AccountCancel') // '_504AccountCancel'
|
|
424
|
-
* ensureValidVarName('Pet') // 'Pet'
|
|
425
|
-
* ensureValidVarName('class') // '_class'
|
|
426
|
-
* ```
|
|
584
|
+
* `returnType`, `output`, and `group` are the client plugin's options for this operation, after its
|
|
585
|
+
* `override` entries, the same resolution its own generator sees. A dependent's generated call body
|
|
586
|
+
* and import path therefore match the `<op>` actually emitted, even when an `override` changes it.
|
|
427
587
|
*/
|
|
428
|
-
function
|
|
429
|
-
|
|
430
|
-
return
|
|
588
|
+
function resolveClientOperation(options) {
|
|
589
|
+
const { clientPlugin, driver, node, root, output, memo } = options;
|
|
590
|
+
if (!clientPlugin) return null;
|
|
591
|
+
return memo(`${clientPlugin.pluginName}:clientOperation:${node.operationId}`, () => {
|
|
592
|
+
const resolver = driver.getResolver(clientPlugin.pluginName);
|
|
593
|
+
const pluginOptions = driver.getPlugin(clientPlugin.pluginName)?.options ?? {};
|
|
594
|
+
const { exclude, include, override } = pluginOptions;
|
|
595
|
+
const operationOptions = Boolean(exclude?.length || include?.length || override?.length) && resolver.default ? resolver.default.options(node, {
|
|
596
|
+
options: pluginOptions,
|
|
597
|
+
exclude,
|
|
598
|
+
include,
|
|
599
|
+
override
|
|
600
|
+
}) : pluginOptions;
|
|
601
|
+
if (!operationOptions) return null;
|
|
602
|
+
const file = resolver.file({
|
|
603
|
+
...operationFileEntry(node, node.operationId),
|
|
604
|
+
root,
|
|
605
|
+
output: operationOptions.output ?? output,
|
|
606
|
+
group: operationOptions.group ?? void 0
|
|
607
|
+
});
|
|
608
|
+
const { importPath } = operationOptions;
|
|
609
|
+
const name = resolver.name(node.operationId);
|
|
610
|
+
return {
|
|
611
|
+
name: resolver.resolveName({
|
|
612
|
+
key: operationKey(node),
|
|
613
|
+
name
|
|
614
|
+
}),
|
|
615
|
+
path: file.path,
|
|
616
|
+
clientPath: importPath === void 0 ? path.resolve(root, ".kubb/client.ts") : resolveImportPath({
|
|
617
|
+
importPath,
|
|
618
|
+
operationFile: file.path
|
|
619
|
+
}),
|
|
620
|
+
clientPathIsFile: importPath === void 0 || isRelativeImport(importPath),
|
|
621
|
+
returnType: operationOptions.returnType ?? "full",
|
|
622
|
+
typedHeaders: importPath === void 0
|
|
623
|
+
};
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
// ../../internals/client/src/resolveOperationTypes.ts
|
|
627
|
+
const pluginZodName = "plugin-zod";
|
|
628
|
+
function getResponseHeadersEntries({ node, types }) {
|
|
629
|
+
const resolve = types.response.headers;
|
|
630
|
+
if (!resolve) return [];
|
|
631
|
+
return node.responses.filter((response) => response.headers?.length).map((response) => [response.statusCode, resolve.call(types.response, node, response.statusCode)]);
|
|
431
632
|
}
|
|
432
633
|
/**
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
436
|
-
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
437
|
-
* deciding whether an object key needs quoting.
|
|
438
|
-
*
|
|
439
|
-
* @example
|
|
440
|
-
* ```ts
|
|
441
|
-
* isIdentifier('name') // true
|
|
442
|
-
* isIdentifier('x-total')// false
|
|
443
|
-
* ```
|
|
634
|
+
* The headers type argument of `RequestResult`, such as `{ '200': LoginUserStatus200Headers }`, or `null` when no response declares headers.
|
|
444
635
|
*/
|
|
445
|
-
function
|
|
446
|
-
|
|
636
|
+
function buildResponseHeadersType({ node, types }) {
|
|
637
|
+
const entries = getResponseHeadersEntries({
|
|
638
|
+
node,
|
|
639
|
+
types
|
|
640
|
+
}).map(([statusCode, name]) => `'${statusCode}': ${name}`);
|
|
641
|
+
return entries.length ? `{ ${entries.join("; ")} }` : null;
|
|
447
642
|
}
|
|
448
|
-
// ../../internals/shared/src/resolver.ts
|
|
449
643
|
/**
|
|
450
|
-
*
|
|
644
|
+
* A runtime result type, such as `RequestResult`, in its `WithHeaders` form when `headers` is set.
|
|
451
645
|
*/
|
|
452
|
-
function
|
|
453
|
-
return
|
|
646
|
+
function resultTypeName(name, headers) {
|
|
647
|
+
return headers ? `${name}WithHeaders` : name;
|
|
454
648
|
}
|
|
455
649
|
/**
|
|
456
|
-
*
|
|
457
|
-
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
458
|
-
* name. Leave `scope` empty for the operation's own declaration.
|
|
459
|
-
*
|
|
460
|
-
* @example
|
|
461
|
-
* ```ts
|
|
462
|
-
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
463
|
-
* ```
|
|
650
|
+
* A runtime result type with its arguments, such as `RequestResultWithHeaders<Responses, Headers, ThrowOnError>`.
|
|
464
651
|
*/
|
|
465
|
-
function
|
|
466
|
-
return [
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
];
|
|
652
|
+
function buildResultTypeRef(name, responses, headers, rest = []) {
|
|
653
|
+
return `${resultTypeName(name, headers !== null)}<${[
|
|
654
|
+
responses,
|
|
655
|
+
headers,
|
|
656
|
+
...rest
|
|
657
|
+
].filter(Boolean).join(", ")}>`;
|
|
472
658
|
}
|
|
473
|
-
// ../../internals/shared/src/group.ts
|
|
474
659
|
/**
|
|
475
|
-
*
|
|
476
|
-
* shared default naming so every plugin groups output consistently:
|
|
477
|
-
*
|
|
478
|
-
* - `path` groups use the second path segment (`/pet/findByStatus` → `pet`).
|
|
479
|
-
* - other groups use the camelCased group (`pet store` → `petStore`).
|
|
480
|
-
*
|
|
481
|
-
* A user-provided `group.name` always wins over the default namer, so callers stay in
|
|
482
|
-
* control of their output folders. Returns `null` when grouping is disabled, matching the
|
|
483
|
-
* per-plugin convention.
|
|
484
|
-
*
|
|
485
|
-
* @param group - The user-supplied group option, or `undefined` to disable grouping.
|
|
486
|
-
*
|
|
487
|
-
* @example
|
|
488
|
-
* ```ts
|
|
489
|
-
* createGroupConfig(group) // shared across every plugin
|
|
490
|
-
* ```
|
|
660
|
+
* The names of the headers types `buildResponseHeadersType` references, for the file's imports.
|
|
491
661
|
*/
|
|
492
|
-
function
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
};
|
|
498
|
-
return {
|
|
499
|
-
...group,
|
|
500
|
-
name: group.name ? group.name : defaultName
|
|
501
|
-
};
|
|
662
|
+
function getResponseHeadersTypeNames({ node, types }) {
|
|
663
|
+
return getResponseHeadersEntries({
|
|
664
|
+
node,
|
|
665
|
+
types
|
|
666
|
+
}).map(([, name]) => name);
|
|
502
667
|
}
|
|
503
|
-
// kubb-extension.json
|
|
504
|
-
var compatibility = {
|
|
505
|
-
"kubb": ">=5.16.0",
|
|
506
|
-
"node": ">=22.22"
|
|
507
|
-
};
|
|
508
|
-
// package.json
|
|
509
|
-
var version = "5.9.0-canary.20261109T201601";
|
|
510
|
-
// ../../internals/client/src/resolveOperationTypes.ts
|
|
511
668
|
/**
|
|
512
669
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
513
670
|
* exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
|
|
@@ -530,6 +687,7 @@ function toZodTypeResolver(resolver) {
|
|
|
530
687
|
status: (node, statusCode) => type(resolver.response.status(node, statusCode)),
|
|
531
688
|
response: (node) => type(resolver.response.response(node)),
|
|
532
689
|
responses: (node) => type(resolver.response.responses(node)),
|
|
690
|
+
headers: (node, statusCode) => type(resolver.response.headers(node, statusCode)),
|
|
533
691
|
options: (node) => resolver.response.options(node)
|
|
534
692
|
}
|
|
535
693
|
};
|
|
@@ -577,179 +735,6 @@ function resolveOperationTypes(driver, pluginName) {
|
|
|
577
735
|
};
|
|
578
736
|
});
|
|
579
737
|
}
|
|
580
|
-
// ../../internals/client/src/resolveClient.ts
|
|
581
|
-
/**
|
|
582
|
-
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
583
|
-
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
584
|
-
*
|
|
585
|
-
* Every client runtime lives in a dedicated client plugin, so a consumer always calls a registered
|
|
586
|
-
* contract client plugin (plugin-fetch, plugin-axios, or plugin-client) and never emits its own
|
|
587
|
-
* inline client:
|
|
588
|
-
*
|
|
589
|
-
* - `contract` — a registered contract client plugin owns the `<op>` functions and the consumer
|
|
590
|
-
* imports and calls them.
|
|
591
|
-
* - `error` — no client plugin is registered, several are registered without a selector, the
|
|
592
|
-
* requested one is missing, or `client` is not a known selector.
|
|
593
|
-
*
|
|
594
|
-
* The `client` string selects explicitly (`'fetch'` / `'axios'` / `'client'`); when it is unset a
|
|
595
|
-
* lone registered contract client plugin is picked up automatically.
|
|
596
|
-
*/
|
|
597
|
-
const pluginFetchName = "plugin-fetch";
|
|
598
|
-
const pluginAxiosName = "plugin-axios";
|
|
599
|
-
const pluginClientName = "plugin-client";
|
|
600
|
-
const selectorToPlugin = {
|
|
601
|
-
fetch: pluginFetchName,
|
|
602
|
-
axios: pluginAxiosName,
|
|
603
|
-
client: pluginClientName
|
|
604
|
-
};
|
|
605
|
-
const contractPlugins = [
|
|
606
|
-
pluginFetchName,
|
|
607
|
-
pluginAxiosName,
|
|
608
|
-
pluginClientName
|
|
609
|
-
];
|
|
610
|
-
/**
|
|
611
|
-
* Applies the `client` resolution rules. See the module comment for the strategy shape.
|
|
612
|
-
*
|
|
613
|
-
* @example
|
|
614
|
-
* ```ts
|
|
615
|
-
* resolveClient({ client: 'fetch', pluginNames: ['plugin-ts', 'plugin-fetch'] })
|
|
616
|
-
* // { kind: 'contract', pluginName: 'plugin-fetch' }
|
|
617
|
-
* ```
|
|
618
|
-
*
|
|
619
|
-
* @example
|
|
620
|
-
* ```ts
|
|
621
|
-
* resolveClient({ client: undefined, pluginNames: ['plugin-ts'] })
|
|
622
|
-
* // { kind: 'error', code: 'KUBB_MISSING_DEPENDENCY', message: 'No client plugin is registered.', help: 'Add …' }
|
|
623
|
-
* ```
|
|
624
|
-
*/
|
|
625
|
-
function resolveClient(options) {
|
|
626
|
-
const { client, pluginNames } = options;
|
|
627
|
-
const has = (name) => pluginNames.includes(name);
|
|
628
|
-
if (client === "fetch" || client === "axios" || client === "client") {
|
|
629
|
-
const pluginName = selectorToPlugin[client];
|
|
630
|
-
if (!has(pluginName)) return {
|
|
631
|
-
kind: "error",
|
|
632
|
-
code: Diagnostics.code.missingDependency,
|
|
633
|
-
message: `\`client: '${client}'\` is set but \`@kubb/plugin-${client}\` is not registered in \`plugins\`.`,
|
|
634
|
-
help: `Add \`@kubb/plugin-${client}\` to \`plugins\`, or drop \`client\` to use a different client plugin.`
|
|
635
|
-
};
|
|
636
|
-
return {
|
|
637
|
-
kind: "contract",
|
|
638
|
-
pluginName
|
|
639
|
-
};
|
|
640
|
-
}
|
|
641
|
-
if (client !== void 0) return {
|
|
642
|
-
kind: "error",
|
|
643
|
-
code: Diagnostics.code.invalidPluginOptions,
|
|
644
|
-
message: `Option \`client\` is ${typeof client === "string" ? `'${client}'` : String(client)}, but it must be one of 'fetch', 'axios', 'client'.`,
|
|
645
|
-
help: "Set `client` to one of those values, or drop it to use the registered client plugin."
|
|
646
|
-
};
|
|
647
|
-
const registered = contractPlugins.filter(has);
|
|
648
|
-
if (registered.length === 1) return {
|
|
649
|
-
kind: "contract",
|
|
650
|
-
pluginName: registered[0]
|
|
651
|
-
};
|
|
652
|
-
if (registered.length > 1) return {
|
|
653
|
-
kind: "error",
|
|
654
|
-
code: Diagnostics.code.invalidPluginOptions,
|
|
655
|
-
message: `Multiple client plugins are registered (${registered.map((name) => `\`@kubb/${name}\``).join(", ")}) and \`client\` is not set.`,
|
|
656
|
-
help: "Set `client: 'fetch' | 'axios' | 'client'` to choose which client the hooks call, or register a single client plugin."
|
|
657
|
-
};
|
|
658
|
-
return {
|
|
659
|
-
kind: "error",
|
|
660
|
-
code: Diagnostics.code.missingDependency,
|
|
661
|
-
message: "No client plugin is registered.",
|
|
662
|
-
help: "Add `@kubb/plugin-fetch`, `@kubb/plugin-axios`, or `@kubb/plugin-client` to `plugins` so the generated code has an HTTP client to call."
|
|
663
|
-
};
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Resolves the contract client during a consumer plugin's setup hook.
|
|
667
|
-
* It extracts the plugin names from the raw `plugins` config and applies {@link resolveClient}.
|
|
668
|
-
* On a misconfiguration it throws a config diagnostic tagged with `plugin`, so every consumer
|
|
669
|
-
* fails fast with the same message.
|
|
670
|
-
*/
|
|
671
|
-
function resolveContractClient(options) {
|
|
672
|
-
const { plugin, client, plugins = [] } = options;
|
|
673
|
-
const resolved = resolveClient({
|
|
674
|
-
client,
|
|
675
|
-
pluginNames: plugins.map((plugin) => plugin.name).filter((name) => Boolean(name))
|
|
676
|
-
});
|
|
677
|
-
if (resolved.kind === "error") {
|
|
678
|
-
const { code, message, help } = resolved;
|
|
679
|
-
throw new Diagnostics.Error({
|
|
680
|
-
code,
|
|
681
|
-
severity: "error",
|
|
682
|
-
message,
|
|
683
|
-
help,
|
|
684
|
-
location: { kind: "config" },
|
|
685
|
-
plugin
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
return resolved;
|
|
689
|
-
}
|
|
690
|
-
// ../../internals/client/src/resolveClientOperation.ts
|
|
691
|
-
function isRelativeImport(importPath) {
|
|
692
|
-
return importPath.startsWith(".");
|
|
693
|
-
}
|
|
694
|
-
function resolveImportPath(options) {
|
|
695
|
-
const { importPath, operationFile } = options;
|
|
696
|
-
return isRelativeImport(importPath) ? path.resolve(path.dirname(operationFile), importPath) : importPath;
|
|
697
|
-
}
|
|
698
|
-
/**
|
|
699
|
-
* Resolves the contract client `<op>` a consumer (query hook, MCP handler) imports, by looking up
|
|
700
|
-
* the registered contract client plugin's resolver and output. Works for any contract client plugin
|
|
701
|
-
* (plugin-fetch, plugin-axios, or plugin-client). Returns `null` when no contract plugin is in play (the inline
|
|
702
|
-
* path), or when the client plugin's `exclude` / `include` leaves this operation out, since it then
|
|
703
|
-
* emits no `<op>` to import. plugin-fetch / plugin-axios inject `.kubb/client.ts` at the global
|
|
704
|
-
* output root, the same path consumers read `RequestConfig` / `ResponseErrorConfig` from.
|
|
705
|
-
* With plugin-client they come from the user's `importPath` module instead.
|
|
706
|
-
*
|
|
707
|
-
* The result is cached in `memo` (the build's `ctx.memo`) under the client plugin's name and the operation,
|
|
708
|
-
* so several dependents reading the same client plugin for one operation in a single pass
|
|
709
|
-
* (react-query's query/mutation/infinite generators, vue-query, swr, the MCP handler, ...) share
|
|
710
|
-
* one computed result instead of each re-deriving the name and path.
|
|
711
|
-
*
|
|
712
|
-
* `returnType`, `output`, and `group` are the client plugin's options for this operation, after its
|
|
713
|
-
* `override` entries, the same resolution its own generator sees. A dependent's generated call body
|
|
714
|
-
* and import path therefore match the `<op>` actually emitted, even when an `override` changes it.
|
|
715
|
-
*/
|
|
716
|
-
function resolveClientOperation(options) {
|
|
717
|
-
const { clientPlugin, driver, node, root, output, memo } = options;
|
|
718
|
-
if (!clientPlugin) return null;
|
|
719
|
-
return memo(`${clientPlugin.pluginName}:clientOperation:${node.operationId}`, () => {
|
|
720
|
-
const resolver = driver.getResolver(clientPlugin.pluginName);
|
|
721
|
-
const pluginOptions = driver.getPlugin(clientPlugin.pluginName)?.options ?? {};
|
|
722
|
-
const { exclude, include, override } = pluginOptions;
|
|
723
|
-
const operationOptions = Boolean(exclude?.length || include?.length || override?.length) && resolver.default ? resolver.default.options(node, {
|
|
724
|
-
options: pluginOptions,
|
|
725
|
-
exclude,
|
|
726
|
-
include,
|
|
727
|
-
override
|
|
728
|
-
}) : pluginOptions;
|
|
729
|
-
if (!operationOptions) return null;
|
|
730
|
-
const file = resolver.file({
|
|
731
|
-
...operationFileEntry(node, node.operationId),
|
|
732
|
-
root,
|
|
733
|
-
output: operationOptions.output ?? output,
|
|
734
|
-
group: operationOptions.group ?? void 0
|
|
735
|
-
});
|
|
736
|
-
const { importPath } = operationOptions;
|
|
737
|
-
const name = resolver.name(node.operationId);
|
|
738
|
-
return {
|
|
739
|
-
name: resolver.resolveName({
|
|
740
|
-
key: operationKey(node),
|
|
741
|
-
name
|
|
742
|
-
}),
|
|
743
|
-
path: file.path,
|
|
744
|
-
clientPath: importPath === void 0 ? path.resolve(root, ".kubb/client.ts") : resolveImportPath({
|
|
745
|
-
importPath,
|
|
746
|
-
operationFile: file.path
|
|
747
|
-
}),
|
|
748
|
-
clientPathIsFile: importPath === void 0 || isRelativeImport(importPath),
|
|
749
|
-
returnType: operationOptions.returnType ?? "full"
|
|
750
|
-
};
|
|
751
|
-
});
|
|
752
|
-
}
|
|
753
738
|
// ../../internals/tanstack-query/src/components/HookFile.tsx
|
|
754
739
|
/**
|
|
755
740
|
* Resolves what every TanStack hook file shares: its file, the operation types file and names (`@kubb/plugin-ts`, or
|
|
@@ -768,14 +753,18 @@ function resolveHookFile(node, ctx, options) {
|
|
|
768
753
|
});
|
|
769
754
|
const types = resolveOperationTypes(ctx.driver, resolver.pluginName);
|
|
770
755
|
const tsResolver = types.resolver;
|
|
756
|
+
const headerTypeNames = returnType === "data" || !contractOp.typedHeaders ? [] : getResponseHeadersTypeNames({
|
|
757
|
+
node,
|
|
758
|
+
types: tsResolver
|
|
759
|
+
});
|
|
771
760
|
const file = resolver.file({
|
|
772
761
|
...operationFileEntry(node, name),
|
|
773
762
|
root,
|
|
774
763
|
output,
|
|
775
764
|
group: group ?? void 0
|
|
776
765
|
});
|
|
777
|
-
const fileTs =
|
|
778
|
-
|
|
766
|
+
const fileTs = resolveOperationFile({
|
|
767
|
+
ctx,
|
|
779
768
|
node,
|
|
780
769
|
resolver: tsResolver,
|
|
781
770
|
root,
|
|
@@ -785,6 +774,7 @@ function resolveHookFile(node, ctx, options) {
|
|
|
785
774
|
const importedTypeNames = [
|
|
786
775
|
tsResolver.response.options(node),
|
|
787
776
|
tsResolver.response.responses(node),
|
|
777
|
+
...headerTypeNames,
|
|
788
778
|
...extraTypeNames,
|
|
789
779
|
...resolveOperationTypeNames(node, tsResolver, {
|
|
790
780
|
exclude: [keyTypeName, returnType !== "data" ? tsResolver.response.response(node) : null].filter((name) => Boolean(name)),
|
|
@@ -802,6 +792,7 @@ function resolveHookFile(node, ctx, options) {
|
|
|
802
792
|
tsResolver,
|
|
803
793
|
contractOp,
|
|
804
794
|
importedTypeNames,
|
|
795
|
+
resultTypeName: headerTypeNames.length ? "RequestResultWithHeaders" : "RequestResult",
|
|
805
796
|
returnType,
|
|
806
797
|
banner: resolver.default.banner(ctx.meta, {
|
|
807
798
|
output,
|
|
@@ -819,7 +810,7 @@ function resolveHookFile(node, ctx, options) {
|
|
|
819
810
|
* The `<File>` shell shared by the TanStack hook generators; renders `children` after the imports.
|
|
820
811
|
*/
|
|
821
812
|
function HookFile({ hook, imports, children }) {
|
|
822
|
-
const { file, fileTs, contractOp, importedTypeNames, banner, footer } = hook;
|
|
813
|
+
const { file, fileTs, contractOp, importedTypeNames, resultTypeName, banner, footer } = hook;
|
|
823
814
|
return /* @__PURE__ */ jsxs(File, {
|
|
824
815
|
baseName: file.baseName,
|
|
825
816
|
path: file.path,
|
|
@@ -836,7 +827,7 @@ function HookFile({ hook, imports, children }) {
|
|
|
836
827
|
name: [
|
|
837
828
|
"RequestConfig",
|
|
838
829
|
"ResponseErrorConfig",
|
|
839
|
-
|
|
830
|
+
resultTypeName
|
|
840
831
|
],
|
|
841
832
|
root: contractOp.clientPathIsFile ? file.path : void 0,
|
|
842
833
|
path: contractOp.clientPath,
|
|
@@ -909,8 +900,8 @@ function buildGroupedRequestParam(node, options) {
|
|
|
909
900
|
/**
|
|
910
901
|
* Builds the shared `({ path, query, body, headers, cookies }, config = {})` parameter list for a
|
|
911
902
|
* TanStack query-options function. The leading parameter mirrors the client signature, and the
|
|
912
|
-
* trailing `config` is
|
|
913
|
-
*
|
|
903
|
+
* trailing `config` is the client's `Options` minus the request groups and `throwOnError`, which the
|
|
904
|
+
* hook passes itself. Framework plugins wrap the result when needed, for example vue-query applies `MaybeRefOrGetter`.
|
|
914
905
|
*/
|
|
915
906
|
function buildQueryOptionsParams(node, options) {
|
|
916
907
|
const { resolver, memberTypeWrapper } = options;
|
|
@@ -920,7 +911,7 @@ function buildQueryOptionsParams(node, options) {
|
|
|
920
911
|
});
|
|
921
912
|
const configParam = createFunctionParameter({
|
|
922
913
|
name: "config",
|
|
923
|
-
type: buildClientOptionType(
|
|
914
|
+
type: buildClientOptionType(),
|
|
924
915
|
default: "{}"
|
|
925
916
|
});
|
|
926
917
|
return createFunctionParameters({ params: [groupedParam, configParam].filter((param) => param !== null) });
|
|
@@ -932,26 +923,30 @@ function buildQueryOptionsParams(node, options) {
|
|
|
932
923
|
* can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
|
|
933
924
|
*
|
|
934
925
|
* When `unwrapName` is set, each request group is passed as an explicit member unwrapped through it,
|
|
935
|
-
* used by vue-query to resolve refs and getters with `toValue(...)` at call time.
|
|
926
|
+
* used by vue-query to resolve refs and getters with `toValue(...)` at call time. `pageParam` names the
|
|
927
|
+
* query parameter an infinite query sets from TanStack's `pageParam`.
|
|
936
928
|
*
|
|
937
929
|
* @example
|
|
938
930
|
* ```ts
|
|
939
931
|
* buildClientCall(node, { clientName: 'getPetById', signal: true })
|
|
940
|
-
* // getPetById({
|
|
932
|
+
* // getPetById({ ...config, path, signal: config.signal ?? signal, throwOnError: true })
|
|
941
933
|
* ```
|
|
942
934
|
*/
|
|
943
935
|
function buildClientCall(node, options) {
|
|
944
|
-
const { clientName, signal = false, unwrapName, withContentType = false,
|
|
936
|
+
const { clientName, signal = false, unwrapName, withContentType = false, pageParam } = options;
|
|
945
937
|
const { groups } = getRequestGroupOptionality(node);
|
|
946
938
|
const names = requestGroupOrder.filter((key) => groups[key]);
|
|
947
|
-
const
|
|
939
|
+
const member = (name) => {
|
|
940
|
+
const value = unwrapName ? unwrapName(name) : name;
|
|
941
|
+
if (name === "query" && pageParam) return `query: { ...${value}, ${JSON.stringify(pageParam)}: pageParam }`;
|
|
942
|
+
return unwrapName ? `${name}: ${value}` : name;
|
|
943
|
+
};
|
|
944
|
+
return `${clientName}({ ${[
|
|
948
945
|
"...config",
|
|
949
|
-
...withContentType && hasRequestContentTypeChoice(node) ? ["...variables"] : names.map(
|
|
946
|
+
...withContentType && hasRequestContentTypeChoice(node) ? ["...variables"] : names.map(member),
|
|
950
947
|
signal ? "signal: config.signal ?? signal" : null,
|
|
951
948
|
"throwOnError: true"
|
|
952
|
-
].filter((part) => part !== null)
|
|
953
|
-
if (withContentType && hasRequestContentTypeChoice(node)) return `${clientName}(${castToClientOptions(`{ ${args.join(", ")} }`, clientName, returnType)})`;
|
|
954
|
-
return `${clientName}({ ${args.join(", ")} })`;
|
|
949
|
+
].filter((part) => part !== null).join(", ")} })`;
|
|
955
950
|
}
|
|
956
951
|
/**
|
|
957
952
|
* Builds the query/mutation body that resolves a `buildClientCall` expression down to the bare
|
|
@@ -974,19 +969,28 @@ function buildCallResultBody(call, options = {}) {
|
|
|
974
969
|
if (clientReturnType === "plain") return `return (await ${call}).data`;
|
|
975
970
|
return `return ${call}.unwrap()`;
|
|
976
971
|
}
|
|
972
|
+
function buildResultType(node, resolver, typedHeaders) {
|
|
973
|
+
const headers = typedHeaders ? buildResponseHeadersType({
|
|
974
|
+
node,
|
|
975
|
+
types: resolver
|
|
976
|
+
}) : null;
|
|
977
|
+
return buildResultTypeRef("RequestResult", resolver.response.responses(node), headers, ["true"]);
|
|
978
|
+
}
|
|
977
979
|
/**
|
|
978
980
|
* Builds the `TData` / `TError` type expressions shared by every generated hook, joining the resolved
|
|
979
981
|
* success responses into `TData` and wrapping the error responses in `ResponseErrorConfig<...>`.
|
|
980
|
-
* With `returnType` `'full'`, `TData` is the client's `RequestResult` over the operation's responses record
|
|
982
|
+
* With `returnType` `'full'`, `TData` is the client's `RequestResult` over the operation's responses record, with the
|
|
983
|
+
* response headers the operation declares.
|
|
981
984
|
*/
|
|
982
985
|
function buildResponseTypes(node, resolver, options = {}) {
|
|
983
|
-
const { returnType = "data" } = options;
|
|
984
|
-
return resolver.memo(`responseTypes:${returnType}:${node.method}\0${node.path}`, () => {
|
|
986
|
+
const { returnType = "data", typedHeaders = true } = options;
|
|
987
|
+
return resolver.memo(`responseTypes:${returnType}:${typedHeaders}:${node.method}\0${node.path}`, () => {
|
|
985
988
|
const successNames = resolveSuccessNames(node, resolver);
|
|
986
|
-
const
|
|
989
|
+
const bodyName = successNames.length > 0 ? successNames.join(" | ") : resolver.response.response(node);
|
|
990
|
+
const responseName = hasStreamVariant(node) ? `${bodyName} | ReadableStream<Uint8Array>` : bodyName;
|
|
987
991
|
const errorNames = resolveErrorNames(node, resolver);
|
|
988
992
|
return {
|
|
989
|
-
TData: returnType
|
|
993
|
+
TData: returnType === "data" ? responseName : buildResultType(node, resolver, typedHeaders),
|
|
990
994
|
TError: `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`
|
|
991
995
|
};
|
|
992
996
|
});
|
|
@@ -1081,6 +1085,18 @@ function buildHookParams(node, options) {
|
|
|
1081
1085
|
}) : null;
|
|
1082
1086
|
return createFunctionParameters({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
|
|
1083
1087
|
}
|
|
1088
|
+
/**
|
|
1089
|
+
* The `TRequest` type argument of a mutation hook: the options type when the operation takes a request, else `undefined`.
|
|
1090
|
+
* When the variables carry the request media type and the response has several, `contentType` can pick the
|
|
1091
|
+
* response one too, as `{ request, response }`.
|
|
1092
|
+
*/
|
|
1093
|
+
function resolveMutationRequestType(node, resolver) {
|
|
1094
|
+
if (!buildGroupedRequestParam(node, { resolver })) return "undefined";
|
|
1095
|
+
const optionsName = resolver.response.options(node);
|
|
1096
|
+
const response = getResponseContentTypeInfo(node);
|
|
1097
|
+
if (!hasRequestContentTypeChoice(node) || !response.isMultipleContentTypes) return optionsName;
|
|
1098
|
+
return `${/^[\w$]+$/.test(optionsName) ? optionsName : `(${optionsName})`} & { contentType?: string | { response?: ${response.contentTypeUnion} } }`;
|
|
1099
|
+
}
|
|
1084
1100
|
// ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
1085
1101
|
const declarationPrinter$4 = functionPrinter({ mode: "declaration" });
|
|
1086
1102
|
/**
|
|
@@ -1144,7 +1160,7 @@ function MutationKey({ name, node, transformer, typeName }) {
|
|
|
1144
1160
|
function createQueryResolver(variant = "") {
|
|
1145
1161
|
return {
|
|
1146
1162
|
name(node) {
|
|
1147
|
-
const name = `use${capitalize(this.name(node.operationId))}${variant}`;
|
|
1163
|
+
const name = `use${casing.capitalize(this.name(node.operationId))}${variant}`;
|
|
1148
1164
|
return this.resolveName({
|
|
1149
1165
|
key: operationKey(node, "query", variant, "name"),
|
|
1150
1166
|
name
|
|
@@ -1165,7 +1181,7 @@ function createQueryResolver(variant = "") {
|
|
|
1165
1181
|
});
|
|
1166
1182
|
},
|
|
1167
1183
|
keyTypeName(node) {
|
|
1168
|
-
const name = `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
|
|
1184
|
+
const name = `${casing.capitalize(this.name(node.operationId))}${variant}QueryKey`;
|
|
1169
1185
|
return this.resolveName({
|
|
1170
1186
|
key: operationKey(node, "query", variant, "keyTypeName"),
|
|
1171
1187
|
name
|
|
@@ -1186,7 +1202,7 @@ function createQueryResolver(variant = "") {
|
|
|
1186
1202
|
function createMutationResolver() {
|
|
1187
1203
|
return {
|
|
1188
1204
|
name(node) {
|
|
1189
|
-
const name = `use${capitalize(this.name(node.operationId))}`;
|
|
1205
|
+
const name = `use${casing.capitalize(this.name(node.operationId))}`;
|
|
1190
1206
|
return this.resolveName({
|
|
1191
1207
|
key: operationKey(node, "mutation", "name"),
|
|
1192
1208
|
name
|
|
@@ -1215,14 +1231,14 @@ const declarationPrinter$3 = functionPrinter({ mode: "declaration" });
|
|
|
1215
1231
|
*/
|
|
1216
1232
|
const queryKeyTransformer = ({ node, variant }) => {
|
|
1217
1233
|
if (!node.path) return [];
|
|
1218
|
-
const { path, query } =
|
|
1234
|
+
const { path, query } = ast.http.groupParameters(node);
|
|
1219
1235
|
const isInfinite = variant === "infiniteQuery" || variant === "suspenseInfiniteQuery";
|
|
1220
1236
|
const urlFields = [`url: '${Url.toPath(node.path)}'`];
|
|
1221
1237
|
if (path.length > 0) urlFields.push("params: path");
|
|
1222
1238
|
if (isInfinite) urlFields.push("infinite: true");
|
|
1223
1239
|
const result = [`{ ${urlFields.join(", ")} }`];
|
|
1224
1240
|
if (query.length > 0) result.push("...(query ? [query] : [])");
|
|
1225
|
-
if (
|
|
1241
|
+
if (ast.http.requestBody.getSchema(node)) result.push("...(body !== undefined ? [body] : [])");
|
|
1226
1242
|
return result;
|
|
1227
1243
|
};
|
|
1228
1244
|
function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "query", memberTypeWrapper, exportType }) {
|
|
@@ -1263,8 +1279,11 @@ function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "qu
|
|
|
1263
1279
|
const declarationPrinter$2 = functionPrinter({ mode: "declaration" });
|
|
1264
1280
|
const callPrinter$1 = functionPrinter({ mode: "call" });
|
|
1265
1281
|
function buildMutationParamsNode(node, options) {
|
|
1266
|
-
const { mutationKeyTypeName, mutationArgTypeName, resolver, returnType } = options;
|
|
1267
|
-
const { TData, TError } = buildResponseTypes(node, resolver, {
|
|
1282
|
+
const { mutationKeyTypeName, mutationArgTypeName, resolver, returnType, typedHeaders } = options;
|
|
1283
|
+
const { TData, TError } = buildResponseTypes(node, resolver, {
|
|
1284
|
+
returnType,
|
|
1285
|
+
typedHeaders
|
|
1286
|
+
});
|
|
1268
1287
|
return createFunctionParameters({ params: [createFunctionParameter({
|
|
1269
1288
|
name: "options",
|
|
1270
1289
|
type: `{
|
|
@@ -1275,21 +1294,23 @@ function buildMutationParamsNode(node, options) {
|
|
|
1275
1294
|
default: "{}"
|
|
1276
1295
|
})] });
|
|
1277
1296
|
}
|
|
1278
|
-
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, clientReturnType, returnType }) {
|
|
1279
|
-
const { TData, TError } = buildResponseTypes(node, tsResolver, {
|
|
1297
|
+
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, clientReturnType, returnType, typedHeaders }) {
|
|
1298
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, {
|
|
1299
|
+
returnType,
|
|
1300
|
+
typedHeaders
|
|
1301
|
+
});
|
|
1280
1302
|
const groupedParam = buildGroupedRequestParam(node, {
|
|
1281
1303
|
resolver: tsResolver,
|
|
1282
1304
|
withContentType: true
|
|
1283
1305
|
});
|
|
1284
1306
|
const hasMutationParams = groupedParam !== null;
|
|
1285
1307
|
const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] });
|
|
1286
|
-
const argTypeBody = hasMutationParams ?
|
|
1308
|
+
const argTypeBody = hasMutationParams ? resolveMutationRequestType(node, tsResolver) : "";
|
|
1287
1309
|
const argBindingStr = hasMutationParams ? callPrinter$1.print(groupedParamsNode) ?? "" : "";
|
|
1288
1310
|
const mutationFnBody = buildCallResultBody(buildClientCall(node, {
|
|
1289
1311
|
clientName,
|
|
1290
1312
|
signal: false,
|
|
1291
|
-
withContentType: true
|
|
1292
|
-
returnType: clientReturnType
|
|
1313
|
+
withContentType: true
|
|
1293
1314
|
}), {
|
|
1294
1315
|
clientReturnType,
|
|
1295
1316
|
returnType
|
|
@@ -1304,7 +1325,8 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1304
1325
|
mutationKeyTypeName,
|
|
1305
1326
|
mutationArgTypeName,
|
|
1306
1327
|
resolver: tsResolver,
|
|
1307
|
-
returnType
|
|
1328
|
+
returnType,
|
|
1329
|
+
typedHeaders
|
|
1308
1330
|
});
|
|
1309
1331
|
const paramsSignature = declarationPrinter$2.print(paramsNode) ?? "";
|
|
1310
1332
|
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
|
|
@@ -1351,7 +1373,7 @@ const mutationGenerator = defineGenerator({
|
|
|
1351
1373
|
name: "swr-mutation",
|
|
1352
1374
|
renderer: jsxRenderer,
|
|
1353
1375
|
operation(node, ctx) {
|
|
1354
|
-
if (!ast.
|
|
1376
|
+
if (!ast.http.isOperation(node)) return null;
|
|
1355
1377
|
const { driver, resolver, root } = ctx;
|
|
1356
1378
|
const { output, query, mutation, client, group } = ctx.options;
|
|
1357
1379
|
const { isMutation } = classifyOperation(node, {
|
|
@@ -1406,7 +1428,8 @@ const mutationGenerator = defineGenerator({
|
|
|
1406
1428
|
node,
|
|
1407
1429
|
tsResolver: hook.tsResolver,
|
|
1408
1430
|
clientReturnType: contractOp.returnType,
|
|
1409
|
-
returnType: hook.returnType
|
|
1431
|
+
returnType: hook.returnType,
|
|
1432
|
+
typedHeaders: hook.contractOp.typedHeaders
|
|
1410
1433
|
})
|
|
1411
1434
|
] })]
|
|
1412
1435
|
});
|
|
@@ -1415,8 +1438,11 @@ const mutationGenerator = defineGenerator({
|
|
|
1415
1438
|
// src/components/Query.tsx
|
|
1416
1439
|
const declarationPrinter$1 = functionPrinter({ mode: "declaration" });
|
|
1417
1440
|
const callPrinter = functionPrinter({ mode: "call" });
|
|
1418
|
-
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, returnType }) {
|
|
1419
|
-
const { TData, TError } = buildResponseTypes(node, tsResolver, {
|
|
1441
|
+
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, returnType, typedHeaders }) {
|
|
1442
|
+
const { TData, TError } = buildResponseTypes(node, tsResolver, {
|
|
1443
|
+
returnType,
|
|
1444
|
+
typedHeaders
|
|
1445
|
+
});
|
|
1420
1446
|
const generics = [
|
|
1421
1447
|
TData,
|
|
1422
1448
|
TError,
|
|
@@ -1430,7 +1456,7 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1430
1456
|
resolver: tsResolver,
|
|
1431
1457
|
optionsType: `{
|
|
1432
1458
|
query?: SWRConfiguration<${[TData, TError].join(", ")}>,
|
|
1433
|
-
client?: ${buildClientOptionType(
|
|
1459
|
+
client?: ${buildClientOptionType()},
|
|
1434
1460
|
shouldFetch?: boolean,
|
|
1435
1461
|
immutable?: boolean
|
|
1436
1462
|
}`
|
|
@@ -1505,7 +1531,7 @@ const queryGenerator = defineGenerator({
|
|
|
1505
1531
|
name: "swr-query",
|
|
1506
1532
|
renderer: jsxRenderer,
|
|
1507
1533
|
operation(node, ctx) {
|
|
1508
|
-
if (!ast.
|
|
1534
|
+
if (!ast.http.isOperation(node)) return null;
|
|
1509
1535
|
const { driver, resolver, root } = ctx;
|
|
1510
1536
|
const { output, query, mutation, client, group } = ctx.options;
|
|
1511
1537
|
const { isQuery } = classifyOperation(node, {
|
|
@@ -1553,7 +1579,8 @@ const queryGenerator = defineGenerator({
|
|
|
1553
1579
|
node,
|
|
1554
1580
|
tsResolver,
|
|
1555
1581
|
clientReturnType: contractOp.returnType,
|
|
1556
|
-
returnType: hook.returnType
|
|
1582
|
+
returnType: hook.returnType,
|
|
1583
|
+
typedHeaders: hook.contractOp.typedHeaders
|
|
1557
1584
|
}),
|
|
1558
1585
|
query && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1559
1586
|
/* @__PURE__ */ jsx(File.Import, {
|
|
@@ -1572,7 +1599,8 @@ const queryGenerator = defineGenerator({
|
|
|
1572
1599
|
queryKeyTypeName,
|
|
1573
1600
|
node,
|
|
1574
1601
|
tsResolver,
|
|
1575
|
-
returnType: hook.returnType
|
|
1602
|
+
returnType: hook.returnType,
|
|
1603
|
+
typedHeaders: hook.contractOp.typedHeaders
|
|
1576
1604
|
})
|
|
1577
1605
|
] })
|
|
1578
1606
|
]
|
|
@@ -1603,14 +1631,14 @@ const resolverSwr = createResolver({
|
|
|
1603
1631
|
mutation: {
|
|
1604
1632
|
...createMutationResolver(),
|
|
1605
1633
|
keyTypeName(node) {
|
|
1606
|
-
const name = `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1634
|
+
const name = `${casing.capitalize(this.name(node.operationId))}MutationKey`;
|
|
1607
1635
|
return this.resolveName({
|
|
1608
1636
|
key: operationKey(node, "mutation", "keyTypeName"),
|
|
1609
1637
|
name
|
|
1610
1638
|
});
|
|
1611
1639
|
},
|
|
1612
1640
|
argTypeName(node) {
|
|
1613
|
-
const name = `${capitalize(this.name(node.operationId))}MutationArg`;
|
|
1641
|
+
const name = `${casing.capitalize(this.name(node.operationId))}MutationArg`;
|
|
1614
1642
|
return this.resolveName({
|
|
1615
1643
|
key: operationKey(node, "mutation", "argTypeName"),
|
|
1616
1644
|
name
|
|
@@ -1674,7 +1702,7 @@ const pluginSwr = defineExtension({
|
|
|
1674
1702
|
query: resolveQueryConfig(query, { importPath: "swr" }),
|
|
1675
1703
|
mutationKey,
|
|
1676
1704
|
mutation: resolveMutationConfig(mutation, { importPath: "swr/mutation" }),
|
|
1677
|
-
group
|
|
1705
|
+
group,
|
|
1678
1706
|
exclude,
|
|
1679
1707
|
include,
|
|
1680
1708
|
override,
|