all files / lib/ body.es6

100% Statements 4/4
100% Branches 11/11
100% Functions 3/3
100% Lines 4/4
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                                                          11×                                                                                                                                                                          
import FormData from 'isomorphic-form-data';
import { default as _ } from 'lodash';
import { Promise } from 'es6-promise';
 
/**
 @module body
 */
 
const consumed = (body) => {
 
  if (body.bodyUsed)
    return 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
 */
class Body {
  /**
   * @param {(String|FormData)} [body='']
   * @returns {Object} Body instance
   */
  constructor(body = '') {
 
    this._bodyInit = body;
    this.bodyUsed = false;
 
    if (_.isString(body)) {
 
      this._bodyText = body;
 
    } else if (body instanceof FormData) {
 
      this._bodyFormData = body;
 
    } else {
 
      throw new Error('unsupported body type');
 
    }
 
  }
 
  /**
   * Uses body and returns a Promise that resolves the body.
   * @returns {Promise.<String>}
   */
  text() {
 
    const rejected = consumed(this);
 
    return rejected ? rejected : Promise.resolve(this._bodyText)
 
  }
 
  /**
   * Uses body and returns a Promise that resolves a parsed JSON object from the body.
   * @returns {Promise.<Object>}
   */
  json() {
 
    return this.text()
      .then(JSON.parse)
 
  }
 
  /**
   * Uses body and returns a Promise that resolves an instance of FormData.
   * @returns {Promise.<FormData>}
   */
  formData() {
 
    return this.text()
      .then((body) => {
 
        const form = new FormData();
        const pairs = body.trim().replace(/\+/g, '').split('&');
 
        _.forEach(pairs, (pair) => form.append(..._(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>}
   */
  blob() {
 
    return 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>}
   */
  arrayBuffer() {
 
    return Promise.reject(new Error('Method has not been implemented by farfetchd yet'));
 
  }
 
}
 
export default Body;