Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

superagent

Package Overview
Dependencies
Maintainers
10
Versions
174
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

superagent - npm Package Compare versions

Comparing version 3.2.1 to 3.3.0

12

docs/index.md

@@ -501,3 +501,3 @@

An "error" event is also emitted, with you can listen for:
An "error" event is also emitted, with you can listen for:

@@ -512,3 +512,3 @@ request

Note that a 4xx or 5xx response with super agent **are** considered an error by default. For example if you get a 500 or 403 response, this status information will be available via `err.status`. Errors from such responses also contain an `err.response` field with all of the properties mentioned in "Response properties". The library behaves in this way to handle the common case of wanting success responses and treating HTTP error status codes as errors while still allowing for custom logic around specific error conditions.
Note that a 4xx or 5xx response with super agent **are** considered an error by default. For example if you get a 500 or 403 response, this status information will be available via `err.status`. Errors from such responses also contain an `err.response` field with all of the properties mentioned in "[Response properties](#response-properties)". The library behaves in this way to handle the common case of wanting success responses and treating HTTP error status codes as errors while still allowing for custom logic around specific error conditions.

@@ -528,2 +528,10 @@ Network failures, timeouts, and other errors that produce no response will contain no `err.status` or `err.response` fields.

Alternatively, you can use the `.ok(callback)` method to decide whether a response is an error or not. The callback to the `ok` function gets a response and returns `true` if the response should be interpreted as success.
request.get('/404')
.ok(res => res.status < 500)
.then(response => {
// reads 404 page as a successful response
})
## Promise and Generator support

@@ -530,0 +538,0 @@

@@ -0,1 +1,8 @@

# 3.3.0 (2016-12-14)
* Added `.ok(callback)` that allows customizing which responses are errors (Kornel Lesiński)
* Added `.responseType()` to Node version (Kornel Lesiński)
* Added `.parse()` to browser version (jakepearson)
* Fixed parse error when using `responseType('blob')` (Kornel Lesiński)
# 3.2.0 (2016-12-11)

@@ -2,0 +9,0 @@

41

lib/client.js

