Comparing version 0.0.3 to 0.1.0
{ | ||
"name": "popsicle", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Simple HTTP requests for node and the browser", | ||
@@ -5,0 +5,0 @@ "main": "popsicle.js", |
113
popsicle.js
@@ -18,16 +18,2 @@ (function (root) { | ||
/** | ||
* Set properties onto an error instance. | ||
* | ||
* @param {Error} err | ||
* @param {Popsicle} self | ||
* @param {Error} original | ||
* @return {Error} | ||
*/ | ||
function wrapError (err, self, original) { | ||
err.popsicle = self; | ||
err.original = original; | ||
return err; | ||
} | ||
/** | ||
* Create a stream error instance. | ||
@@ -39,5 +25,5 @@ * | ||
function streamError (self) { | ||
var err = new Error('Request is streaming'); | ||
var err = self.error('Request is streaming'); | ||
err.stream = true; | ||
return wrapError(err, self); | ||
return err; | ||
} | ||
@@ -56,10 +42,10 @@ | ||
if (self.timedout) { | ||
err = new Error('Timeout of ' + timeout + 'ms exceeded'); | ||
err = self.error('Timeout of ' + timeout + 'ms exceeded'); | ||
err.timeout = timeout; | ||
} else { | ||
err = new Error('Request aborted'); | ||
err = self.error('Request aborted'); | ||
err.abort = true; | ||
} | ||
return wrapError(err, self); | ||
return err; | ||
} | ||
@@ -75,5 +61,6 @@ | ||
function parseError (self, e) { | ||
var err = new Error('Unable to parse the response body'); | ||
var err = self.error('Unable to parse the response body'); | ||
err.parse = true; | ||
return wrapError(err, self, e); | ||
err.original = e; | ||
return err; | ||
} | ||
@@ -89,5 +76,6 @@ | ||
function cspError (self, e) { | ||
var err = new Error('Refused to connect to "' + self.fullUrl() + '"'); | ||
var err = self.error('Refused to connect to "' + self.fullUrl() + '"'); | ||
err.csp = true; | ||
return wrapError(err, self, e); | ||
err.original = e; | ||
return err; | ||
} | ||
@@ -102,5 +90,5 @@ | ||
function unavailableError (self) { | ||
var err = new Error('Unable to connect to "' + self.fullUrl() + '"'); | ||
var err = self.error('Unable to connect to "' + self.fullUrl() + '"'); | ||
err.unavailable = true; | ||
return wrapError(err, self); | ||
return err; | ||
} | ||
@@ -115,5 +103,5 @@ | ||
function blockedError (self) { | ||
var err = new Error('The request to "' + self.fullUrl() + '" was blocked'); | ||
var err = self.error('The request to "' + self.fullUrl() + '" was blocked'); | ||
err.blocked = true; | ||
return wrapError(err, self); | ||
return err; | ||
} | ||
@@ -173,9 +161,7 @@ | ||
* @param {Object} obj | ||
* @param {String} [sep] | ||
* @param {String} [eq] | ||
* @return {String} | ||
*/ | ||
function stringifyQuery (obj, sep, eq) { | ||
eq = eq || '='; | ||
sep = sep || '&'; | ||
function stringifyQuery (obj) { | ||
var eq = '='; | ||
var sep = '&'; | ||
@@ -208,12 +194,11 @@ if (Object(obj) !== obj) { | ||
* @param {String} qs | ||
* @param {String} [sep] | ||
* @param {String} [eq] | ||
* @param {Object} [obj] | ||
* @return {Object} | ||
*/ | ||
function parseQuery (qs, sep, eq) { | ||
eq = eq || '='; | ||
sep = sep || '&'; | ||
function parseQuery (qs, obj) { | ||
obj = obj || {}; | ||
qs = qs.split(sep); | ||
var obj = {}; | ||
var sep = '&'; | ||
var eq = '='; | ||
var maxKeys = 1000; | ||
@@ -356,3 +341,3 @@ var len = qs.length > maxKeys ? maxKeys : qs.length; | ||
} catch (e) { | ||
throw parseError(response.request, e); | ||
throw parseError(response, e); | ||
} | ||
@@ -468,2 +453,6 @@ | ||
if (self.rejectUnauthorized) { | ||
request.rejectUnauthorized = true; | ||
} | ||
return request; | ||
@@ -526,3 +515,3 @@ } | ||
this.headers = {}; | ||
this._headers = {}; | ||
this._headerNames = {}; | ||
} | ||
@@ -547,3 +536,3 @@ | ||
this.headers[lower] = value; | ||
this._headers[lower] = key; | ||
this._headerNames[lower] = key; | ||
@@ -560,3 +549,3 @@ return this; | ||
Headers.prototype.name = function (key) { | ||
return this._headers[lowerHeader(key)]; | ||
return this._headerNames[lowerHeader(key)]; | ||
}; | ||
@@ -584,3 +573,3 @@ | ||
delete this.headers[lower]; | ||
delete this._headers[lower]; | ||
delete this._headerNames[lower]; | ||
@@ -633,2 +622,5 @@ return this; | ||
// Alias the response instance. | ||
this.request.response = this; | ||
setHeaders(this, options.headers); | ||
@@ -689,2 +681,12 @@ } | ||
/** | ||
* Create a popsicle error instance. | ||
* | ||
* @param {String} str | ||
* @return {Error} | ||
*/ | ||
Response.prototype.error = function (str) { | ||
return this.request.error(str); | ||
}; | ||
/** | ||
* Initialise a request instance. | ||
@@ -701,3 +703,4 @@ * | ||
this.timeout = options.timeout; | ||
this.withCredentials = !!options.withCredentials; | ||
this.withCredentials = options.withCredentials === true; | ||
this.rejectUnauthorized = options.rejectUnauthorized !== false; | ||
@@ -713,2 +716,10 @@ // Default to GET and uppercase anything else. | ||
setHeaders(this, options.headers); | ||
// Parse query strings already set. | ||
var queryIndex = this.url.indexOf('?'); | ||
if (queryIndex > -1) { | ||
this.query = parseQuery(this.url.substr(queryIndex + 1), this.query); | ||
this.url = this.url.substr(0, queryIndex); | ||
} | ||
} | ||
@@ -774,3 +785,3 @@ | ||
if (timeout && !this._timer) { | ||
if (timeout) { | ||
this._timer = setTimeout(function () { | ||
@@ -823,2 +834,14 @@ self.timedout = true; | ||
/** | ||
* Create a popsicle error instance. | ||
* | ||
* @param {String} str | ||
* @return {Error} | ||
*/ | ||
Request.prototype.error = function (str) { | ||
var err = new Error(str); | ||
err.popsicle = this; | ||
return err; | ||
}; | ||
/** | ||
* Support node-style callbacks. | ||
@@ -825,0 +848,0 @@ * |
@@ -233,13 +233,20 @@ var REMOTE_URL = 'http://localhost:4567'; | ||
it('should stringify and append to current query', function () { | ||
return popsicle({ | ||
it('should stringify and append to query object', function () { | ||
var req = popsicle({ | ||
url: REMOTE_URL + '/echo/query?query=true', | ||
query: EXAMPLE_BODY | ||
}) | ||
}); | ||
var query = { | ||
username: 'blakeembrey', | ||
password: 'hunter2', | ||
query: 'true' | ||
}; | ||
expect(req.url).to.equal(REMOTE_URL + '/echo/query'); | ||
expect(req.query).to.deep.equal(query); | ||
return req | ||
.then(function (res) { | ||
expect(res.body).to.deep.equal({ | ||
username: 'blakeembrey', | ||
password: 'hunter2', | ||
query: 'true' | ||
}); | ||
expect(res.body).to.deep.equal(query); | ||
}); | ||
@@ -246,0 +253,0 @@ }); |
Sorry, the diff of this file is not supported yet
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
58835
18
1560