mirror of
https://github.com/usebruno/bruno.git
synced 2024-12-22 23:02:40 +01:00
refactor createFormData to handle data as an array of objects and improve file handling
This commit is contained in:
parent
09120a96e8
commit
57e6af703c
@ -22,26 +22,34 @@ const buildFormUrlEncodedPayload = (params) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const createFormData = (datas, collectionPath) => {
|
const createFormData = (data, collectionPath) => {
|
||||||
// make axios work in node using form data
|
// make axios work in node using form data
|
||||||
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
|
||||||
const form = new FormData();
|
const form = new FormData();
|
||||||
forOwn(datas, (value, key) => {
|
forEach(data, (datum) => {
|
||||||
if (typeof value == 'string') {
|
const { name, type, value } = datum;
|
||||||
form.append(key, value);
|
|
||||||
|
if (type === 'text') {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
value.forEach((val) => form.append(name, val));
|
||||||
|
} else {
|
||||||
|
form.append(name, value);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filePaths = value || [];
|
if (type === 'file') {
|
||||||
filePaths?.forEach?.((filePath) => {
|
const filePaths = value || [];
|
||||||
let trimmedFilePath = filePath.trim();
|
filePaths.forEach((filePath) => {
|
||||||
|
let trimmedFilePath = filePath.trim();
|
||||||
|
|
||||||
if (!path.isAbsolute(trimmedFilePath)) {
|
if (!path.isAbsolute(trimmedFilePath)) {
|
||||||
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
|
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
form.append(key, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
|
form.append(name, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return form;
|
return form;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user