From a45628dd853104d7a10d0c7870b673df18485550 Mon Sep 17 00:00:00 2001 From: Anoop M D Date: Wed, 25 Jan 2023 10:39:07 +0530 Subject: [PATCH] feat: moved env var interpolation logic to electron --- .../ReduxStore/slices/collections/actions.js | 9 +-- .../bruno-app/src/utils/collections/index.js | 55 --------------- packages/bruno-app/src/utils/network/index.js | 8 +-- packages/bruno-electron/package.json | 1 + .../bruno-electron/src/ipc/network/index.js | 4 +- .../src/ipc/network/interpolate-vars.js | 68 +++++++++++++++++++ .../src/ipc/network/prepare-request.js | 4 +- 7 files changed, 80 insertions(+), 69 deletions(-) create mode 100644 packages/bruno-electron/src/ipc/network/interpolate-vars.js diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 67b27e2c..31c570d5 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -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); - } - } + const environment = findEnvironmentInCollection(collectionCopy, collection.activeEnvironmentUid); - sendNetworkRequest(itemCopy, collectionUid) + sendNetworkRequest(itemCopy, collectionUid, environment) .then((response) => { return dispatch( responseReceived({ diff --git a/packages/bruno-app/src/utils/collections/index.js b/packages/bruno-app/src/utils/collections/index.js index 45cb01df..55081ce5 100644 --- a/packages/bruno-app/src/utils/collections/index.js +++ b/packages/bruno-app/src/utils/collections/index.js @@ -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', []); diff --git a/packages/bruno-app/src/utils/network/index.js b/packages/bruno-app/src/utils/network/index.js index d8c989fd..43589b78 100644 --- a/packages/bruno-app/src/utils/network/index.js +++ b/packages/bruno-app/src/utils/network/index.js @@ -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); }); diff --git a/packages/bruno-electron/package.json b/packages/bruno-electron/package.json index 2b3707b6..104d1058 100644 --- a/packages/bruno-electron/package.json +++ b/packages/bruno-electron/package.json @@ -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" diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 98409472..a4cfce6e 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -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 diff --git a/packages/bruno-electron/src/ipc/network/interpolate-vars.js b/packages/bruno-electron/src/ipc/network/interpolate-vars.js new file mode 100644 index 00000000..5f2df21d --- /dev/null +++ b/packages/bruno-electron/src/ipc/network/interpolate-vars.js @@ -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; diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 778f8946..7fefd1d7 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -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) {