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,
envVariables,
collectionVariables,
collectionPath
processEnvVars
);
each(assertionResults, (r) => {

View File

@ -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', {

View File

@ -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",

View File

@ -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) {

View File

@ -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 = {

View File

@ -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
};