@kubb/plugin-fetch 5.3.0-canary.20260907T104724 → 5.3.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 +71 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +23 -0
- package/dist/index.js +71 -31
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/plugin.ts +2 -0
- package/templates/fetch.ts +21 -0
package/dist/index.cjs
CHANGED
|
@@ -476,23 +476,49 @@ function createGroupConfig(group) {
|
|
|
476
476
|
function buildRequestResultGenerics({ node, types }) {
|
|
477
477
|
return `${types.response.responses(node)}, ThrowOnError`;
|
|
478
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* Builds the full return type an operation's function signature uses: `Unwrappable<RequestResult>`
|
|
481
|
+
* for the default `returnType: 'full'`, already a promise so no further wrapping is needed, or a
|
|
482
|
+
* `Promise` of the runtime's `UnwrappedResult` when `returnType: 'data'` narrows a resolved call
|
|
483
|
+
* down to the bare success body.
|
|
484
|
+
*
|
|
485
|
+
* @example
|
|
486
|
+
* `buildResultType({ node, types, returnType: 'data' }) // 'Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>'`
|
|
487
|
+
*/
|
|
488
|
+
function buildResultType({ node, types, returnType }) {
|
|
489
|
+
const generics = buildRequestResultGenerics({
|
|
490
|
+
node,
|
|
491
|
+
types
|
|
492
|
+
});
|
|
493
|
+
return returnType === "data" ? `Promise<UnwrappedResult<${generics}>>` : `Unwrappable<RequestResult<${generics}>>`;
|
|
494
|
+
}
|
|
479
495
|
//#endregion
|
|
480
496
|
//#region ../../internals/client/src/builders/returnStatement.ts
|
|
481
497
|
/**
|
|
482
|
-
* Builds the return statement of a generated operation function.
|
|
483
|
-
* to `{ data, error, request, response }`.
|
|
484
|
-
*
|
|
485
|
-
* `.unwrap()` for the bare success
|
|
498
|
+
* Builds the return statement of a generated operation function. With the default
|
|
499
|
+
* `returnType: 'full'` the runtime call already resolves to `{ data, error, request, response }`.
|
|
500
|
+
* The generated code casts that result to the operation's `RequestResult`, then wraps it with
|
|
501
|
+
* `withUnwrap`, so the caller can `await` it directly or call `.unwrap()` for the bare success
|
|
502
|
+
* body. With `returnType: 'data'` it instead routes the call through the runtime's `unwrapResult`,
|
|
503
|
+
* 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.
|
|
486
505
|
*
|
|
487
|
-
* Cast first, wrap second. That order keeps `withUnwrap`'s generic inferred
|
|
488
|
-
* instead of the runtime's own internal result type. Casting an `Unwrappable<A>`
|
|
489
|
-
* `Unwrappable<B>` does not work: the `.then` overload stays pinned to `A`, and `as`
|
|
490
|
-
* two-generic swap even where `A` and `B` on their own would satisfy it.
|
|
506
|
+
* Cast first, wrap second, for the `'full'` path. That order keeps `withUnwrap`'s generic inferred
|
|
507
|
+
* as `RequestResult` instead of the runtime's own internal result type. Casting an `Unwrappable<A>`
|
|
508
|
+
* straight to `Unwrappable<B>` does not work: the `.then` overload stays pinned to `A`, and `as`
|
|
509
|
+
* rejects that two-generic swap even where `A` and `B` on their own would satisfy it.
|
|
491
510
|
*
|
|
492
511
|
* @example
|
|
493
512
|
* `return withUnwrap(request({ method: 'POST', url: '/pet', ...config }) as Promise<RequestResult<AddPetResponses, ThrowOnError>>)`
|
|
513
|
+
* @example
|
|
514
|
+
* `return unwrapResult(request({ method: 'POST', url: '/pet', ...config }), config.throwOnError) as Promise<UnwrappedResult<AddPetResponses, ThrowOnError>>`
|
|
494
515
|
*/
|
|
495
|
-
function buildReturnStatement({ node, types, callConfig }) {
|
|
516
|
+
function buildReturnStatement({ node, types, callConfig, returnType }) {
|
|
517
|
+
if (returnType === "data") return `return unwrapResult(request(${callConfig}), config.throwOnError) as ${buildResultType({
|
|
518
|
+
node,
|
|
519
|
+
types,
|
|
520
|
+
returnType
|
|
521
|
+
})}`;
|
|
496
522
|
return `return withUnwrap(request(${callConfig}) as Promise<RequestResult<${buildRequestResultGenerics({
|
|
497
523
|
node,
|
|
498
524
|
types
|
|
@@ -578,7 +604,7 @@ const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declara
|
|
|
578
604
|
* per-operation input type has to be emitted. Both names come from `types`, which is `plugin-ts` or
|
|
579
605
|
* `plugin-zod`'s inferred types (see `resolveOperationTypes`).
|
|
580
606
|
*/
|
|
581
|
-
function buildGroupedOptionsSignature({ node, types }) {
|
|
607
|
+
function buildGroupedOptionsSignature({ node, types, returnType }) {
|
|
582
608
|
const optionsName = types.response.options(node);
|
|
583
609
|
const { isOptional } = getRequestGroupOptionality(node);
|
|
584
610
|
return {
|
|
@@ -587,10 +613,11 @@ function buildGroupedOptionsSignature({ node, types }) {
|
|
|
587
613
|
type: `Options<${optionsName}, ThrowOnError>`,
|
|
588
614
|
...isOptional ? { default: "{}" } : {}
|
|
589
615
|
})] })) ?? "",
|
|
590
|
-
returnType:
|
|
616
|
+
returnType: buildResultType({
|
|
591
617
|
node,
|
|
592
|
-
types
|
|
593
|
-
|
|
618
|
+
types,
|
|
619
|
+
returnType
|
|
620
|
+
}),
|
|
594
621
|
generics: ["ThrowOnError extends boolean = true"]
|
|
595
622
|
};
|
|
596
623
|
}
|
|
@@ -711,11 +738,12 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
|
711
738
|
* instance client, so one operation can be routed to a different environment without a new
|
|
712
739
|
* instance.
|
|
713
740
|
*/
|
|
714
|
-
function buildSdkMethod({ node, name, types, zodResolver, validator, security }) {
|
|
741
|
+
function buildSdkMethod({ node, name, types, zodResolver, validator, security, returnType }) {
|
|
715
742
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return "";
|
|
716
743
|
const signature = buildGroupedOptionsSignature({
|
|
717
744
|
node,
|
|
718
|
-
types
|
|
745
|
+
types,
|
|
746
|
+
returnType
|
|
719
747
|
});
|
|
720
748
|
const returnStatement = buildReturnStatement({
|
|
721
749
|
node,
|
|
@@ -725,7 +753,8 @@ function buildSdkMethod({ node, name, types, zodResolver, validator, security })
|
|
|
725
753
|
validator,
|
|
726
754
|
zodResolver,
|
|
727
755
|
security
|
|
728
|
-
})
|
|
756
|
+
}),
|
|
757
|
+
returnType
|
|
729
758
|
});
|
|
730
759
|
const generics = signature.generics.length ? `<${signature.generics.join(", ")}>` : "";
|
|
731
760
|
const jsdoc = buildJSDoc(buildOperationComments(node, {
|
|
@@ -801,11 +830,12 @@ function buildStyles({ node }) {
|
|
|
801
830
|
* type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
|
|
802
831
|
* the source.
|
|
803
832
|
*/
|
|
804
|
-
function Operation({ name, node, types, zodResolver, validator, security, isExportable = true, isIndexable = true }) {
|
|
833
|
+
function Operation({ name, node, types, zodResolver, validator, returnType, security, isExportable = true, isIndexable = true }) {
|
|
805
834
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
806
835
|
const signature = buildGroupedOptionsSignature({
|
|
807
836
|
node,
|
|
808
|
-
types
|
|
837
|
+
types,
|
|
838
|
+
returnType
|
|
809
839
|
});
|
|
810
840
|
const validators = buildValidatorHooks({
|
|
811
841
|
node,
|
|
@@ -838,11 +868,12 @@ function Operation({ name, node, types, zodResolver, validator, security, isExpo
|
|
|
838
868
|
"...config"
|
|
839
869
|
].filter(Boolean).join(", ")} }`;
|
|
840
870
|
const eventType = `SuccessOf<${types.response.responses(node)}>`;
|
|
841
|
-
const
|
|
871
|
+
const functionReturnType = eventStream ? `Promise<EventStreamResult<${eventType}>>` : signature.returnType;
|
|
842
872
|
const returnStatement = eventStream ? `return toEventStream<${eventType}>(request(${callConfig}))` : buildReturnStatement({
|
|
843
873
|
node,
|
|
844
874
|
types,
|
|
845
|
-
callConfig
|
|
875
|
+
callConfig,
|
|
876
|
+
returnType
|
|
846
877
|
});
|
|
847
878
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
848
879
|
name,
|
|
@@ -853,7 +884,7 @@ function Operation({ name, node, types, zodResolver, validator, security, isExpo
|
|
|
853
884
|
export: isExportable,
|
|
854
885
|
generics: signature.generics,
|
|
855
886
|
params: signature.paramsSignature,
|
|
856
|
-
returnType,
|
|
887
|
+
returnType: functionReturnType,
|
|
857
888
|
JSDoc: { comments: buildOperationComments(node, {
|
|
858
889
|
link: "urlPath",
|
|
859
890
|
linkPosition: "beforeDeprecated",
|
|
@@ -875,14 +906,15 @@ function Operation({ name, node, types, zodResolver, validator, security, isExpo
|
|
|
875
906
|
* instance: `const api = new PetClient({ baseURL }); api.getPetById(...)`. A per-call `client` option
|
|
876
907
|
* still overrides the instance client for a one-off call.
|
|
877
908
|
*/
|
|
878
|
-
function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, children }) {
|
|
909
|
+
function SdkClient({ name, isExportable = true, isIndexable = true, operations, validator, returnType, children }) {
|
|
879
910
|
const methods = operations.map(({ node, name: methodName, types, zodResolver, security }) => buildSdkMethod({
|
|
880
911
|
node,
|
|
881
912
|
name: methodName,
|
|
882
913
|
types,
|
|
883
914
|
zodResolver,
|
|
884
915
|
validator,
|
|
885
|
-
security
|
|
916
|
+
security,
|
|
917
|
+
returnType
|
|
886
918
|
}));
|
|
887
919
|
const classCode = `export class ${name} {\n${[
|
|
888
920
|
" private readonly client: ClientInstance",
|
|
@@ -962,7 +994,7 @@ function createClientGenerator(name) {
|
|
|
962
994
|
operation(node, ctx) {
|
|
963
995
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
964
996
|
const { config, driver, resolver, root } = ctx;
|
|
965
|
-
const { output, validator, group } = ctx.options;
|
|
997
|
+
const { output, validator, returnType, group } = ctx.options;
|
|
966
998
|
const types = resolveOperationTypes(driver);
|
|
967
999
|
if (!types) {
|
|
968
1000
|
ctx.warn(MISSING_OPERATION_TYPES_WARNING);
|
|
@@ -1029,7 +1061,7 @@ function createClientGenerator(name) {
|
|
|
1029
1061
|
}),
|
|
1030
1062
|
children: [
|
|
1031
1063
|
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1032
|
-
name: eventStream ? ["client", "toEventStream"] : ["client", "withUnwrap"],
|
|
1064
|
+
name: eventStream ? ["client", "toEventStream"] : ["client", returnType === "data" ? "unwrapResult" : "withUnwrap"],
|
|
1033
1065
|
root: meta.file.path,
|
|
1034
1066
|
path: clientPath
|
|
1035
1067
|
}),
|
|
@@ -1038,7 +1070,7 @@ function createClientGenerator(name) {
|
|
|
1038
1070
|
"Options",
|
|
1039
1071
|
"EventStreamResult",
|
|
1040
1072
|
"SuccessOf"
|
|
1041
|
-
] : [
|
|
1073
|
+
] : returnType === "data" ? ["Options", "UnwrappedResult"] : [
|
|
1042
1074
|
"Options",
|
|
1043
1075
|
"Unwrappable",
|
|
1044
1076
|
"RequestResult"
|
|
@@ -1064,6 +1096,7 @@ function createClientGenerator(name) {
|
|
|
1064
1096
|
types,
|
|
1065
1097
|
zodResolver,
|
|
1066
1098
|
validator,
|
|
1099
|
+
returnType,
|
|
1067
1100
|
security
|
|
1068
1101
|
})
|
|
1069
1102
|
]
|
|
@@ -1204,7 +1237,7 @@ function createSdkGenerator() {
|
|
|
1204
1237
|
renderer: kubb_jsx.jsxRenderer,
|
|
1205
1238
|
operations(nodes, ctx) {
|
|
1206
1239
|
const { config, resolver, root } = ctx;
|
|
1207
|
-
const { output, group, validator, sdk } = ctx.options;
|
|
1240
|
+
const { output, group, validator, returnType, sdk } = ctx.options;
|
|
1208
1241
|
if (!sdk) return null;
|
|
1209
1242
|
const types = resolveOperationTypes(ctx.driver);
|
|
1210
1243
|
if (!types) {
|
|
@@ -1249,12 +1282,17 @@ function createSdkGenerator() {
|
|
|
1249
1282
|
footer: footer(file),
|
|
1250
1283
|
children: [
|
|
1251
1284
|
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1252
|
-
name: ["createClient", "withUnwrap"],
|
|
1285
|
+
name: ["createClient", returnType === "data" ? "unwrapResult" : "withUnwrap"],
|
|
1253
1286
|
root: file.path,
|
|
1254
1287
|
path: clientPath
|
|
1255
1288
|
}),
|
|
1256
1289
|
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1257
|
-
name: [
|
|
1290
|
+
name: returnType === "data" ? [
|
|
1291
|
+
"ClientConfig",
|
|
1292
|
+
"ClientInstance",
|
|
1293
|
+
"Options",
|
|
1294
|
+
"UnwrappedResult"
|
|
1295
|
+
] : [
|
|
1258
1296
|
"ClientConfig",
|
|
1259
1297
|
"ClientInstance",
|
|
1260
1298
|
"Options",
|
|
@@ -1284,7 +1322,8 @@ function createSdkGenerator() {
|
|
|
1284
1322
|
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(SdkClient, {
|
|
1285
1323
|
name: className,
|
|
1286
1324
|
operations: ops,
|
|
1287
|
-
validator
|
|
1325
|
+
validator,
|
|
1326
|
+
returnType
|
|
1288
1327
|
})
|
|
1289
1328
|
]
|
|
1290
1329
|
}, file.path);
|
|
@@ -1421,7 +1460,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1421
1460
|
const { output = {
|
|
1422
1461
|
path: "clients",
|
|
1423
1462
|
barrel: { type: "named" }
|
|
1424
|
-
}, exclude = [], include, override = [], baseURL, validator = false, group, sdk, resolver: userResolver } = options;
|
|
1463
|
+
}, exclude = [], include, override = [], baseURL, validator = false, returnType = "full", group, sdk, resolver: userResolver } = options;
|
|
1425
1464
|
const resolved = {
|
|
1426
1465
|
output,
|
|
1427
1466
|
exclude,
|
|
@@ -1430,6 +1469,7 @@ const pluginFetch = (0, kubb_kit.definePlugin)((options) => {
|
|
|
1430
1469
|
group: createGroupConfig(group),
|
|
1431
1470
|
baseURL,
|
|
1432
1471
|
validator,
|
|
1472
|
+
returnType,
|
|
1433
1473
|
sdk: sdk ? {
|
|
1434
1474
|
mode: sdk.mode ?? "tag",
|
|
1435
1475
|
name: sdk.name
|