-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support prefix in VariableScope
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
Showing
6 changed files
with
3,144 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.