@@ -315,5 +315,10 @@ /**

this._setHeaderProperties(this.header);
this.body = this.req.method != 'HEAD'
? this._parseBody(this.text ? this.text : this.xhr.response)
: null;
if (null === this.text && req._responseType) {
this.body = this.xhr.response;
} else {
this.body = this.req.method != 'HEAD'
? this._parseBody(this.text ? this.text : this.xhr.response)
: null;
}
}

@@ -336,2 +341,5 @@

var parse = request.parse[this.type];
if(this.req._parser) {
return this.req._parser(this, str);
}
if (!parse && isJSON(this.type)) {

@@ -402,6 +410,7 @@ parse = request.parse['application/json'];

// issue #876: return the http status code if the response parsing fails
err.statusCode = self.xhr.status ? self.xhr.status : null;
err.status = self.xhr.status ? self.xhr.status : null;
err.statusCode = err.status; // backwards-compat only
} else {
err.rawResponse = null;
err.statusCode = null;
err.status = null;
}

@@ -416,3 +425,3 @@

try {
if (res.status < 200 || res.status >= 300) {
if (!self._isResponseOK(res)) {
new_err = new Error(res.statusText || 'Unsuccessful HTTP response');

@@ -471,22 +480,2 @@ new_err.original = err;

/**
* Set responseType to `val`. Presently valid responseTypes are 'blob' and
* 'arraybuffer'.
*
* Examples:
*
* req.get('/')
* .responseType('blob')
* .end(callback);
*
* @param {String} val
* @return {Request} for chaining
* @api public
*/
Request.prototype.responseType = function(val){
this._responseType = val;
return this;
};
/**
* Set Accept to `type`, mapping values from `request.types`.

@@ -493,0 +482,0 @@ *

@@ -640,7 +640,7 @@

if (!err && res && res.status >= 200 && res.status < 300) {
return fn(err, res);
}
if (!err) {
if (this._isResponseOK(res)) {
return fn(err, res);
}
if (!err) {
var msg = 'Unsuccessful HTTP response';

@@ -806,3 +806,6 @@ if (res) {

if (!parser) {
if (multipart) {
if (this._responseType) {
parser = exports.parse.image; // It's actually a generic Buffer
buffer = true;
} else if (multipart) {
var form = new formidable.IncomingForm();

@@ -809,0 +812,0 @@ parser = form.parse.bind(form);

@@ -67,2 +67,25 @@ /**

/**
* Set format of binary response body.
* In browser valid formats are 'blob' and 'arraybuffer',
* which return Blob and ArrayBuffer, respectively.
*
* In Node all values result in Buffer.
*
* Examples:
*
* req.get('/')
* .responseType('blob')
* .end(callback);
*
* @param {String} val
* @return {Request} for chaining
* @api public
*/
RequestBase.prototype.responseType = function(val){
this._responseType = val;
return this;
};
/**
* Override default request body serializer

@@ -146,3 +169,21 @@ *

RequestBase.prototype.ok = function(cb) {
if ('function' !== typeof cb) throw Error("Callback required");
this._okCallback = cb;
return this;
};
RequestBase.prototype._isResponseOK = function(res) {
if (!res) {
return false;
}
if (this._okCallback) {
return this._okCallback(res);
}
return res.status >= 200 && res.status < 300;
};
/**

@@ -149,0 +190,0 @@ * Get request header `field`.

{
"name": "superagent",
"version": "3.2.1",
"version": "3.3.0",
"description": "elegant & feature rich browser / node HTTP with a fluent API",

@@ -5,0 +5,0 @@ "scripts": {

@@ -100,2 +100,25 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superagent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

/**
* Set format of binary response body.
* In browser valid formats are 'blob' and 'arraybuffer',
* which return Blob and ArrayBuffer, respectively.
*
* In Node all values result in Buffer.
*
* Examples:
*
* req.get('/')
* .responseType('blob')
* .end(callback);
*
* @param {String} val
* @return {Request} for chaining
* @api public
*/
RequestBase.prototype.responseType = function(val){
this._responseType = val;
return this;
};
/**
* Override default request body serializer

@@ -179,3 +202,21 @@ *

RequestBase.prototype.ok = function(cb) {
if ('function' !== typeof cb) throw Error("Callback required");
this._okCallback = cb;
return this;
};
RequestBase.prototype._isResponseOK = function(res) {
if (!res) {
return false;
}
if (this._okCallback) {
return this._okCallback(res);
}
return res.status >= 200 && res.status < 300;
};
/**

@@ -1215,5 +1256,10 @@ * Get request header `field`.

this._setHeaderProperties(this.header);
this.body = this.req.method != 'HEAD'
? this._parseBody(this.text ? this.text : this.xhr.response)
: null;
if (null === this.text && req._responseType) {
this.body = this.xhr.response;
} else {
this.body = this.req.method != 'HEAD'
? this._parseBody(this.text ? this.text : this.xhr.response)
: null;
}
}

@@ -1236,2 +1282,5 @@

var parse = request.parse[this.type];
if(this.req._parser) {
return this.req._parser(this, str);
}
if (!parse && isJSON(this.type)) {

@@ -1302,6 +1351,7 @@ parse = request.parse['application/json'];

// issue #876: return the http status code if the response parsing fails
err.statusCode = self.xhr.status ? self.xhr.status : null;
err.status = self.xhr.status ? self.xhr.status : null;
err.statusCode = err.status; // backwards-compat only
} else {
err.rawResponse = null;
err.statusCode = null;
err.status = null;
}

@@ -1316,3 +1366,3 @@

try {
if (res.status < 200 || res.status >= 300) {
if (!self._isResponseOK(res)) {
new_err = new Error(res.statusText || 'Unsuccessful HTTP response');

@@ -1371,22 +1421,2 @@ new_err.original = err;

/**
* Set responseType to `val`. Presently valid responseTypes are 'blob' and
* 'arraybuffer'.
*
* Examples:
*
* req.get('/')
* .responseType('blob')
* .end(callback);
*
* @param {String} val
* @return {Request} for chaining
* @api public
*/
Request.prototype.responseType = function(val){
this._responseType = val;
return this;
};
/**
* Set Accept to `type`, mapping values from `request.types`.

@@ -1393,0 +1423,0 @@ *

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc