-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-variables.js
27 lines (27 loc) · 1.41 KB
/
set-variables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Set variables in a specified scope
* @param {Object} variables An object containing variable names and values
* @param {string} variableScope The scope in which to set the variables: e (environment), c (collection), or g (globals)
* @param {boolean} [verbose=false] Optional. If true, the function logs the validation process and errors.
*/
let setVariables = (variables, variableScope, verbose = false) => {
const environmentScope = ['e', 'environment', 'env'];
const collectionScope = ['c', 'collection', 'coll'];
const globalsScope = ['g', 'globals', 'glob'];
for (let variableName in variables) {
let variableValue = variables[variableName];
if (environmentScope.includes(variableScope)) {
pm.environment.set(variableName, variableValue);
if (verbose) console.info(variableName, 'saved in environment!');
} else if (collectionScope.includes(variableScope)) {
pm.collectionVariables.set(variableName, variableValue);
if (verbose) console.info(variableName, 'saved in collection!');
} else if (globalsScope.includes(variableScope)) {
pm.globals.set(variableName, variableValue);
if (verbose) console.info(variableName, 'saved in globals!');
} else {
if (verbose) console.error('Wrong variable type!');
}
}
}
pm.globals.set('function_set_variables', setVariables.toString());