Repository: auth0/auth0.js
Pull request #484 (2017-08-09 23:16:26)

Add flag to retry requests


Patch:
diff --git a/src/helper/request-builder.js b/src/helper/request-builder.js
index 9a228b1..807bbe5 100644
--- a/src/helper/request-builder.js
+++ b/src/helper/request-builder.js
@@ -64,6 +64,7 @@ RequestObj.prototype.end = function(cb) {
 function RequestBuilder(options) {
   this._sendTelemetry = options._sendTelemetry === false ? options._sendTelemetry : true;
   this._telemetryInfo = options._telemetryInfo || null;
+  this._timesToRetryFailedRequests = options._timesToRetryFailedRequests;
   this.headers = options.headers || {};
 }
 
@@ -86,6 +87,9 @@ RequestBuilder.prototype.setCommonConfiguration = function(ongoingRequest, optio
   if (this._sendTelemetry) {
     ongoingRequest = ongoingRequest.set('Auth0-Client', this.getTelemetryData());
   }
+  if (this._timesToRetryFailedRequests > 0) {
+    ongoingRequest = ongoingRequest.retry(this._timesToRetryFailedRequests);
+  }
   return ongoingRequest;
 };
 
diff --git a/src/web-auth/index.js b/src/web-auth/index.js
index af9e332..cfce345 100644
--- a/src/web-auth/index.js
+++ b/src/web-auth/index.js
@@ -24,6 +24,7 @@ var CrossOriginAuthentication = require('./cross-origin-authentication');
  * @param {String} [options.scope] scopes to be requested during Auth. e.g. `openid email`
  * @param {String} [options.audience] identifier of the resource server who will consume the access token issued after Auth
  * @param {Array} [options.plugins]
+ * @param {Number} [options._timesToRetryFailedRequests] Number of times to retry a failed request, according to {@link https://github.com/visionmedia/superagent/blob/master/lib/should-retry.js}
  * @see {@link https://auth0.com/docs/api/authentication}
  */
 function WebAuth(options) {
@@ -55,6 +56,11 @@ function WebAuth(options) {
         optional: true,
         type: 'object',
         message: '_telemetryInfo option is not valid'
+      },
+      _timesToRetryFailedRequests: {
+        optional: true,
+        type: 'number',
+        message: '_timesToRetryFailedRequests option is not valid'
       }
     }
   );
@@ -78,6 +84,10 @@ function WebAuth(options) {
     ? this.baseOptions._sendTelemetry
     : true;
 
+  this.baseOptions._timesToRetryFailedRequests = options._timesToRetryFailedRequests
+    ? parseInt(options._timesToRetryFailedRequests, 0)
+    : 0;
+
   this.baseOptions.tenant =
     (this.baseOptions.overrides && this.baseOptions.overrides.__tenant) ||
     this.baseOptions.domain.split('.')[0];


Test patch:
diff --git a/test/helper/request-builder.test.js b/test/helper/request-builder.test.js
index 7c2ef3b..14ae2fa 100644
--- a/test/helper/request-builder.test.js
+++ b/test/helper/request-builder.test.js
@@ -103,6 +103,16 @@ describe('helpers requestBuilder', function() {
     });
   });
 
+  it('should retry request', function() {
+    var retryTimes = 2;
+    var req = new RequestBuilder({
+      _timesToRetryFailedRequests: retryTimes
+    });
+    var handler = req.get('https://test.com').withCredentials().end(function(err, data) {});
+
+    expect(handler.request.willRetry).to.eql(retryTimes);
+  });
+
   it('should post stuff', function() {
     var req = new RequestBuilder({});
     var handler = req
diff --git a/test/mock/request-mock.js b/test/mock/request-mock.js
index d44e68c..1b68cd2 100644
--- a/test/mock/request-mock.js
+++ b/test/mock/request-mock.js
@@ -33,4 +33,9 @@ RequestMock.prototype.end = function(cb) {
   return this;
 };
 
+RequestMock.prototype.retry = function(times) {
+  this.willRetry = times;
+  return this;
+};
+
 module.exports = RequestMock;
diff --git a/test/web-auth/web-auth.test.js b/test/web-auth/web-auth.test.js
index 326ebc7..08579c2 100644
--- a/test/web-auth/web-auth.test.js
+++ b/test/web-auth/web-auth.test.js
@@ -33,6 +33,7 @@ describe('auth0.WebAuth', function() {
         scope: 'openid name read:blog',
         audience: 'urn:site:demo:blog',
         _sendTelemetry: false,
+        _timesToRetryFailedRequests: 2,
         overrides: {
           __tenant: 'tenant1',
           __token_issuer: 'issuer1'