requestretry
Advanced tools
Comparing version 1.0.4 to 1.1.0
45
index.js
@@ -10,4 +10,4 @@ 'use strict'; | ||
*/ | ||
var request = require('request'); | ||
var _ = require('fg-lodash'); | ||
var request = require('request'); | ||
var _ = require('fg-lodash'); | ||
var Cancelable = require('cancelable'); | ||
@@ -17,14 +17,14 @@ | ||
var DEFAULTS = { | ||
maxAttempts: 5, // try 5 times | ||
var DEFAULTS = { | ||
maxAttempts: 5, // try 5 times | ||
retryDelay: 5000, // wait for 5s before trying again | ||
}; | ||
function Request(options, f, maxAttempts, retryDelay){ | ||
function Request(options, f, maxAttempts, retryDelay) { | ||
this.maxAttempts = maxAttempts; | ||
this.retryDelay = retryDelay; | ||
this.options = options; | ||
this.f = _.once(f); | ||
this._timeout = null; | ||
this._req = null; | ||
this.retryDelay = retryDelay; | ||
this.options = options; | ||
this.f = _.once(f); | ||
this._timeout = null; | ||
this._req = null; | ||
} | ||
@@ -34,7 +34,7 @@ | ||
Request.prototype._tryUntilFail = function(){ | ||
Request.prototype._tryUntilFail = function () { | ||
this.maxAttempts--; | ||
this._req = Request.request(this.options, function(err, response, body){ | ||
if(this._isRetriable(err, response) && this.maxAttempts >= 0){ | ||
this._req = Request.request(this.options, function (err, response, body) { | ||
if (this._isRetriable(err, response) && this.maxAttempts >= 0) { | ||
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.retryDelay); | ||
@@ -48,3 +48,3 @@ return; | ||
Request.prototype._isRetriable = function(err, response){ | ||
Request.prototype._isRetriable = function (err, response) { | ||
// Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107 | ||
@@ -54,4 +54,4 @@ return (err && _.contains(RETRIABLE_ERRORS, err.code)) || (response && 500 <= response.statusCode && response.statusCode < 600); | ||
Request.prototype.abort = function(){ | ||
if(this._req){ | ||
Request.prototype.abort = function () { | ||
if (this._req) { | ||
this._req.abort(); | ||
@@ -63,3 +63,14 @@ } | ||
function Factory(options, f){ | ||
// expose request methods from RequestRetry | ||
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe'].forEach(function (methodName) { | ||
Request.prototype[methodName] = makeGateway(methodName); | ||
}); | ||
function makeGateway(methodName) { | ||
return function () { | ||
return this._req[methodName].apply(this._req, Array.prototype.slice.call(arguments)); | ||
}; | ||
} | ||
function Factory(options, f) { | ||
f = _.isFunction(f) ? f : _.noop; | ||
@@ -66,0 +77,0 @@ var retry = _(options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value(); |
{ | ||
"name": "requestretry", | ||
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error", | ||
"version": "1.1.0", | ||
"author": { | ||
@@ -9,3 +10,8 @@ "name": "Francois-Guillaume Ribreau", | ||
}, | ||
"version": "1.0.4", | ||
"contributors": [ | ||
{ | ||
"name": "juliendangers", | ||
"email": "dev@juliencrestin.com" | ||
} | ||
], | ||
"repository": { | ||
@@ -18,4 +24,4 @@ "url": "https://github.com/FGRibreau/node-request-retry" | ||
"cancelable": "~0.1.0", | ||
"fg-lodash": "0.0.1", | ||
"request": "~2.44.0" | ||
"fg-lodash": "0.0.2", | ||
"request": "~2.47.0" | ||
}, | ||
@@ -22,0 +28,0 @@ "devDependencies": { |
@@ -40,2 +40,6 @@ # Request-retry [](https://david-dm.org/FGRibreau/node-request-retry) | ||
v1.1.0 | ||
- support for 'end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe' request methods by @juliendangers | ||
v1.0.4 | ||
@@ -42,0 +46,0 @@ |
6775
7
80
70
+ Addedfg-lodash@0.0.2(transitive)
+ Addedqs@2.3.3(transitive)
+ Addedrequest@2.47.0(transitive)
- Removedfg-lodash@0.0.1(transitive)
- Removedqs@1.2.2(transitive)
- Removedrequest@2.44.0(transitive)
Updatedfg-lodash@0.0.2
Updatedrequest@~2.47.0