fetch.js
/**
 * Calls fetch, adds Auth and Content header if needed. Automatically parses content based on type.
 *
 * @returns Promise
 */
Calls fetch, adds Auth and Content header if needed. Automatically parses content based on type.
@returns Promise
Calls fetch adds Auth and Content header if needed . Automatically parses content based on type . @returns Promise
function runFetch(_ref, state) {
  var _this = this;
  var _ref$options = _ref.options,
      options = _ref$options === undefined ? {} : _ref$options,
      _ref$retry = _ref.retry,
      retry = _ref$retry === undefined ? false : _ref$retry,
      url = _ref.url;
  var headers = (0, _extends3.default)({}, createAuthorizationHeader(state), createContentHeader(options.body), options.headers || {});
  var filteredHeaders = {};
  (0, _keys2.default)(headers).filter(function (key) {
    return headers[key] !== null && headers[key] !== undefined;
  }).forEach(function (key) {
    filteredHeaders[key] = headers[key];
  });
  return fetch(url, (0, _extends3.default)({}, options, {
    body: serialize(options.body),
    headers: filteredHeaders
  })).then(checkStatus).then(createResponse).then(function () {
    var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(response) {
      return _regenerator2.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.t0 = retry;
              if (!_context.t0) {
                _context.next = 5;
                break;
              }
              _context.next = 4;
              return retry(response);
            case 4:
              _context.t0 = _context.sent;
            case 5:
              if (!_context.t0) {
                _context.next = 9;
                break;
              }
              _context.t1 = runFetch({ options: options, retry: retry, url: url }, state);
              _context.next = 10;
              break;
            case 9:
              _context.t1 = response;
            case 10:
              return _context.abrupt('return', _context.t1);
            case 11:
            case 'end':
              return _context.stop();
          }
        }
      }, _callee, _this);
    }));
    return function (_x) {
      return _ref2.apply(this, arguments);
    };
  }());
}
function runFetch ( _ref , state ) { var _this = this ; var _ref$options = _ref . options , options = _ref$options === undefined ? { } : _ref$options , _ref$retry = _ref . retry , retry = _ref$retry === undefined ? false : _ref$retry , url = _ref . url ; var headers = ( 0 , _extends3 . default ) ( { } , createAuthorizationHeader ( state ) , createContentHeader ( options . body ) , options . headers || { } ) ; var filteredHeaders = { } ; ( 0 , _keys2 . default ) ( headers ) . filter ( function ( key ) { return headers [ key ] !== null && headers [ key ] !== undefined ; } ) . forEach ( function ( key ) { filteredHeaders [ key ] = headers [ key ] ; } ) ; return fetch ( url , ( 0 , _extends3 . default ) ( { } , options , { body : serialize ( options . body ) , headers : filteredHeaders } ) ) . then ( checkStatus ) . then ( createResponse ) . then ( function ( ) { var _ref2 = ( 0 , _asyncToGenerator3 . default ) ( _regenerator2 . default . mark ( function _callee ( response ) { return _regenerator2 . default . wrap ( function _callee$ ( _context ) { while ( 1 ) { switch ( _context . prev = _context . next ) { case 0 : _context . t0 = retry ; if ( ! _context . t0 ) { _context . next = 5 ; break ; } _context . next = 4 ; return retry ( response ) ; case 4 : _context . t0 = _context . sent ; case 5 : if ( ! _context . t0 ) { _context . next = 9 ; break ; } _context . t1 = runFetch ( { options : options , retry : retry , url : url } , state ) ; _context . next = 10 ; break ; case 9 : _context . t1 = response ; case 10 : return _context . abrupt ( 'return' , _context . t1 ) ; case 11 : case 'end' : return _context . stop ( ) ; } } } , _callee , _this ) ; } ) ) ; return function ( _x ) { return _ref2 . apply ( this , arguments ) ; } ; } ( ) ) ; }
Calls fetch, adds Auth and Content header if needed.
Calls fetch adds Auth and Content header if needed .
// allow removing generated headers by specifiying { header: null } in options.headers // do this in two steps because otherwise we're modifying the object as we're iterating over it