diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index df8bfea09..aea3859ac 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -102,6 +102,7 @@ const registerNetworkIpc = (mainWindow) => { const _request = item.draft ? item.draft.request : item.request; const request = prepareRequest(_request); const envVars = getEnvVars(environment); + const processEnvVars = getProcessEnvVars(collectionUid); try { // make axios work in node using form data @@ -128,7 +129,8 @@ const registerNetworkIpc = (mainWindow) => { request, envVars, collectionVariables, - collectionPath + collectionPath, + processEnvVars ); if (result) { @@ -151,7 +153,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:script-environment-update', { @@ -162,8 +165,6 @@ const registerNetworkIpc = (mainWindow) => { }); } - const processEnvVars = getProcessEnvVars(collectionUid); - const brunoConfig = getBrunoConfig(collectionUid); const proxyEnabled = get(brunoConfig, 'proxy.enabled', false); if (proxyEnabled) { @@ -250,7 +251,8 @@ const registerNetworkIpc = (mainWindow) => { response, envVars, collectionVariables, - collectionPath + collectionPath, + processEnvVars ); if (result) { @@ -274,7 +276,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:script-environment-update', { @@ -318,7 +321,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:run-request-event', { @@ -391,7 +395,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:run-request-event', { @@ -536,6 +541,7 @@ const registerNetworkIpc = (mainWindow) => { const _request = item.draft ? item.draft.request : item.request; const request = prepareRequest(_request); + const processEnvVars = getProcessEnvVars(collectionUid); try { // make axios work in node using form data @@ -580,7 +586,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:script-environment-update', { @@ -590,8 +597,6 @@ const registerNetworkIpc = (mainWindow) => { }); } - const processEnvVars = getProcessEnvVars(collectionUid); - // interpolate variables inside request interpolateVars(request, envVars, collectionVariables, processEnvVars); @@ -633,7 +638,8 @@ const registerNetworkIpc = (mainWindow) => { response, envVars, collectionVariables, - collectionPath + collectionPath, + processEnvVars ); if (result) { @@ -656,7 +662,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:script-environment-update', { @@ -698,7 +705,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:run-folder-event', { @@ -776,7 +784,8 @@ const registerNetworkIpc = (mainWindow) => { envVars, collectionVariables, collectionPath, - onConsoleLog + onConsoleLog, + processEnvVars ); mainWindow.webContents.send('main:run-folder-event', { diff --git a/packages/bruno-js/package.json b/packages/bruno-js/package.json index daf2b05ec..ecc4afc14 100644 --- a/packages/bruno-js/package.json +++ b/packages/bruno-js/package.json @@ -21,6 +21,7 @@ "btoa": "^1.2.1", "chai": "^4.3.7", "crypto-js": "^4.1.1", + "handlebars": "^4.7.8", "json-query": "^2.2.2", "lodash": "^4.17.21", "moment": "^2.29.4", diff --git a/packages/bruno-js/src/bru.js b/packages/bruno-js/src/bru.js index af6134633..6eec791fa 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -1,19 +1,39 @@ +const Handlebars = require('handlebars'); +const { cloneDeep } = require('lodash'); + class Bru { - constructor(envVariables, collectionVariables) { + constructor(envVariables, collectionVariables, processEnvVars) { this.envVariables = envVariables; this.collectionVariables = collectionVariables; + this.processEnvVars = cloneDeep(processEnvVars || {}); } + _interpolateEnvVar = (str) => { + if (!str || !str.length || typeof str !== 'string') { + return str; + } + + const template = Handlebars.compile(str, { noEscape: true }); + + return template({ + process: { + env: { + ...this.processEnvVars + } + } + }); + }; + getEnvName() { return this.envVariables.__name__; } getProcessEnv(key) { - return process.env[key]; + return this.processEnvVars[key]; } getEnvVar(key) { - return this.envVariables[key]; + return this._interpolateEnvVar(this.envVariables[key]); } setEnvVar(key, value) { diff --git a/packages/bruno-js/src/runtime/script-runtime.js b/packages/bruno-js/src/runtime/script-runtime.js index dcca7a2ec..41af51eee 100644 --- a/packages/bruno-js/src/runtime/script-runtime.js +++ b/packages/bruno-js/src/runtime/script-runtime.js @@ -27,8 +27,16 @@ const CryptoJS = require('crypto-js'); class ScriptRuntime { constructor() {} - async runRequestScript(script, request, envVariables, collectionVariables, collectionPath, onConsoleLog) { - const bru = new Bru(envVariables, collectionVariables); + async runRequestScript( + script, + request, + envVariables, + collectionVariables, + collectionPath, + onConsoleLog, + processEnvVars + ) { + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const context = { @@ -89,8 +97,17 @@ class ScriptRuntime { }; } - async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog) { - const bru = new Bru(envVariables, collectionVariables); + async runResponseScript( + script, + request, + response, + envVariables, + collectionVariables, + collectionPath, + onConsoleLog, + processEnvVars + ) { + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const res = new BrunoResponse(response); diff --git a/packages/bruno-js/src/runtime/test-runtime.js b/packages/bruno-js/src/runtime/test-runtime.js index 6d7b86611..3a7c1f9b5 100644 --- a/packages/bruno-js/src/runtime/test-runtime.js +++ b/packages/bruno-js/src/runtime/test-runtime.js @@ -20,8 +20,17 @@ const CryptoJS = require('crypto-js'); class TestRuntime { constructor() {} - runTests(testsFile, request, response, envVariables, collectionVariables, collectionPath, onConsoleLog) { - const bru = new Bru(envVariables, collectionVariables); + runTests( + testsFile, + request, + response, + envVariables, + collectionVariables, + collectionPath, + onConsoleLog, + processEnvVars + ) { + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const res = new BrunoResponse(response); diff --git a/packages/bruno-js/src/runtime/vars-runtime.js b/packages/bruno-js/src/runtime/vars-runtime.js index be23a6fe6..0185ebddc 100644 --- a/packages/bruno-js/src/runtime/vars-runtime.js +++ b/packages/bruno-js/src/runtime/vars-runtime.js @@ -4,13 +4,13 @@ const BrunoRequest = require('../bruno-request'); const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils'); class VarsRuntime { - runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath) { + runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars) { const enabledVars = _.filter(vars, (v) => v.enabled); if (!enabledVars.length) { return; } - const bru = new Bru(envVariables, collectionVariables); + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const bruContext = { @@ -34,13 +34,13 @@ class VarsRuntime { }; } - runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath) { + runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath, processEnvVars) { const enabledVars = _.filter(vars, (v) => v.enabled); if (!enabledVars.length) { return; } - const bru = new Bru(envVariables, collectionVariables); + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const res = createResponseParser(response);