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