Skip to content

Commit

Permalink
feat: support prefix in VariableScope
Browse files Browse the repository at this point in the history
This change allows us to use the current VariableScope
with VaultVariables which have a fixed 'vault' prefix
  • Loading branch information
Pranav Joglekar committed Jul 10, 2024
1 parent e1dfe7a commit 50958a9
Show file tree
Hide file tree
Showing 6 changed files with 3,144 additions and 7 deletions.
37 changes: 30 additions & 7 deletions lib/collection/variable-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ _.inherit((
if (mutations) {
this.mutations = new MutationTracker(mutations);
}

if (definition && definition.prefix) {
this._postman_scopePrefix = definition.prefix;
}
}), Property);

/**
Expand Down Expand Up @@ -176,7 +180,9 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* false otherwise
*/
has: function (key) {
var variable = this.values.oneNormalizedVariable(key),
const keyWithPrefix = this._postman_scopePrefix ? this._postman_scopePrefix + key : key;

var variable = this.values.oneNormalizedVariable(keyWithPrefix),
i,
ii;

Expand All @@ -199,7 +205,9 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @returns {*} The value of the specified variable across scopes.
*/
get: function (key) {
var variable = this.values.oneNormalizedVariable(key),
const keyWithPrefix = this._postman_scopePrefix ? this._postman_scopePrefix + key : key;

var variable = this.values.oneNormalizedVariable(keyWithPrefix),
i,
ii;

Expand All @@ -222,10 +230,12 @@ _.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),
const keyWithPrefix = this._postman_scopePrefix ? this._postman_scopePrefix + key : key;

var variable = this.values.oneNormalizedVariable(keyWithPrefix),

// create an object that will be used as setter
update = { key, value };
update = { key: keyWithPrefix, value: value };

_.isString(type) && (update.type = type);

Expand All @@ -248,11 +258,13 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @param {String} key -
*/
unset: function (key) {
const keyWithPrefix = this._postman_scopePrefix ? this._postman_scopePrefix + key : key;

var lastDisabledVariable;

this.values.remove(function (variable) {
// bail out if variable name didn't match
if (variable.key !== key) {
if (variable.key !== keyWithPrefix) {
return false;
}

Expand All @@ -269,7 +281,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[keyWithPrefix] = lastDisabledVariable;
}

// track the change if mutation tracking is enabled
Expand Down Expand Up @@ -300,9 +312,20 @@ _.assign(VariableScope.prototype, /** @lends VariableScope.prototype */ {
* @returns {String|Object} The string or object with variables (if any) substituted with their values
*/
replaceIn: function (template) {
let values = this.values;

if (this._postman_scopePrefix) {
values = new VariableList(this, this.values.toJSON().map((v) => {
return {
...v,
key: v.key.replace(this._postman_scopePrefix, '')
};
}));
}

if (_.isString(template) || _.isArray(template)) {
// convert template to object because replaceSubstitutionsIn only accepts objects
var result = Property.replaceSubstitutionsIn({ template }, _.concat(this.values, this._layers));
var result = Property.replaceSubstitutionsIn({ template }, _.concat(values, this._layers));

return result.template;
}
Expand Down
96 changes: 96 additions & 0 deletions lib/collection/vault-variable-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const _ = require('../util').lodash,
VariableList = require('./variable-list').VariableList,
Property = require('./property').Property,
{ VariableScope } = require('./variable-scope'),
VAULT_PREFIX = 'vault:';

function addVaultPrefix (key) {
return VAULT_PREFIX + key;
}

function removeVaultPrefix (key) {
return key.replace(VAULT_PREFIX, '');
}

let VaultVariableScope;

_.inherit((

VaultVariableScope = function PostmanVaultVariableScope (definition, layers) {
if (definition) {
definition.id = 'vault';
}

VaultVariableScope.super_.call(this, definition, layers);
}), VariableScope);


// eslint-disable-next-line object-shorthand
_.assign(VaultVariableScope.prototype, {
has: function (key) {
return VariableScope.prototype.has.call(this, addVaultPrefix(key));
},
get: function (key) {
return VariableScope.prototype.get.call(this, addVaultPrefix(key));
},
set: function (key, value, type) {
return VariableScope.prototype.set.call(this, addVaultPrefix(key), value, type);
},
unset: function (key) {
return VariableScope.prototype.unset.call(this, addVaultPrefix(key));
},
replaceIn (template) {
const values = new VariableList(this, this.values.toJSON().map((v) => {
return {
...v,
key: removeVaultPrefix(v.key)
};
}));

if (_.isString(template) || _.isArray(template)) {
// convert template to object because replaceSubstitutionsIn only accepts objects
var result = Property.replaceSubstitutionsIn({ template }, _.concat(values, this._layers));

return result.template;
}

if (_.isObject(template)) {
return Property.replaceSubstitutionsIn(template, _.concat(values, this._layers));
}

return template;
},
applyMutation (instruction, key, value) {
if (this[instruction]) {
this[instruction](removeVaultPrefix(key), value);
}
}
});

_.assign(VaultVariableScope, {
/**
* Defines the name of this property for internal use.
*
* @private
* @readOnly
* @type {String}
*
* @note that this is directly accessed only in case of VariableScope from _.findValue lodash util mixin
*/
_postman_propertyName: 'VaultVariableScope',

/**
* Check whether an object is an instance of {@link VariableScope}.
*
* @param {*} obj -
* @returns {Boolean}
*/
isVariableScope: function (obj) {
return Boolean(obj) && ((obj instanceof VariableScope) ||
_.inSuperChain(obj.constructor, '_postman_propertyName', VariableScope._postman_propertyName));
}
});

module.exports = {
VaultVariableScope
};
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
Variable: require('./collection/variable').Variable,
VariableList: require('./collection/variable-list').VariableList,
VariableScope: require('./collection/variable-scope').VariableScope,
VaultVariableScope: require('./collection/vault-variable-scope').VaultVariableScope,
ProxyConfig: require('./collection/proxy-config').ProxyConfig,
ProxyConfigList: require('./collection/proxy-config-list').ProxyConfigList,
Version: require('./collection/version').Version
Expand Down
Loading

0 comments on commit 50958a9

Please sign in to comment.