'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;
|