@kubb/plugin-fetch 5.5.0-canary.20260924T110844 → 5.5.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.cjs CHANGED
@@ -31,6 +31,7 @@ let _kubb_plugin_ts = require("@kubb/plugin-ts");
31
31
  let kubb_jsx = require("kubb/jsx");
32
32
  let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
33
33
  let _kubb_plugin_zod = require("@kubb/plugin-zod");
34
+ let node_fs = require("node:fs");
34
35
  let node_url = require("node:url");
35
36
  //#region ../../internals/shared/src/params.ts
36
37
  /**
@@ -501,7 +502,7 @@ function buildResultType({ node, types, returnType }) {
501
502
  * `withUnwrap`, so the caller can `await` it directly or call `.unwrap()` for the bare success
502
503
  * body. With `returnType: 'data'` it instead routes the call through the runtime's `unwrapResult`,
503
504
  * which narrows the resolved value down to the bare success body the same way `RequestResult`
504
- * already does, keeping the `throwOnError` default in one place instead of restating it per call.
505
+ * already does, using the plugin default when the call leaves `throwOnError` unset.
505
506
  *
506
507
  * Cast first, wrap second, for the `'full'` path. That order keeps `withUnwrap`'s generic inferred
507
508
  * as `RequestResult` instead of the runtime's own internal result type. Casting an `Unwrappable<A>`
@@ -511,10 +512,10 @@ function buildResultType({ node, types, returnType }) {
511
512
  * @example
512
513
  * `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>)`
513
514
  * @example
514
- * `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
515
+ * `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError ?? false) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
515
516
  */
