@kubb/plugin-fetch 5.5.0-canary.20260924T111237 → 5.5.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 +37 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -5
- package/dist/index.js +37 -68
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/plugin.ts +5 -33
- package/templates/fetch.ts +4 -9
package/dist/index.cjs
CHANGED
|
@@ -31,7 +31,6 @@ 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");
|
|
35
34
|
let node_url = require("node:url");
|
|
36
35
|
//#region ../../internals/shared/src/params.ts
|
|
37
36
|
/**
|
|
@@ -502,7 +501,7 @@ function buildResultType({ node, types, returnType }) {
|
|
|
502
501
|
* `withUnwrap`, so the caller can `await` it directly or call `.unwrap()` for the bare success
|
|
503
502
|
* body. With `returnType: 'data'` it instead routes the call through the runtime's `unwrapResult`,
|
|
504
503
|
* which narrows the resolved value down to the bare success body the same way `RequestResult`
|
|
505
|
-
* already does,
|
|
504
|
+
* already does, using the plugin default when the call leaves `throwOnError` unset.
|
|
506
505
|
*
|
|
507
506
|
* Cast first, wrap second, for the `'full'` path. That order keeps `withUnwrap`'s generic inferred
|
|
508
507
|
* as `RequestResult` instead of the runtime's own internal result type. Casting an `Unwrappable<A>`
|
|
@@ -512,10 +511,10 @@ function buildResultType({ node, types, returnType }) {
|
|
|
512
511
|
* @example
|
|
513
512
|
* `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>)`
|
|
514
513
|
* @example
|
|
515
|
-
* `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
|
|
514
|
+
* `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError ?? false) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
|
|
516
515
|
*/
|
|
517
|
-
function buildReturnStatement({ node, types, callConfig, returnType }) {
|
|
518
|
-
if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError) as ${buildResultType({
|
|
516
|
+
function buildReturnStatement({ node, types, callConfig, returnType, throwOnErrorDefault }) {
|
|
517
|
+
if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError ?? ${throwOnErrorDefault}) as ${buildResultType({
|
|
519
518
|
node,
|
|
520
519
|
types,
|
|
521
520
|
returnType
|
|
@@ -605,7 +604,7 @@ const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declara
|
|
|
605
604
|
* per-operation input type has to be emitted. Both names come from `types`, which is `plugin-ts` or
|
|
606
605
|
* `plugin-zod`'s inferred types (see `resolveOperationTypes`).
|
|
607
606
|
*/
|
|
608
|
-
function buildGroupedOptionsSignature({ node, types, returnType }) {
|
|
607
|
+
function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDefault }) {
|
|
609
608
|
const optionsName = types.response.options(node);
|
|
610
609
|
const { isOptional } = getRequestGroupOptionality(node);
|
|
611
610
|
return {
|
|
@@ -619,7 +618,7 @@ function buildGroupedOptionsSignature({ node, types, returnType }) {
|
|
|
619
618
|
types,
|
|
620
619
|
returnType
|
|
621
620
|
}),
|
|
622
|
-
generics: [
|
|
621
|
+
generics: [`ThrowOnError extends boolean = ${throwOnErrorDefault}`]
|
|
623
622
|
};
|
|
624
623
|
}
|
|
625
624
|
//#endregion
|
|
@@ -713,9 +712,10 @@ function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
|
713
712
|
/**
|
|
714
713
|
* Builds the call config literal forwarded to the contract client, mirroring the shared `Operation`
|
|
715
714
|
* component: `{ method, url, security?, validator?, ...config }`. The `...config` spread carries every
|
|
716
|
-
* per-call field
|
|
715
|
+
* per-call field. The plugin's `throwOnError` fallback is applied after the spread so it also wins
|
|
716
|
+
* over a client instance's runtime config when the call omits an override.
|
|
717
717
|
*/
|
|
718
|
-
function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
718
|
+
function buildCallConfig({ node, validator, zodResolver, security, throwOnErrorDefault }) {
|
|
719
719
|
const validators = buildValidatorHooks({
|
|
720
720
|
node,
|
|
721
721
|
validator,
|
|
@@ -729,7 +729,8 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
|
729
729
|
`url: '${node.path}'`,
|
|
730
730
|
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
731
731
|
validatorLiteral,
|
|
732
|
-
"...config"
|
|
732
|
+
"...config",
|
|
733
|
+
`throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
|
|
733
734
|
].filter(Boolean).join(", ")} }`;
|
|
734
735
|
}
|
|
735
736
|
/**
|
|
@@ -739,12 +740,13 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
|
739
740
|
* instance client, so one operation can be routed to a different environment without a new
|
|
740
741
|
* instance.
|
|
741
742
|
*/
|
|
742
|
-
function buildSdkMethod({ node, name, types, zodResolver, validator, security, returnType }) {
|
|
743
|
+
function buildSdkMethod({ node, name, types, zodResolver, validator, security, returnType, throwOnErrorDefault }) {
|
|
743
744
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return "";
|
|
744
745
|
const signature = buildGroupedOptionsSignature({
|
|
745
746
|
node,
|
|
746
747
|
types,
|
|
747
|
-
returnType
|
|
748
|
+
returnType,
|
|
749
|
+
throwOnErrorDefault
|
|
748
750
|
});
|
|
749
751
|
const returnStatement = buildReturnStatement({
|
|
750
752
|
node,
|
|
@@ -753,9 +755,11 @@ function buildSdkMethod({ node, name, types, zodResolver, validator, security, r
|
|
|
753
755
|
node,
|
|
754
756
|
validator,
|
|
755
757
|
zodResolver,
|
|
756
|
-
security
|
|
758
|
+
security,
|
|
759
|
+
throwOnErrorDefault
|
|
757
760
|
}),
|
|
758
|
-
returnType
|
|
761
|
+
returnType,
|
|
762
|
+
throwOnErrorDefault
|
|
759
763
|
});
|
|
760
764
|
const generics = signature.generics.length ? `<${signature.generics.join(", ")}>` : "";
|
|
761
765
|
const jsdoc = buildJSDoc(buildOperationComments(node, {
|
|
@@ -831,12 +835,13 @@ function buildStyles({ node }) {
|
|
|
831
835
|
* type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
|
|
832
836
|
* the source.
|
|
833
837
|
*/
|
|
834
|
-
function Operation({ name, node, types, zodResolver, validator, returnType, security, isExportable = true, isIndexable = true }) {
|
|
838
|
+
function Operation({ name, node, types, zodResolver, validator, returnType, throwOnErrorDefault, security, isExportable = true, isIndexable = true }) {
|
|
835
839
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
836
840
|
const signature = buildGroupedOptionsSignature({
|
|
837
841
|
node,
|
|
838
842
|
types,
|
|
839
|
-
returnType
|
|
843
|
+
returnType,
|
|
844
|
+
throwOnErrorDefault
|
|
840
845
|
});
|
|
841
846
|
const validators = buildValidatorHooks({
|
|
842
847
|
node,
|
|
@@ -866,7 +871,8 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
|
|
|
866
871
|
validatorLiteral,
|
|
867
872
|
contentTypeLiteral,
|
|
868
873
|
responseTypeLiteral,
|
|
869
|
-
"...config"
|
|
874
|
+
"...config",
|
|
875
|
+
`throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
|
|
870
876
|
].filter(Boolean).join(", ")} }`;
|
|
871
877
|
const eventType = `SuccessOf<${types.response.responses(node)}>`;
|
|
872
878
|
const functionReturnType = eventStream ? `Promise<EventStreamResult<${eventType}>>` : signature.returnType;
|
|
@@ -874,7 +880,8 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
|
|
|
874
880
|
node,
|
|
875
881
|
types,
|
|
876
882
|
callConfig,
|
|
877
|
-
returnType
|
|
883
|
+
returnType,
|
|
884
|
+
throwOnErrorDefault
|
|
878
885
|
});
|
|
879
886
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
880
887
|
name,
|
|
@@ -907,7 +914,7 @@ function Operation({ name, node, types, zodResolver, validator, returnType, secu
|
|
|
907
914
|
* instance: `const api = new PetClient({ baseURL }); api.getPetById(...)`. A per-call `client` option
|
|
908
915
|
* still overrides the instance client for a one-off call.
|
|
909
916
|
*/
|
|
910
|
-
function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, returnType, children }) {
|
|
917
|
+
function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, returnType, throwOnErrorDefault, children }) {
|
|
911
918
|
const methods = operations.map(({ node, name: methodName, types, zodResolver, security }) => buildSdkMethod({
|
|
912
919
|
node,
|
|
913
920
|
name: methodName,
|
|
@@ -915,7 +922,8 @@ function SdkClient({ name, isExportable = true, isIndexable = true, operations,
|
|
|
915
922
|
zodResolver,
|
|
916
923
|
validator,
|
|
917
924
|
security,
|
|
918
|
-
returnType
|
|
925
|
+
returnType,
|
|
926
|
+
throwOnErrorDefault
|
|
919
927
|
}));
|
|
920
928
|
const classCode = `export class ${name} {\n${[
|
|
921
929
|
" private readonly client: ClientInstance",
|
|
@@ -995,7 +1003,7 @@ function createClientGenerator(name) {
|
|
|
995
1003
|
operation(node, ctx) {
|
|
996
1004
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
997
1005
|
const { config, driver, resolver, root } = ctx;
|
|
998
|
-
const { output, validator, returnType, group } = ctx.options;
|
|
1006
|
+
const { output, validator, returnType, throwOnErrorDefault, group } = ctx.options;
|
|
999
1007
|
const types = resolveOperationTypes(driver);
|
|
1000
1008
|
if (!types) {
|
|
1001
1009
|
ctx.warn(MISSING_OPERATION_TYPES_WARNING);
|
|
@@ -1098,6 +1106,7 @@ function createClientGenerator(name) {
|
|
|
1098
1106
|
zodResolver,
|
|
1099
1107
|
validator,
|
|
1100
1108
|
returnType,
|
|
1109
|
+
throwOnErrorDefault,
|
|
1101
1110
|
security
|
|
1102
1111
|
})
|
|
1103
1112
|
]
|
|
@@ -1238,7 +1247,7 @@ function createSdkGenerator() {
|
|
|
1238
1247
|
renderer: kubb_jsx.jsxRenderer,
|
|
1239
1248
|
operations(nodes, ctx) {
|
|
1240
1249
|
const { config, resolver, root } = ctx;
|
|
1241
|
-
const { output, group, validator, returnType, sdk } = ctx.options;
|
|
1250
|
+
const { output, group, validator, returnType, throwOnErrorDefault, sdk } = ctx.options;
|
|
1242
1251
|
if (!sdk) return null;
|
|
1243
1252
|
const types = resolveOperationTypes(ctx.driver);
|
|
1244
1253
|
if (!types) {
|
|
@@ -1324,7 +1333,8 @@ function createSdkGenerator() {
|
|
|
1324
1333
|
name: className,
|
|
1325
1334
|
operations: ops,
|
|
1326
1335
|
validator,
|
|
1327
|
-
returnType
|
|
1336
|
+
returnType,
|
|
1337
|
+
throwOnErrorDefault
|
|
1328
1338
|
})
|
|
1329
1339
|
]
|
|
1330
1340
|
}, file.path);
|
|
@@ -1461,7 +1471,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1461
1471
|
const { output = {
|
|
1462
1472
|
path: "clients",
|
|
1463
1473
|
barrel: { type: "named" }
|
|
1464
|
-
}, exclude = [], include, override = [], baseURL, validator = false, returnType = "full", group, sdk, resolver: userResolver } = options;
|
|
1474
|
+
}, exclude = [], include, override = [], baseURL, throwOnErrorDefault = true, validator = false, returnType = "full", group, sdk, resolver: userResolver } = options;
|
|
1465
1475
|
const resolved = {
|
|
1466
1476
|
output,
|
|
1467
1477
|
exclude,
|
|
@@ -1469,6 +1479,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1469
1479
|
override,
|
|
1470
1480
|
group: createGroupConfig(group),
|
|
1471
1481
|
baseURL,
|
|
1482
|
+
throwOnErrorDefault,
|
|
1472
1483
|
validator,
|
|
1473
1484
|
returnType,
|
|
1474
1485
|
sdk: sdk ? {
|
|
@@ -1494,52 +1505,10 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1494
1505
|
path: node_path.default.resolve(root, ".kubb/serializers.ts"),
|
|
1495
1506
|
copy: fetchSerializersTemplatePath
|
|
1496
1507
|
});
|
|
1497
|
-
const clientPath = node_path.default.resolve(root, ".kubb/client.ts");
|
|
1498
|
-
const clientSource = (0, node_fs.readFileSync)(fetchClientTemplatePath, "utf8");
|
|
1499
|
-
const clientBody = clientSource.slice(clientSource.indexOf("\n\n") + 2);
|
|
1500
|
-
const runtimeRoot = node_path.default.dirname(clientPath);
|
|
1501
1508
|
ctx.injectFile({
|
|
1502
1509
|
baseName: "client.ts",
|
|
1503
|
-
path:
|
|
1504
|
-
|
|
1505
|
-
kubb_kit.ast.factory.createImport({
|
|
1506
|
-
name: [
|
|
1507
|
-
"applyHeaderStyles",
|
|
1508
|
-
"defaultBodySerializer",
|
|
1509
|
-
"defaultPathSerializer",
|
|
1510
|
-
"defaultQuerySerializer",
|
|
1511
|
-
"isDefaultJsonBody",
|
|
1512
|
-
"serializeCookies"
|
|
1513
|
-
],
|
|
1514
|
-
path: node_path.default.resolve(root, ".kubb/serializers.ts"),
|
|
1515
|
-
root: runtimeRoot
|
|
1516
|
-
}),
|
|
1517
|
-
kubb_kit.ast.factory.createImport({
|
|
1518
|
-
name: [
|
|
1519
|
-
"HeadersInit",
|
|
1520
|
-
"PathParamStyle",
|
|
1521
|
-
"PathSerializer",
|
|
1522
|
-
"RequestBody",
|
|
1523
|
-
"Serializers",
|
|
1524
|
-
"Styles"
|
|
1525
|
-
],
|
|
1526
|
-
path: node_path.default.resolve(root, ".kubb/serializers.ts"),
|
|
1527
|
-
root: runtimeRoot,
|
|
1528
|
-
isTypeOnly: true
|
|
1529
|
-
}),
|
|
1530
|
-
kubb_kit.ast.factory.createImport({
|
|
1531
|
-
name: ["ParseError", "validateStandardSchema"],
|
|
1532
|
-
path: node_path.default.resolve(root, ".kubb/standardSchema.ts"),
|
|
1533
|
-
root: runtimeRoot
|
|
1534
|
-
}),
|
|
1535
|
-
kubb_kit.ast.factory.createImport({
|
|
1536
|
-
name: ["StandardSchemaValidator"],
|
|
1537
|
-
path: node_path.default.resolve(root, ".kubb/standardSchema.ts"),
|
|
1538
|
-
root: runtimeRoot,
|
|
1539
|
-
isTypeOnly: true
|
|
1540
|
-
})
|
|
1541
|
-
],
|
|
1542
|
-
sources: [kubb_kit.ast.factory.createSource({ nodes: [kubb_kit.ast.factory.createText(clientBody)] })],
|
|
1510
|
+
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1511
|
+
copy: fetchClientTemplatePath,
|
|
1543
1512
|
footer: baseURLExpression ? `client.setConfig({ baseURL: ${baseURLExpression} })` : void 0
|
|
1544
1513
|
});
|
|
1545
1514
|
ctx.injectFile({
|