mirror of
https://github.com/usebruno/bruno.git
synced 2025-08-08 03:04:36 +02:00
chore: updates
This commit is contained in:
@ -2,35 +2,7 @@ const { get, each, filter } = require('lodash');
|
|||||||
const decomment = require('decomment');
|
const decomment = require('decomment');
|
||||||
const crypto = require('node:crypto');
|
const crypto = require('node:crypto');
|
||||||
const { mergeHeaders, mergeScripts, mergeVars, getTreePathFromCollectionToItem } = require('../utils/collection');
|
const { mergeHeaders, mergeScripts, mergeVars, getTreePathFromCollectionToItem } = require('../utils/collection');
|
||||||
|
const { createFormData } = require('../utils/form-data');
|
||||||
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 prepareRequest = (item = {}, collection = {}) => {
|
const prepareRequest = (item = {}, collection = {}) => {
|
||||||
const request = item?.request;
|
const request = item?.request;
|
||||||
@ -164,7 +136,8 @@ const prepareRequest = (item = {}, collection = {}) => {
|
|||||||
if (request.body.mode === 'multipartForm') {
|
if (request.body.mode === 'multipartForm') {
|
||||||
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
axiosRequest.headers['content-type'] = 'multipart/form-data';
|
||||||
const enabledParams = filter(request.body.multipartForm, (p) => p.enabled);
|
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') {
|
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 FormData = require('form-data');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
Reference in New Issue
Block a user