diff --git a/lib/collection/variable-scope.js b/lib/collection/variable-scope.js index bab70895c..64f8759d9 100644 --- a/lib/collection/variable-scope.js +++ b/lib/collection/variable-scope.js @@ -111,6 +111,10 @@ _.inherit(( if (mutations) { this.mutations = new MutationTracker(mutations); } + + if (definition && definition.prefix) { + this.prefix = definition.prefix; + } }), Property); /** @@ -136,6 +140,16 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { */ _postman_propertyRequiresId: true, + /** + * Defines the prefix associated with the VariableList variables for internal use. + * + * @private + * @type {String} + * + */ + prefix: '', + + /** * @private * @deprecated discontinued in v4.0 @@ -176,7 +190,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { * false otherwise */ has: function (key) { - var variable = this.values.oneNormalizedVariable(key), + var variable = this.values.oneNormalizedVariable(this.prefix + key), i, ii; @@ -199,7 +213,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { * @returns {*} The value of the specified variable across scopes. */ get: function (key) { - var variable = this.values.oneNormalizedVariable(key), + var variable = this.values.oneNormalizedVariable(this.prefix + key), i, ii; @@ -222,10 +236,10 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { * @param {Variable.types} [type] - Optionally, the value of the variable can be set to a type */ set: function (key, value, type) { - var variable = this.values.oneNormalizedVariable(key), + var variable = this.values.oneNormalizedVariable(this.prefix + key), // create an object that will be used as setter - update = { key, value }; + update = { key: this.prefix + key, value: value }; _.isString(type) && (update.type = type); @@ -250,9 +264,9 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { unset: function (key) { var lastDisabledVariable; - this.values.remove(function (variable) { + this.values.remove((variable) => { // bail out if variable name didn't match - if (variable.key !== key) { + if (variable.key !== (this.prefix + key)) { return false; } @@ -269,7 +283,7 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ { // restore the reference with the last disabled variable if (lastDisabledVariable) { - this.values.reference[key] = lastDisabledVariable; + this.values.reference[this.prefix + key] = lastDisabledVariable; } // track the change if mutation tracking is enabled diff --git a/test/unit/variable-scope.test.js b/test/unit/variable-scope.test.js index 394f89532..5616b675e 100644 --- a/test/unit/variable-scope.test.js +++ b/test/unit/variable-scope.test.js @@ -325,6 +325,17 @@ describe('VariableScope', function () { expect(scope.get('random')).to.be.undefined; }); + it('should get variables with prefix', function () { + var scope = new VariableScope({ + prefix: 'vault:', + values: [ + { key: 'vault:var-1', value: 'var-1-value' }, + { key: 'vault:var-2', value: 'var-2-value' } + ] }); + + expect(scope.get('var-2')).to.equal('var-2-value'); + }); + describe('multi layer search', function () { it('should get from parent scope', function () { var scope = new VariableScope([ @@ -506,6 +517,19 @@ describe('VariableScope', function () { scope.set('var-4', 3.142, 'boolean'); expect(scope.get('var-4')).to.be.true; }); + + it('should correctly update an existing value with prefix', function () { + var scope = new VariableScope({ + prefix: 'vault:', + values: [{ + key: 'vault:var-1', + value: 'var-1-value' + }] + }); + + scope.set('var-1', 'new-var-1-value'); + expect(scope.get('var-1')).to.equal('new-var-1-value'); + }); }); describe('unset', function () { @@ -607,6 +631,24 @@ describe('VariableScope', function () { // check reference list expect(scope.values.reference).to.have.property('var-2'); }); + + it('should correctly remove an existing variable with prefix', function () { + var scope = new VariableScope({ + prefix: 'vault:', + values: [{ + key: 'vault:var-1', + value: 'var-1-value' + }, { + key: 'vault:var-2', + value: 'var-2-value' + }] + }); + + scope.unset('var-1'); + + expect(scope.values.count()).to.equal(1); + expect(scope.get('var-1')).to.be.undefined; + }); }); describe('clear', function () { @@ -1233,6 +1275,20 @@ describe('VariableScope', function () { expect(scope.has('alpha')).to.be.true; }); + + it('should find variable from current scope with prefix', function () { + var scope = new VariableScope({ + prefix: 'vault:', + values: [ + { key: 'vault:alpha', value: 'foo' }, + { key: 'vault:gamma', value: 'baz', disabled: true } + ] + }); + + expect(scope.has('alpha')).to.be.true; + expect(scope.has('gamma')).to.be.false; + expect(scope.has('random')).to.be.false; + }); }); describe('disabled variable', function () { diff --git a/types/index.d.ts b/types/index.d.ts index 7f799c0da..9d91099f5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2327,6 +2327,7 @@ declare module "postman-collection" { type definition = { id?: string; name?: string; + prefix?: string; values?: Variable.definition[]; }; }