@kubb/plugin-client 5.8.0-canary.20261025T201031 → 5.8.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 CHANGED
@@ -2,7 +2,7 @@ Object.defineProperties(exports, {
2
2
  __esModule: { value: true },
3
3
  [Symbol.toStringTag]: { value: "Module" }
4
4
  });
5
- //#region \0rolldown/runtime.js
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
- //#region ../../internals/shared/src/operation.ts
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
@@ -209,6 +208,16 @@ function getOperationParameters(node) {
209
208
  operationParameterGroupsByNode.set(node, groups);
210
209
  return groups;
211
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
+ }
212
221
  function getStatusCodeNumber(statusCode) {
213
222
  const code = Number(statusCode);
214
223
  return Number.isNaN(code) ? null : code;
@@ -226,58 +235,7 @@ function getOperationSuccessResponses(node) {
226
235
  function getPrimarySuccessResponse(node) {
227
236
  return getOperationSuccessResponses(node)[0] ?? null;
228
237
  }
229
- //#endregion
230
- //#region ../../internals/utils/src/casing.ts
231
- /**
232
- * Shared implementation for camelCase and PascalCase conversion.
233
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
234
- * and capitalizes each word according to `pascal`.
235
- *
236
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
237
- */
238
- function convert(text, pascal) {
239
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
240
- if (word.length > 1 && word === word.toUpperCase()) return word;
241
- return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
242
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
243
- }
244
- const cache = /* @__PURE__ */ new Map();
245
- const cacheLimit = 1e4;
246
- function toCamelOrPascal(text, pascal) {
247
- const key = `${pascal ? "P" : "c"}${text}`;
248
- const cached = cache.get(key);
249
- if (cached !== void 0) return cached;
250
- const result = convert(text, pascal);
251
- if (cache.size >= cacheLimit) cache.clear();
252
- cache.set(key, result);
253
- return result;
254
- }
255
- /**
256
- * Converts `text` to camelCase.
257
- *
258
- * @example Word boundaries
259
- * `camelCase('hello-world') // 'helloWorld'`
260
- *
261
- * @example With a prefix
262
- * `camelCase('tag', { prefix: 'create' }) // 'createTag'`
263
- */
264
- function camelCase(text, { prefix = "", suffix = "" } = {}) {
265
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
266
- }
267
- /**
268
- * Converts `text` to PascalCase.
269
- *
270
- * @example Word boundaries
271
- * `pascalCase('hello-world') // 'HelloWorld'`
272
- *
273
- * @example With a suffix
274
- * `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
275
- */
276
- function pascalCase(text, { prefix = "", suffix = "" } = {}) {
277
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
278
- }
279
- //#endregion
280
- //#region ../../internals/utils/src/reserved.ts
238
+ // ../../internals/utils/src/reserved.ts
281
239
  /**
282
240
  * JavaScript and Java reserved words.
283
241
  * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
@@ -285,6 +243,7 @@ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
285
243
  const reservedWords = /* @__PURE__ */ new Set([
286
244
  "abstract",
287
245
  "arguments",
246
+ "await",
288
247
  "boolean",
289
248
  "break",
290
249
  "byte",
@@ -415,8 +374,32 @@ function ensureValidVarName(name) {
415
374
  function isIdentifier(name) {
416
375
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
417
376
  }
418
- //#endregion
419
- //#region ../../internals/shared/src/group.ts
377
+ // ../../internals/shared/src/resolver.ts
378
+ /**
379
+ * The shared top-level `name` of the operation plugins, computed once per operation name and build.
380
+ */
381
+ function resolveOperationName(name) {
382
+ return this.memo(`operation:${name}`, () => ensureValidVarName((0, kubb_kit.camelCase)(name)));
383
+ }
384
+ /**
385
+ * Builds the allocation key for a declaration an operation owns. `scope` names which declaration
386
+ * it is, so two of them never share an allocation even when a custom resolver gives both the same
387
+ * name. Leave `scope` empty for the operation's own declaration.
388
+ *
389
+ * @example
390
+ * ```ts
391
+ * resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
392
+ * ```
393
+ */
394
+ function operationKey(node, ...scope) {
395
+ return [
396
+ "operation",
397
+ node.method,
398
+ node.path,
399
+ ...scope.map(String)
400
+ ];
401
+ }
402
+ // ../../internals/shared/src/group.ts
420
403
  /**
421
404
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
422
405
  * shared default naming so every plugin groups output consistently:
@@ -439,24 +422,21 @@ function createGroupConfig(group) {
439
422
  if (!group) return null;
440
423
  const defaultName = (ctx) => {
441
424
  if (group.type === "path") return `${ctx.group.split("/")[1]}`;
442
- return camelCase(ctx.group);
425
+ return (0, kubb_kit.camelCase)(ctx.group);
443
426
  };
444
427
  return {
445
428
  ...group,
446
429
  name: group.name ? group.name : defaultName
447
430
  };
448
431
  }
449
- //#endregion
450
- //#region kubb-extension.json
432
+ // kubb-extension.json
451
433
  var compatibility = {
452
- "kubb": ">=5.8.3",
434
+ "kubb": ">=5.14.6",
453
435
  "node": ">=22.22"
454
436
  };
455
- //#endregion
456
- //#region package.json
457
- var version = "5.8.0-canary.20261025T201031";
458
- //#endregion
459
- //#region ../../internals/client/src/builders/validatorOptions.ts
437
+ // package.json
438
+ var version = "5.8.1";
439
+ // ../../internals/client/src/builders/validatorOptions.ts
460
440
  /**
461
441
  * Returns `true` when any direction of the validator uses zod (used for dependency checks).
462
442
  */
@@ -499,8 +479,7 @@ function buildZodErrorParse(node, zodResolver) {
499
479
  if (!node.responses.some((res) => !isSuccessStatusCode(res.statusCode) && res.content?.some((entry) => entry.schema))) return null;
500
480
  return zodResolver.response.error?.(node) || null;
501
481
  }
502
- //#endregion
503
- //#region ../../internals/client/src/builders/security.ts
482
+ // ../../internals/client/src/builders/security.ts
504
483
  function serializeAuth(auth) {
505
484
  const parts = [`type: '${auth.type}'`];
506
485
  if (auth.scheme) parts.push(`scheme: '${auth.scheme}'`);
@@ -571,8 +550,7 @@ function buildSecurityMetadata({ security }) {
571
550
  if (!security?.length) return null;
572
551
  return `[${security.map(serializeAuth).join(", ")}]`;
573
552
  }
574
- //#endregion
575
- //#region ../../internals/client/src/builders/validator.ts
553
+ // ../../internals/client/src/builders/validator.ts
576
554
  /**
577
555
  * Builds the validator-hook references for one operation. Request body and params validation run before the send.
578
556
  * Response validation runs on the success body only. Returns `null` references when the matching
@@ -614,17 +592,16 @@ function buildValidatorLiteral(hooks, keys) {
614
592
  const entries = keys.flatMap((key) => hooks?.[key] ? [`${key}: ${hooks[key]}`] : []);
615
593
  return entries.length ? `validator: { ${entries.join(", ")} }` : null;
616
594
  }
617
- //#endregion
618
- //#region ../../internals/client/src/builders/contentType.ts
595
+ // ../../internals/client/src/builders/contentType.ts
619
596
  /**
620
- * Builds the `contentType` entry baked into the call config, or `null` for the JSON default.
597
+ * Builds the `contentType` entry baked into the call config, or `null` without a request body.
621
598
  *
622
599
  * `merge` is `true` when the generated function must destructure `contentType` from `options` and
623
600
  * merge it over the baked request default, because the caller can also pick a response content type.
624
601
  */
625
602
  function buildContentType({ node }) {
626
603
  const { defaultContentType } = getContentTypeInfo(node);
627
- const requestContentType = Boolean(node.requestBody?.content?.[0]?.schema) && defaultContentType !== "application/json" ? defaultContentType : null;
604
+ const requestContentType = Boolean(node.requestBody?.content?.[0]?.schema) ? defaultContentType : null;
628
605
  if (!requestContentType) return {
629
606
  literal: null,
630
607
  merge: false
@@ -635,8 +612,7 @@ function buildContentType({ node }) {
635
612
  literal: merge ? `contentType: { request: '${requestContentType}', ...(typeof contentType === 'string' ? { request: contentType } : contentType) }` : `contentType: { request: '${requestContentType}' }`
636
613
  };
637
614
  }
638
- //#endregion
639
- //#region ../../internals/client/src/builders/generics.ts
615
+ // ../../internals/client/src/builders/generics.ts
640
616
  /**
641
617
  * Builds the `RequestResult` generic arguments for one operation: the per-status responses record
642
618
  * plus the per-call `ThrowOnError` flag. `SuccessOf` / `ErrorOf` split the record inside the
@@ -667,8 +643,7 @@ function buildResultType({ node, types, returnType }) {
667
643
  if (returnType === "plain") return `Promise<RequestResult<${generics}>>`;
668
644
  return `Unwrappable<RequestResult<${generics}>>`;
669
645
  }
670
- //#endregion
671
- //#region ../../internals/client/src/builders/returnStatement.ts
646
+ // ../../internals/client/src/builders/returnStatement.ts
672
647
  /**
673
648
  * Builds the return statement of a generated operation function. With the default
674
649
  * `returnType: 'full'` the runtime call already resolves to `{ data, error, request, response }`.
@@ -692,22 +667,22 @@ function buildResultType({ node, types, returnType }) {
692
667
  * `return request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>`
693
668
  */
694
669
  function buildReturnStatement({ node, types, callConfig, returnType, throwOnErrorDefault }) {
695
- if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
670
+ const request = returnType !== "plain" && hasRequestContentTypeChoice(node) ? `request<${types.response.options(node)}['body']>` : "request";
671
+ if (returnType === "data") return `return unwrapResult(${request}(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
696
672
  node,
697
673
  types,
698
674
  returnType
699
675
  })}`;
700
- if (returnType === "plain") return `return request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
676
+ if (returnType === "plain") return `return ${request}(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
701
677
  node,
702
678
  types
703
679
  })}>>`;
704
- return `return withUnwrap(request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
680
+ return `return withUnwrap(${request}(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
705
681
  node,
706
682
  types
707
683
  })}>>)`;
708
684
  }
709
- //#endregion
710
- //#region ../../internals/client/src/builders/signature.ts
685
+ // ../../internals/client/src/builders/signature.ts
711
686
  const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
712
687
  /**
713
688
  * Builds the grouped-options signature for one operation: a single `options` object whose `TData`
@@ -735,8 +710,7 @@ function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDef
735
710
  generics: [`ThrowOnError extends boolean = ${throwOnErrorDefault}`]
736
711
  };
737
712
  }
738
- //#endregion
739
- //#region ../../internals/client/src/builders/styles.ts
713
+ // ../../internals/client/src/builders/styles.ts
740
714
  /**
741
715
  * Renders a parameter name as an object-literal key, quoted when it is not a bare identifier. Every
742
716
  * location keeps the spec name: the runtime looks path styles up by the `{name}` placeholder in the
@@ -808,8 +782,7 @@ function buildStyles({ node }) {
808
782
  const entries = [...Object.keys(groups).filter((location) => groups[location].length > 0).map((location) => `${location}: { ${groups[location].join(", ")} }`), serializeBodyEncoding(node)].filter(Boolean);
809
783
  return entries.length > 0 ? `{ ${entries.join(", ")} }` : null;
810
784
  }
811
- //#endregion
812
- //#region ../../internals/client/src/components/Operation.tsx
785
+ // ../../internals/client/src/components/Operation.tsx
813
786
  /**
814
787
  * Renders one client operation: the grouped `<Name>Request` type and the function that forwards a
815
788
  * single `options` object to the resolved client and returns the `Unwrappable<RequestResult>`. The
@@ -886,8 +859,7 @@ function Operation({ name, node, types, zodResolver, validator, returnType, thro
886
859
  })
887
860
  });
888
861
  }
889
- //#endregion
890
- //#region ../../internals/client/src/resolveOperationTypes.ts
862
+ // ../../internals/client/src/resolveOperationTypes.ts
891
863
  /**
892
864
  * A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
893
865
  * exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
@@ -897,6 +869,7 @@ function toZodTypeResolver(resolver) {
897
869
  return {
898
870
  ...resolver,
899
871
  file: resolver.file.bind(resolver),
872
+ memo: (key, compute) => resolver.memo(`zodTypes:${key}`, compute),
900
873
  param: {
901
874
  name: (node, p) => type(resolver.param.name(node, p)),
902
875
  path: (node, p) => type(resolver.param.path(node, p)),
@@ -953,8 +926,7 @@ function resolveOperationTypes(driver, pluginName) {
953
926
  group: pluginZod.options.group ?? void 0
954
927
  };
955
928
  }
956
- //#endregion
957
- //#region ../../internals/client/src/generators/clientGenerator.tsx
929
+ // ../../internals/client/src/generators/clientGenerator.tsx
958
930
  /**
959
931
  * Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`,
960
932
  * `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared
@@ -981,8 +953,12 @@ function createClientGenerator(name, resolveClientPath) {
981
953
  zodResolver
982
954
  });
983
955
  const importedZodNames = validatorHooks.importedZodNames;
956
+ const operationName = resolver.name(node.operationId);
984
957
  const meta = {
985
- name: resolver.name(node.operationId),
958
+ name: resolver.resolveName({
959
+ key: operationKey(node),
960
+ name: operationName
961
+ }),
986
962
  file: resolver.file({
987
963
  ...operationFileEntry(node, node.operationId),
988
964
  root,
@@ -1097,19 +1073,17 @@ function createClientGenerator(name, resolveClientPath) {
1097
1073
  }
1098
1074
  });
1099
1075
  }
1100
- //#endregion
1101
- //#region ../../internals/client/src/macros.ts
1076
+ // ../../internals/client/src/macros.ts
1102
1077
  /**
1103
1078
  * Macros the client plugins apply by default, ahead of any user macros. `macroSimplifyUnion`
1104
1079
  * drops union members a broader scalar already covers, keeping the generated response and error
1105
1080
  * unions tidy. A plugin wires them with `ctx.setMacros([...defaultMacros, ...userMacros])`.
1106
1081
  */
1107
1082
  const defaultMacros = [kubb_kit.macroSimplifyUnion];
1108
- //#endregion
1109
- //#region ../../internals/client/src/resolver.ts
1083
+ // ../../internals/client/src/resolver.ts
1110
1084
  /**
1111
- * Default resolver shared by the client plugins. Functions and files inherit the built-in camelCase
1112
- * `name` and `file`; classes and tag groups use PascalCase.
1085
+ * Default resolver shared by the client plugins. Functions use safe camelCase identifiers and
1086
+ * files inherit the built-in casing; classes and tag groups use PascalCase.
1113
1087
  *
1114
1088
  * @example
1115
1089
  * ```ts
@@ -1119,26 +1093,25 @@ const defaultMacros = [kubb_kit.macroSimplifyUnion];
1119
1093
  */
1120
1094
  const resolverClient = (0, kubb_kit.createResolver)({
1121
1095
  pluginName: "plugin-contract-client",
1096
+ name: resolveOperationName,
1122
1097
  className(name) {
1123
- return ensureValidVarName(pascalCase(name));
1098
+ return ensureValidVarName((0, kubb_kit.pascalCase)(name));
1124
1099
  },
1125
1100
  groupName(name) {
1126
- return ensureValidVarName(pascalCase(`${name} Client`));
1101
+ return ensureValidVarName((0, kubb_kit.pascalCase)(`${name} Client`));
1127
1102
  },
1128
1103
  propertyName(name) {
1129
- return ensureValidVarName(camelCase(name));
1104
+ return ensureValidVarName((0, kubb_kit.camelCase)(name));
1130
1105
  }
1131
1106
  });
1132
- //#endregion
1133
- //#region src/generators/clientGenerator.tsx
1107
+ // src/generators/clientGenerator.tsx
1134
1108
  /**
1135
1109
  * Built-in operation generator for `@kubb/plugin-client`. Emits one async function per OpenAPI
1136
1110
  * operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
1137
1111
  * that forwards a single `options` object to the user-provided client module and returns the `RequestResult`.
1138
1112
  */
1139
1113
  const clientGenerator = createClientGenerator("client", (options) => options.importPath);
1140
- //#endregion
1141
- //#region src/plugin.ts
1114
+ // src/plugin.ts
1142
1115
  /**
1143
1116
  * Canonical plugin name for `@kubb/plugin-client`.
1144
1117
  */
@@ -1189,7 +1162,6 @@ const pluginClient = (0, kubb_kit.defineExtension)({
1189
1162
  };
1190
1163
  }
1191
1164
  });
1192
- //#endregion
1193
1165
  exports.default = pluginClient;
1194
1166
  exports.pluginClient = pluginClient;
1195
1167
  exports.pluginClientName = pluginClientName;