bruno/main/ipc.js

48 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-03-22 13:48:20 +01:00
const axios = require('axios');
const FormData = require('form-data');
2022-03-22 13:48:20 +01:00
const { ipcMain } = require('electron');
const { forOwn, extend } = require('lodash');
2022-03-22 13:48:20 +01:00
const registerIpc = () => {
// handler for sending http request
ipcMain.handle('send-http-request', async (event, request) => {
try {
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
if(request.headers && request.headers['content-type'] === 'multipart/form-data') {
const form = new FormData();
forOwn(request.data, (value, key) => {
form.append(key, value);
});
extend(request.headers, form.getHeaders());
request.data = form;
}
2022-03-22 13:48:20 +01:00
const result = await axios(request);
return {
status: result.status,
headers: result.headers,
data: result.data
};
} catch (error) {
if(error.response) {
return {
status: error.response.status,
headers: error.response.headers,
data: error.response.data
};
}
return {
status: -1,
headers: [],
data: null
};
}
});
};
module.exports = registerIpc;