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

@ -2,35 +2,7 @@ const { get, each, filter } = require('lodash');
const decomment = require('decomment');
const crypto = require('node:crypto');
const { mergeHeaders, mergeScripts, mergeVars, getTreePathFromCollectionToItem } = require('../utils/collection');
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();
datas.forEach((item) => {
const value = item.value;
const name = item.name;
let options = {};
if (item.contentType) {
options.contentType = item.contentType;
}
if (item.type === 'file') {
const filePaths = value || [];
filePaths.forEach((filePath) => {
let trimmedFilePath = filePath.trim();
if (!path.isAbsolute(trimmedFilePath)) {
trimmedFilePath = path.join(collectionPath, trimmedFilePath);
}
options.filename = path.basename(trimmedFilePath);
form.append(name, fs.createReadStream(trimmedFilePath), options);
});
} else {
form.append(name, value, options);
}
});
return form;
};
const { createFormData } = require('../utils/form-data');
const prepareRequest = (item = {}, collection = {}) => {
const request = item?.request;
@ -164,7 +136,8 @@ const prepareRequest = (item = {}, collection = {}) => {
if (request.body.mode === 'multipartForm') {
axiosRequest.headers['content-type'] = 'multipart/form-data';
const enabledParams = filter(request.body.multipartForm, (p) => p.enabled);
axiosRequest.data = createFormData(enabledParams);
const collectionPath = process.cwd();
axiosRequest.data = createFormData(enabledParams, collectionPath);
}
if (request.body.mode === 'graphql') {

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
}

View File

@ -1,4 +1,4 @@
const { forOwn } = require('lodash');
const { forEach } = require('lodash');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');