Creating references to environment variables dynamically is definitely feasible in Paw. First you will need to create the environment variable, and then create a Dynamic Value that references to it.
Setting up the environment domain dynamically
Since environment variables are stored in domains, also called groups in the app, you should first create an environment domain with the desired name using context.getEnvironmentDomainByName
and context.createEnvironmentDomain
. You can read up more about these two methods on the documentation page for the context object.
getOrCreateEnvironmentDomain(name) {
let env = this.context.getEnvironmentDomainByName(name)
if (typeof env === 'undefined') {
env = context.createEnvironmentDomain(name)
}
return env
}
Once the environment domain is created, you need to add an environment in which to store the variable. The process is very similar to the creation of an environment domain. You can find more information on the methods used here on the documentation page for the EnvironmentDomain.
getOrCreateEnvironment(domain, name) {
let env = domain.getEnvironmentByName(name)
if (typeof env === 'undefined') {
env = domain.createEnvironment(name)
}
return env
}
The next step is to create the variable, if it doesn't exist, or to return it if it does.
/*
uses:
@getOrCreateEnvironmentDomain
@getOrCreateEnvironment
*/
updateOrCreateEnvironmentVariable(domainName, envName, name, value) {
let domain = this.getOrCreateEnvironmentDomain(domainName)
let env = this.getOrCreateEnvironment(domain, envName)
let varDict = {}
varDict[name] = typeof value !== 'undefined' ? value: ''
env.setVariablesValues(varDict)
return domain.getVariableByName(name)
}
Setting up the reference
To create a reference to an environment variable, you need to create an Environment Variable Dynamic Value. its identifier is com.luckymarmot.EnvironmentVariableDynamicValue
, and it takes only one parameter environmentVariable
, which is the id of the variable it refers to.
...
let envVariable = this.updateOrCreateEnvironmentVariable('Server', 'api-blueprint', 'protocol', 'https')
let dv = new DynamicValue(
'com.luckymarmot.EnvironmentVariableDynamicValue',
{
environmentVariable: envVariable.id
}
)
/* use the Dynamic Value like any other */
...