From ca22ad06df95da943559103e31aa52313199018b Mon Sep 17 00:00:00 2001 From: lohit Date: Fri, 21 Jun 2024 11:15:23 +0530 Subject: [PATCH] update interpolate function in bruno js (#2479) * update interpolate function in bruno js * removeed handlebars import, updated validations * removed handlebars import --- .../src/runner/run-single-request.js | 2 +- .../bruno-electron/src/ipc/network/index.js | 4 +- packages/bruno-js/package.json | 2 +- packages/bruno-js/src/bru.js | 22 ++++++----- packages/bruno-js/src/interpolate-string.js | 38 ++----------------- .../bruno-js/src/runtime/assert-runtime.js | 5 ++- 6 files changed, 22 insertions(+), 51 deletions(-) diff --git a/packages/bruno-cli/src/runner/run-single-request.js b/packages/bruno-cli/src/runner/run-single-request.js index 59cd21592..33e8216c3 100644 --- a/packages/bruno-cli/src/runner/run-single-request.js +++ b/packages/bruno-cli/src/runner/run-single-request.js @@ -322,7 +322,7 @@ const runSingleRequest = async function ( response, envVariables, collectionVariables, - collectionPath + processEnvVars ); each(assertionResults, (r) => { diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index f4e36c667..4d4e8d8e4 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -589,7 +589,7 @@ const registerNetworkIpc = (mainWindow) => { response, envVars, collectionVariables, - collectionPath + processEnvVars ); mainWindow.webContents.send('main:run-request-event', { @@ -1028,7 +1028,7 @@ const registerNetworkIpc = (mainWindow) => { response, envVars, collectionVariables, - collectionPath + processEnvVars ); mainWindow.webContents.send('main:run-folder-event', { diff --git a/packages/bruno-js/package.json b/packages/bruno-js/package.json index 8682608d2..901ab0073 100644 --- a/packages/bruno-js/package.json +++ b/packages/bruno-js/package.json @@ -14,6 +14,7 @@ "test": "jest --testPathIgnorePatterns test.js" }, "dependencies": { + "@usebruno/common": "0.1.0", "@usebruno/query": "0.1.0", "ajv": "^8.12.0", "ajv-formats": "^2.1.1", @@ -23,7 +24,6 @@ "chai": "^4.3.7", "chai-string": "^1.5.0", "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 964d7b609..53039fd06 100644 --- a/packages/bruno-js/src/bru.js +++ b/packages/bruno-js/src/bru.js @@ -1,30 +1,32 @@ -const Handlebars = require('handlebars'); const { cloneDeep } = require('lodash'); +const { interpolate } = require('@usebruno/common'); const variableNameRegex = /^[\w-.]*$/; class Bru { constructor(envVariables, collectionVariables, processEnvVars, collectionPath) { - this.envVariables = envVariables; - this.collectionVariables = collectionVariables; + this.envVariables = envVariables || {}; + this.collectionVariables = collectionVariables || {}; this.processEnvVars = cloneDeep(processEnvVars || {}); this.collectionPath = collectionPath; } - _interpolateEnvVar = (str) => { + _interpolate = (str) => { if (!str || !str.length || typeof str !== 'string') { return str; } - const template = Handlebars.compile(str, { noEscape: true }); - - return template({ + const combinedVars = { + ...this.envVariables, + ...this.collectionVariables, process: { env: { ...this.processEnvVars } } - }); + }; + + return interpolate(str, combinedVars); }; cwd() { @@ -40,7 +42,7 @@ class Bru { } getEnvVar(key) { - return this._interpolateEnvVar(this.envVariables[key]); + return this._interpolate(this.envVariables[key]); } setEnvVar(key, value) { @@ -74,7 +76,7 @@ class Bru { ); } - return this.collectionVariables[key]; + return this._interpolate(this.collectionVariables[key]); } setNextRequest(nextRequest) { diff --git a/packages/bruno-js/src/interpolate-string.js b/packages/bruno-js/src/interpolate-string.js index 22910c1a3..ff6980e53 100644 --- a/packages/bruno-js/src/interpolate-string.js +++ b/packages/bruno-js/src/interpolate-string.js @@ -1,42 +1,10 @@ -const Handlebars = require('handlebars'); -const { forOwn, cloneDeep } = require('lodash'); +const { interpolate } = require('@usebruno/common'); -const interpolateEnvVars = (str, processEnvVars) => { +const interpolateString = (str, { envVariables = {}, collectionVariables = {}, processEnvVars = {} }) => { if (!str || !str.length || typeof str !== 'string') { return str; } - const template = Handlebars.compile(str, { noEscape: true }); - - return template({ - process: { - env: { - ...processEnvVars - } - } - }); -}; - -const interpolateString = (str, { envVariables, collectionVariables, processEnvVars }) => { - if (!str || !str.length || typeof str !== 'string') { - return str; - } - - processEnvVars = processEnvVars || {}; - collectionVariables = collectionVariables || {}; - - // we clone envVariables because we don't want to modify the original object - envVariables = envVariables ? cloneDeep(envVariables) : {}; - - // envVariables can inturn have values as {{process.env.VAR_NAME}} - // so we need to interpolate envVariables first with processEnvVars - forOwn(envVariables, (value, key) => { - envVariables[key] = interpolateEnvVars(value, processEnvVars); - }); - - const template = Handlebars.compile(str, { noEscape: true }); - - // collectionVariables take precedence over envVariables const combinedVars = { ...envVariables, ...collectionVariables, @@ -47,7 +15,7 @@ const interpolateString = (str, { envVariables, collectionVariables, processEnvV } }; - return template(combinedVars); + return interpolate(str, combinedVars); }; module.exports = { diff --git a/packages/bruno-js/src/runtime/assert-runtime.js b/packages/bruno-js/src/runtime/assert-runtime.js index dae68fc1a..e37b33182 100644 --- a/packages/bruno-js/src/runtime/assert-runtime.js +++ b/packages/bruno-js/src/runtime/assert-runtime.js @@ -203,13 +203,13 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => { }; class AssertRuntime { - runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath) { + runAssertions(assertions, request, response, envVariables, collectionVariables, processEnvVars) { const enabledAssertions = _.filter(assertions, (a) => a.enabled); if (!enabledAssertions.length) { return []; } - const bru = new Bru(envVariables, collectionVariables); + const bru = new Bru(envVariables, collectionVariables, processEnvVars); const req = new BrunoRequest(request); const res = createResponseParser(response); @@ -222,6 +222,7 @@ class AssertRuntime { const context = { ...envVariables, ...collectionVariables, + ...processEnvVars, ...bruContext };