all files / lib/ response.es6

100% Statements 11/11
100% Branches 44/44
100% Functions 1/1
100% Lines 11/11
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                                                                    29×     28×     27×     26×     25×     24×     23×     22×                         21×     21×                                                                  
import * as _ from 'lodash';
import Body from './body';
import Headers from './headers';
 
/**
 @module response
 */
 
const initKeys = [
  'status',
  'statusText',
  'type',
  'cacheState',
  'httpsState',
  'headers',
  'url',
  'urlList',
  'terminationReason',
];
 
class Response extends Body {
 
  constructor(body = '', init = {}) {
 
    const defaults = {
      status: 200,
      statusText: 'OK',
      type: 'default',
      cacheState: 'none',
      httpsState: 'none',
      headers: {},
    };
 
    super(body);
 
    if (!_.isPlainObject(init))
      throw new Error('Response init must be an object');
 
    if (!_.isNumber(init.status) && !_.isUndefined(init.status))
      throw new Error('Response init.status must be a number');
 
    if (!_.isString(init.type) && !_.isUndefined(init.type))
      throw new Error('Response init.type must be a string');
 
    if (!_.isString(init.statusText) && !_.isUndefined(init.statusText))
      throw new Error('Response init.statusText must be a string');
 
    if (!_.isArray(init.urlList) && !_.isUndefined(init.urlList))
      throw new Error('Response init.urlList must be an array');
 
    if (!_.isString(init.terminationReason) && !_.isUndefined(init.terminationReason))
      throw new Error('Response init.terminationReason must be a string');
 
    if (!_.isString(init.cacheState) && !_.isUndefined(init.cacheState))
      throw new Error('Response init.cacheState must be a string');
 
    if (!_.isString(init.httpsState) && !_.isUndefined(init.httpsState))
      throw new Error('Response init.httpsState must be a string');
 
    _.extend(this, defaults, _.pick(init, initKeys));
 
    this._body = body;
 
    this.ok  = this.status >= 200 && this.status < 300;
 
    this.headers  = new Headers(init.headers);
 
    this.headerList  = this.headers.map;
 
    if (_.isString(init.url) && !_.isArray(init.urlList))
      this.url = decodeURIComponent(init.url);
 
    if (_.isArray(init.urlList)) {
 
      this.urlList = _.map(init.urlList, decodeURIComponent);
      this.url = _.last(this.urlList);
 
    }
 
  }
 
  clone() {
 
    return new Response(this._body, _.pick(this, initKeys));
 
  }
 
  error() {
 
    return new Response('', {
      type: 'error',
      status: 0,
      statusText: 'Network Error',
    })
 
  }
 
  redirect() {
 
    throw new Error('Method has not been implemented by farfetchd yet');
 
  }
 
}
 
export default Response;