boldsign
Version:
NodeJS client for boldsign
137 lines • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toFormData = exports.generateFormData = exports.USER_AGENT = exports.queryParamsSerializer = exports.HttpError = void 0;
class HttpError extends Error {
constructor(response, body, statusCode) {
super(JSON.stringify(body, null, 2));
this.response = response;
this.body = body;
this.statusCode = statusCode;
this.name = 'HttpError';
}
}
exports.HttpError = HttpError;
const model_1 = require("../model");
const formData = require("form-data");
const Qs = require("qs");
const queryParamsSerializer = (params) => {
return Qs.stringify(params, { arrayFormat: 'repeat' });
};
exports.queryParamsSerializer = queryParamsSerializer;
exports.USER_AGENT = "boldsign-node-sdk/3.0.1";
const generateFormData = (obj, typemap) => {
var _a;
const data = {};
let localVarUseFormData = false;
let attributeTypeMap;
if (Array.isArray(typemap)) {
const itemType = (_a = typemap[0]) === null || _a === void 0 ? void 0 : _a.constructor;
if (itemType && itemType.attributeTypeMap) {
attributeTypeMap = itemType.attributeTypeMap;
}
}
else if (typemap && typemap.attributeTypeMap) {
attributeTypeMap = typemap.attributeTypeMap;
}
if (typeof obj !== "object" || Array.isArray(obj) || obj === null) {
return {
localVarUseFormData,
data,
};
}
attributeTypeMap.forEach((paramInfo) => {
if (obj[paramInfo.name] === undefined) {
return;
}
if (paramInfo.type.indexOf("RequestFile") !== -1) {
localVarUseFormData = true;
if (Array.isArray(obj[paramInfo.name])) {
const fileList = [];
obj[paramInfo.name].forEach((childObject, i) => {
fileList.push(childObject);
});
data[paramInfo.name] = fileList;
return;
}
data[paramInfo.baseName] = obj[paramInfo.name];
return;
}
if (paramInfo.type.indexOf("Array") !== -1) {
const serializedArray = [];
obj[paramInfo.name].forEach((childObject, i) => {
const key = `${paramInfo.baseName}[${i}]`;
const serializedItem = typeof childObject === "object" ? JSON.stringify(childObject) : childObject;
serializedArray.push(serializedItem);
});
data[paramInfo.baseName] = serializedArray;
return;
}
if (paramInfo.type.indexOf("boolean") !== -1) {
data[paramInfo.baseName] = JSON.stringify(obj[paramInfo.name]);
return;
}
if (paramInfo.type.includes("{ [key: string]:") && typeof obj[paramInfo.name] === "object" && obj[paramInfo.name] !== null) {
Object.keys(obj[paramInfo.name]).forEach((key) => {
const value = obj[paramInfo.name][key];
if (value !== null && value !== undefined) {
data[`${paramInfo.baseName}[${key}]`] = value.toString();
}
});
return;
}
const serialized = model_1.ObjectSerializer.serialize(obj[paramInfo.name], paramInfo.type);
data[paramInfo.baseName] = shouldJsonify(serialized)
? JSON.stringify(serialized)
: serialized;
});
return {
localVarUseFormData,
data,
};
};
exports.generateFormData = generateFormData;
const toFormData = (obj) => {
const form = new formData();
Object.keys(obj).forEach((key) => {
if (Array.isArray(obj[key])) {
obj[key].forEach(function (item, index) {
if (isBufferDetailedFile(item)) {
form.append(key, item.value, item.options);
}
else if (item && typeof item === 'object' && typeof item.pipe === 'function') {
form.append(key, item);
}
else {
form.append(key, item);
}
});
}
else if (isBufferDetailedFile(obj[key])) {
form.append(key, obj[key].value, obj[key].options);
return;
}
else if (obj[key] && typeof obj[key] === 'object' && typeof obj[key].pipe === 'function') {
form.append(key, obj[key]);
}
else {
const value = (typeof obj[key] !== 'object') ? obj[key].toString() : obj[key];
form.append(key, value);
}
});
return form;
};
exports.toFormData = toFormData;
function isBufferDetailedFile(obj) {
var _a, _b;
return obj !== null
&& obj !== undefined
&& typeof obj === 'object'
&& obj.value !== undefined
&& Buffer.isBuffer(obj.value)
&& obj.options !== undefined
&& typeof obj.options === 'object'
&& ((_a = obj.options) === null || _a === void 0 ? void 0 : _a.filename) !== undefined
&& ((_b = obj.options) === null || _b === void 0 ? void 0 : _b.contentType) !== undefined;
}
const shouldJsonify = (val) => val === Object(val);
//# sourceMappingURL=apis.js.map