@kubb/plugin-fetch 5.2.0-canary.20260902T141305 → 5.2.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
@@ -454,17 +454,23 @@ function buildRequestResultGenerics({ node, types }) {
454
454
  //#region ../../internals/client/src/builders/returnStatement.ts
455
455
  /**
456
456
  * Builds the return statement of a generated operation function. The runtime call already resolves
457
- * to `{ data, error, request, response }`; the generated code forwards that result and casts it to
458
- * the operation's `RequestResult`, which carries the `throwOnError` discrimination.
457
+ * to `{ data, error, request, response }`. The generated code casts that result to the operation's
458
+ * `RequestResult`, then wraps it with `withUnwrap`, so the caller can `await` it directly or call
459
+ * `.unwrap()` for the bare success body.
460
+ *
461
+ * Cast first, wrap second. That order keeps `withUnwrap`'s generic inferred as `RequestResult`
462
+ * instead of the runtime's own internal result type. Casting an `Unwrappable<A>` straight to
463
+ * `Unwrappable<B>` does not work: the `.then` overload stays pinned to `A`, and `as` rejects that
464
+ * two-generic swap even where `A` and `B` on their own would satisfy it.
459
465
  *
460
466
  * @example
461
- * `return request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>`
467
+ * `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>)`
462
468
  */
463
469
  function buildReturnStatement({ node, types, callConfig }) {
464
- return `return request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
470
+ return `return withUnwrap(request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
465
471
  node,
466
472
  types
467
- })}>>`;
473
+ })}>>)`;
468
474
  }
469
475
  //#endregion
470
476
  //#region ../../internals/client/src/builders/security.ts
@@ -548,10 +554,6 @@ const declarationPrinter = functionPrinter({ mode: "declaration" });
548
554
  */
549
555
  function buildGroupedOptionsSignature({ node, types }) {
550
556
  const optionsName = types.response.options(node);
551
- const resultGenerics = buildRequestResultGenerics({
552
- node,
553
- types
554
- });
555
557
  const { isOptional } = getRequestGroupOptionality(node);
556
558
  return {
557
559
  paramsSignature: declarationPrinter.print(createFunctionParameters({ params: [createFunctionParameter({
@@ -559,7 +561,10 @@ function buildGroupedOptionsSignature({ node, types }) {
559
561
  type: `Options<${optionsName}, ThrowOnError>`,
560
562
  ...isOptional ? { default: "{}" } : {}
561
563
  })] })) ?? "",
562
- returnType: `Promise<RequestResult<${resultGenerics}>>`,
564
+ returnType: `Unwrappable<RequestResult<${buildRequestResultGenerics({
565
+ node,
566
+ types
567
+ })}>>`,
563
568
  generics: ["ThrowOnError extends boolean = true"]
564
569
  };
565
570
  }
@@ -676,8 +681,9 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
676
681
  /**
677
682
  * Builds a single instance method for a generated SDK class. The body forwards the single grouped
678
683
  * `options` object to the instance's own client (`this.client`, built once in the constructor) and
679
- * returns the `RequestResult`. A per-call `options.client` still overrides the instance client, so
680
- * one operation can be routed to a different environment without a new instance.
684
+ * returns the `Unwrappable<RequestResult>`. A per-call `options.client` still overrides the
685
+ * instance client, so one operation can be routed to a different environment without a new
686
+ * instance.
681
687
  */
682
688
  function buildSdkMethod({ node, name, types, zodResolver, validator, security }) {
683
689
  if (!ast.isHttpOperationNode(node)) return "";
@@ -765,8 +771,9 @@ function buildStyles({ node }) {
765
771
  //#region ../../internals/client/src/components/Operation.tsx
766
772
  /**
767
773
  * Renders one client operation: the grouped `<Name>Request` type and the function that forwards a
768
- * single `options` object to the resolved client and returns the `RequestResult`. The type, signature,
769
- * and call config are built with the AST factory, and only the jsx-renderer emits the source.
774
+ * single `options` object to the resolved client and returns the `Unwrappable<RequestResult>`. The
775
+ * type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
776
+ * the source.
770
777
  */
771
778
  function Operation({ name, node, types, zodResolver, validator, security, isExportable = true, isIndexable = true }) {
772
779
  if (!ast.isHttpOperationNode(node)) return null;
@@ -918,8 +925,9 @@ function resolveOperationTypes(driver) {
918
925
  * Builds the built-in per-operation generator shared by the client plugins (`@kubb/plugin-fetch`,
919
926
  * `@kubb/plugin-axios`). Emits one async function per OpenAPI operation using the shared
920
927
  * `Operation` component: a grouped `<Name>Request` type and a function that forwards a single
921
- * `options` object to the bundled `client` and returns the `RequestResult`. Only the generator
922
- * `name` differs between plugins; every other resolution, import, and rendering step is identical.
928
+ * `options` object to the bundled `client` and returns the `Unwrappable<RequestResult>`. Only the
929
+ * generator `name` differs between plugins. Every other resolution, import, and rendering step is
930
+ * identical.
923
931
  */
924
932
  function createClientGenerator(name) {
925
933
  return defineGenerator({
@@ -995,7 +1003,7 @@ function createClientGenerator(name) {
995
1003
  }),
996
1004
  children: [
997
1005
  /* @__PURE__ */ jsx(File.Import, {
998
- name: eventStream ? ["client", "toEventStream"] : ["client"],
1006
+ name: eventStream ? ["client", "toEventStream"] : ["client", "withUnwrap"],
999
1007
  root: meta.file.path,
1000
1008
  path: clientPath
1001
1009
  }),
@@ -1004,7 +1012,11 @@ function createClientGenerator(name) {
1004
1012
  "Options",
1005
1013
  "EventStreamResult",
1006
1014
  "SuccessOf"
1007
- ] : ["Options", "RequestResult"],
1015
+ ] : [
1016
+ "Options",
1017
+ "Unwrappable",
1018
+ "RequestResult"
1019
+ ],
1008
1020
  root: meta.file.path,
1009
1021
  path: clientPath,
1010
1022
  isTypeOnly: true
@@ -1211,7 +1223,7 @@ function createSdkGenerator() {
1211
1223
  footer: footer(file),
1212
1224
  children: [
1213
1225
  /* @__PURE__ */ jsx(File.Import, {
1214
- name: ["createClient"],
1226
+ name: ["createClient", "withUnwrap"],
1215
1227
  root: file.path,
1216
1228
  path: clientPath
1217
1229
  }),
@@ -1220,6 +1232,7 @@ function createSdkGenerator() {
1220
1232
  "ClientConfig",
1221
1233
  "ClientInstance",
1222
1234
  "Options",
1235
+ "Unwrappable",
1223
1236
  "RequestResult"
1224
1237
  ],
1225
1238
  root: file.path,