var Backbone = require('backbone');

/**
 * Network Interceptors
 *
 * Tool to easily intercept network responses in order
 * to override or add behaviours on top of the usual ones
 */

function NetworkResponseInterceptor () {
  this._originalSync = Backbone.sync;
  this._successInterceptors = [];
  this._errorInterceptors = [];
}

NetworkResponseInterceptor.prototype.patchBackboneSync = function () {
  Backbone.sync = this._onSyncStart.bind(this);
};

NetworkResponseInterceptor.prototype.addSuccessInterceptor = function (interceptorFn) {
  this._successInterceptors.push(interceptorFn);
};

NetworkResponseInterceptor.prototype.addErrorInterceptor = function (interceptorFn) {
  this._errorInterceptors.push(interceptorFn);
};

NetworkResponseInterceptor.prototype._onSyncStart = function (method, model, options) {
  const errorCallback = options.error;
  const successCallback = options.success;

  const interceptErrorFn = function () {
    this._applyErrorInterceptors.apply(this, arguments);
    errorCallback.apply(this, arguments);
  };

  const interceptSuccessFn = function () {
    this._applySuccessInterceptors.apply(this, arguments);
    successCallback.apply(this, arguments);
  };

  options.error = interceptErrorFn.bind(this);
  options.success = interceptSuccessFn.bind(this);

  return this._originalSync.apply(this, arguments);
};

NetworkResponseInterceptor.prototype._applyErrorInterceptors = function (xhr, textStatus, errorThrown) {
  if (!this._errorInterceptors.length) {
    return;
  }

  this._errorInterceptors.map(function (interceptorFunction) {
    interceptorFunction(xhr, textStatus, errorThrown);
  });
};

NetworkResponseInterceptor.prototype._applySuccessInterceptors = function (response, textStatus, xhr) {
  if (!this._successInterceptors.length) {
    return;
  }

  this._successInterceptors.map(function (interceptorFunction) {
    interceptorFunction(response, textStatus, xhr);
  });
};

module.exports = new NetworkResponseInterceptor();

@@ -1,8 +1,39 @@
+var _ = require('underscore');%0A
 var Back
@@ -355,16 +355,42 @@
 s = %5B%5D;%0A
+  this._urlPatterns = %5B%5D;%0A
 %7D%0A%0ANetwo
@@ -570,32 +570,44 @@
 tor = function (
+urlPattern, 
 interceptorFn) %7B
@@ -648,32 +648,64 @@
 interceptorFn);%0A
+  this._addPattern(urlPattern);%0A
 %7D;%0A%0ANetworkRespo
@@ -753,32 +753,44 @@
 tor = function (
+urlPattern, 
 interceptorFn) %7B
@@ -833,24 +833,56 @@
 rceptorFn);%0A
+  this._addPattern(urlPattern);%0A
 %7D;%0A%0ANetworkR
@@ -961,16 +961,158 @@
 ions) %7B%0A
+  var requestURL = model.url();%0A%0A  if (!this._shouldListenToRequest(requestURL)) %7B%0A    return this._originalSync.apply(this, arguments);%0A  %7D%0A%0A
   const 
@@ -1631,32 +1631,143 @@
 arguments);%0A%7D;%0A%0A
+NetworkResponseInterceptor.prototype._addPattern = function (pattern) %7B%0A  this._urlPatterns.push(pattern);%0A%7D;%0A%0A
 NetworkResponseI
@@ -2332,16 +2332,241 @@
 %7D);%0A%7D;%0A%0A
+NetworkResponseInterceptor.prototype._shouldListenToRequest = function (url) %7B%0A  var patternChecks = this._urlPatterns.map(function (pattern) %7B%0A    return url.indexOf(pattern) %3E -1;%0A  %7D);%0A%0A  return _.some(patternChecks);%0A%7D;%0A%0A
 module.e

Add URL pattern filter to interceptor