VariableScope

VariableScope

new VariableScope(definition, layersopt)

VariableScope is a representation of a list of variables in Postman, such as the environment variables or the globals. Using this object, it is easy to perform operations on this list of variables such as get a variable or set a variable.

Parameters:
Name Type Attributes Description
definition VariableScope~definition

The constructor accepts an initial set of values for initialising the scope

layers Array.<VariableList> <optional>

Additional parent scopes to search for and resolve variables

Source:
Example

Load a environment from file, modify and save back

var fs = require('fs'), // assuming NodeJS
    env,
    sum;

// load env from file assuming it has initial data
env = new VariableScope(JSON.parse(fs.readFileSync('./my-postman-environment.postman_environment').toString()));

// get two variables and add them
sum = env.get('one-var') + env.get('another-var');

// save it back in environment and write to file
env.set('sum', sum, 'number');
fs.writeFileSync('./sum-of-vars.postman_environment', JSON.stringify(env.toJSON()));

Extends

Members

disabled :Boolean

This (optional) flag denotes whether this property is disabled or not. Usually, this is helpful when a property is part of a PropertyList. For example, in a PropertyList of Headers, the ones that are disabled can be filtered out and not processed.

Type:
  • Boolean
Inherited From:
Source:

id :String

The id of the property is a unique string that identifies this property and can be used to refer to this property from relevant other places. It is a good practice to define the id or let the system auto generate a UUID if one is not defined for properties that require an id.

Type:
  • String
Inherited From:
Source:

name :String

A property can have a distinctive and human-readable name. This is to be used to display the name of the property within Postman, Newman or other runtimes that consume collection. In certain cases, the absence of name might cause the runtime to use the id as a fallback.

Type:
  • String
Inherited From:
Source:

Methods

(static) isVariableScope(obj) → {Boolean}

Check whether an object is an instance of VariableScope.

Parameters:
Name Type Description
obj *
Source:
Returns:
Type
Boolean

clear()

Removes all variables from the current scope. This is a destructive action.

Source:

describe(content, typeopt)

This function allows to describe the property for the purpose of detailed identification or documentation generation. This function sets or updates the description child-property of this property.

Parameters:
Name Type Attributes Default Description
content String

The content of the description can be provided here as a string. Note that it is expected that if the content is formatted in any other way than simple text, it should be specified in the subsequent type parameter.

type String <optional>
"text/plain"

The type of the content can be one of the values mentioned in Description.format enumeration - namely text/plain, text/markdown or text/html.

Inherited From:
Source:
Example

Add a description to an instance of Collection

 var Collection = require('postman-collection').Collection,
    mycollection;

// create a blank collection
myCollection = new Collection();
myCollection.describe('Hey! This is a cool collection.');

console.log(myCollection.description.toString()); // read the description

disableTracking()

Disable mutation tracking.

Source:

enableTracking(optionsopt)

Enable mutation tracking.

Parameters:
Name Type Attributes Description
options MutationTracker~definition <optional>

Options for Mutation Tracker. See MutationTracker

Source:

findInParents(property, customizeropt) → {*|undefined}

Tries to find the given property locally, and then proceeds to lookup in each parent, going up the chain as necessary. Lookup will continue until customizer returns a truthy value. If used without a customizer, the lookup will stop at the first parent that contains the property.

Parameters:
Name Type Attributes Description
property String
customizer function <optional>
Inherited From:
Source:
Returns:
Type
* | undefined

forEachParent(optionsopt, iterator)

Invokes the given iterator for every parent in the parent chain of the given element.

Parameters:
Name Type Attributes Default Description
options Object | Boolean <optional>
{}

A set of options for the parent chain traversal.

Properties
Name Type Attributes Default Description
withRoot Boolean <optional>
<nullable>
false

Set to true to include the collection object as well.

iterator function

The function to call for every parent in the ancestry chain.

Inherited From:
Source:
To Do:
  • Cache the results

get(key) → {*}

Fetches a variable from the current scope or from parent scopes if present.

Parameters:
Name Type Description
key String

The name of the variable to get.

Source:
Returns:

The value of the specified variable across scopes.

Type
*

has(variableName) → {Boolean}

Determines whether one particular variable is defined in this scope of variables.

Parameters:
Name Type Description
variableName String
Source:
Returns:
Type
Boolean

meta() → {*}

Returns the meta keys associated with the property

Inherited From:
Source:
Returns:
Type
*

parent() → {*|undefined}

Returns the parent of item

Inherited From:
Source:
Returns:
Type
* | undefined

set(key, value, typeopt)

Creates a new variable, or updates an existing one.

Parameters:
Name Type Attributes Description
key String

The name of the variable to set.

value *

The value of the variable to be set.

type Variable.types <optional>

Optionally, the value of the variable can be set to a type

Source:

toJSON() → {Object}

Convert this variable scope into a JSON serialisable object. Useful to transport or store, environment and globals as a whole.

Overrides:
Source:
Returns:
Type
Object

toObject(excludeDisabled, caseSensitive) → {Object}

Converts a list of Variables into an object where key is _postman_propertyIndexKey and value is determined by the valueOf function

Parameters:
Name Type Description
excludeDisabled Boolean
caseSensitive Boolean
Source:
Returns:
Type
Object

unset(key)

Removes the variable with the specified name.

Parameters:
Name Type Description
key String
Source:

variables() → {Object}

Deprecated:
  • since v1.2.5.* Get a copy of all variables in form of a plain object. This is useful for iteration and other use.
Source:
Returns:
Type
Object
Example

Iterate on all variables

var VariableScope = require('postman-collection').VariableScope,
    env = new VariableScope([{
        key: 'var1',
        value: 'one'
    }, {
        key: 'var2',
        value: 2,
        type: 'number'
    }, {
        key: 'var3',
        value: true,
        type: 'boolean'
    }]),
    obj;

// get all variables consolidated as an object
obj = env.variables();

Object.keys(obj).forEach(function(varname) {
    console.log(obj[varname]); // log all variables
});

Type Definitions

definition

Environment and Globals of postman is exported and imported in a specified data structure. This data structure can be passed on to the constructor parameter of VariableScope or VariableList to instantiate an instance of the same with pre-populated values from arguments.

Properties:
Name Type Attributes Description
id String <optional>

ID of the scope

name String <optional>

A name of the scope

values Array.<Variable~definition> <optional>

A list of variables defined in an array in form of {name:String, value:String}

Source:
Example

JSON definition of a VariableScope (environment, globals, etc)

{
  "name": "globals",
  "values": [{
    "key": "var-1",
    "value": "value-1"
  }, {
    "key": "var-2",
    "value": "value-2"
  }]
}