@kubb/plugin-fetch 5.0.0-beta.79 → 5.0.0-beta.81
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 +36 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +36 -2
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/plugin.ts +2 -1
- package/templates/fetch.ts +179 -128
package/dist/index.cjs
CHANGED
|
@@ -456,6 +456,37 @@ function getResponseType(node) {
|
|
|
456
456
|
if (baseType.startsWith("text/")) return "text";
|
|
457
457
|
if (baseType === "application/octet-stream" || baseType === "application/pdf" || /^(image|audio|video)\//.test(baseType)) return "blob";
|
|
458
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* Which of the grouped request options an operation carries.
|
|
461
|
+
*/
|
|
462
|
+
function getRequestGroups(node) {
|
|
463
|
+
const { path, query, header } = getOperationParameters(node);
|
|
464
|
+
return {
|
|
465
|
+
path: path.length > 0,
|
|
466
|
+
query: query.length > 0,
|
|
467
|
+
body: Boolean(node.requestBody?.content?.[0]?.schema),
|
|
468
|
+
headers: header.length > 0
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Resolves which grouped request options an operation carries together with whether each group
|
|
473
|
+
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
474
|
+
* required, matching the generated `RequestConfig` type.
|
|
475
|
+
*/
|
|
476
|
+
function getRequestGroupOptionality(node) {
|
|
477
|
+
const groups = getRequestGroups(node);
|
|
478
|
+
const { path, query, header } = getOperationParameters(node);
|
|
479
|
+
const hasRequiredPath = path.some((param) => param.required);
|
|
480
|
+
const hasRequiredQuery = query.some((param) => param.required);
|
|
481
|
+
const hasRequiredHeader = header.some((param) => param.required);
|
|
482
|
+
return {
|
|
483
|
+
groups,
|
|
484
|
+
hasRequiredPath,
|
|
485
|
+
hasRequiredQuery,
|
|
486
|
+
hasRequiredHeader,
|
|
487
|
+
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
|
|
488
|
+
};
|
|
489
|
+
}
|
|
459
490
|
function buildOperationComments(node, options = {}) {
|
|
460
491
|
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
461
492
|
const linkComment = getOperationLink(node, link);
|
|
@@ -705,11 +736,13 @@ function buildGroupedOptionsSignature({ node, tsResolver }) {
|
|
|
705
736
|
node,
|
|
706
737
|
tsResolver
|
|
707
738
|
});
|
|
739
|
+
const { isOptional } = getRequestGroupOptionality(node);
|
|
708
740
|
return {
|
|
709
741
|
dataTypeName: requestConfigName,
|
|
710
742
|
paramsSignature: declarationPrinter.print((0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
711
743
|
name: "options",
|
|
712
|
-
type: `Options<${requestConfigName}, ThrowOnError
|
|
744
|
+
type: `Options<${requestConfigName}, ThrowOnError>`,
|
|
745
|
+
...isOptional ? { default: "{}" } : {}
|
|
713
746
|
})] })) ?? "",
|
|
714
747
|
returnType: `Promise<RequestResult<${resultGenerics}>>`,
|
|
715
748
|
generics: ["ThrowOnError extends boolean = true"],
|
|
@@ -1446,6 +1479,7 @@ const pluginFetch = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1446
1479
|
ctx.setMacros([...defaultMacros, ...options.macros ?? []]);
|
|
1447
1480
|
ctx.addGenerator(...selectedGenerators);
|
|
1448
1481
|
const root = node_path.default.resolve(ctx.config.root, ctx.config.output.path);
|
|
1482
|
+
const baseURLExpression = baseURL ? baseURL.includes("${") ? `\`${baseURL.replaceAll("`", "\\`")}\`` : JSON.stringify(baseURL) : void 0;
|
|
1449
1483
|
ctx.injectFile({
|
|
1450
1484
|
baseName: "serializers.ts",
|
|
1451
1485
|
path: node_path.default.resolve(root, ".kubb/serializers.ts"),
|
|
@@ -1455,7 +1489,7 @@ const pluginFetch = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1455
1489
|
baseName: "client.ts",
|
|
1456
1490
|
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
1457
1491
|
copy: fetchClientTemplatePath,
|
|
1458
|
-
footer:
|
|
1492
|
+
footer: baseURLExpression ? `client.setConfig({ baseURL: ${baseURLExpression} })` : void 0
|
|
1459
1493
|
});
|
|
1460
1494
|
ctx.injectFile({
|
|
1461
1495
|
baseName: "standardSchema.ts",
|