516
- function buildReturnStatement({ node, types, callConfig, returnType }) {
517
- if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError) as ${buildResultType({
517
+ function buildReturnStatement({ node, types, callConfig, returnType, throwOnErrorDefault }) {
518
+ if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
518
519
  node,
519
520
  types,
520
521
  returnType
@@ -604,7 +605,7 @@ const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declara
604
605
  * per-operation input type has to be emitted. Both names come from `types`, which is `plugin-ts` or
605
606
  * `plugin-zod`'s inferred types (see `resolveOperationTypes`).
606
607
  */
607
- function buildGroupedOptionsSignature({ node, types, returnType }) {
608
+ function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDefault }) {
608
609
  const optionsName = types.response.options(node);
609
610
  const { isOptional } = getRequestGroupOptionality(node);
610
611
  return {
@@ -618,7 +619,7 @@ function buildGroupedOptionsSignature({ node, types, returnType }) {
618
619
  types,
619
620
  returnType
620
621
  }),
621
- generics: ["ThrowOnError extends boolean = true"]
622
+ generics: [`ThrowOnError extends boolean = ${throwOnErrorDefault}`]
622
623
  };
623
624
  }
624
625
  //#endregion
@@ -712,9 +713,10 @@ function buildValidatorHooks({ node, validator, zodResolver }) {
712
713
  /**
713
714
  * Builds the call config literal forwarded to the contract client, mirroring the shared `Operation`
714
715
  * component: `{ method, url, security?, validator?, ...config }`. The `...config` spread carries every
715
- * per-call field (including `throwOnError`), so the method stays a thin wrapper over the contract.
716
+ * per-call field. The plugin's `throwOnError` fallback is applied after the spread so it also wins
717
+ * over a client instance's runtime config when the call omits an override.
716
718
  */
717
- function buildCallConfig({ node, validator, zodResolver, security }) {
719
+ function buildCallConfig({ node, validator, zodResolver, security, throwOnErrorDefault }) {
718
720
  const validators = buildValidatorHooks({
719
721
  node,
720
722
  validator,
@@ -728,7 +730,8 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
728
730
  `url: '${node.path}'`,
729
731
  securityLiteral ? `security: ${securityLiteral}` : null,
730
732
  validatorLiteral,
731
- "...config"
733
+ "...config",
734
+ `throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
732
735
  ].filter(Boolean).join(", ")} }`;
733
736
  }
734
737
  /**
@@ -738,12 +741,13 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
738
741
  * instance client, so one operation can be routed to a different environment without a new
739
742
  * instance.
740
743
  */
741
- function buildSdkMethod({ node, name, types, zodResolver, validator, security, returnType }) {
744
+ function buildSdkMethod({ node, name, types, zodResolver, validator, security, returnType, throwOnErrorDefault }) {
742
745
  if (!kubb_kit.ast.isHttpOperationNode(node)) return "";
743
746
  const signature = buildGroupedOptionsSignature({
744
747
  node,
745
748
  types,
746
- returnType
749
+ returnType,
750
+ throwOnErrorDefault
747
751
  });
748
752
  const returnStatement = buildReturnStatement({
749
753
  node,
@@ -752,9 +756,11 @@ function buildSdkMethod({ node, name, types, zodResolver, validator, security, r
752
756
  node,
753
757
  validator,
754
758
  zodResolver,
755
- security
759
+ security,
760
+ throwOnErrorDefault
756
761
  }),
757
- returnType
762
+ returnType,
763
+ throwOnErrorDefault
758
764
  });
759
765
  const generics = signature.generics.length ? `<${signature.generics.join(", ")}>` : "";
760
766
  const jsdoc = buildJSDoc(buildOperationComments(node, {
@@ -830,12 +836,13 @@ function buildStyles({ node }) {
830
836
  * type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
831
837
  * the source.
832
838
  */
833
- function Operation({ name, node, types, zodResolver, validator, returnType, security, isExportable = true, isIndexable = true }) {
839
+ function Operation({ name, node, types, zodResolver, validator, returnType, throwOnErrorDefault, security, isExportable = true, isIndexable = true }) {
834
840
  if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
835
841
  const signature = buildGroupedOptionsSignature({
836
842
  node,
837
843
  types,
838
- returnType
844
+ returnType,
845
+ throwOnErrorDefault
839
846
  });
840
847
  const validators = buildValidatorHooks({
841
848
  node,
@@ -865,7 +872,8 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
865
872
  validatorLiteral,
866
873
  contentTypeLiteral,
867
874
  responseTypeLiteral,
868
- "...config"
875
+ "...config",
876
+ `throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
869
877
  ].filter(Boolean).join(", ")} }`;
870
878
  const eventType = `SuccessOf<${types.response.responses(node)}>`;
871
879
  const functionReturnType = eventStream ? `Promise<EventStreamResult<${eventType}>>` : signature.returnType;
@@ -873,7 +881,8 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
873
881
  node,
874
882
  types,
875
883
  callConfig,
876
- returnType
884
+ returnType,
885
+ throwOnErrorDefault
877
886
  });
878
887
  return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
879
888
  name,
@@ -906,7 +915,7 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
906
915
  * instance: `const api = new PetClient({ baseURL }); api.getPetById(...)`. A per-call `client` option
907
916
  * still overrides the instance client for a one-off call.
908
917
  */
909
- function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, returnType, children }) {
918
+ function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, returnType, throwOnErrorDefault, children }) {
910
919
  const methods = operations.map(({ node, name: methodName, types, zodResolver, security }) => buildSdkMethod({
911
920
  node,
912
921
  name: methodName,
@@ -914,7 +923,8 @@ function SdkClient({ name, isExportable = true, isIndexable = true, operations,
914
923
  zodResolver,
915
924
  validator,
916
925
  security,
917
- returnType
926
+ returnType,
927
+ throwOnErrorDefault
918
928
  }));
919
929
  const classCode = `export class ${name} {\n${[
920
930
  " private readonly client: ClientInstance",
@@ -994,7 +1004,7 @@ function createClientGenerator(name) {
994
1004
  operation(node, ctx) {
995
1005
  if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
996
1006
  const { config, driver, resolver, root } = ctx;
997
- const { output, validator, returnType, group } = ctx.options;
1007
+ const { output, validator, returnType, throwOnErrorDefault, group } = ctx.options;
998
1008
  const types = resolveOperationTypes(driver);
999
1009
  if (!types) {
1000
1010
  ctx.warn(MISSING_OPERATION_TYPES_WARNING);
@@ -1097,6 +1107,7 @@ function createClientGenerator(name) {
1097
1107
  zodResolver,
1098
1108
  validator,
1099
1109
  returnType,
1110
+ throwOnErrorDefault,
1100
1111
  security
1101
1112
  })
1102
1113
  ]
@@ -1237,7 +1248,7 @@ function createSdkGenerator() {
1237
1248
  renderer: kubb_jsx.jsxRenderer,
1238
1249
  operations(nodes, ctx) {
1239
1250
  const { config, resolver, root } = ctx;
1240
- const { output, group, validator, returnType, sdk } = ctx.options;
1251
+ const { output, group, validator, returnType, throwOnErrorDefault, sdk } = ctx.options;
1241
1252
  if (!sdk) return null;
1242
1253
  const types = resolveOperationTypes(ctx.driver);
1243
1254
  if (!types) {
@@ -1323,7 +1334,8 @@ function createSdkGenerator() {
1323
1334
  name: className,
1324
1335
  operations: ops,
1325
1336
  validator,
1326
- returnType
1337
+ returnType,
1338
+ throwOnErrorDefault
1327
1339
  })
1328
1340
  ]
1329
1341
  }, file.path);
@@ -1409,6 +1421,27 @@ const resolverClient = (0, kubb_kit.createResolver)({
1409
1421
  }
1410
1422
  });
1411
1423
  //#endregion
1424
+ //#region ../../internals/client/src/runtimeTemplate.ts
1425
+ /** Apply the active TypeScript parser's import extension to a copied client runtime. */
1426
+ function runtimeTemplate(path, config) {
1427
+ const parser = config.parsers.find((item) => item.extNames?.includes(".ts"));
1428
+ const probe = kubb_kit.ast.factory.createFile({
1429
+ baseName: "probe.ts",
1430
+ path: "probe.ts",
1431
+ imports: [kubb_kit.ast.factory.createImport({
1432
+ name: ["KubbRuntimeImport"],
1433
+ path: "/probeDependency.ts",
1434
+ root: "/"
1435
+ })],
1436
+ sources: [kubb_kit.ast.factory.createSource({
1437
+ name: "probe",
1438
+ nodes: [kubb_kit.ast.factory.createText("KubbRuntimeImport")]
1439
+ })]
1440
+ });
1441
+ const extension = parser?.parse(probe).match(/from ['"]\.\/probeDependency(\.[^'"]+)?['"]/)?.[1] ?? "";
1442
+ return (0, node_fs.readFileSync)(path, "utf8").replace(/(from ['"]\.\/(?:serializers|standardSchema))(?:\.ts|\.js)?(['"])/g, `$1${extension}$2`);
1443
+ }
1444
+ //#endregion
1412
1445
  //#region src/generators/clientGenerator.tsx
1413
1446
  /**
1414
1447
  * Built-in operation generator for `@kubb/plugin-fetch`. Emits one async function per OpenAPI
@@ -1460,7 +1493,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
1460
1493
  const { output = {
1461
1494
  path: "clients",
1462
1495
  barrel: { type: "named" }
1463
- }, exclude = [], include, override = [], baseURL, validator = false, returnType = "full", group, sdk, resolver: userResolver } = options;
1496
+ }, exclude = [], include, override = [], baseURL, throwOnErrorDefault = true, validator = false, returnType = "full", group, sdk, resolver: userResolver } = options;
1464
1497
  const resolved = {
1465
1498
  output,
1466
1499
  exclude,
@@ -1468,6 +1501,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
1468
1501
  override,
1469
1502
  group: createGroupConfig(group),
1470
1503
  baseURL,
1504
+ throwOnErrorDefault,
1471
1505
  validator,
1472
1506
  returnType,
1473
1507
  sdk: sdk ? {
@@ -1496,7 +1530,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
1496
1530
  ctx.injectFile({
1497
1531
  baseName: "client.ts",
1498
1532
  path: node_path.default.resolve(root, ".kubb/client.ts"),
1499
- copy: fetchClientTemplatePath,
1533
+ banner: runtimeTemplate(fetchClientTemplatePath, ctx.config),
1500
1534
  footer: baseURLExpression ? `client.setConfig({ baseURL: ${baseURLExpression} })` : void 0
1501
1535
  });
1502
1536
  ctx.injectFile({