@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.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { Diagnostics, Resolver, Url, addGenerator, ast, createResolver, defineExtension, defineGenerator, setMacros, setResolver, useConfig } from "kubb/kit";
|
|
1
|
+
import { Diagnostics, Resolver, Url, addGenerator, ast, camelCase, createResolver, defineExtension, defineGenerator, setMacros, setResolver, useConfig } from "kubb/kit";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { createFunctionParameter, createFunctionParameters, createObjectBindingPattern, createTypeLiteral, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
4
4
|
import { pluginZodName } from "@kubb/plugin-zod";
|
|
5
5
|
import { File, Function, Type, jsxRenderer } from "kubb/jsx";
|
|
6
6
|
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
7
|
-
|
|
8
|
-
//#region ../../internals/shared/src/operation.ts
|
|
7
|
+
// ../../internals/shared/src/operation.ts
|
|
9
8
|
/**
|
|
10
9
|
* Builds the `ResolverFileParams` every operation generator passes to
|
|
11
10
|
* `resolver.file`: a file named `name`, tagged by the operation's first
|
|
@@ -74,6 +73,24 @@ function getContentTypeInfo(node) {
|
|
|
74
73
|
function getResponseContentTypeInfo(node) {
|
|
75
74
|
return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
|
|
76
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Reads the single base content type of an operation's primary success response, lowercased and
|
|
78
|
+
* stripped of any `; charset=...` suffix. Returns `undefined` when the response declares zero or
|
|
79
|
+
* more than one content type, since neither case has a single type to act on.
|
|
80
|
+
*/
|
|
81
|
+
function getPrimarySuccessContentType(node) {
|
|
82
|
+
const contentTypes = getPrimarySuccessResponse(node)?.content?.map((entry) => entry.contentType) ?? [];
|
|
83
|
+
if (contentTypes.length !== 1) return void 0;
|
|
84
|
+
return contentTypes[0].split(";")[0].trim().toLowerCase();
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether an operation streams its primary success response as Server-Sent Events
|
|
88
|
+
* (`text/event-stream`). The client generator uses this to return a typed event stream instead of a
|
|
89
|
+
* one-shot `RequestResult`.
|
|
90
|
+
*/
|
|
91
|
+
function isEventStream(node) {
|
|
92
|
+
return getPrimarySuccessContentType(node) === "text/event-stream";
|
|
93
|
+
}
|
|
77
94
|
const requestConfigBaseType = `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`;
|
|
78
95
|
function buildRequestConfigType(node) {
|
|
79
96
|
const request = getContentTypeInfo(node);
|
|
@@ -165,6 +182,22 @@ function getOperationParameters(node) {
|
|
|
165
182
|
operationParameterGroupsByNode.set(node, groups);
|
|
166
183
|
return groups;
|
|
167
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
187
|
+
* referencing the already-resolved body and grouped param names. Shared by `@kubb/plugin-ts`'s
|
|
188
|
+
* `Options` type and `@kubb/plugin-zod`'s inferred options schema, so both printers emit the same
|
|
189
|
+
* shape from the same inputs. `primitive: 'object'` is a no-op for the TS printer and tells the Zod
|
|
190
|
+
* printer to emit `z.object(…)` rather than a record.
|
|
191
|
+
*/
|
|
192
|
+
function hasRequestContentTypeChoice(node) {
|
|
193
|
+
return (node.requestBody?.content ?? []).filter((entry) => entry.schema).length > 1;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Casts a call argument to the client's parameter type; a `'plain'` client is non-generic.
|
|
197
|
+
*/
|
|
198
|
+
function castToClientOptions(callArg, clientName, returnType = "full") {
|
|
199
|
+
return `${callArg} as Parameters<typeof ${clientName}${returnType === "plain" ? "" : "<true>"}>[0]`;
|
|
200
|
+
}
|
|
168
201
|
function getStatusCodeNumber(statusCode) {
|
|
169
202
|
const code = Number(statusCode);
|
|
170
203
|
return Number.isNaN(code) ? null : code;
|
|
@@ -195,88 +228,209 @@ function resolveSuccessNames(node, resolver) {
|
|
|
195
228
|
function resolveStatusCodeNames(node, resolver) {
|
|
196
229
|
return node.responses.map((response) => resolver.response.status(node, response.statusCode));
|
|
197
230
|
}
|
|
198
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Lists the type names an operation's types file declares, computed once per resolver and `options` so every
|
|
233
|
+
* dependent reads the same list.
|
|
234
|
+
*/
|
|
199
235
|
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
const
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
...responseStatusNames
|
|
222
|
-
] : [
|
|
223
|
-
...paramNames,
|
|
224
|
-
...bodyAndResponseNames,
|
|
225
|
-
...responseStatusNames
|
|
226
|
-
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
227
|
-
byResolver.set(cacheKey, result);
|
|
228
|
-
return result;
|
|
236
|
+
const key = `${node.operationId}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${options.includeParams === false ? "noparams" : ""}\0${(options.exclude ?? []).join(",")}`;
|
|
237
|
+
return resolver.memo(`operationTypeNames:${key}`, () => {
|
|
238
|
+
const { path, query, header } = getOperationParameters(node);
|
|
239
|
+
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
240
|
+
const exclude = new Set(options.exclude ?? []);
|
|
241
|
+
const paramNames = options.includeParams === false ? [] : [
|
|
242
|
+
...path.map((param) => resolver.param.path(node, param)),
|
|
243
|
+
...query.map((param) => resolver.param.query(node, param)),
|
|
244
|
+
...header.map((param) => resolver.param.headers(node, param))
|
|
245
|
+
];
|
|
246
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.response.body(node) : null, resolver.response.response(node)];
|
|
247
|
+
return (options.order === "body-response-first" ? [
|
|
248
|
+
...bodyAndResponseNames,
|
|
249
|
+
...paramNames,
|
|
250
|
+
...responseStatusNames
|
|
251
|
+
] : [
|
|
252
|
+
...paramNames,
|
|
253
|
+
...bodyAndResponseNames,
|
|
254
|
+
...responseStatusNames
|
|
255
|
+
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
256
|
+
});
|
|
229
257
|
}
|
|
230
|
-
|
|
231
|
-
//#region ../../internals/utils/src/casing.ts
|
|
258
|
+
// ../../internals/utils/src/casing.ts
|
|
232
259
|
/**
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
* and capitalizes each word according to `pascal`.
|
|
260
|
+
* Uppercases only the first character of `text`, leaving the rest untouched.
|
|
261
|
+
* Unlike `pascalCase` it never re-splits word boundaries or strips characters.
|
|
236
262
|
*
|
|
237
|
-
*
|
|
263
|
+
* @example
|
|
264
|
+
* `capitalize('getPetById') // 'GetPetById'`
|
|
238
265
|
*/
|
|
239
|
-
function
|
|
240
|
-
return text.
|
|
241
|
-
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
242
|
-
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
243
|
-
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
266
|
+
function capitalize(text) {
|
|
267
|
+
return `${text.charAt(0).toUpperCase()}${text.slice(1)}`;
|
|
244
268
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
269
|
+
// ../../internals/utils/src/reserved.ts
|
|
270
|
+
/**
|
|
271
|
+
* JavaScript and Java reserved words.
|
|
272
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
273
|
+
*/
|
|
274
|
+
const reservedWords = /* @__PURE__ */ new Set([
|
|
275
|
+
"abstract",
|
|
276
|
+
"arguments",
|
|
277
|
+
"await",
|
|
278
|
+
"boolean",
|
|
279
|
+
"break",
|
|
280
|
+
"byte",
|
|
281
|
+
"case",
|
|
282
|
+
"catch",
|
|
283
|
+
"char",
|
|
284
|
+
"class",
|
|
285
|
+
"const",
|
|
286
|
+
"continue",
|
|
287
|
+
"debugger",
|
|
288
|
+
"default",
|
|
289
|
+
"delete",
|
|
290
|
+
"do",
|
|
291
|
+
"double",
|
|
292
|
+
"else",
|
|
293
|
+
"enum",
|
|
294
|
+
"eval",
|
|
295
|
+
"export",
|
|
296
|
+
"extends",
|
|
297
|
+
"false",
|
|
298
|
+
"final",
|
|
299
|
+
"finally",
|
|
300
|
+
"float",
|
|
301
|
+
"for",
|
|
302
|
+
"function",
|
|
303
|
+
"goto",
|
|
304
|
+
"if",
|
|
305
|
+
"implements",
|
|
306
|
+
"import",
|
|
307
|
+
"in",
|
|
308
|
+
"instanceof",
|
|
309
|
+
"int",
|
|
310
|
+
"interface",
|
|
311
|
+
"let",
|
|
312
|
+
"long",
|
|
313
|
+
"native",
|
|
314
|
+
"new",
|
|
315
|
+
"null",
|
|
316
|
+
"package",
|
|
317
|
+
"private",
|
|
318
|
+
"protected",
|
|
319
|
+
"public",
|
|
320
|
+
"return",
|
|
321
|
+
"short",
|
|
322
|
+
"static",
|
|
323
|
+
"super",
|
|
324
|
+
"switch",
|
|
325
|
+
"synchronized",
|
|
326
|
+
"this",
|
|
327
|
+
"throw",
|
|
328
|
+
"throws",
|
|
329
|
+
"transient",
|
|
330
|
+
"true",
|
|
331
|
+
"try",
|
|
332
|
+
"typeof",
|
|
333
|
+
"var",
|
|
334
|
+
"void",
|
|
335
|
+
"volatile",
|
|
336
|
+
"while",
|
|
337
|
+
"with",
|
|
338
|
+
"yield",
|
|
339
|
+
"Array",
|
|
340
|
+
"Date",
|
|
341
|
+
"hasOwnProperty",
|
|
342
|
+
"Infinity",
|
|
343
|
+
"isFinite",
|
|
344
|
+
"isNaN",
|
|
345
|
+
"isPrototypeOf",
|
|
346
|
+
"length",
|
|
347
|
+
"Math",
|
|
348
|
+
"name",
|
|
349
|
+
"NaN",
|
|
350
|
+
"Number",
|
|
351
|
+
"Object",
|
|
352
|
+
"prototype",
|
|
353
|
+
"String",
|
|
354
|
+
"toString",
|
|
355
|
+
"undefined",
|
|
356
|
+
"valueOf"
|
|
357
|
+
]);
|
|
358
|
+
/**
|
|
359
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
362
|
+
* ```ts
|
|
363
|
+
* isValidVarName('status') // true
|
|
364
|
+
* isValidVarName('class') // false (reserved word)
|
|
365
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
function isValidVarName(name) {
|
|
369
|
+
if (!name || reservedWords.has(name)) return false;
|
|
370
|
+
return isIdentifier(name);
|
|
255
371
|
}
|
|
256
372
|
/**
|
|
257
|
-
*
|
|
373
|
+
* Returns `name` when it's a syntactically valid JavaScript variable name,
|
|
374
|
+
* otherwise prefixes it with `_` so the result is a valid identifier.
|
|
258
375
|
*
|
|
259
|
-
*
|
|
260
|
-
* `
|
|
376
|
+
* Useful for sanitizing OpenAPI schema names or operation IDs that start with
|
|
377
|
+
* a digit (e.g. `409`, `504AccountCancel`) before using them as exported
|
|
378
|
+
* variable, type, or function names.
|
|
261
379
|
*
|
|
262
|
-
* @example
|
|
263
|
-
*
|
|
380
|
+
* @example
|
|
381
|
+
* ```ts
|
|
382
|
+
* ensureValidVarName('409') // '_409'
|
|
383
|
+
* ensureValidVarName('504AccountCancel') // '_504AccountCancel'
|
|
384
|
+
* ensureValidVarName('Pet') // 'Pet'
|
|
385
|
+
* ensureValidVarName('class') // '_class'
|
|
386
|
+
* ```
|
|
264
387
|
*/
|
|
265
|
-
function
|
|
266
|
-
|
|
388
|
+
function ensureValidVarName(name) {
|
|
389
|
+
if (!name || isValidVarName(name)) return name;
|
|
390
|
+
return `_${name}`;
|
|
267
391
|
}
|
|
268
392
|
/**
|
|
269
|
-
*
|
|
270
|
-
*
|
|
393
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
394
|
+
*
|
|
395
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
396
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
397
|
+
* deciding whether an object key needs quoting.
|
|
271
398
|
*
|
|
272
399
|
* @example
|
|
273
|
-
*
|
|
400
|
+
* ```ts
|
|
401
|
+
* isIdentifier('name') // true
|
|
402
|
+
* isIdentifier('x-total')// false
|
|
403
|
+
* ```
|
|
274
404
|
*/
|
|
275
|
-
function
|
|
276
|
-
return
|
|
405
|
+
function isIdentifier(name) {
|
|
406
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
407
|
+
}
|
|
408
|
+
// ../../internals/shared/src/resolver.ts
|
|
409
|
+
/**
|
|
410
|
+
* The shared top-level `name` of the operation plugins, computed once per operation name and build.
|
|
411
|
+
*/
|
|
412
|
+
function resolveOperationName(name) {
|
|
413
|
+
return this.memo(`operation:${name}`, () => ensureValidVarName(camelCase(name)));
|
|
277
414
|
}
|
|
278
|
-
|
|
279
|
-
|
|
415
|
+
/**
|
|
416
|
+
* Builds the allocation key for a declaration an operation owns. `scope` names which declaration
|
|
417
|
+
* it is, so two of them never share an allocation even when a custom resolver gives both the same
|
|
418
|
+
* name. Leave `scope` empty for the operation's own declaration.
|
|
419
|
+
*
|
|
420
|
+
* @example
|
|
421
|
+
* ```ts
|
|
422
|
+
* resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
|
|
423
|
+
* ```
|
|
424
|
+
*/
|
|
425
|
+
function operationKey(node, ...scope) {
|
|
426
|
+
return [
|
|
427
|
+
"operation",
|
|
428
|
+
node.method,
|
|
429
|
+
node.path,
|
|
430
|
+
...scope.map(String)
|
|
431
|
+
];
|
|
432
|
+
}
|
|
433
|
+
// ../../internals/shared/src/group.ts
|
|
280
434
|
/**
|
|
281
435
|
* Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
|
|
282
436
|
* shared default naming so every plugin groups output consistently:
|
|
@@ -306,17 +460,14 @@ function createGroupConfig(group) {
|
|
|
306
460
|
name: group.name ? group.name : defaultName
|
|
307
461
|
};
|
|
308
462
|
}
|
|
309
|
-
|
|
310
|
-
//#region kubb-extension.json
|
|
463
|
+
// kubb-extension.json
|
|
311
464
|
var compatibility = {
|
|
312
|
-
"kubb": ">=5.
|
|
465
|
+
"kubb": ">=5.14.6",
|
|
313
466
|
"node": ">=22.22"
|
|
314
467
|
};
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
//#endregion
|
|
319
|
-
//#region ../../internals/client/src/resolveOperationTypes.ts
|
|
468
|
+
// package.json
|
|
469
|
+
var version = "5.6.1";
|
|
470
|
+
// ../../internals/client/src/resolveOperationTypes.ts
|
|
320
471
|
/**
|
|
321
472
|
* A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
|
|
322
473
|
* exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
|
|
@@ -326,6 +477,7 @@ function toZodTypeResolver(resolver) {
|
|
|
326
477
|
return {
|
|
327
478
|
...resolver,
|
|
328
479
|
file: resolver.file.bind(resolver),
|
|
480
|
+
memo: (key, compute) => resolver.memo(`zodTypes:${key}`, compute),
|
|
329
481
|
param: {
|
|
330
482
|
name: (node, p) => type(resolver.param.name(node, p)),
|
|
331
483
|
path: (node, p) => type(resolver.param.path(node, p)),
|
|
@@ -382,8 +534,7 @@ function resolveOperationTypes(driver, pluginName) {
|
|
|
382
534
|
group: pluginZod.options.group ?? void 0
|
|
383
535
|
};
|
|
384
536
|
}
|
|
385
|
-
|
|
386
|
-
//#region ../../internals/client/src/resolveClient.ts
|
|
537
|
+
// ../../internals/client/src/resolveClient.ts
|
|
387
538
|
/**
|
|
388
539
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
389
540
|
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
@@ -493,8 +644,7 @@ function resolveContractClient(options) {
|
|
|
493
644
|
}
|
|
494
645
|
return resolved;
|
|
495
646
|
}
|
|
496
|
-
|
|
497
|
-
//#region ../../internals/client/src/resolveClientOperation.ts
|
|
647
|
+
// ../../internals/client/src/resolveClientOperation.ts
|
|
498
648
|
function isRelativeImport(importPath) {
|
|
499
649
|
return importPath.startsWith(".");
|
|
500
650
|
}
|
|
@@ -541,8 +691,12 @@ function resolveClientOperation(options) {
|
|
|
541
691
|
group: operationOptions.group ?? void 0
|
|
542
692
|
});
|
|
543
693
|
const { importPath } = operationOptions;
|
|
694
|
+
const name = resolver.name(node.operationId);
|
|
544
695
|
return {
|
|
545
|
-
name: resolver.
|
|
696
|
+
name: resolver.resolveName({
|
|
697
|
+
key: operationKey(node),
|
|
698
|
+
name
|
|
699
|
+
}),
|
|
546
700
|
path: file.path,
|
|
547
701
|
clientPath: importPath === void 0 ? path.resolve(root, ".kubb/client.ts") : resolveImportPath({
|
|
548
702
|
importPath,
|
|
@@ -553,8 +707,7 @@ function resolveClientOperation(options) {
|
|
|
553
707
|
};
|
|
554
708
|
});
|
|
555
709
|
}
|
|
556
|
-
|
|
557
|
-
//#region ../../internals/tanstack-query/src/components/HookFile.tsx
|
|
710
|
+
// ../../internals/tanstack-query/src/components/HookFile.tsx
|
|
558
711
|
/**
|
|
559
712
|
* Resolves what every TanStack hook file shares: its file, the operation types file and names (`@kubb/plugin-ts`, or
|
|
560
713
|
* `pluginZod({ inferred: 'codec' })`), banner and footer.
|
|
@@ -643,8 +796,7 @@ function HookFile({ hook, imports, children }) {
|
|
|
643
796
|
]
|
|
644
797
|
});
|
|
645
798
|
}
|
|
646
|
-
|
|
647
|
-
//#region ../../internals/tanstack-query/src/utils.ts
|
|
799
|
+
// ../../internals/tanstack-query/src/utils.ts
|
|
648
800
|
/**
|
|
649
801
|
* The grouped request options, ordered for both the destructured signature and the
|
|
650
802
|
* call passed to the underlying client.
|
|
@@ -667,7 +819,7 @@ const requestGroupOrder = [
|
|
|
667
819
|
* `MaybeRefOrGetter` per group.
|
|
668
820
|
*/
|
|
669
821
|
function buildGroupedRequestParam(node, options) {
|
|
670
|
-
const { resolver, keys = requestGroupOrder, memberTypeWrapper } = options;
|
|
822
|
+
const { resolver, keys = requestGroupOrder, memberTypeWrapper, withContentType = false } = options;
|
|
671
823
|
const { groups, hasRequiredPath, hasRequiredQuery, hasRequiredHeader } = getRequestGroupOptionality(node);
|
|
672
824
|
const names = keys.filter((key) => groups[key]);
|
|
673
825
|
if (names.length === 0) return null;
|
|
@@ -679,6 +831,11 @@ function buildGroupedRequestParam(node, options) {
|
|
|
679
831
|
};
|
|
680
832
|
const isOptional = names.every((name) => !requiredByGroup[name]);
|
|
681
833
|
const optionsName = resolver.response.options(node);
|
|
834
|
+
if (withContentType && hasRequestContentTypeChoice(node)) return createFunctionParameter({
|
|
835
|
+
name: "variables",
|
|
836
|
+
type: optionsName,
|
|
837
|
+
optional: false
|
|
838
|
+
});
|
|
682
839
|
const omittedKeys = requestGroupOrder.filter((key) => !keys.includes(key));
|
|
683
840
|
const optionsType = omittedKeys.length > 0 ? `Omit<${optionsName}, ${omittedKeys.map((key) => `'${key}'`).join(" | ")}>` : optionsName;
|
|
684
841
|
if (memberTypeWrapper) {
|
|
@@ -736,14 +893,17 @@ function buildQueryOptionsParams(node, options) {
|
|
|
736
893
|
* ```
|
|
737
894
|
*/
|
|
738
895
|
function buildClientCall(node, options) {
|
|
739
|
-
const { clientName, signal = false, unwrapName } = options;
|
|
896
|
+
const { clientName, signal = false, unwrapName, withContentType = false, returnType = "full" } = options;
|
|
740
897
|
const { groups } = getRequestGroupOptionality(node);
|
|
741
|
-
|
|
898
|
+
const names = requestGroupOrder.filter((key) => groups[key]);
|
|
899
|
+
const args = [
|
|
742
900
|
"...config",
|
|
743
|
-
...
|
|
901
|
+
...withContentType && hasRequestContentTypeChoice(node) ? ["...variables"] : names.map((name) => unwrapName ? `${name}: ${unwrapName(name)}` : name),
|
|
744
902
|
signal ? "signal: config.signal ?? signal" : null,
|
|
745
903
|
"throwOnError: true"
|
|
746
|
-
].filter((part) => part !== null)
|
|
904
|
+
].filter((part) => part !== null);
|
|
905
|
+
if (withContentType && hasRequestContentTypeChoice(node)) return `${clientName}(${castToClientOptions(`{ ${args.join(", ")} }`, clientName, returnType)})`;
|
|
906
|
+
return `${clientName}({ ${args.join(", ")} })`;
|
|
747
907
|
}
|
|
748
908
|
/**
|
|
749
909
|
* Builds the query/mutation body that resolves a `buildClientCall` expression down to the bare
|
|
@@ -812,9 +972,14 @@ function resolveMutationConfig(mutation, options) {
|
|
|
812
972
|
/**
|
|
813
973
|
* Classifies an operation as a query or a mutation from the resolved `query` / `mutation` method lists.
|
|
814
974
|
* Disabled query and mutation configs do not claim operations, and a method already claimed by an
|
|
815
|
-
* enabled query config never counts as a mutation.
|
|
975
|
+
* enabled query config never counts as a mutation. Event streams are consumed through the client
|
|
976
|
+
* directly, so they do not generate query or mutation hooks.
|
|
816
977
|
*/
|
|
817
978
|
function classifyOperation(node, { query, mutation }) {
|
|
979
|
+
if (isEventStream(node)) return {
|
|
980
|
+
isQuery: false,
|
|
981
|
+
isMutation: false
|
|
982
|
+
};
|
|
818
983
|
const isQuery = !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
819
984
|
const queryMethods = new Set(query ? query.methods : []);
|
|
820
985
|
return {
|
|
@@ -862,8 +1027,7 @@ function buildHookParams(node, options) {
|
|
|
862
1027
|
}) : null;
|
|
863
1028
|
return createFunctionParameters({ params: [groupedParam, optionsParam].filter((param) => param !== null) });
|
|
864
1029
|
}
|
|
865
|
-
|
|
866
|
-
//#region ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
1030
|
+
// ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
867
1031
|
const declarationPrinter$4 = functionPrinter({ mode: "declaration" });
|
|
868
1032
|
/**
|
|
869
1033
|
* Default `mutationKey` builder. Call it from a custom builder to extend the default key.
|
|
@@ -910,8 +1074,7 @@ function MutationKey({ name, node, transformer, typeName }) {
|
|
|
910
1074
|
})
|
|
911
1075
|
})] });
|
|
912
1076
|
}
|
|
913
|
-
|
|
914
|
-
//#region ../../internals/tanstack-query/src/resolver.ts
|
|
1077
|
+
// ../../internals/tanstack-query/src/resolver.ts
|
|
915
1078
|
/**
|
|
916
1079
|
* Builds the shared query namespace for a variant. Spread the result into
|
|
917
1080
|
* `createResolver` once per variant the plugin supports.
|
|
@@ -927,19 +1090,39 @@ function MutationKey({ name, node, transformer, typeName }) {
|
|
|
927
1090
|
function createQueryResolver(variant = "") {
|
|
928
1091
|
return {
|
|
929
1092
|
name(node) {
|
|
930
|
-
|
|
1093
|
+
const name = `use${capitalize(this.name(node.operationId))}${variant}`;
|
|
1094
|
+
return this.resolveName({
|
|
1095
|
+
key: operationKey(node, "query", variant, "name"),
|
|
1096
|
+
name
|
|
1097
|
+
});
|
|
931
1098
|
},
|
|
932
1099
|
optionsName(node) {
|
|
933
|
-
|
|
1100
|
+
const name = `${this.name(node.operationId)}${variant}QueryOptions`;
|
|
1101
|
+
return this.resolveName({
|
|
1102
|
+
key: operationKey(node, "query", variant, "optionsName"),
|
|
1103
|
+
name
|
|
1104
|
+
});
|
|
934
1105
|
},
|
|
935
1106
|
keyName(node) {
|
|
936
|
-
|
|
1107
|
+
const name = `${this.name(node.operationId)}${variant}QueryKey`;
|
|
1108
|
+
return this.resolveName({
|
|
1109
|
+
key: operationKey(node, "query", variant, "keyName"),
|
|
1110
|
+
name
|
|
1111
|
+
});
|
|
937
1112
|
},
|
|
938
1113
|
keyTypeName(node) {
|
|
939
|
-
|
|
1114
|
+
const name = `${capitalize(this.name(node.operationId))}${variant}QueryKey`;
|
|
1115
|
+
return this.resolveName({
|
|
1116
|
+
key: operationKey(node, "query", variant, "keyTypeName"),
|
|
1117
|
+
name
|
|
1118
|
+
});
|
|
940
1119
|
},
|
|
941
1120
|
clientName(node) {
|
|
942
|
-
|
|
1121
|
+
const name = `${this.name(node.operationId)}${variant}`;
|
|
1122
|
+
return this.resolveName({
|
|
1123
|
+
key: operationKey(node, "query", variant, "clientName"),
|
|
1124
|
+
name
|
|
1125
|
+
});
|
|
943
1126
|
}
|
|
944
1127
|
};
|
|
945
1128
|
}
|
|
@@ -956,15 +1139,22 @@ function createQueryResolver(variant = "") {
|
|
|
956
1139
|
function createMutationResolver() {
|
|
957
1140
|
return {
|
|
958
1141
|
name(node) {
|
|
959
|
-
|
|
1142
|
+
const name = `use${capitalize(this.name(node.operationId))}`;
|
|
1143
|
+
return this.resolveName({
|
|
1144
|
+
key: operationKey(node, "mutation", "name"),
|
|
1145
|
+
name
|
|
1146
|
+
});
|
|
960
1147
|
},
|
|
961
1148
|
keyName(node) {
|
|
962
|
-
|
|
1149
|
+
const name = `${this.name(node.operationId)}MutationKey`;
|
|
1150
|
+
return this.resolveName({
|
|
1151
|
+
key: operationKey(node, "mutation", "keyName"),
|
|
1152
|
+
name
|
|
1153
|
+
});
|
|
963
1154
|
}
|
|
964
1155
|
};
|
|
965
1156
|
}
|
|
966
|
-
|
|
967
|
-
//#region ../../internals/tanstack-query/src/components/QueryKey.tsx
|
|
1157
|
+
// ../../internals/tanstack-query/src/components/QueryKey.tsx
|
|
968
1158
|
const declarationPrinter$3 = functionPrinter({ mode: "declaration" });
|
|
969
1159
|
/**
|
|
970
1160
|
* Default `queryKey` builder. Call it from a custom builder to extend the default key.
|
|
@@ -1022,8 +1212,7 @@ function QueryKey({ name, node, tsResolver, typeName, transformer, variant = "qu
|
|
|
1022
1212
|
})
|
|
1023
1213
|
})] });
|
|
1024
1214
|
}
|
|
1025
|
-
|
|
1026
|
-
//#region src/components/Mutation.tsx
|
|
1215
|
+
// src/components/Mutation.tsx
|
|
1027
1216
|
const declarationPrinter$2 = functionPrinter({ mode: "declaration" });
|
|
1028
1217
|
const callPrinter$1 = functionPrinter({ mode: "call" });
|
|
1029
1218
|
function buildMutationParamsNode(node, options) {
|
|
@@ -1041,14 +1230,19 @@ function buildMutationParamsNode(node, options) {
|
|
|
1041
1230
|
}
|
|
1042
1231
|
function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, mutationArgTypeName, node, tsResolver, returnType = "full" }) {
|
|
1043
1232
|
const { TData, TError } = buildResponseTypes(node, tsResolver);
|
|
1044
|
-
const groupedParam = buildGroupedRequestParam(node, {
|
|
1233
|
+
const groupedParam = buildGroupedRequestParam(node, {
|
|
1234
|
+
resolver: tsResolver,
|
|
1235
|
+
withContentType: true
|
|
1236
|
+
});
|
|
1045
1237
|
const hasMutationParams = groupedParam !== null;
|
|
1046
1238
|
const groupedParamsNode = createFunctionParameters({ params: groupedParam ? [groupedParam] : [] });
|
|
1047
1239
|
const argTypeBody = hasMutationParams ? tsResolver.response.options(node) : "";
|
|
1048
1240
|
const argBindingStr = hasMutationParams ? callPrinter$1.print(groupedParamsNode) ?? "" : "";
|
|
1049
1241
|
const mutationFnBody = buildCallResultBody(buildClientCall(node, {
|
|
1050
1242
|
clientName,
|
|
1051
|
-
signal: false
|
|
1243
|
+
signal: false,
|
|
1244
|
+
withContentType: true,
|
|
1245
|
+
returnType
|
|
1052
1246
|
}), { returnType });
|
|
1053
1247
|
const generics = [
|
|
1054
1248
|
TData,
|
|
@@ -1096,8 +1290,7 @@ function Mutation({ name, clientName, mutationKeyName, mutationKeyTypeName, muta
|
|
|
1096
1290
|
})
|
|
1097
1291
|
})] });
|
|
1098
1292
|
}
|
|
1099
|
-
|
|
1100
|
-
//#region src/generators/mutationGenerator.tsx
|
|
1293
|
+
// src/generators/mutationGenerator.tsx
|
|
1101
1294
|
/**
|
|
1102
1295
|
* Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
|
|
1103
1296
|
* per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
|
|
@@ -1166,8 +1359,7 @@ const mutationGenerator = defineGenerator({
|
|
|
1166
1359
|
});
|
|
1167
1360
|
}
|
|
1168
1361
|
});
|
|
1169
|
-
|
|
1170
|
-
//#region src/components/Query.tsx
|
|
1362
|
+
// src/components/Query.tsx
|
|
1171
1363
|
const declarationPrinter$1 = functionPrinter({ mode: "declaration" });
|
|
1172
1364
|
const callPrinter = functionPrinter({ mode: "call" });
|
|
1173
1365
|
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver }) {
|
|
@@ -1221,8 +1413,7 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
|
|
|
1221
1413
|
})
|
|
1222
1414
|
});
|
|
1223
1415
|
}
|
|
1224
|
-
|
|
1225
|
-
//#region src/components/QueryOptions.tsx
|
|
1416
|
+
// src/components/QueryOptions.tsx
|
|
1226
1417
|
const declarationPrinter = functionPrinter({ mode: "declaration" });
|
|
1227
1418
|
function QueryOptions({ name, clientName, node, tsResolver, returnType = "full" }) {
|
|
1228
1419
|
const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
|
|
@@ -1249,8 +1440,7 @@ function QueryOptions({ name, clientName, node, tsResolver, returnType = "full"
|
|
|
1249
1440
|
})
|
|
1250
1441
|
});
|
|
1251
1442
|
}
|
|
1252
|
-
|
|
1253
|
-
//#region src/generators/queryGenerator.tsx
|
|
1443
|
+
// src/generators/queryGenerator.tsx
|
|
1254
1444
|
/**
|
|
1255
1445
|
* Built-in generator for `useSWR` hooks. Emits one `useFoo` hook per read operation plus the
|
|
1256
1446
|
* matching `fooQueryKey` helper and `fooQueryOptions` factory.
|
|
@@ -1330,14 +1520,13 @@ const queryGenerator = defineGenerator({
|
|
|
1330
1520
|
});
|
|
1331
1521
|
}
|
|
1332
1522
|
});
|
|
1333
|
-
|
|
1334
|
-
//#region src/resolvers/resolverSwr.ts
|
|
1523
|
+
// src/resolvers/resolverSwr.ts
|
|
1335
1524
|
/**
|
|
1336
1525
|
* Default resolver used by `@kubb/plugin-swr`. Decides the names and file paths for every generated
|
|
1337
1526
|
* SWR hook (`useFoo`, `useFooMutation`) and its companion helpers (`fooQueryKey`, `fooQueryOptions`,
|
|
1338
1527
|
* `fooMutationKey`).
|
|
1339
1528
|
*
|
|
1340
|
-
* Functions
|
|
1529
|
+
* Functions use safe camelCase identifiers; hooks get the `use` prefix.
|
|
1341
1530
|
*
|
|
1342
1531
|
* @example Resolve hook and helper names
|
|
1343
1532
|
* ```ts
|
|
@@ -1350,19 +1539,27 @@ const queryGenerator = defineGenerator({
|
|
|
1350
1539
|
*/
|
|
1351
1540
|
const resolverSwr = createResolver({
|
|
1352
1541
|
pluginName: "plugin-swr",
|
|
1542
|
+
name: resolveOperationName,
|
|
1353
1543
|
query: createQueryResolver(),
|
|
1354
1544
|
mutation: {
|
|
1355
1545
|
...createMutationResolver(),
|
|
1356
1546
|
keyTypeName(node) {
|
|
1357
|
-
|
|
1547
|
+
const name = `${capitalize(this.name(node.operationId))}MutationKey`;
|
|
1548
|
+
return this.resolveName({
|
|
1549
|
+
key: operationKey(node, "mutation", "keyTypeName"),
|
|
1550
|
+
name
|
|
1551
|
+
});
|
|
1358
1552
|
},
|
|
1359
1553
|
argTypeName(node) {
|
|
1360
|
-
|
|
1554
|
+
const name = `${capitalize(this.name(node.operationId))}MutationArg`;
|
|
1555
|
+
return this.resolveName({
|
|
1556
|
+
key: operationKey(node, "mutation", "argTypeName"),
|
|
1557
|
+
name
|
|
1558
|
+
});
|
|
1361
1559
|
}
|
|
1362
1560
|
}
|
|
1363
1561
|
});
|
|
1364
|
-
|
|
1365
|
-
//#region src/plugin.ts
|
|
1562
|
+
// src/plugin.ts
|
|
1366
1563
|
/**
|
|
1367
1564
|
* Canonical plugin name for `@kubb/plugin-swr`. Used for driver lookups and cross-plugin
|
|
1368
1565
|
* dependency references.
|
|
@@ -1425,7 +1622,6 @@ const pluginSwr = defineExtension({
|
|
|
1425
1622
|
};
|
|
1426
1623
|
}
|
|
1427
1624
|
});
|
|
1428
|
-
//#endregion
|
|
1429
1625
|
export { pluginSwr as default, pluginSwr, mutationKeyTransformer, pluginSwrName, queryKeyTransformer, resolverSwr };
|
|
1430
1626
|
|
|
1431
1627
|
//# sourceMappingURL=index.js.map
|