unstructured-client
Version:
<h3 align="center"> <img src="https://raw.githubusercontent.com/Unstructured-IO/unstructured/main/img/unstructured_logo.png" height="200" > </h3>
48 lines • 1.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.FixArrayParamsHook = void 0;
const request_js_1 = require("./utils/request.js");
/**
* If the given key in FormData is present and contains a comma-separated list of values,
* split the values into separate entries with the key suffixed by "[]".
*
* @param formData - The FormData object to modify.
* @param key - The key to extract and split.
*/
function flattenArrayParameter(formData, key) {
const value = formData.get(key);
if (formData && typeof value === "string" && value.includes(",")) {
formData.delete(key);
const values = value.split(",").map(v => v.trim()).filter(Boolean);
for (const v of values) {
formData.append(`${key}[]`, v);
}
}
}
/**
* Represents a hook for fixing array parameters before sending a request.
*/
class FixArrayParamsHook {
/**
* Fixes specific array parameters in the request.
* The SDK creates FormData with {extract_image_block_types: "a,b,c"},
* and the server expects it to be {extract_image_block_types[]: ["a", "b", "c"]}.
* Speakeasy will fix this upstream soon.
*
* @param _hookCtx - The context object for the hook, containing metadata about the request.
* @param request - The original Request object.
* @returns A new Request object with modified form data and headers.
*/
async beforeRequest(_hookCtx, request) {
const requestClone = request.clone();
const formData = await requestClone.formData();
flattenArrayParameter(formData, "extract_image_block_types");
const headers = (0, request_js_1.prepareRequestHeaders)(requestClone);
return new Request(requestClone, {
body: formData,
headers: headers,
});
}
}
exports.FixArrayParamsHook = FixArrayParamsHook;
//# sourceMappingURL=FixArrayParamsHook.js.map
;