feat: add bru.hasGlobalEnvVar method and update translations (#8037)

* feat: add bru.hasGlobalEnvVar method and update translations

- Introduced the `bru.hasGlobalEnvVar(key)` method to check for the existence of global environment variables.
- Updated translation mappings in Postman converters to include `pm.globals.has` for `bru.hasGlobalEnvVar`.
- Enhanced test cases to validate the new method and its translation in both directions between Bruno and Postman.

* feat: add hasGlobalEnvVar method to bru shim

- Implemented the `hasGlobalEnvVar` method in the bru shim to check for the existence of global environment variables.
- Updated the context setup to include the new method, enhancing the functionality of the environment variable management.
This commit is contained in:
sanish chirayath
2026-05-21 00:51:51 +05:30
committed by GitHub
parent 2d25b2cfb0
commit 113e28dc3c
8 changed files with 23 additions and 30 deletions

View File

@@ -136,6 +136,7 @@ const STATIC_API_HINTS = {
'bru.getCollectionName()',
'bru.isSafeMode()',
'bru.getOauth2CredentialVar(key)',
'bru.hasGlobalEnvVar(key)',
'bru.getGlobalEnvVar(key)',
'bru.setGlobalEnvVar(key, value)',
// 'bru.deleteGlobalEnvVar(key)',

View File

@@ -30,6 +30,7 @@ const replacements = {
'pm\\.response\\.responseTime': 'res.getResponseTime()',
'pm\\.globals\\.set\\(': 'bru.setGlobalEnvVar(',
'pm\\.globals\\.get\\(': 'bru.getGlobalEnvVar(',
'pm\\.globals\\.has\\(': 'bru.hasGlobalEnvVar(',
// 'pm\\.globals\\.unset\\(': 'bru.deleteGlobalEnvVar(',
'pm\\.globals\\.toObject\\(': 'bru.getAllGlobalEnvVars(',
// 'pm\\.globals\\.clear\\(': 'bru.deleteAllGlobalEnvVars(',

View File

@@ -20,6 +20,7 @@ const simpleTranslations = {
// Global variables
'bru.getGlobalEnvVar': 'pm.globals.get',
'bru.setGlobalEnvVar': 'pm.globals.set',
'bru.hasGlobalEnvVar': 'pm.globals.has',
// 'bru.deleteGlobalEnvVar': 'pm.globals.unset',
'bru.getAllGlobalEnvVars': 'pm.globals.toObject',
// 'bru.deleteAllGlobalEnvVars': 'pm.globals.clear',

View File

@@ -12,6 +12,7 @@ const simpleTranslations = {
// Global Variables
'pm.globals.get': 'bru.getGlobalEnvVar',
'pm.globals.set': 'bru.setGlobalEnvVar',
'pm.globals.has': 'bru.hasGlobalEnvVar',
'pm.globals.replaceIn': 'bru.interpolate',
// 'pm.globals.unset': 'bru.deleteGlobalEnvVar',
'pm.globals.toObject': 'bru.getAllGlobalEnvVars',
@@ -313,30 +314,6 @@ const complexTransformations = [
}
},
// pm.globals.has requires special handling
{
pattern: 'pm.globals.has',
transform: (path, j) => {
const callExpr = path.parent.value;
const args = callExpr.arguments;
// Create: bru.getGlobalEnvVar(arg) !== undefined && bru.getGlobalEnvVar(arg) !== null
return j.logicalExpression(
'&&',
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getGlobalEnvVar'), args),
j.identifier('undefined')
),
j.binaryExpression(
'!==',
j.callExpression(j.identifier('bru.getGlobalEnvVar'), args),
j.identifier('null')
)
);
}
},
// pm.request.headers.add({key, value}) -> req.setHeader(key, value)
{
pattern: 'pm.request.headers.add',

View File

@@ -51,6 +51,12 @@ describe('Bruno to Postman Variables Translation', () => {
expect(translatedCode).toBe('pm.globals.set("test", "value");');
});
it('should translate bru.hasGlobalEnvVar', () => {
const code = 'bru.hasGlobalEnvVar("token");';
const translatedCode = translateBruToPostman(code);
expect(translatedCode).toBe('pm.globals.has("token");');
});
// Collection variables tests
it('should translate bru.getCollectionVar', () => {
const code = 'bru.getCollectionVar("baseUrl");';

View File

@@ -244,23 +244,20 @@ describe('Variables Translation', () => {
const code = 'pm.globals.has("token");';
const translatedCode = translateCode(code);
expect(translatedCode).toContain('bru.getGlobalEnvVar("token") !== undefined');
expect(translatedCode).toContain('bru.getGlobalEnvVar("token") !== null');
expect(translatedCode).toBe('bru.hasGlobalEnvVar("token");');
});
it('should translate pm.globals.has in conditional', () => {
const code = 'if (pm.globals.has("authToken")) { console.log("Token exists"); }';
const translatedCode = translateCode(code);
expect(translatedCode).toContain('bru.getGlobalEnvVar("authToken") !== undefined');
expect(translatedCode).toContain('bru.getGlobalEnvVar("authToken") !== null');
expect(translatedCode).toContain('console.log("Token exists");');
expect(translatedCode).toBe('if (bru.hasGlobalEnvVar("authToken")) { console.log("Token exists"); }');
});
it('should translate pm.globals.has with variable assignment', () => {
const code = 'const hasGlobal = pm.globals.has("config");';
const translatedCode = translateCode(code);
expect(translatedCode).toContain('const hasGlobal = bru.getGlobalEnvVar("config") !== undefined && bru.getGlobalEnvVar("config") !== null');
expect(translatedCode).toBe('const hasGlobal = bru.hasGlobalEnvVar("config");');
});
});

View File

@@ -222,6 +222,10 @@ class Bru {
}
}
hasGlobalEnvVar(key) {
return Object.hasOwn(this.globalEnvironmentVariables, key);
}
getGlobalEnvVar(key) {
return this.interpolate(this.globalEnvironmentVariables[key]);
}

View File

@@ -116,6 +116,12 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'getAllGlobalEnvVars', getAllGlobalEnvVars);
getAllGlobalEnvVars.dispose();
let hasGlobalEnvVar = vm.newFunction('hasGlobalEnvVar', function (key) {
return marshallToVm(bru.hasGlobalEnvVar(vm.dump(key)), vm);
});
vm.setProp(bruObject, 'hasGlobalEnvVar', hasGlobalEnvVar);
hasGlobalEnvVar.dispose();
// TODO: deleteAllGlobalEnvVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let deleteAllGlobalEnvVars = vm.newFunction('deleteAllGlobalEnvVars', function () {