all files / lib/ body.js

95.24% Statements 20/21
78.57% Branches 22/28
100% Functions 16/16
100% Lines 8/8
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                                      69×                                                           69×                                                                                                                                                                                      
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
var _createClass = 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; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _isomorphicFormData = require('isomorphic-form-data');
 
var _isomorphicFormData2 = _interopRequireDefault(_isomorphicFormData);
 
var _lodash = require('lodash');
 
var _lodash2 = _interopRequireDefault(_lodash);
 
var _es6Promise = require('es6-promise');
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _toConsumableArray(arr) { Eif (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
/**
 @module body
 */
 
var consumed = function consumed(body) {
 
  if (body.bodyUsed) return _es6Promise.Promise.reject(new TypeError('Already read'));
 
  body.bodyUsed = true;
};
 
/**
 * [Body mixin](https://fetch.spec.whatwg.org/#body-mixin) representing the body of a request or response. A body can be used through one of the class methods after which is can not be used again.
 * @alias module:body
 */
 
var Body = function () {
  /**
   * @param {(String|FormData)} [body='']
   * @returns {Object} Body instance
   */
 
  function Body() {
    var body = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0];
 
    _classCallCheck(this, Body);
 
    this._bodyInit = body;
    this.bodyUsed = false;
 
    if (_lodash2.default.isString(body)) {
 
      this._bodyText = body;
    } else if (body instanceof _isomorphicFormData2.default) {
 
      this._bodyFormData = body;
    } else {
 
      throw new Error('unsupported body type');
    }
  }
 
  /**
   * Uses body and returns a Promise that resolves the body.
   * @returns {Promise.<String>}
   */
 
 
  _createClass(Body, [{
    key: 'text',
    value: function text() {
 
      var rejected = consumed(this);
 
      return rejected ? rejected : _es6Promise.Promise.resolve(this._bodyText);
    }
 
    /**
     * Uses body and returns a Promise that resolves a parsed JSON object from the body.
     * @returns {Promise.<Object>}
     */
 
  }, {
    key: 'json',
    value: function json() {
 
      return this.text().then(JSON.parse);
    }
 
    /**
     * Uses body and returns a Promise that resolves an instance of FormData.
     * @returns {Promise.<FormData>}
     */
 
  }, {
    key: 'formData',
    value: function formData() {
 
      return this.text().then(function (body) {
 
        var form = new _isomorphicFormData2.default();
        var pairs = body.trim().replace(/\+/g, '').split('&');
 
        _lodash2.default.forEach(pairs, function (pair) {
          return form.append.apply(form, _toConsumableArray((0, _lodash2.default)(pair).thru(decodeURIComponent).split('=').value()));
        });
 
        return form;
      });
    }
 
    /**
     * Uses body and returns a Promise that resolves a Blob. Note this is not implemented yet.
     * @ignore
     * @returns {Promise.<Blob>}
     */
 
  }, {
    key: 'blob',
    value: function blob() {
 
      return _es6Promise.Promise.reject(new Error('Method has not been implemented by farfetchd yet'));
    }
 
    /**
     * Uses body and returns a Promise that resolves an ArrayBuffer. Note this is not implemented yet.
     * @ignore
     * @returns {Promise.<ArrayBuffer>}
     */
 
  }, {
    key: 'arrayBuffer',
    value: function arrayBuffer() {
 
      return _es6Promise.Promise.reject(new Error('Method has not been implemented by farfetchd yet'));
    }
  }]);
 
  return Body;
}();
 
exports.default = Body;