All files JSendInterceptor.js

74.55% Statements 123/165
60.64% Branches 57/94
77.14% Functions 27/35
78.87% Lines 112/142

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354    1x   1x   1x     4x           1x 2x 2x 2x 2x 2x         1x 1x 1x       16x               16x     16x       1x       1x             1x       1x     1x       1x         1x       1x 1x 1x                     1x 1x                       1x       1x       1x   1x 1x   1x       1x 1x   1x       4x     1x               1x     1x       10x 10x 10x     10x 36x 36x 26x     10x       10x   10x       10x 10x   10x               10x       4x       4x       4x 4x                               1x 4x 4x       4x 4x 4x   4x                                   4x         1x 1x         4x 4x 4x   4x   4x 4x 4x   4x 4x 4x   4x                       4x     1x                       1x     1x   1x 4x 4x   3x 3x 3x   3x       1x       1x   5x 4x   3x 3x 3x   3x       1x       1x         4x 4x 4x           4x 2x     2x       6x 24x 16x       1x 1x  
'use strict';
 
Object.defineProperty(exports, '__esModule', { value: true });
 
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
 
var JSend = _interopDefault(require('jsend'));
 
function _classCallCheck(instance, Constructor) {
  Iif (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}
 
function _defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    Iif ("value" in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor);
  }
}
 
function _createClass(Constructor, protoProps, staticProps) {
  Eif (protoProps) _defineProperties(Constructor.prototype, protoProps);
  Iif (staticProps) _defineProperties(Constructor, staticProps);
  return Constructor;
}
 
function _defineProperty(obj, key, value) {
  Iif (key in obj) {
    Object.defineProperty(obj, key, {
      value: value,
      enumerable: true,
      configurable: true,
      writable: true
    });
  } else {
    obj[key] = value;
  }
 
  return obj;
}
 
function _inherits(subClass, superClass) {
  Iif (typeof superClass !== "function" && superClass !== null) {
    throw new TypeError("Super expression must either be null or a function");
  }
 
  subClass.prototype = Object.create(superClass && superClass.prototype, {
    constructor: {
      value: subClass,
      writable: true,
      configurable: true
    }
  });
  Eif (superClass) _setPrototypeOf(subClass, superClass);
}
 
function _getPrototypeOf(o) {
  _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
    return o.__proto__ || Object.getPrototypeOf(o);
  };
  return _getPrototypeOf(o);
}
 
function _setPrototypeOf(o, p) {
  _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
    o.__proto__ = p;
    return o;
  };
 
  return _setPrototypeOf(o, p);
}
 
function isNativeReflectConstruct() {
  Iif (typeof Reflect === "undefined" || !Reflect.construct) return false;
  Iif (Reflect.construct.sham) return false;
  Eif (typeof Proxy === "function") return true;
 
  try {
    Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
    return true;
  } catch (e) {
    return false;
  }
}
 
function _construct(Parent, args, Class) {
  Eif (isNativeReflectConstruct()) {
    _construct = Reflect.construct;
  } else {
    _construct = function _construct(Parent, args, Class) {
      var a = [null];
      a.push.apply(a, args);
      var Constructor = Function.bind.apply(Parent, a);
      var instance = new Constructor();
      if (Class) _setPrototypeOf(instance, Class.prototype);
      return instance;
    };
  }
 
  return _construct.apply(null, arguments);
}
 
