2022-03-19 14:03:16 +01:00
|
|
|
import each from 'lodash/each';
|
2022-03-18 10:15:27 +01:00
|
|
|
import { rawRequest, gql } from 'graphql-request';
|
|
|
|
|
|
|
|
const sendNetworkRequest = async (item) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if(item.type === 'http-request') {
|
|
|
|
const timeStart = Date.now();
|
2022-03-18 14:08:49 +01:00
|
|
|
sendHttpRequest(item.draft ? item.draft.request : item.request)
|
2022-03-18 10:15:27 +01:00
|
|
|
.then((response) => {
|
|
|
|
const timeEnd = Date.now();
|
|
|
|
resolve({
|
|
|
|
state: 'success',
|
|
|
|
data: response.data,
|
|
|
|
headers: Object.entries(response.headers),
|
|
|
|
size: response.headers["content-length"],
|
|
|
|
status: response.status,
|
|
|
|
duration: timeEnd - timeStart
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => reject(err));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const sendHttpRequest = async (request) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const { ipcRenderer } = window.require("electron");
|
|
|
|
|
|
|
|
console.log(request);
|
2022-03-19 14:03:16 +01:00
|
|
|
const headers = {};
|
|
|
|
each(request.headers, (h) => {
|
|
|
|
if(h.enabled) {
|
|
|
|
headers[h.name] = h.value;
|
|
|
|
}
|
|
|
|
});
|
2022-03-18 10:15:27 +01:00
|
|
|
|
|
|
|
let options = {
|
|
|
|
method: request.method,
|
|
|
|
url: request.url,
|
2022-03-19 14:03:16 +01:00
|
|
|
headers: headers
|
2022-03-18 10:15:27 +01:00
|
|
|
};
|
|
|
|
|
2022-03-20 09:32:33 +01:00
|
|
|
if(request.body && request.body.mode === 'json' && request.body.content) {
|
|
|
|
options.data = request.body.content;
|
|
|
|
}
|
|
|
|
|
2022-03-18 10:15:27 +01:00
|
|
|
ipcRenderer
|
|
|
|
.invoke('send-http-request', options)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const sendGraphqlRequest = async (request,) => {
|
|
|
|
const query = gql`${request.request.body.graphql.query}`;
|
|
|
|
|
|
|
|
const { data, errors, extensions, headers, status } = await rawRequest(request.request.url, query);
|
|
|
|
|
|
|
|
return {
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
data,
|
|
|
|
errors
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export {
|
|
|
|
sendNetworkRequest
|
|
|
|
};
|