postman-collection
Advanced tools
Comparing version 0.1.4 to 0.2.0
@@ -26,4 +26,7 @@ var _ = require('../util').lodash, | ||
* | ||
* @param {Header~definition|String} options Pass the initial definition of the header (key, value) as the options | ||
* parameter. In case a string is given, it is parsed to extract the header key and value. | ||
* @param {Header~definition|String} [value] - Pass the header definition as an object or the value of the header. | ||
* If the value is passed as a string, it should either be in `name:value` format or the second "name" parameter | ||
* should be used to pass the name as string | ||
* @param {String} [name] - optional override the header name or use when the first parameter is the header value as | ||
* string. | ||
* | ||
@@ -43,6 +46,3 @@ * @example <caption>Parse a string of headers into an array of Header objects</caption> | ||
*/ | ||
Header = function PostmanHeader (options) { | ||
// this constructor is intended to inherit and as such the super constructor is required to be excuted | ||
Header.super_.apply(this, arguments); | ||
Header = function PostmanHeader (options, name) { | ||
if (_.isString(options)) { | ||
@@ -52,2 +52,10 @@ options = Header.parseSingle(options); | ||
if (options && _.isString(name)) { | ||
options.value = options.key; | ||
options.key = name; | ||
} | ||
// this constructor is intended to inherit and as such the super constructor is required to be excuted | ||
Header.super_.apply(this, arguments); | ||
/** | ||
@@ -86,2 +94,10 @@ * The header Key | ||
/** | ||
* Specify the key to be used while indexing this object | ||
* @private | ||
* @readOnly | ||
* @type {String} | ||
*/ | ||
_postman_propertyIndexKey: 'key', | ||
/** | ||
* Parses a multi line header string into an array of {@link Header~definition}. | ||
@@ -140,2 +156,29 @@ * | ||
}).join('\n'); | ||
}, | ||
/** | ||
* Check whether an object is an instance of PostmanHeader. | ||
* | ||
* @param {*} obj | ||
* @returns {Boolean} | ||
*/ | ||
isHeader: function (obj) { | ||
return obj && ((obj instanceof Header) || | ||
_.inSuperChain(obj.constructor, '_postman_propertyName', Header._postman_propertyName)); | ||
}, | ||
/** | ||
* Create a new header instance | ||
* | ||
* @param {Header~definition|String} [value] - Pass the header definition as an object or the value of the header. | ||
* If the value is passed as a string, it should either be in `name:value` format or the second "name" parameter | ||
* should be used to pass the name as string | ||
* @param {String} [name] - optional override the header name or use when the first parameter is the header value as | ||
* string. | ||
* @returns {Header} | ||
*/ | ||
create: function () { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(Header); | ||
return new (Header.bind.apply(Header, args))(); | ||
} | ||
@@ -142,0 +185,0 @@ }); |
@@ -40,3 +40,3 @@ var _ = require('../util').lodash, | ||
*/ | ||
indexBy: (options && options.indexBy) || DEFAULT_INDEX_ATTR | ||
indexBy: (options && options.indexBy) || (type && type._postman_propertyIndexKey) || DEFAULT_INDEX_ATTR | ||
}); | ||
@@ -122,3 +122,5 @@ | ||
// create new instance of the item based on the type specified if it is not already | ||
this.insert((item.constructor === this.Type) ? item : new this.Type(item)); | ||
this.insert((item.constructor === this.Type) ? item : | ||
// if the prperty has acreate static function, use it. | ||
(_.has(this.Type, 'create') ? this.Type.create.apply(this.Type, arguments) : new this.Type(item))); | ||
}, | ||
@@ -169,3 +171,6 @@ | ||
// add a single item or an array of items. | ||
_.each(_.isArray(items) ? items : [items], this.add, this); | ||
_.each(_.isArray(items) ? items : | ||
// if population is not an array, we send this as single item in an array or send each property separately | ||
// if the core Type supports Type.create | ||
((_.isPlainObject(items) && _.has(this.Type, 'create')) ? items : [items]), this.add, this); | ||
}, | ||
@@ -172,0 +177,0 @@ |
@@ -1,2 +0,5 @@ | ||
var _ = require('../util').lodash, | ||
var util = require('../util'), | ||
_ = util.lodash, | ||
fileType = require('file-type'), | ||
mimeType = require('mime-types'), | ||
Property = require('./property').Property, | ||
@@ -8,2 +11,21 @@ Request = require('./request').Request, | ||
/** | ||
* @private | ||
* @const | ||
* @type {string} | ||
*/ | ||
E = '', | ||
/** | ||
* @private | ||
* @const | ||
* @type {string} | ||
*/ | ||
DOT = '.', | ||
/** | ||
* @private | ||
* @const | ||
* @type {string} | ||
*/ | ||
DEFAULT_RESPONSE_FILENAME = 'response', | ||
Response; | ||
@@ -24,5 +46,3 @@ | ||
Response.super_.apply(this, arguments); | ||
if (!options) { return; } // in case definition object is missing, there is no point moving forward | ||
this.update(options); | ||
this.update(options || {}); | ||
}), Property); | ||
@@ -40,4 +60,4 @@ | ||
* @type {String} | ||
* @deprecated use .reason() | ||
*/ | ||
// todo rename this to "reason" | ||
status: options.status ? options.status : Response.HTTP_REASON_PHRASES[options.code], | ||
@@ -59,4 +79,10 @@ | ||
body: options.body, | ||
/** | ||
* @private | ||
* | ||
* @type {Buffer|UInt8Array} | ||
*/ | ||
stream: options.body && !_.isString(options.body) && _.isObject(options.body) ? | ||
options.body : options.stream, | ||
/** | ||
* @type {PropertyList<Cookie>} | ||
@@ -71,3 +97,9 @@ */ | ||
*/ | ||
responseTime: options.responseTime | ||
responseTime: options.responseTime, | ||
/** | ||
* @private | ||
* @type {Number} | ||
*/ | ||
responseSize: options.stream && options.stream.byteLength | ||
}); | ||
@@ -77,2 +109,75 @@ } | ||
_.extend(Response.prototype, /** @lends Response.prototype */ { | ||
/** | ||
* Get the http response reason phrase based on the current response code. | ||
* @returns {String|undefined} | ||
*/ | ||
reason: function () { | ||
return this.status || Response.HTTP_REASON_PHRASES[this.code]; | ||
}, | ||
/** | ||
* @private | ||
* | ||
* @param {String} contentType | ||
* | ||
* @returns {Object} | ||
* | ||
* @note example object returned | ||
* { | ||
* source: 'header' // or 'content', 'default' or 'forced' | ||
* type: string // the content type of response | ||
* ext: string // extension of file that stores this type of data | ||
* name: string // file name | ||
* format: 'text' // or 'audio', 'video', 'image' ... | ||
* detected: {} // same as root object, but based on what is detected from content | ||
* } | ||
*/ | ||
mime: function (contentType, contentDisposition) { | ||
var responseBody = this.stream == null ? new Uint8Array() : this.stream, | ||
// detect the mime from response body | ||
detected = fileType(responseBody), | ||
source = 'forced', | ||
mime; | ||
// if no overrides provided, we take the values from headers | ||
!contentDisposition && (contentDisposition = this.headers.one('content-disposition')); | ||
if (!contentType) { | ||
contentType = this.headers.one('content-type') && this.headers.one('content-type').value; | ||
source = 'header'; | ||
} | ||
// if content type is not found in header, we fall back to the mime type detected | ||
if (!contentType && detected) { | ||
contentType = detected.mime; | ||
source = 'content'; | ||
} | ||
// if styill not found, then we use default text | ||
if (!contentType) { | ||
contentType = 'text/plain'; | ||
source = 'default'; | ||
} | ||
mime = Response.mimeInfo(contentType, contentDisposition); | ||
mime.source = source; | ||
mime.detected = detected && Response.mimeInfo(detected.mime, contentDisposition); | ||
return mime; | ||
}, | ||
/** | ||
* @private | ||
* | ||
* @returns {String} | ||
*/ | ||
dataURI: function () { | ||
var mime = this.mime(); | ||
if (mime.source !== 'default' && this.stream != null) { | ||
return 'data:' + mime.type + ';base64, ' + util.bufferOrArrayBufferToBase64(this.stream); | ||
} | ||
} | ||
}); | ||
_.extend(Response, /** @lends Response */ { | ||
@@ -88,2 +193,29 @@ /** | ||
/** | ||
* @private | ||
* | ||
* @param {String|Header} type | ||
* @param {String|Header} disposition | ||
* @returns {Object} | ||
*/ | ||
mimeInfo: function (type, disposition) { | ||
Header.isHeader(type) && (type = type.value); | ||
Header.isHeader(disposition) && (disposition = disposition.value); | ||
if (!(type && _.isString(type))) { return; } | ||
var info = {}; | ||
info.type = type; | ||
info.ext = mimeType.extension(type) || E; | ||
info.name = DEFAULT_RESPONSE_FILENAME; // @todo return file name from disposition | ||
info.format = type.replace(/^([\s\S]+)\/[\s\S]+/g, '$1') || undefined; | ||
// build the file name from extension | ||
info.filename = info.name; | ||
info.ext && (info.filename += (DOT + info.ext)); | ||
return info; | ||
}, | ||
/** | ||
* Enum for all the HTTP Reason phrases | ||
@@ -90,0 +222,0 @@ * |
@@ -1,2 +0,4 @@ | ||
var _ = require('lodash'); | ||
/* global btoa */ | ||
var _ = require('lodash').noConflict(), | ||
util; | ||
/** | ||
@@ -135,4 +137,55 @@ * @module util | ||
module.exports = { | ||
lodash: _ | ||
util = { | ||
lodash: _, | ||
/** | ||
* | ||
* @param {String} data | ||
* | ||
* @returns {String} [description] | ||
*/ | ||
btoa: ((typeof btoa !== 'function' && typeof Buffer === 'function') ? function (data) { | ||
return new Buffer(data).toString('base64'); | ||
} : btoa), // @todo use browserify to normalise this | ||
/** | ||
* ArrayBuffer to String | ||
* | ||
* @param {ArrayBuffer} buffer | ||
* @returns {String} | ||
*/ | ||
arrayBufferToString: function (buffer) { | ||
var str = '', | ||
uArrayVal = Uint8Array.from(buffer), | ||
i, | ||
ii; | ||
for (i = 0, ii = uArrayVal.length; i < ii; i++) { | ||
str += String.fromCharCode(uArrayVal[i]); | ||
} | ||
return str; | ||
}, | ||
bufferOrArrayBufferToBase64: function (buffer) { | ||
if (!buffer) { return ''; } | ||
var base64; | ||
// handle when buffer is pure string | ||
if (_.isString(buffer)) { | ||
return util.btoa(buffer); | ||
} | ||
// check if tostring works | ||
base64 = buffer.toString('base64') || ''; | ||
if (base64 === '[object ArrayBuffer]') { | ||
return util.btoa(util.arrayBufferToString(buffer)); | ||
} | ||
return base64; | ||
} | ||
}; | ||
module.exports = util; |
@@ -5,3 +5,3 @@ { | ||
"author": "Postman Labs <help@getpostman.com>", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"keywords": [ | ||
@@ -39,5 +39,7 @@ "postman" | ||
"escape-html": "^1.0.3", | ||
"file-type": "^3.8.0", | ||
"hawk": "3.1.3", | ||
"lodash": "^3.10.1", | ||
"marked": "^0.3.5", | ||
"mime-types": "^2.1.11", | ||
"node-oauth1": "^1.1.1", | ||
@@ -44,0 +46,0 @@ "node-uuid": "^1.4.7", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
269798
6128
15
+ Addedfile-type@^3.8.0
+ Addedmime-types@^2.1.11
+ Addedfile-type@3.9.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)