@cognigy/rest-api-client
Version:
Cognigy REST-Client
38 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericUploadFn = void 0;
/* Node Modules */
const isBrowser = typeof window !== "undefined";
const FormData = isBrowser ? window.FormData : require("form-data");
const GenericUploadFn = (url, method, _this) => {
return async (data, options = {}) => {
var _a;
options.withAuthentication = (_a = options.withAuthentication) !== null && _a !== void 0 ? _a : true;
const httpAdapter = _this.getHttpAdapter();
const formData = new FormData();
for (const key of Object.keys(data)) {
if (data[key]) {
if (isBrowser) {
// Buffer is undefined in browser environment
const blobData = typeof Buffer !== "undefined" && data[key] instanceof Buffer
? new Blob([data[key]])
: data[key];
formData.append(key, blobData);
}
else {
formData.append(key, data[key], key === "file" ? data.fileName || "fake.txt" : undefined);
}
}
}
const payload = Object.assign(Object.assign({}, options), { headers: Object.assign({ Accept: "application/json" }, options === null || options === void 0 ? void 0 : options.headers), method,
url, data: formData });
if (formData.getHeaders) {
payload.headers = Object.assign(Object.assign({}, payload.headers), formData.getHeaders());
}
const response = await httpAdapter.request(payload, _this);
const responseData = response.data;
return responseData;
};
};
exports.GenericUploadFn = GenericUploadFn;
//# sourceMappingURL=GenericUploadFn.js.map