123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- "use strict";
- var _ = require('../../lodash'), PropertyBase = require('./property-base').PropertyBase, DISABLED = 'disabled', DESCRIPTION = 'description', Property; // constructor
- /**
- * @typedef Property.definition
- * @property {String=} [id] A unique string that identifies the property.
- * @property {String=} [name] A distinctive and human-readable name of the property.
- * @property {Boolean=} [disabled] Denotes whether the property is disabled or not.
- * @property {Object=} [info] The meta information regarding the Property is provided as the `info` object.
- * @property {String=} [info.id] If set, this is used instead of the definition root's id.
- * @property {String=} [info.name] If set, this is used instead of the definition root's name.
- */
- _.inherit((
- /**
- * The Property class forms the base of all postman collection SDK elements. This is to be used only for SDK
- * development or to extend the SDK with additional functionalities. All SDK classes (constructors) that are
- * supposed to be identifyable (i.e. ones that can have a `name` and `id`) are derived from this class.
- *
- * For more information on what is the structure of the `definition` the function parameter, have a look at
- * {@link Property.definition}.
- *
- * > This is intended to be a private class except for those who want to extend the SDK itself and add more
- * > functionalities.
- *
- * @constructor
- * @extends {PropertyBase}
- *
- * @param {Property.definition=} [definition] Every constructor inherited from `Property` is required to call the
- * super constructor function. This implies that construction parameters of every inherited member is propagated
- * to be sent up to this point.
- *
- * @see Property.definition
- */
- Property = function PostmanProperty(definition) {
- // this constructor is intended to inherit and as such the super constructor is required to be executed
- Property.super_.apply(this, arguments);
- // The definition can have an `info` object that stores the identification of this property. If that is present,
- // we use it instead of the definition root.
- var src = definition && definition.info || definition, id;
- // first we extract id from all possible sources
- // we also check if this property is marked to require an ID, we generate one if not found.
- id = (src && src.id) || this.id || (this._ && this._.postman_id) || (this._postman_propertyRequiresId &&
- Math.random().toString(16).substr(2) /* uuid.v4() */);
- /**
- * 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`.
- * @name id
- * @type {String}
- * @memberOf Property.prototype
- *
- * @note The property can also be present in the `postman_id` meta in case it is not specified in the
- * object. An auto-generated property is used wherever one is not specified
- */
- id && (this.id = id);
- /**
- * 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.
- * @name name
- * @memberOf Property.prototype
- * @type {String}
- */
- src && src.name && (this.name = src.name);
- /**
- * This (optional) flag denotes whether this property is disabled or not. Usually, this is helpful when a
- * property is part of a {@link PropertyList}. For example, in a PropertyList of {@link Header}s, the ones
- * that are disabled can be filtered out and not processed.
- * @type {Boolean}
- * @optional
- * @name disabled
- *
- * @memberOf Property.prototype
- */
- definition && _.has(definition, DISABLED) && (this.disabled = Boolean(definition.disabled));
- }), PropertyBase);
- _.assign(Property.prototype, /** @lends Property.prototype */ {
- /**
- * Returns an object representation of the Property with its variable references substituted.
- *
- * @example <caption>Resolve an object using variable definitions from itself and its parents</caption>
- * property.toObjectResolved();
- *
- * @example <caption>Resolve an object using variable definitions on a different object</caption>
- * property.toObjectResolved(item);
- *
- * @example <caption>Resolve an object using variables definitions as a flat list of variables</caption>
- * property.toObjectResolved(null, [variablesDefinition1, variablesDefinition1], {ignoreOwnVariables: true});
- *
- * @private
- * @draft
- * @param {?Item|ItemGroup=} [scope] - One can specifically provide an item or group with `.variables`. In
- * the event one is not provided, the variables are taken from this object or one from the parent tree.
- * @param {Array<Object>} overrides - additional objects to lookup for variable values
- * @param {Object} [options]
- * @param {Boolean} [options.ignoreOwnVariables] - if set to true, `.variables` on self(or scope)
- * will not be used for variable resolution. Only variables in `overrides` will be used for resolution.
- * @returns {Object|undefined}
- * @throws {Error} If `variables` cannot be resolved up the parent chain.
- */
- toObjectResolved: function (scope, overrides, options) {
- var ignoreOwnVariables = options && options.ignoreOwnVariables, variableSourceObj, variables, reference;
- // ensure you do not substitute variables itself!
- reference = this.toJSON();
- _.isArray(reference.variable) && (delete reference.variable);
- // if `ignoreScopeVariables` is turned on, ignore `.variables` and resolve with only `overrides`
- // otherwise find `.variables` on current object or `scope`
- if (ignoreOwnVariables) {
- return Property.replaceSubstitutionsIn(reference, overrides);
- }
- // 1. if variables is passed as params, use it or fall back to oneself
- // 2. for a source from point (1), and look for `.variables`
- // 3. if `.variables` is not found, then rise up the parent to find first .variables
- variableSourceObj = scope || this;
- do {
- variables = variableSourceObj.variables;
- variableSourceObj = variableSourceObj.__parent;
- } while (!variables && variableSourceObj);
- if (!variables) { // worst case = no variable param and none detected in tree or object
- throw Error('Unable to resolve variables. Require a List type property for variable auto resolution.');
- }
- return variables.substitute(reference, overrides);
- }
- });
- _.assign(Property, /** @lends Property */ {
- /**
- * Defines the name of this property for internal use.
- * @private
- * @readOnly
- * @type {String}
- */
- _postman_propertyName: 'Property',
- });
- module.exports = {
- Property: Property
- };
|