@kubb/plugin-swr 5.6.0-canary.20261025T201031 → 5.6.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 +321 -125
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -15
- package/dist/index.d.ts +1 -15
- package/dist/index.js +320 -124
- package/dist/index.js.map +1 -1
- package/kubb-extension.json +1 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
|
|
5
|
+
// \0rolldown/runtime.js
|
|
6
6
|
var __create = Object.create;
|
|
7
7
|
var __defProp = Object.defineProperty;
|
|
8
8
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -23,7 +23,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
value: mod,
|
|
24
24
|
enumerable: true
|
|
25
25
|
}) : target, mod));
|
|
26
|
-
//#endregion
|
|
27
26
|
let kubb_kit = require("kubb/kit");
|
|
28
27
|
let node_path = require("node:path");
|
|
29
28
|
node_path = __toESM(node_path, 1);
|
|
@@ -31,7 +30,7 @@ let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
|
31
30
|
let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
32
31
|
let kubb_jsx = require("kubb/jsx");
|
|
33
32
|
let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
|
|
34
|
-
|
|
33
|
+
// ../../internals/shared/src/operation.ts
|
|
35
34
|
/**
|
|
36
35
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
37
36
|
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
@@ -100,6 +99,24 @@ function getContentTypeInfo(node) {
|
|
|
100
99
|
function getResponseContentTypeInfo(node) {
|
|
101
100
|
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
102
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Reads the single base content type of an operation's primary success response, lowercased and
|
|
104
|
+
* stripped of any `; charset=...` suffix. Returns `undefined` when the response declares zero or
|
|
105
|
+
* more than one content type, since neither case has a single type to act on.
|
|
106
|
+
*/
|
|
107
|
+
function getPrimarySuccessContentType(node) {
|
|
108
|
+
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
109
|
+
if (contentTypes.length !== 1) return void 0;
|
|
110
|
+
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Whether an operation streams its primary success response as Server-Sent Events
|
|
114
|
+
* (`text/event-stream`). The client generator uses this to return a typed event stream instead of a
|
|
115
|
+
* one-shot `RequestResult`.
|
|
116
|
+
*/
|
|
117
|
+
function isEventStream(node) {
|
|
118
|
+
return getPrimarySuccessContentType(node) === "text/event-stream";
|
|
119
|
+
}
|
|
103
120
|
const requestConfigBaseType = `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`;
|
|
104
121
|
function buildRequestConfigType(node) {
|
|
105
122
|
const request = getContentTypeInfo(node);
|
|
@@ -191,6 +208,22 @@ function getOperationParameters(node) {
|
|
|
191
208
|
operationParameterGroupsByNode.set(node, groups);
|
|
192
209
|
return groups;
|
|
193
210
|
}
|
|
211
|
+
/**
|
|
212
|
+
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
213
|
+
* referencing the already-resolved body and grouped param names. Shared by `@kubb/plugin-ts`'s
|
|
214
|
+
* `Options` type and `@kubb/plugin-zod`'s inferred options schema, so both printers emit the same
|
|
215
|
+
* shape from the same inputs. `primitive: 'object'` is a no-op for the TS printer and tells the Zod
|
|
216
|
+
* printer to emit `z.object(…)` rather than a record.
|
|
217
|
+
*/
|
|
218
|
+
function hasRequestContentTypeChoice(node) {
|
|
219
|
+
return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Casts a call argument to the client's parameter type; a `'plain'` client is non-generic.
|
|
223
|
+
*/
|
|
224
|
+
function castToClientOptions(callArg, clientName, returnType = "full") {
|
|
225
|
+
return `${callArg} as Parameters<typeof ${clientName}${returnType === "plain" ? "" : "<true>"}>[0]`;
|
|
226
|
+
}
|
|
194
227
|
function getStatusCodeNumber(statusCode) {
|
|
195
228
|
const code = Number(statusCode);
|
|
196
229
|
return Number.isNaN(code) ? null : code;
|
|
@@ -221,88 +254,209 @@ function resolveSuccessNames(node, resolver) {
|
|
|
221
254
|
function resolveStatusCodeNames(node, resolver) {
|
|
222
255
|
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
223
256
|
}
|
|
224
|
-
|
|
257
|
+
/**
|
|
258
|
+
* Lists the type names an operation's types file declares, computed once per resolver and `options` so every
|
|
259
|
+
* dependent reads the same list.
|
|
260
|
+
*/
|
|
225
261
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
...responseStatusNames
|
|
248
|
-
] : [
|
|
249
|
-
...paramNames,
|
|
250
|
-
...bodyAndResponseNames,
|
|
251
|
-
...responseStatusNames
|
|
252
|
-
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
253
|
-
byResolver.set(cacheKey, result);
|
|
254
|
-
return result;
|
|
262
|
+
const key = `${node.operationId}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${options.includeParams === false ? "noparams" : ""}\0${(options.exclude ?? []).join(",")}`;
|
|
263
|
+
return resolver.memo(`operationTypeNames:${key}`, () => {
|
|
264
|
+
const { path, query, header } = getOperationParameters(node);
|
|
265
|
+
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
266
|
+
const exclude = new Set(options.exclude ?? []);
|
|
267
|
+
const paramNames = options.includeParams === false ? [] : [
|
|
268
|
+
...path.map((param) => resolver.param.path(node, param)),
|
|
269
|
+
...query.map((param) => resolver.param.query(node, param)),
|
|
270
|
+
...header.map((param) => resolver.param.headers(node, param))
|
|
271
|
+
];
|
|
272
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
273
|
+
return (options.order === "body-response-first" ? [
|
|
274
|
+
...bodyAndResponseNames,
|
|
275
|
+
...paramNames,
|
|
276
|
+
...responseStatusNames
|
|
277
|
+
] : [
|
|
278
|
+
...paramNames,
|
|
279
|
+
...bodyAndResponseNames,
|
|
280
|
+
...responseStatusNames
|
|
281
|
+
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
282
|
+
});
|
|
255
283
|
}
|
|
256
|
-
|
|
257
|
-
//#region ../../internals/utils/src/casing.ts
|
|
284
|
+
// ../../internals/utils/src/casing.ts
|
|
258
285
|
/**
|
|
259
|
-
*
|
|
260
|
-
*
|
|
261
|
-
* and capitalizes each word according to `pascal`.
|
|
286
|
+
* Uppercases only the first character of `text`, leaving the rest untouched.
|
|
287
|
+
* Unlike `pascalCase` it never re-splits word boundaries or strips characters.
|
|
262
288
|
*
|
|
263
|
-
*
|
|
289
|
+
* @example
|
|
290
|
+
* `capitalize('getPetById') // 'GetPetById'`
|
|
264
291
|
*/
|
|
265
|
-
function
|
|
266
|
-
return text.
|
|
267
|
-
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
268
|
-
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
269
|
-
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
292
|
+
function capitalize(text) {
|
|
293
|
+
return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
|
|
270
294
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
295
|
+
// ../../internals/utils/src/reserved.ts
|
|
296
|
+
/**
|
|
297
|
+
* JavaScript and Java reserved words.
|
|
298
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
299
|
+
*/
|
|
300
|
+
const reservedWords = /* @__PURE__ */ new Set([
|
|
301
|
+
"abstract",
|
|
302
|
+
"arguments",
|
|
303
|
+
"await",
|
|
304
|
+
"boolean",
|
|
305
|
+
"break",
|
|
306
|
+
"byte",
|
|
307
|
+
"case",
|
|
308
|
+
"catch",
|
|
309
|
+
"char",
|
|
310
|
+
"class",
|
|
311
|
+
"const",
|
|
312
|
+
"continue",
|
|
313
|
+
"debugger",
|
|
314
|
+
"default",
|
|
315
|
+
"delete",
|
|
316
|
+
"do",
|
|
317
|
+
"double",
|
|
318
|
+
"else",
|
|
319
|
+
"enum",
|
|
320
|
+
"eval",
|
|
321
|
+
"export",
|
|
322
|
+
"extends",
|
|
323
|
+
"false",
|
|
324
|
+
"final",
|
|
325
|
+
"finally",
|
|
326
|
+
"float",
|
|
327
|
+
"for",
|
|
328
|
+
"function",
|
|
329
|
+
"goto",
|
|
330
|
+
"if",
|
|
331
|
+
"implements",
|
|
332
|
+
"import",
|
|
333
|
+
"in",
|
|
334
|
+
"instanceof",
|
|
335
|
+
"int",
|
|
336
|
+
"interface",
|
|
337
|
+
"let",
|
|
338
|
+
"long",
|
|
339
|
+
"native",
|
|
340
|
+
"new",
|
|
341
|
+
"null",
|
|
342
|
+
"package",
|
|
343
|
+
"private",
|
|
344
|
+
"protected",
|
|
345
|
+
"public",
|
|
346
|
+
"return",
|
|
347
|
+
"short",
|
|
348
|
+
"static",
|
|
349
|
+
"super",
|
|
350
|
+
"switch",
|
|
351
|
+
"synchronized",
|
|
352
|
+
"this",
|
|
353
|
+
"throw",
|
|
354
|
+
"throws",
|
|
355
|
+
"transient",
|
|
356
|
+
"true",
|
|
357
|
+
"try",
|
|
358
|
+
"typeof",
|
|
359
|
+
"var",
|
|
360
|
+
"void",
|
|
361
|
+
"volatile",
|
|
362
|
+
"while",
|
|
363
|
+
"with",
|
|
364
|
+
"yield",
|
|
365
|
+
"Array",
|
|
366
|
+
"Date",
|
|
367
|
+
"hasOwnProperty",
|
|
368
|
+
"Infinity",
|
|
369
|
+
"isFinite",
|
|
370
|
+
"isNaN",
|
|
371
|
+
"isPrototypeOf",
|
|
372
|
+
"length",
|
|
373
|
+
"Math",
|
|
374
|
+
"name",
|
|
375
|
+
"NaN",
|
|
376
|
+
"Number",
|
|
377
|
+
"Object",
|
|
378
|
+
"prototype",
|
|
379
|
+
"String",
|
|
380
|
+
"toString",
|
|
381
|
+
"undefined",
|
|
382
|
+
"valueOf"
|
|
383
|
+
]);
|
|
384
|
+
/**
|
|
385
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
386
|
+
*
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* isValidVarName('status') // true
|
|
390
|
+
* isValidVarName('class') // false (reserved word)
|
|
391
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
function isValidVarName(name) {
|
|
395
|
+
if (!name || reservedWords.has(name)) return false;
|
|
396
|
+
return isIdentifier(name);
|
|
281
397
|
}
|
|
282
398
|
/**
|
|
283
|
-
*
|
|
399
|
+
* Returns `name` when it's a syntactically valid JavaScript variable name,
|
|
400
|
+
* otherwise prefixes it with `_` so the result is a valid identifier.
|
|
284
401
|
*
|
|
285
|
-
*
|
|
286
|
-
* `
|
|
402
|
+
* Useful for sanitizing OpenAPI schema names or operation IDs that start with
|
|
403
|
+
* a digit (e.g. `409`, `504AccountCancel`) before using them as exported
|
|
404
|
+
* variable, type, or function names.
|
|
287
405
|
*
|
|
288
|
-
* @example
|
|
289
|
-
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* ensureValidVarName('409') // '_409'
|
|
409
|
+
* ensureValidVarName('504AccountCancel') // '_504AccountCancel'
|
|
410
|
+
* ensureValidVarName('Pet') // 'Pet'
|
|
411
|
+
* ensureValidVarName('class') // '_class'
|
|
412
|
+
* ```
|
|
290
413
|
*/
|
|
291
|
-
function
|
|
292
|
-
|
|
414
|
+
function ensureValidVarName(name) {
|
|
415
|
+
if (!name || isValidVarName(name)) return name;
|
|
416
|
+
return `_${name}`;
|
|
293
417
|
}
|
|
294
418
|
/**
|
|
295
|
-
*
|
|
296
|
-
*
|
|
419
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
420
|
+
*
|
|
421
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
422
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
423
|
+
* deciding whether an object key needs quoting.
|
|
297
424
|
*
|
|
298
425
|
* @example
|
|
299
|
-
*
|
|
426
|
+
* ```ts
|
|
427
|
+
* isIdentifier('name') // true
|
|
428
|
+
* isIdentifier('x-total')// false
|
|
429
|
+
* ```
|
|
300
430
|
*/
|
|
301
|
-
function
|
|
302
|
-
return
|
|
431
|
+
function isIdentifier(name) {
|
|
432
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
433
|
+
}
|
|
434
|
+
// ../../internals/shared/src/resolver.ts
|
|
435
|
+
/**
|
|
436
|
+
* The shared top-level `name` of the operation plugins, computed once per operation name and build.
|
|
437
|
+
*/
|
|
438
|
+
function resolveOperationName(name) {
|
|
439
|
+
return this.memo(`operation:${name}`, () => ensureValidVarName((0, kubb_kit.camelCase)(name)));
|
|
303
440
|
}
|
|
304
|
-
|
|
305
|
-
|
|
441
|
+
/**
|
|
442
|
+
* Builds the allocation key for a declaration an operation owns. `scope` names which declaration
|
|
443
|
+
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
444
|
+
* name. Leave `scope` empty for the operation's own declaration.
|
|
445
|
+
*
|
|
446
|
+
* @example
|
|
447
|
+
* ```ts
|
|
448
|
+
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
function operationKey(node, ...scope) {
|
|
452
|
+
return [
|
|
453
|
+
"operation",
|
|
454
|
+
node.method,
|
|
455
|
+
node.path,
|
|
456
|
+
...scope.map(String)
|
|
457
|
+
];
|
|
458
|
+
}
|
|
459
|
+
// ../../internals/shared/src/group.ts
|
|
306
460
|
/**
|
|
307
461
|
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
308
462
|
* shared default naming so every plugin groups output consistently:
|
|
@@ -325,24 +479,21 @@ function createGroupConfig(group) {
|
|
|
325
479
|
if (!group) return null;
|
|
326
480
|
const defaultName = (ctx) => {
|
|
327
481
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
328
|
-
return camelCase(ctx.group);
|
|
482
|
+
return (0, kubb_kit.camelCase)(ctx.group);
|
|
329
483
|
};
|
|
330
484
|
return {
|
|
331
485
|
...group,
|
|
332
486
|
name: group.name ? group.name : defaultName
|
|
333
487
|
};
|
|
334
488
|
}
|
|
335
|
-
|
|
336
|
-
//#region kubb-extension.json
|
|
489
|
+
// kubb-extension.json
|
|
337
490
|
var compatibility = {
|
|
338
|
-
"kubb": ">=5.
|
|
491
|
+
"kubb": ">=5.14.6",
|
|
339
492
|
"node": ">=22.22"
|
|
340
493
|
};
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
//#endregion
|
|
345
|
-
//#region ../../internals/client/src/resolveOperationTypes.ts
|
|
494
|
+
// package.json
|
|
495
|
+
var version = "5.6.1";
|
|
496
|
+
// ../../internals/client/src/resolveOperationTypes.ts
|
|
346
497
|
/**
|
|
347
498
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
348
499
|
* exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
|
|
@@ -352,6 +503,7 @@ function toZodTypeResolver(resolver) {
|
|
|
352
503
|
return {
|
|
353
504
|
...resolver,
|
|
354
505
|
file: resolver.file.bind(resolver),
|
|
506
|
+
memo: (key, compute) => resolver.memo(`zodTypes:${key}`, compute),
|
|
355
507
|
param: {
|
|
356
508
|
name: (node, p) => type(resolver.param.name(node, p)),
|
|
357
509
|
path: (node, p) => type(resolver.param.path(node, p)),
|
|
@@ -408,8 +560,7 @@ function resolveOperationTypes(driver, pluginName) {
|
|
|
408
560
|
group: pluginZod.options.group ?? void 0
|
|
409
561
|
};
|
|
410
562
|
}
|
|
411
|
-
|
|
412
|
-
//#region ../../internals/client/src/resolveClient.ts
|
|
563
|
+
// ../../internals/client/src/resolveClient.ts
|
|
413
564
|
/**
|
|
414
565
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
415
566
|
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
@@ -519,8 +670,7 @@ function resolveContractClient(options) {
|
|
|
519
670
|
}
|
|
520
671
|
return resolved;
|
|
521
672
|
}
|
|
522
|
-
|
|
523
|
-
//#region ../../internals/client/src/resolveClientOperation.ts
|
|
673
|
+
// ../../internals/client/src/resolveClientOperation.ts
|
|
524
674
|
function isRelativeImport(importPath) {
|
|
525
675
|
return importPath.startsWith(".");
|
|
526
676
|
}
|
|
@@ -567,8 +717,12 @@ function resolveClientOperation(options) {
|
|
|
567
717
|
group: operationOptions.group ?? void 0
|
|
568
718
|
});
|
|
569
719
|
const { importPath } = operationOptions;
|
|
720
|
+
const name = resolver.name(node.operationId);
|
|
570
721
|
return {
|
|
571
|
-
name: resolver.
|
|
722
|
+
name: resolver.resolveName({
|
|
723
|
+
key: operationKey(node),
|
|
724
|
+
name
|
|
725
|
+
}),
|
|
572
726
|
path: file.path,
|
|
573
727
|
clientPath: importPath === void 0 ? node_path.default.resolve(root, ".kubb/client.ts") : resolveImportPath({
|
|
574
728
|
importPath,
|
|
@@ -579,8 +733,7 @@ function resolveClientOperation(options) {
|
|
|
579
733
|
};
|
|
580
734
|
});
|
|
581
735
|
}
|
|
582
|
-
|
|
583
|
-
//#region ../../internals/tanstack-query/src/components/HookFile.tsx
|
|
736
|
+
// ../../internals/tanstack-query/src/components/HookFile.tsx
|
|
584
737
|
/**
|
|
585
738
|
* Resolves what every TanStack hook file shares: its file, the operation types file and names (`@kubb/plugin-ts`, or
|
|
586
739
|
* `pluginZod({ inferred: 'codec' })`), banner and footer.
|
|
@@ -669,8 +822,7 @@ function HookFile({ hook, imports, children }) {
|
|
|
669
822
|
]
|
|
670
823
|
});
|
|
671
824
|
}
|
|
672
|
-
|
|
673
|
-
//#region ../../internals/tanstack-query/src/utils.ts
|
|
825
|
+
// ../../internals/tanstack-query/src/utils.ts
|
|
674
826
|
/**
|
|
675
827
|
* The grouped request options, ordered for both the destructured signature and the
|
|
676
828
|
* call passed to the underlying client.
|
|
@@ -693,7 +845,7 @@ const requestGroupOrder = [
|
|
|
693
845
|
* `MaybeRefOrGetter` per group.
|
|
694
846
|
*/
|
|
695
847
|
function buildGroupedRequestParam(node, options) {
|
|
696
|
-
const { resolver, keys = requestGroupOrder, memberTypeWrapper } = options;
|
|
848
|
+
const { resolver, keys = requestGroupOrder, memberTypeWrapper, withContentType = false } = options;
|
|
697
849
|
const { groups, hasRequiredPath, hasRequiredQuery, hasRequiredHeader } = getRequestGroupOptionality(node);
|
|
698
850
|
const names = keys.filter((key) => groups[key]);
|
|
699
851
|
if (names.length === 0) return null;
|
|
@@ -705,6 +857,11 @@ function buildGroupedRequestParam(node, options) {
|
|
|
705
857
|
};
|
|
706
858
|
const isOptional = names.every((name) => !requiredByGroup[name]);
|
|
707
859
|
const optionsName = resolver.response.options(node);
|
|
860
|
+
if (withContentType && hasRequestContentTypeChoice(node)) return (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
861
|
+
name: "variables",
|
|
862
|
+
type: optionsName,
|
|
863
|
+
optional: false
|
|
864
|
+
});
|
|
708
865
|
const omittedKeys = requestGroupOrder.filter((key) => !keys.includes(key));
|
|
709
866
|
const optionsType = omittedKeys.length > 0 ? `Omit<${optionsName}, ${omittedKeys.map((key) => `'${key}'`).join(" | ")}>` : optionsName;
|
|
710
867
|
if (memberTypeWrapper) {
|
|
@@ -762,14 +919,17 @@ function buildQueryOptionsParams(node, options) {
|
|
|
762
919
|
* ```
|
|
763
920
|
*/
|
|
764
921
|
function buildClientCall(node, options) {
|
|
765
|
-
const { clientName, signal = false, unwrapName } = options;
|
|
922
|
+
const { clientName, signal = false, unwrapName, withContentType = false, returnType = "full" } = options;
|
|
766
923
|
const { groups } = getRequestGroupOptionality(node);
|
|
767
|
-
|
|
924
|
+
const names = requestGroupOrder.filter((key) => groups[key]);
|
|
925
|
+
const args = [
|
|
768
926
|
"...config",
|
|
769
|
-
...
|
|
927
|
+
...withContentType && hasRequestContentTypeChoice(node) ? ["...variables"] : names.map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
|
|
770
928
|
signal ? "signal: config.signal ?? signal" : null,
|
|
771
929
|
"throwOnError: true"
|
|
772
|
-
].filter((part) => part !== null)
|
|
930
|
+
].filter((part) => part !== null);
|
|
931
|
+
if (withContentType && hasRequestContentTypeChoice(node)) return `${clientName}(${castToClientOptions(`{ ${args.join(", ")} }`, clientName, returnType)})`;
|
|
932
|
+
return `${clientName}({ ${args.join(", ")} })`;
|
|
773
933
|
}
|
|
774
934
|
/**
|
|
775
935
|
* Builds the query/mutation body that resolves a `buildClientCall` expression down to the bare
|
|
@@ -838,9 +998,14 @@ function resolveMutationConfig(mutation, options) {
|
|
|
838
998
|
/**
|
|
839
999
|
* Classifies an operation as a query or a mutation from the resolved `query` / `mutation` method lists.
|
|
840
1000
|
* Disabled query and mutation configs do not claim operations, and a method already claimed by an
|
|
841
|
-
* enabled query config never counts as a mutation.
|
|
1001
|
+
* enabled query config never counts as a mutation. Event streams are consumed through the client
|
|
1002
|
+
* directly, so they do not generate query or mutation hooks.
|
|
842
1003
|
*/
|
|
843
1004
|
function classifyOperation(node, { query, mutation }) {
|
|
1005
|
+
if (isEventStream(node)) return {
|
|
1006
|
+
isQuery: false,
|
|
1007
|
+
isMutation: false
|
|
1008
|
+
};
|
|
844
1009
|
const isQuery = !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
845
1010
|
const queryMethods = new Set(query ? query.methods : []);
|
|
846
1011
|
return {
|
|
@@ -888,8 +1053,7 @@ function buildHookParams(node, options) {
|
|
|
888
1053
|
}) : null;
|
|
889
1054
|
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
|
|
890
1055
|
}
|
|
891
|
-
|
|
892
|
-
//#region ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
1056
|
+
// ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
893
1057
|
const declarationPrinter$4 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
894
1058
|
/**
|
|
895
1059
|
* Default `mutationKey` builder. Call it from a custom builder to extend the default key.
|
|
@@ -936,8 +1100,7 @@ function MutationKey({ name, node, transformer, typeName }) {
|
|
|
936
1100
|
})
|
|
937
1101
|
})] });
|
|
938
1102
|
}
|
|
939
|
-
|
|
940
|
-
//#region ../../internals/tanstack-query/src/resolver.ts
|
|
1103
|
+
// ../../internals/tanstack-query/src/resolver.ts
|
|
941
1104
|
/**
|
|
942
1105
|
* Builds the shared query namespace for a variant. Spread the result into
|
|
943
1106
|
* `createResolver` once per variant the plugin supports.
|
|
@@ -953,19 +1116,39 @@ function MutationKey({ name, node, transformer, typeName }) {
|
|
|
953
1116
|
function createQueryResolver(variant = "") {
|
|
954
1117
|
return {
|
|
955
1118
|
name(node) {
|
|
956
|
-
|
|
1119
|
+
const name = `use${capitalize(this.name(node.operationId))}${variant}`;
|
|
1120
|
+
return this.resolveName({
|
|
1121
|
+
key: operationKey(node, "query", variant, "name"),
|
|
1122
|
+
name
|
|
1123
|
+
});
|
|
957
1124
|
},
|
|
958
1125
|
optionsName(node) {
|
|
959
|
-
|
|
1126
|
+
const name = `${this.name(node.operationId)}${variant}QueryOptions`;
|
|
1127
|
+
return this.resolveName({
|
|
1128
|
+
key: operationKey(node, "query", variant, "optionsName"),
|
|
1129
|
+
name
|
|
1130
|
+
});
|
|
960
1131
|
},
|
|
961
1132
|
keyName(node) {
|
|
962
|
-
|
|
1133
|
+
const name = `${this.name(node.operationId)}${variant}QueryKey`;
|
|
1134
|
+
return this.resolveName({
|
|
1135
|
+
key: operationKey(node, "query", variant, "keyName"),
|
|
1136
|
+
name
|
|
1137
|
+
});
|
|
963
1138
|
},
|
|
964
1139
|
keyTypeName(node) {
|
|
965
|
-
|
|
1140
|
+
const name = `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
|
|
1141
|
+
return this.resolveName({
|
|
1142
|
+
key: operationKey(node, "query", variant, "keyTypeName"),
|
|
1143
|
+
name
|
|
1144
|
+
});
|
|
966
1145
|
},
|
|
967
1146
|
clientName(node) {
|
|
968
|
-
|
|
1147
|
+
const name = `${this.name(node.operationId)}${variant}`;
|
|
1148
|
+
return this.resolveName({
|
|
1149
|
+
key: operationKey(node, "query", variant, "clientName"),
|
|
1150
|
+
name
|
|
1151
|
+
});
|
|
969
1152
|
}
|
|
970
1153
|
};
|
|
971
1154
|
}
|
|
@@ -982,15 +1165,22 @@ function createQueryResolver(variant = "") {
|
|
|
982
1165
|
function createMutationResolver() {
|
|
983
1166
|
return {
|
|
984
1167
|
name(node) {
|
|
985
|
-
|
|
1168
|
+
const name = `use${capitalize(this.name(node.operationId))}`;
|
|
1169
|
+
return this.resolveName({
|
|
1170
|
+
key: operationKey(node, "mutation", "name"),
|
|
1171
|
+
name
|
|
1172
|
+
});
|
|
986
1173
|
},
|
|
987
1174
|
keyName(node) {
|
|
988
|
-
|
|
1175
|
+
const name = `${this.name(node.operationId)}MutationKey`;
|
|
1176
|
+
return this.resolveName({
|
|
1177
|
+
key: operationKey(node, "mutation", "keyName"),
|
|
1178
|
+
name
|
|
1179
|
+
});
|
|
989
1180
|
}
|
|
990
1181
|
};
|
|
991
1182
|
}
|
|
992
|
-
|
|
993
|
-
//#region ../../internals/tanstack-query/src/components/QueryKey.tsx
|
|
1183
|
+
// ../../internals/tanstack-query/src/components/QueryKey.tsx
|
|
994
1184
|
const declarationPrinter$3 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
995
1185
|
/**
|
|
996
1186
|
* Default `queryKey` builder. Call it from a custom builder to extend the default key.
|
|
@@ -1048,8 +1238,7 @@ function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "qu
|
|
|
1048
1238
|
})
|
|
1049
1239
|
})] });
|
|
1050
1240
|
}
|
|
1051
|
-
|
|
1052
|
-
//#region src/components/Mutation.tsx
|
|
1241
|
+
// src/components/Mutation.tsx
|
|
1053
1242
|
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1054
1243
|
const callPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1055
1244
|
function buildMutationParamsNode(node, options) {
|
|
@@ -1067,14 +1256,19 @@ function buildMutationParamsNode(node, options) {
|
|
|
1067
1256
|
}
|
|
1068
1257
|
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, returnType = "full" }) {
|
|
1069
1258
|
const { TData, TError } = buildResponseTypes(node, tsResolver);
|
|
1070
|
-
const groupedParam = buildGroupedRequestParam(node, {
|
|
1259
|
+
const groupedParam = buildGroupedRequestParam(node, {
|
|
1260
|
+
resolver: tsResolver,
|
|
1261
|
+
withContentType: true
|
|
1262
|
+
});
|
|
1071
1263
|
const hasMutationParams = groupedParam !== null;
|
|
1072
1264
|
const groupedParamsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: groupedParam ? [groupedParam] : [] });
|
|
1073
1265
|
const argTypeBody = hasMutationParams ? tsResolver.response.options(node) : "";
|
|
1074
1266
|
const argBindingStr = hasMutationParams ? callPrinter$1.print(groupedParamsNode) ?? "" : "";
|
|
1075
1267
|
const mutationFnBody = buildCallResultBody(buildClientCall(node, {
|
|
1076
1268
|
clientName,
|
|
1077
|
-
signal: false
|
|
1269
|
+
signal: false,
|
|
1270
|
+
withContentType: true,
|
|
1271
|
+
returnType
|
|
1078
1272
|
}), { returnType });
|
|
1079
1273
|
const generics = [
|
|
1080
1274
|
TData,
|
|
@@ -1122,8 +1316,7 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1122
1316
|
})
|
|
1123
1317
|
})] });
|
|
1124
1318
|
}
|
|
1125
|
-
|
|
1126
|
-
//#region src/generators/mutationGenerator.tsx
|
|
1319
|
+
// src/generators/mutationGenerator.tsx
|
|
1127
1320
|
/**
|
|
1128
1321
|
* Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
|
|
1129
1322
|
* per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
|
|
@@ -1192,8 +1385,7 @@ const mutationGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1192
1385
|
});
|
|
1193
1386
|
}
|
|
1194
1387
|
});
|
|
1195
|
-
|
|
1196
|
-
//#region src/components/Query.tsx
|
|
1388
|
+
// src/components/Query.tsx
|
|
1197
1389
|
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1198
1390
|
const callPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1199
1391
|
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
|
|
@@ -1247,8 +1439,7 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1247
1439
|
})
|
|
1248
1440
|
});
|
|
1249
1441
|
}
|
|
1250
|
-
|
|
1251
|
-
//#region src/components/QueryOptions.tsx
|
|
1442
|
+
// src/components/QueryOptions.tsx
|
|
1252
1443
|
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1253
1444
|
function QueryOptions({ name, clientName, node, tsResolver, returnType = "full" }) {
|
|
1254
1445
|
const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
@@ -1275,8 +1466,7 @@ function QueryOptions({ name, clientName, node, tsResolver, returnType = "full"
|
|
|
1275
1466
|
})
|
|
1276
1467
|
});
|
|
1277
1468
|
}
|
|
1278
|
-
|
|
1279
|
-
//#region src/generators/queryGenerator.tsx
|
|
1469
|
+
// src/generators/queryGenerator.tsx
|
|
1280
1470
|
/**
|
|
1281
1471
|
* Built-in generator for `useSWR` hooks. Emits one `useFoo` hook per read operation plus the
|
|
1282
1472
|
* matching `fooQueryKey` helper and `fooQueryOptions` factory.
|
|
@@ -1356,14 +1546,13 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1356
1546
|
});
|
|
1357
1547
|
}
|
|
1358
1548
|
});
|
|
1359
|
-
|
|
1360
|
-
//#region src/resolvers/resolverSwr.ts
|
|
1549
|
+
// src/resolvers/resolverSwr.ts
|
|
1361
1550
|
/**
|
|
1362
1551
|
* Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
|
|
1363
1552
|
* SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
|
|
1364
1553
|
* `fooMutationKey`).
|
|
1365
1554
|
*
|
|
1366
|
-
* Functions
|
|
1555
|
+
* Functions use safe camelCase identifiers; hooks get the `use` prefix.
|
|
1367
1556
|
*
|
|
1368
1557
|
* @example Resolve hook and helper names
|
|
1369
1558
|
* ```ts
|
|
@@ -1376,19 +1565,27 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1376
1565
|
*/
|
|
1377
1566
|
const resolverSwr = (0, kubb_kit.createResolver)({
|
|
1378
1567
|
pluginName: "plugin-swr",
|
|
1568
|
+
name: resolveOperationName,
|
|
1379
1569
|
query: createQueryResolver(),
|
|
1380
1570
|
mutation: {
|
|
1381
1571
|
...createMutationResolver(),
|
|
1382
1572
|
keyTypeName(node) {
|
|
1383
|
-
|
|
1573
|
+
const name = `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1574
|
+
return this.resolveName({
|
|
1575
|
+
key: operationKey(node, "mutation", "keyTypeName"),
|
|
1576
|
+
name
|
|
1577
|
+
});
|
|
1384
1578
|
},
|
|
1385
1579
|
argTypeName(node) {
|
|
1386
|
-
|
|
1580
|
+
const name = `${capitalize(this.name(node.operationId))}MutationArg`;
|
|
1581
|
+
return this.resolveName({
|
|
1582
|
+
key: operationKey(node, "mutation", "argTypeName"),
|
|
1583
|
+
name
|
|
1584
|
+
});
|
|
1387
1585
|
}
|
|
1388
1586
|
}
|
|
1389
1587
|
});
|
|
1390
|
-
|
|
1391
|
-
//#region src/plugin.ts
|
|
1588
|
+
// src/plugin.ts
|
|
1392
1589
|
/**
|
|
1393
1590
|
* Canonical plugin name for `@kubb/plugin-swr`. Used for driver lookups and cross-plugin
|
|
1394
1591
|
* dependency references.
|
|
@@ -1451,7 +1648,6 @@ const pluginSwr = (0, kubb_kit.defineExtension)({
|
|
|
1451
1648
|
};
|
|
1452
1649
|
}
|
|
1453
1650
|
});
|
|
1454
|
-
//#endregion
|
|
1455
1651
|
exports.default = pluginSwr;
|
|
1456
1652
|
exports.mutationKeyTransformer = mutationKeyTransformer;
|
|
1457
1653
|
exports.pluginSwr = pluginSwr;
|