update interpolate function in bruno js (#2479)

* update interpolate function in bruno js

* removeed handlebars import, updated validations

* removed handlebars import
This commit is contained in:
lohit 2024-06-21 11:15:23 +05:30 committed by GitHub
parent 5259c5fb4a
commit ca22ad06df
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 22 additions and 51 deletions

View File

@ -322,7 +322,7 @@ const runSingleRequest = async function (
response, response,
envVariables, envVariables,
collectionVariables, collectionVariables,
collectionPath processEnvVars
); );
each(assertionResults, (r) => { each(assertionResults, (r) => {

View File

@ -589,7 +589,7 @@ const registerNetworkIpc = (mainWindow) => {
response, response,
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath processEnvVars
); );
mainWindow.webContents.send('main:run-request-event', { mainWindow.webContents.send('main:run-request-event', {
@ -1028,7 +1028,7 @@ const registerNetworkIpc = (mainWindow) => {
response, response,
envVars, envVars,
collectionVariables, collectionVariables,
collectionPath processEnvVars
); );
mainWindow.webContents.send('main:run-folder-event', { mainWindow.webContents.send('main:run-folder-event', {

View File

@ -14,6 +14,7 @@
"test": "jest --testPathIgnorePatterns test.js" "test": "jest --testPathIgnorePatterns test.js"
}, },
"dependencies": { "dependencies": {
"@usebruno/common": "0.1.0",
"@usebruno/query": "0.1.0", "@usebruno/query": "0.1.0",
"ajv": "^8.12.0", "ajv": "^8.12.0",
"ajv-formats": "^2.1.1", "ajv-formats": "^2.1.1",
@ -23,7 +24,6 @@
"chai": "^4.3.7", "chai": "^4.3.7",
"chai-string": "^1.5.0", "chai-string": "^1.5.0",
"crypto-js": "^4.1.1", "crypto-js": "^4.1.1",
"handlebars": "^4.7.8",
"json-query": "^2.2.2", "json-query": "^2.2.2",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"moment": "^2.29.4", "moment": "^2.29.4",

View File

@ -1,30 +1,32 @@
const Handlebars = require('handlebars');
const { cloneDeep } = require('lodash'); const { cloneDeep } = require('lodash');
const { interpolate } = require('@usebruno/common');
const variableNameRegex = /^[\w-.]*$/; const variableNameRegex = /^[\w-.]*$/;
class Bru { class Bru {
constructor(envVariables, collectionVariables, processEnvVars, collectionPath) { constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
this.envVariables = envVariables; this.envVariables = envVariables || {};
this.collectionVariables = collectionVariables; this.collectionVariables = collectionVariables || {};
this.processEnvVars = cloneDeep(processEnvVars || {}); this.processEnvVars = cloneDeep(processEnvVars || {});
this.collectionPath = collectionPath; this.collectionPath = collectionPath;
} }
_interpolateEnvVar = (str) => { _interpolate = (str) => {
if (!str || !str.length || typeof str !== 'string') { if (!str || !str.length || typeof str !== 'string') {
return str; return str;
} }
const template = Handlebars.compile(str, { noEscape: true }); const combinedVars = {
...this.envVariables,
return template({ ...this.collectionVariables,
process: { process: {
env: { env: {
...this.processEnvVars ...this.processEnvVars
} }
} }
}); };
return interpolate(str, combinedVars);
}; };
cwd() { cwd() {
@ -40,7 +42,7 @@ class Bru {
} }
getEnvVar(key) { getEnvVar(key) {
return this._interpolateEnvVar(this.envVariables[key]); return this._interpolate(this.envVariables[key]);
} }
setEnvVar(key, value) { setEnvVar(key, value) {
@ -74,7 +76,7 @@ class Bru {
); );
} }
return this.collectionVariables[key]; return this._interpolate(this.collectionVariables[key]);
} }
setNextRequest(nextRequest) { setNextRequest(nextRequest) {

View File

@ -1,42 +1,10 @@
const Handlebars = require('handlebars'); const { interpolate } = require('@usebruno/common');
const { forOwn, cloneDeep } = require('lodash');
const interpolateEnvVars = (str, processEnvVars) => { const interpolateString = (str, { envVariables = {}, collectionVariables = {}, processEnvVars = {} }) => {
if (!str || !str.length || typeof str !== 'string') { if (!str || !str.length || typeof str !== 'string') {
return str; 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 = { const combinedVars = {
...envVariables, ...envVariables,
...collectionVariables, ...collectionVariables,
@ -47,7 +15,7 @@ const interpolateString = (str, { envVariables, collectionVariables, processEnvV
} }
}; };
return template(combinedVars); return interpolate(str, combinedVars);
}; };
module.exports = { module.exports = {

View File

@ -203,13 +203,13 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
}; };
class AssertRuntime { class AssertRuntime {
runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath) { runAssertions(assertions, request, response, envVariables, collectionVariables, processEnvVars) {
const enabledAssertions = _.filter(assertions, (a) => a.enabled); const enabledAssertions = _.filter(assertions, (a) => a.enabled);
if (!enabledAssertions.length) { if (!enabledAssertions.length) {
return []; return [];
} }
const bru = new Bru(envVariables, collectionVariables); const bru = new Bru(envVariables, collectionVariables, processEnvVars);
const req = new BrunoRequest(request); const req = new BrunoRequest(request);
const res = createResponseParser(response); const res = createResponseParser(response);
@ -222,6 +222,7 @@ class AssertRuntime {
const context = { const context = {
...envVariables, ...envVariables,
...collectionVariables, ...collectionVariables,
...processEnvVars,
...bruContext ...bruContext
}; };