node-request-retry
Advanced tools
Comparing version 0.0.2 to 1.0.0
44
index.js
@@ -17,4 +17,4 @@ var request = require('request') | ||
// Array of response codes to attempt a retry with | ||
this.retryCodes = []; | ||
// Array of response codes and functions to evaluate and attempt a retry with | ||
this.retryConditions = []; | ||
} | ||
@@ -33,3 +33,3 @@ | ||
// and there are retries remaining, schedule a retry. | ||
if ((err || (response && isRetryCode(this, response.statusCode))) && | ||
if ((err || (response && isRetryCondition(this, response, body))) && | ||
this.numRetries < MAX_RETRIES) { | ||
@@ -45,11 +45,23 @@ this.numRetries++; | ||
/** | ||
* Check if the response code is one that warrants a retry. | ||
* Check if response meets a condition where we should attempt a retry. | ||
*/ | ||
function isRetryCode(obj, code) { | ||
if (obj.retryCodes && obj.retryCodes.indexOf(code) >= 0) { | ||
return true; | ||
} | ||
else { | ||
function isRetryCondition(obj, response, body) { | ||
if (obj.retryConditions.length == 0) { | ||
return false; | ||
} | ||
for (var i = 0; i < obj.retryConditions.length; i++) { | ||
if (typeof obj.retryConditions[i] === 'number') { | ||
if (response != null && obj.retryConditions[i] == response.statusCode) { | ||
return true; | ||
} | ||
} | ||
else if (typeof obj.retryConditions[i] === 'function') { | ||
if (obj.retryConditions[i](response, body)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -79,10 +91,10 @@ | ||
/** | ||
* Set the array of response codes that warrant a retry. | ||
* Set the array of response codes and functions that warrant a retry. | ||
*/ | ||
RequestRetry.prototype.setRetryCodes = function(codes) { | ||
if (typeof codes !== 'object' || !codes || codes.length == 0) { | ||
RequestRetry.prototype.setRetryConditions = function(conditions) { | ||
if (typeof conditions !== 'object' || !conditions || conditions.length == 0) { | ||
return; | ||
} | ||
this.retryCodes = codes; | ||
this.retryConditions = conditions; | ||
} | ||
@@ -124,7 +136,7 @@ | ||
RequestRetry.isRetryCode = isRetryCode; | ||
RequestRetry.isRetryCondition = isRetryCondition; | ||
RequestRetry.prototype.getRetryCodes = function() { | ||
return this.retryCodes; | ||
RequestRetry.prototype.getRetryConditions = function() { | ||
return this.retryConditions; | ||
} | ||
} |
{ | ||
"name": "node-request-retry", | ||
"description": "A simple Node.js request wrapper for retrying http requests.", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "DoSomething.org", |
@@ -11,5 +11,17 @@ # node-request-retry | ||
// Retry if the response received is one of the following codes | ||
requestRetry.setRetryCodes([400, 404, 408, 500]); | ||
var retryConditions = []; | ||
// Retries can be triggered by either receiving a certain status code | ||
Array.prototype.push.apply(retryConditions, [400, 404, 408, 500]); | ||
// ... or by evaluating a function. Returning true means the request should be retried. | ||
var fnShouldRetry = function(response, body) { | ||
if (body == false) | ||
return true; | ||
else | ||
return false; | ||
}; | ||
retryConditions[retryConditions.length] = fnShouldRetry; | ||
requestRetry.setRetryCodes(retryConditions); | ||
// POST | ||
@@ -16,0 +28,0 @@ requestRetry.post(url, postData, function(err, response, body) { |
@@ -37,8 +37,8 @@ var assert = require('assert') | ||
describe('RequestRetry.prototype.setRetryCodes()', function() { | ||
describe('RequestRetry.prototype.setRetryConditions()', function() { | ||
var request = new RequestRetry(); | ||
it('should set an empty array if passed `undefined`', function() { | ||
request.setRetryCodes(undefined); | ||
var test = request.getRetryCodes(); | ||
request.setRetryConditions(undefined); | ||
var test = request.getRetryConditions(); | ||
assert(typeof test === 'object' && test.length === 0); | ||
@@ -48,4 +48,4 @@ }); | ||
it('should set an array if passed `[400,408,500]`', function() { | ||
request.setRetryCodes([400, 408, 500]); | ||
var test = request.getRetryCodes(); | ||
request.setRetryConditions([400, 408, 500]); | ||
var test = request.getRetryConditions(); | ||
assert(typeof test === 'object' && test.length === 3); | ||
@@ -60,15 +60,15 @@ assert(test[0] === 400 && test[1] === 408 && test[2] === 500); | ||
describe('RequestRetry.isRetryCode() when retryCodes = [408]', function() { | ||
describe('RequestRetry.isRetryCondition() when retryConditions = [408]', function() { | ||
var request = new RequestRetry(); | ||
before(function() { | ||
request.setRetryCodes([408]); | ||
request.setRetryConditions([408]); | ||
}); | ||
it('should return false if code is 200', function() { | ||
assert(RequestRetry.isRetryCode(request, 200) === false); | ||
assert(RequestRetry.isRetryCondition(request, {statusCode: 200}, null) === false); | ||
}); | ||
it('should return false if code is 408', function() { | ||
assert(RequestRetry.isRetryCode(request, 408) === true); | ||
it('should return true if code is 408', function() { | ||
assert(RequestRetry.isRetryCondition(request, {statusCode: 408}, null) === true); | ||
}); | ||
@@ -80,1 +80,27 @@ | ||
}); | ||
describe('RequestRetry.isRetryCondition() with function in retryConditions', function() { | ||
var request = new RequestRetry(); | ||
before(function() { | ||
var retry = function(response, body) { | ||
if (body == null) | ||
return true; | ||
else | ||
return false; | ||
}; | ||
request.setRetryConditions([retry]); | ||
}) | ||
it('should evaluate to false when body != null with body = true', function() { | ||
assert(RequestRetry.isRetryCondition(request, null, true) === false); | ||
}); | ||
it('should evaluate to true when body != null with body = null', function() { | ||
assert(RequestRetry.isRetryCondition(request, null, null) === true); | ||
}); | ||
after(function() { | ||
request = null; | ||
}) | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7459
194
0
45