@kubb/plugin-client 5.8.0-canary.20261025T201031 → 5.8.0

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.js CHANGED
@@ -4,8 +4,7 @@ import { createFunctionParameter, createFunctionParameters, functionPrinter, plu
4
4
  import { pluginZodName } from "@kubb/plugin-zod";
5
5
  import { File, Function, jsxRenderer } from "kubb/jsx";
6
6
  import { jsx, jsxs } from "kubb/jsx/jsx-runtime";
7
- //#endregion
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
@@ -183,6 +182,16 @@ function getOperationParameters(node) {
183
182
  operationParameterGroupsByNode.set(node, groups);
184
183
  return groups;
185
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
+ }
186
195
  function getStatusCodeNumber(statusCode) {
187
196
  const code = Number(statusCode);
188
197
  return Number.isNaN(code) ? null : code;
@@ -200,8 +209,7 @@ function getOperationSuccessResponses(node) {
200
209
  function getPrimarySuccessResponse(node) {
201
210
  return getOperationSuccessResponses(node)[0] ?? null;
202
211
  }
203
- //#endregion
204
- //#region ../../internals/utils/src/casing.ts
212
+ // ../../internals/utils/src/casing.ts
205
213
  /**
206
214
  * Shared implementation for camelCase and PascalCase conversion.
207
215
  * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
@@ -250,8 +258,7 @@ function camelCase(text, { prefix = "", suffix = "" } = {}) {
250
258
  function pascalCase(text, { prefix = "", suffix = "" } = {}) {
251
259
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
252
260
  }
253
- //#endregion
254
- //#region ../../internals/utils/src/reserved.ts
261
+ // ../../internals/utils/src/reserved.ts
255
262
  /**
256
263
  * JavaScript and Java reserved words.
257
264
  * @link https://github.com/jonschlinkert/reserved/blob/master/index.js
@@ -259,6 +266,7 @@ function pascalCase(text, { prefix = "", suffix = "" } = {}) {
259
266
  const reservedWords = /* @__PURE__ */ new Set([
260
267
  "abstract",
261
268
  "arguments",
269
+ "await",
262
270
  "boolean",
263
271
  "break",
264
272
  "byte",
@@ -389,8 +397,29 @@ function ensureValidVarName(name) {
389
397
  function isIdentifier(name) {
390
398
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
391
399
  }
392
- //#endregion
393
- //#region ../../internals/shared/src/group.ts
400
+ // ../../internals/shared/src/resolver.ts
401
+ function resolveOperationName(name) {
402
+ return ensureValidVarName(camelCase(name));
403
+ }
404
+ /**
405
+ * Builds the allocation key for a declaration an operation owns. `scope` names which declaration
406
+ * it is, so two of them never share an allocation even when a custom resolver gives both the same
407
+ * name. Leave `scope` empty for the operation's own declaration.
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * resolver.resolveName({ key: operationKey(node, 'response', 'status', 404), name })
412
+ * ```
413
+ */
414
+ function operationKey(node, ...scope) {
415
+ return [
416
+ "operation",
417
+ node.method,
418
+ node.path,
419
+ ...scope.map(String)
420
+ ];
421
+ }
422
+ // ../../internals/shared/src/group.ts
394
423
  /**
395
424
  * Builds the `group` config a Kubb plugin passes to `ctx.setOptions`, applying the
396
425
  * shared default naming so every plugin groups output consistently:
@@ -420,17 +449,14 @@ function createGroupConfig(group) {
420
449
  name: group.name ? group.name : defaultName
421
450
  };
422
451
  }
423
- //#endregion
424
- //#region kubb-extension.json
452
+ // kubb-extension.json
425
453
  var compatibility = {
426
- "kubb": ">=5.8.3",
454
+ "kubb": ">=5.14.6",
427
455
  "node": ">=22.22"
428
456
  };
429
- //#endregion
430
- //#region package.json
431
- var version = "5.8.0-canary.20261025T201031";
432
- //#endregion
433
- //#region ../../internals/client/src/builders/validatorOptions.ts
457
+ // package.json
458
+ var version = "5.8.0";
459
+ // ../../internals/client/src/builders/validatorOptions.ts
434
460
  /**
435
461
  * Returns `true` when any direction of the validator uses zod (used for dependency checks).
436
462
  */
@@ -473,8 +499,7 @@ function buildZodErrorParse(node, zodResolver) {
473
499
  if (!node.responses.some((res) => !isSuccessStatusCode(res.statusCode) && res.content?.some((entry) => entry.schema))) return null;
474
500
  return zodResolver.response.error?.(node) || null;
475
501
  }
476
- //#endregion
477
- //#region ../../internals/client/src/builders/security.ts
502
+ // ../../internals/client/src/builders/security.ts
478
503
  function serializeAuth(auth) {
479
504
  const parts = [`type: '${auth.type}'`];
480
505
  if (auth.scheme) parts.push(`scheme: '${auth.scheme}'`);
@@ -545,8 +570,7 @@ function buildSecurityMetadata({ security }) {
545
570
  if (!security?.length) return null;
546
571
  return `[${security.map(serializeAuth).join(", ")}]`;
547
572
  }
548
- //#endregion
549
- //#region ../../internals/client/src/builders/validator.ts
573
+ // ../../internals/client/src/builders/validator.ts
550
574
  /**
551
575
  * Builds the validator-hook references for one operation. Request body and params validation run before the send.
552
576
  * Response validation runs on the success body only. Returns `null` references when the matching
@@ -588,17 +612,16 @@ function buildValidatorLiteral(hooks, keys) {
588
612
  const entries = keys.flatMap((key) => hooks?.[key] ? [`${key}: ${hooks[key]}`] : []);
589
613
  return entries.length ? `validator: { ${entries.join(", ")} }` : null;
590
614
  }
591
- //#endregion
592
- //#region ../../internals/client/src/builders/contentType.ts
615
+ // ../../internals/client/src/builders/contentType.ts
593
616
  /**
594
- * Builds the `contentType` entry baked into the call config, or `null` for the JSON default.
617
+ * Builds the `contentType` entry baked into the call config, or `null` without a request body.
595
618
  *
596
619
  * `merge` is `true` when the generated function must destructure `contentType` from `options` and
597
620
  * merge it over the baked request default, because the caller can also pick a response content type.
598
621
  */
599
622
  function buildContentType({ node }) {
600
623
  const { defaultContentType } = getContentTypeInfo(node);
601
- const requestContentType = Boolean(node.requestBody?.content?.[0]?.schema) && defaultContentType !== "application/json" ? defaultContentType : null;
624
+ const requestContentType = Boolean(node.requestBody?.content?.[0]?.schema) ? defaultContentType : null;
602
625
  if (!requestContentType) return {
603
626
  literal: null,
604
627
  merge: false
@@ -609,8 +632,7 @@ function buildContentType({ node }) {
609
632
  literal: merge ? `contentType: { request: '${requestContentType}', ...(typeof contentType === 'string' ? { request: contentType } : contentType) }` : `contentType: { request: '${requestContentType}' }`
610
633
  };
611
634
  }
612
- //#endregion
613
- //#region ../../internals/client/src/builders/generics.ts
635
+ // ../../internals/client/src/builders/generics.ts
614
636
  /**
615
637
  * Builds the `RequestResult` generic arguments for one operation: the per-status responses record
616
638
  * plus the per-call `ThrowOnError` flag. `SuccessOf` / `ErrorOf` split the record inside the
@@ -641,8 +663,7 @@ function buildResultType({ node, types, returnType }) {
641
663
  if (returnType === "plain") return `Promise<RequestResult<${generics}>>`;
642
664
  return `Unwrappable<RequestResult<${generics}>>`;
643
665
  }
644
- //#endregion
645
- //#region ../../internals/client/src/builders/returnStatement.ts
666
+ // ../../internals/client/src/builders/returnStatement.ts
646
667
  /**
647
668
  * Builds the return statement of a generated operation function. With the default
648
669
  * `returnType: 'full'` the runtime call already resolves to `{ data, error, request, response }`.
@@ -666,22 +687,22 @@ function buildResultType({ node, types, returnType }) {
666
687
  * `return request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>`
667
688
  */
668
689
  function buildReturnStatement({ node, types, callConfig, returnType, throwOnErrorDefault }) {
669
- if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
690
+ const request = returnType !== "plain" && hasRequestContentTypeChoice(node) ? `request<${types.response.options(node)}['body']>` : "request";
691
+ if (returnType === "data") return `return unwrapResult(${request}(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
670
692
  node,
671
693
  types,
672
694
  returnType
673
695
  })}`;
674
- if (returnType === "plain") return `return request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
696
+ if (returnType === "plain") return `return ${request}(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
675
697
  node,
676
698
  types
677
699
  })}>>`;
678
- return `return withUnwrap(request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
700
+ return `return withUnwrap(${request}(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
679
701
  node,
680
702
  types
681
703
  })}>>)`;
682
704
  }
683
- //#endregion
684
- //#region ../../internals/client/src/builders/signature.ts
705
+ // ../../internals/client/src/builders/signature.ts
685
706
  const declarationPrinter = functionPrinter({ mode: "declaration" });
686
707
  /**
687
708
  * Builds the grouped-options signature for one operation: a single `options` object whose `TData`
@@ -709,8 +730,7 @@ function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDef
709
730
  generics: [`ThrowOnError extends boolean = ${throwOnErrorDefault}`]
710
731
  };
711
732
  }
712
- //#endregion
713
- //#region ../../internals/client/src/builders/styles.ts
733
+ // ../../internals/client/src/builders/styles.ts
714
734
  /**
715
735
  * Renders a parameter name as an object-literal key, quoted when it is not a bare identifier. Every
716
736
  * location keeps the spec name: the runtime looks path styles up by the `{name}` placeholder in the
@@ -782,8 +802,7 @@ function buildStyles({ node }) {
782
802
  const entries = [...Object.keys(groups).filter((location) => groups[location].length > 0).map((location) => `${location}: { ${groups[location].join(", ")} }`), serializeBodyEncoding(node)].filter(Boolean);
783
803
  return entries.length > 0 ? `{ ${entries.join(", ")} }` : null;
784
804
  }
785
- //#endregion
786
- //#region ../../internals/client/src/components/Operation.tsx
805
+ // ../../internals/client/src/components/Operation.tsx
787
806
  /**
788
807
  * Renders one client operation: the grouped `<Name>Request` type and the function that forwards a
789
808
  * single `options` object to the resolved client and returns the `Unwrappable<RequestResult>`. The
@@ -860,8 +879,7 @@ function Operation({ name, node, types, zodResolver, validator, returnType, thro
860
879
  })
861
880
  });
862
881
  }
863
- //#endregion
864
- //#region ../../internals/client/src/resolveOperationTypes.ts
882
+ // ../../internals/client/src/resolveOperationTypes.ts
865
883
  /**
866
884
  * A `plugin-ts` shaped resolver whose operation type names point at the types `pluginZod({ inferred })`
867
885
  * exports next to each schema, so code written against `plugin-ts` names picks up the zod types.
@@ -927,8 +945,7 @@ function resolveOperationTypes(driver, pluginName) {
927
945
  group: pluginZod.options.group ?? void 0
928
946
  };
929
947
  }
930
- //#endregion
931
- //#region ../../internals/client/src/generators/clientGenerator.tsx
948
+ // ../../internals/client/src/generators/clientGenerator.tsx
932
949
  /**
933
950
  * Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`,
934
951
  * `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared
@@ -955,8 +972,12 @@ function createClientGenerator(name, resolveClientPath) {
955
972
  zodResolver
956
973
  });
957
974
  const importedZodNames = validatorHooks.importedZodNames;
975
+ const operationName = resolver.name(node.operationId);
958
976
  const meta = {
959
- name: resolver.name(node.operationId),
977
+ name: resolver.resolveName({
978
+ key: operationKey(node),
979
+ name: operationName
980
+ }),
960
981
  file: resolver.file({
961
982
  ...operationFileEntry(node, node.operationId),
962
983
  root,
@@ -1071,19 +1092,17 @@ function createClientGenerator(name, resolveClientPath) {
1071
1092
  }
1072
1093
  });
1073
1094
  }
1074
- //#endregion
1075
- //#region ../../internals/client/src/macros.ts
1095
+ // ../../internals/client/src/macros.ts
1076
1096
  /**
1077
1097
  * Macros the client plugins apply by default, ahead of any user macros. `macroSimplifyUnion`
1078
1098
  * drops union members a broader scalar already covers, keeping the generated response and error
1079
1099
  * unions tidy. A plugin wires them with `ctx.setMacros([...defaultMacros, ...userMacros])`.
1080
1100
  */
1081
1101
  const defaultMacros = [macroSimplifyUnion];
1082
- //#endregion
1083
- //#region ../../internals/client/src/resolver.ts
1102
+ // ../../internals/client/src/resolver.ts
1084
1103
  /**
1085
- * Default resolver shared by the client plugins. Functions and files inherit the built-in camelCase
1086
- * `name` and `file`; classes and tag groups use PascalCase.
1104
+ * Default resolver shared by the client plugins. Functions use safe camelCase identifiers and
1105
+ * files inherit the built-in casing; classes and tag groups use PascalCase.
1087
1106
  *
1088
1107
  * @example
1089
1108
  * ```ts
@@ -1093,6 +1112,7 @@ const defaultMacros = [macroSimplifyUnion];
1093
1112
  */
1094
1113
  const resolverClient = createResolver({
1095
1114
  pluginName: "plugin-contract-client",
1115
+ name: resolveOperationName,
1096
1116
  className(name) {
1097
1117
  return ensureValidVarName(pascalCase(name));
1098
1118
  },
@@ -1103,16 +1123,14 @@ const resolverClient = createResolver({
1103
1123
  return ensureValidVarName(camelCase(name));
1104
1124
  }
1105
1125
  });
1106
- //#endregion
1107
- //#region src/generators/clientGenerator.tsx
1126
+ // src/generators/clientGenerator.tsx
1108
1127
  /**
1109
1128
  * Built-in operation generator for `@kubb/plugin-client`. Emits one async function per OpenAPI
1110
1129
  * operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
1111
1130
  * that forwards a single `options` object to the user-provided client module and returns the `RequestResult`.
1112
1131
  */
1113
1132
  const clientGenerator = createClientGenerator("client", (options) => options.importPath);
1114
- //#endregion
1115
- //#region src/plugin.ts
1133
+ // src/plugin.ts
1116
1134
  /**
1117
1135
  * Canonical plugin name for `@kubb/plugin-client`.
1118
1136
  */
@@ -1163,7 +1181,6 @@ const pluginClient = defineExtension({
1163
1181
  };
1164
1182
  }
1165
1183
  });
1166
- //#endregion
1167
1184
  export { pluginClient as default, pluginClient, pluginClientName };
1168
1185
 
1169
1186
  //# sourceMappingURL=index.js.map