mirror of
https://github.com/usebruno/bruno.git
synced 2024-12-22 23:02:40 +01:00
chore: updates
This commit is contained in:
parent
83bbbe3fb3
commit
7ae64605c2
@ -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') {
|
||||
|
40
packages/bruno-cli/src/utils/form-data.js
Normal file
40
packages/bruno-cli/src/utils/form-data.js
Normal 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
|
||||
}
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user