function _isNativeFunction(fn) {
  return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
 
function _wrapNativeSuper(Class) {
  var _cache = typeof Map === "function" ? new Map() : undefined;
 
  _wrapNativeSuper = function _wrapNativeSuper(Class) {
    Iif (Class === null || !_isNativeFunction(Class)) return Class;
 
    Iif (typeof Class !== "function") {
      throw new TypeError("Super expression must either be null or a function");
    }
 
    Eif (typeof _cache !== "undefined") {
      Iif (_cache.has(Class)) return _cache.get(Class);
 
      _cache.set(Class, Wrapper);
    }
 
    function Wrapper() {
      return _construct(Class, arguments, _getPrototypeOf(this).constructor);
    }
 
    Wrapper.prototype = Object.create(Class.prototype, {
      constructor: {
        value: Wrapper,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
    return _setPrototypeOf(Wrapper, Class);
  };
 
  return _wrapNativeSuper(Class);
}
 
function _objectWithoutPropertiesLoose(source, excluded) {
  Iif (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
 
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
 
  return target;
}
 
function _objectWithoutProperties(source, excluded) {
  Iif (source == null) return {};
 
  var target = _objectWithoutPropertiesLoose(source, excluded);
 
  var key, i;
 
  Eif (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
 
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
 
  return target;
}
 
function _assertThisInitialized(self) {
  Iif (self === void 0) {
    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  }
 
  return self;
}
 
function _possibleConstructorReturn(self, call) {
  Eif (call && (typeof call === "object" || typeof call === "function")) {
    return call;
  }
 
  return _assertThisInitialized(self);
}
 
/**
 * Update an Error with the specified config, error code, and response.
 *
 * @param {Error} error The error to update.
 * @param {Object} config The config.
 * @param {string} [code] The error code (for example, 'ECONNABORTED').
 * @param {Object} [request] The request.
 * @param {Object} [response] The response.
 * @returns {Error} The error.
 */
var enhanceError = function enhanceError(error, config, code, request, response) {
  error.config = config;
  Iif (code) {
    error.code = code;
  }
 
  error.request = request;
  error.response = response;
  error.isAxiosError = true;
 
  error.toJSON = function() {
    return {
      // Standard
      message: this.message,
      name: this.name,
      // Microsoft
      description: this.description,
      number: this.number,
      // Mozilla
      fileName: this.fileName,
      lineNumber: this.lineNumber,
      columnNumber: this.columnNumber,
      stack: this.stack,
      // Axios
      config: this.config,
      code: this.code
    };
  };
  return error;
};
 
var JSendError =
/*#__PURE__*/
function (_Error) {
  _inherits(JSendError, _Error);
 
  function JSendError(message, _ref) {
    var _this;
 
    var jsend = _ref.jsend,
        data = _ref.data,
        response = _ref.response;
 
    _classCallCheck(this, JSendError);
 
    _this = _possibleConstructorReturn(this, _getPrototypeOf(JSendError).call(this, message));
    var config = response.config,
        request = response.request;
 
    var _enhanceError = enhanceError({}, config, null, request, response),
        _toJSON = _enhanceError.toJSON,
        info = _objectWithoutProperties(_enhanceError, ["toJSON"]);
 
    Object.assign(_assertThisInitialized(_this), info, {
      jsend: jsend,
      data: data,
      toJSON: function toJSON() {
        var json = _toJSON.call(this);
 
        return Object.assign(json, {
          jsend: jsend,
          data: data
        });
      }
    });
    return _this;
  }
 
  _createClass(JSendError, [{
    key: "name",
    get: function get() {
      return this.constructor.name;
    }
  }, {
    key: "isJSendError",
    get: function get() {
      return true;
    }
  }]);
 
  return JSendError;
}(_wrapNativeSuper(Error));
 
var JSEND_FIELDS = ['status', 'code', 'data', 'message'];
function apply(axios) {
  return axios.interceptors.response.use(function (response) {
    Iif (!response.data) return response;
    if (!JSend.isValid(response.data)) return response;
 
    var _pick = pick(JSEND_FIELDS, response.data),
        data = _pick.data,
        jsend = _objectWithoutProperties(_pick, ["data"]);
 
    if (jsend.status !== 'success') throw createError(response, {
      jsend: jsend,
      data: data
    });
    Object.assign(response, {
      jsend: jsend,
      data: data
    });
    return response;
  }, function (err) {
    if (!err.response || !err.response.data) throw err;
    if (!JSend.isValid(err.response.data)) throw err;
 
    var _pick2 = pick(JSEND_FIELDS, err.response.data),
        data = _pick2.data,
        jsend = _objectWithoutProperties(_pick2, ["data"]);
 
    if (jsend.status !== 'success') throw createError(err.response, {
      jsend: jsend,
      data: data
    });
    Object.assign(err, {
      jsend: jsend,
      data: data
    });
    throw err;
  });
}
 
function createError(response, _ref) {
  var jsend = _ref.jsend,
      data = _ref.data;
  var options = {
    jsend: jsend,
    data: data,
    response: response
  };
 
  if (jsend.status === 'fail') {
    return new JSendError('Request failed', options);
  }
 
  return new JSendError("Request returned an error: ".concat(jsend.message), options);
}
 
function pick(props, obj) {
  return props.reduce(function (acc, key) {
    if (!obj.hasOwnProperty(key)) return acc;
    return Object.assign(acc, _defineProperty({}, key, obj[key]));
  }, {});
}
 
exports.JSendError = JSendError;
exports.default = apply;