feat: moved env var interpolation logic to electron

This commit is contained in:
Anoop M D 2023-01-25 10:39:07 +05:30
parent 977637e556
commit a45628dd85
7 changed files with 80 additions and 69 deletions

View File

@ -108,14 +108,9 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
const itemCopy = cloneDeep(item);
const collectionCopy = cloneDeep(collection);
if (collection.activeEnvironmentUid) {
const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid);
if (environment) {
interpolateEnvironmentVars(itemCopy, environment.variables);
}
}
sendNetworkRequest(itemCopy, collectionUid)
sendNetworkRequest(itemCopy, collectionUid, environment)
.then((response) => {
return dispatch(
responseReceived({

View File

@ -454,61 +454,6 @@ export const isLocalCollection = (collection) => {
return collection.pathname ? true : false;
};
export const interpolateEnvironmentVars = (item, variables) => {
const envVars = {
interpolation: { escapeValue: true }
};
const _item = item.draft ? item.draft : item;
const { request } = _item;
each(variables, (variable) => {
envVars[variable.name] = variable.value;
});
// TODO: Find a better interpolation library
const interpolate = (str) => {
if(!str || !str.length || typeof str !== "string") {
return str;
}
return str.reckon(envVars);
};
request.url = interpolate(request.url);
each(request.headers, (header) => {
header.value = interpolate(header.value);
});
each(request.params, (param) => {
param.value = interpolate(param.value);
});
// Todo: Make interpolation work with body mode json
switch (request.body.mode) {
case 'text': {
request.body.text = interpolate(request.body.text);
break;
}
case 'xml': {
request.body.text = interpolate(request.body.text);
break;
}
case 'multipartForm': {
each(request.body.multipartForm, (param) => {
param.value = interpolate(param.value);
});
break;
}
case 'formUrlEncoded': {
each(request.body.formUrlEncoded, (param) => {
param.value = interpolate(param.value);
});
break;
}
}
return request;
};
export const deleteUidsInItem = (item) => {
delete item.uid;
const params = get(item, 'request.params', []);

View File

@ -1,8 +1,8 @@
export const sendNetworkRequest = async (item, collectionUid) => {
export const sendNetworkRequest = async (item, collectionUid, environment) => {
return new Promise((resolve, reject) => {
if (['http-request', 'graphql-request'].includes(item.type)) {
const timeStart = Date.now();
sendHttpRequest(item, collectionUid)
sendHttpRequest(item, collectionUid, environment)
.then((response) => {
const timeEnd = Date.now();
resolve({
@ -20,12 +20,12 @@ export const sendNetworkRequest = async (item, collectionUid) => {
});
};
const sendHttpRequest = async (item, collectionUid) => {
const sendHttpRequest = async (item, collectionUid, environment) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
ipcRenderer
.invoke('send-http-request', item, collectionUid)
.invoke('send-http-request', item, collectionUid, environment)
.then(resolve)
.catch(reject);
});

View File

@ -25,6 +25,7 @@
"fs-extra": "^10.1.0",
"is-valid-path": "^0.1.1",
"lodash": "^4.17.21",
"mustache": "^4.2.0",
"nanoid": "3.3.4",
"vm2": "^3.9.13",
"yup": "^0.32.11"

View File

@ -9,12 +9,12 @@ const { uuid } = require('../../utils/common');
const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
// handler for sending http request
ipcMain.handle('send-http-request', async (event, item, collectionUid) => {
ipcMain.handle('send-http-request', async (event, item, collectionUid, environment) => {
const cancelTokenUid = uuid();
try {
const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request);
const request = prepareRequest(_request, environment);
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427

View File

@ -0,0 +1,68 @@
const Mustache = require('mustache');
const { each } = require('lodash');
// override the default escape function to prevent escaping
Mustache.escape = function (value) {
return value;
};
const interpolateVars = (request, environment) => {
if(!environment) {
return request;
}
const variables = environment.variables;
if(!variables || !variables.length) {
return request;
}
const envVars = {};
each(variables, (variable) => {
envVars[variable.name] = Mustache.escape(variable.value);
});
const interpolate = (str) => {
if(!str || !str.length || typeof str !== "string") {
return str;
}
return Mustache.render(str, envVars);
};
request.url = interpolate(request.url);
each(request.headers, (header) => {
header.value = interpolate(header.value);
});
each(request.params, (param) => {
param.value = interpolate(param.value);
});
// Todo: Make interpolation work with body mode json
switch (request.body.mode) {
case 'text': {
request.body.text = interpolate(request.body.text);
break;
}
case 'xml': {
request.body.text = interpolate(request.body.text);
break;
}
case 'multipartForm': {
each(request.body.multipartForm, (param) => {
param.value = interpolate(param.value);
});
break;
}
case 'formUrlEncoded': {
each(request.body.formUrlEncoded, (param) => {
param.value = interpolate(param.value);
});
break;
}
}
return request;
};
module.exports = interpolateVars;

View File

@ -1,7 +1,9 @@
const { get, each, filter } = require('lodash');
const qs = require('qs');
const interpolateVars = require('./interpolate-vars');
const prepareRequest = (request) => {
const prepareRequest = (request, environment) => {
interpolateVars(request, environment);
const headers = {};
each(request.headers, (h) => {
if (h.enabled) {