Merge branch 'main' into feature/add-bru-setNextRequest

This commit is contained in:
Martin Hoecker
2023-10-24 22:38:44 +02:00
88 changed files with 2826 additions and 1126 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@usebruno/js",
"version": "0.8.0",
"version": "0.9.1",
"license": "MIT",
"main": "src/index.js",
"files": [
@ -20,6 +20,7 @@
"axios": "^0.26.0",
"btoa": "^1.2.1",
"chai": "^4.3.7",
"chai-string": "^1.5.0",
"crypto-js": "^4.1.1",
"handlebars": "^4.7.8",
"json-query": "^2.2.2",

View File

@ -0,0 +1,55 @@
const Handlebars = require('handlebars');
const { forOwn, cloneDeep } = require('lodash');
const interpolateEnvVars = (str, 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,
process: {
env: {
...processEnvVars
}
}
};
return template(combinedVars);
};
module.exports = {
interpolateString
};

View File

@ -4,6 +4,7 @@ const { nanoid } = require('nanoid');
const Bru = require('../bru');
const BrunoRequest = require('../bruno-request');
const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
const { interpolateString } = require('../interpolate-string');
const { expect } = chai;
chai.use(require('chai-string'));
@ -161,17 +162,27 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
return;
}
const interpolationContext = {
collectionVariables: context.bru.collectionVariables,
envVariables: context.bru.envVariables,
processEnvVars: context.bru.processEnvVars
};
// gracefully allow both a,b as well as [a, b]
if (operator === 'in' || operator === 'notIn') {
if (rhsOperand.startsWith('[') && rhsOperand.endsWith(']')) {
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
}
return rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
return rhsOperand
.split(',')
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
}
if (operator === 'between') {
const [lhs, rhs] = rhsOperand.split(',').map((v) => evaluateJsTemplateLiteral(v.trim(), context));
const [lhs, rhs] = rhsOperand
.split(',')
.map((v) => evaluateJsTemplateLiteral(interpolateString(v.trim(), interpolationContext), context));
return [lhs, rhs];
}
@ -181,10 +192,10 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
rhsOperand = rhsOperand.substring(1, rhsOperand.length - 1);
}
return rhsOperand;
return interpolateString(rhsOperand, interpolationContext);
}
return evaluateJsTemplateLiteral(rhsOperand, context);
return evaluateJsTemplateLiteral(interpolateString(rhsOperand, interpolationContext), context);
};
class AssertRuntime {