fix: multipart/form-data body interpolation (#3142)

* feat: updates

* feat: updates

* feat: updates

* feat: updates
This commit is contained in:
lohit
2024-09-23 15:54:54 +05:30
committed by GitHub
parent eb33504f19
commit ed20eccc25
13 changed files with 165 additions and 47 deletions

View File

@@ -1,3 +1,8 @@
const fs = require('fs');
const FormData = require('form-data');
const { forOwn } = require('lodash');
const path = require('path');
const lpad = (str, width) => {
let paddedStr = str;
while (paddedStr.length < width) {
@@ -14,7 +19,33 @@ const rpad = (str, width) => {
return paddedStr;
};
const createFormData = (datas, collectionPath) => {
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
const form = new FormData();
forOwn(datas, (value, key) => {
if (typeof value == 'string') {
form.append(key, value);
return;
}
const filePaths = value || [];
filePaths?.forEach?.((filePath) => {
let trimmedFilePath = filePath.trim();
if (!path.isAbsolute(trimmedFilePath)) {
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
}
form.append(key, fs.createReadStream(trimmedFilePath), path.basename(trimmedFilePath));
});
});
return form;
};
module.exports = {
lpad,
rpad
rpad,
createFormData
};