chore: updates

This commit is contained in:
lohxt1
2024-12-15 18:19:55 +05:30
parent 83bbbe3fb3
commit 7ae64605c2
3 changed files with 44 additions and 31 deletions

View File

@@ -0,0 +1,40 @@
const { forEach } = require('lodash');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const createFormData = (data, collectionPath) => {
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
const form = new FormData();
forEach(data, (datum) => {
const { name, type, value } = datum;
if (type === 'text') {
if (Array.isArray(value)) {
value.forEach((val) => form.append(name, val));
} else {
form.append(name, value);
}
return;
}
if (type === 'file') {
const filePaths = value || [];
filePaths.forEach((filePath) => {
let trimmedFilePath = filePath.trim();
console.log(trimmedFilePath, collectionPath);
if (!path.isAbsolute(trimmedFilePath)) {
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
}
form.append(name, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
});
}
});
return form;
};
module.exports = {
createFormData
}