Feature: implemented bru.interpolate (#4122)

* feat: enhance variable highlighting in CodeMirror and update interpolation method

* feat: add interpolate function to bru shim and corresponding tests

- Implemented the `interpolate` function in the bru shim to handle variable interpolation.
- Added a new test case for the `interpolate` function to verify its functionality with mock variables.

* feat: enhance interpolate function to support object interpolation

* feat: add translation support for pm.variables.replaceIn to bru.interpolate

* revert: eslint config changes

* revert: eslint config changes

* fix: update method call to use correct interpolation function in Bru class

* refactor: added jsdoc to codemirror highlighting code

* fix: higlighting for multiline editor
This commit is contained in:
sanish chirayath
2025-05-22 15:37:15 +05:30
committed by GitHub
parent 553f7675f2
commit 9a35302d4b
16 changed files with 161 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
const { cloneDeep } = require('lodash');
const { interpolate } = require('@usebruno/common');
const { interpolate: _interpolate } = require('@usebruno/common');
const variableNameRegex = /^[\w-.]*$/;
@@ -28,10 +28,10 @@ class Bru {
};
}
_interpolate = (str) => {
if (!str || !str.length || typeof str !== 'string') {
return str;
}
interpolate = (strOrObj) => {
if (!strOrObj) return strOrObj;
const isObj = typeof strOrObj === 'object';
const strToInterpolate = isObj ? JSON.stringify(strOrObj) : strOrObj;
const combinedVars = {
...this.globalEnvironmentVariables,
@@ -48,7 +48,8 @@ class Bru {
}
};
return interpolate(str, combinedVars);
const interpolatedStr = _interpolate(strToInterpolate, combinedVars);
return isObj ? JSON.parse(interpolatedStr) : interpolatedStr;
};
cwd() {
@@ -68,7 +69,7 @@ class Bru {
}
getEnvVar(key) {
return this._interpolate(this.envVariables[key]);
return this.interpolate(this.envVariables[key]);
}
setEnvVar(key, value) {
@@ -84,7 +85,7 @@ class Bru {
}
getGlobalEnvVar(key) {
return this._interpolate(this.globalEnvironmentVariables[key]);
return this.interpolate(this.globalEnvironmentVariables[key]);
}
setGlobalEnvVar(key, value) {
@@ -96,7 +97,7 @@ class Bru {
}
getOauth2CredentialVar(key) {
return this._interpolate(this.oauth2CredentialVariables[key]);
return this.interpolate(this.oauth2CredentialVariables[key]);
}
hasVar(key) {
@@ -111,7 +112,7 @@ class Bru {
if (variableNameRegex.test(key) === false) {
throw new Error(
`Variable name: "${key}" contains invalid characters!` +
' Names must only contain alpha-numeric characters, "-", "_", "."'
' Names must only contain alpha-numeric characters, "-", "_", "."'
);
}
@@ -122,11 +123,11 @@ class Bru {
if (variableNameRegex.test(key) === false) {
throw new Error(
`Variable name: "${key}" contains invalid characters!` +
' Names must only contain alpha-numeric characters, "-", "_", "."'
' Names must only contain alpha-numeric characters, "-", "_", "."'
);
}
return this._interpolate(this.runtimeVariables[key]);
return this.interpolate(this.runtimeVariables[key]);
}
deleteVar(key) {
@@ -142,15 +143,15 @@ class Bru {
}
getCollectionVar(key) {
return this._interpolate(this.collectionVariables[key]);
return this.interpolate(this.collectionVariables[key]);
}
getFolderVar(key) {
return this._interpolate(this.folderVariables[key]);
return this.interpolate(this.folderVariables[key]);
}
getRequestVar(key) {
return this._interpolate(this.requestVariables[key]);
return this.interpolate(this.requestVariables[key]);
}
setNextRequest(nextRequest) {

View File

@@ -98,7 +98,7 @@ class ScriptRuntime {
};
}
if(runRequestByItemPathname) {
if (runRequestByItemPathname) {
context.bru.runRequest = runRequestByItemPathname;
}
@@ -151,7 +151,7 @@ class ScriptRuntime {
chai,
'node-fetch': fetch,
'crypto-js': CryptoJS,
'xml2js': xml2js,
xml2js: xml2js,
cheerio,
tv4,
...whitelistedModules,
@@ -235,7 +235,7 @@ class ScriptRuntime {
};
}
if(runRequestByItemPathname) {
if (runRequestByItemPathname) {
context.bru.runRequest = runRequestByItemPathname;
}

View File

@@ -29,6 +29,12 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'getProcessEnv', getProcessEnv);
getProcessEnv.dispose();
let interpolate = vm.newFunction('interpolate', function (str) {
return marshallToVm(bru.interpolate(vm.dump(str)), vm);
});
vm.setProp(bruObject, 'interpolate', interpolate);
interpolate.dispose();
let hasEnvVar = vm.newFunction('hasEnvVar', function (key) {
return marshallToVm(bru.hasEnvVar(vm.dump(key)), vm);
});
@@ -157,7 +163,8 @@ const addBruShimToContext = (vm, bru) => {
let getTestResults = vm.newFunction('getTestResults', () => {
const promise = vm.newPromise();
bru.getTestResults()
bru
.getTestResults()
.then((results) => {
promise.resolve(marshallToVm(cleanJson(results), vm));
})
@@ -178,7 +185,8 @@ const addBruShimToContext = (vm, bru) => {
let getAssertionResults = vm.newFunction('getAssertionResults', () => {
const promise = vm.newPromise();
bru.getAssertionResults()
bru
.getAssertionResults()
.then((results) => {
promise.resolve(marshallToVm(cleanJson(results), vm));
})
@@ -199,7 +207,8 @@ const addBruShimToContext = (vm, bru) => {
let runRequestHandle = vm.newFunction('runRequest', (args) => {
const promise = vm.newPromise();
bru.runRequest(vm.dump(args))
bru
.runRequest(vm.dump(args))
.then((response) => {
const { status, headers, data, dataBuffer, size, statusText } = response || {};
promise.resolve(marshallToVm(cleanJson({ status, statusText, headers, data, dataBuffer, size }), vm));