import Base from './base';

export default class Service extends Base {
  request(options) {
    let fetchOptions = Object.assign({}, options);

    fetchOptions.headers = Object.assign({
      Accept: 'application/json'
    }, fetchOptions.headers);

    if (options.body) {
      fetchOptions.body = JSON.stringify(options.body);
    }

    const fetch = this.connection;

    return fetch(options.url, fetchOptions)
        .then(this.checkStatus)
        .then(this.parseJSON);
  }

  checkStatus(response) {
    if (response.ok) {
      return response;
    }

    return new Promise((resolve, reject) => {
      let body = response.body;
      let buffer = '';

      body.on('data', data => buffer += data.toString());
      body.on('error', reject);
      body.on('end', () => {
        let error = new Error(buffer);

        try {
          error = JSON.parse(buffer);
        } catch(e) {
          error.code = response.status;
        }

        error.response = response;

        reject(error);
      });
    });
  }

  parseJSON(response) {
    return response.json();
  }
}

@@ -462,22 +462,35 @@
 hen(
-this.parseJSON
+response =%3E response.json()
 );%0A 
@@ -587,384 +587,40 @@
 urn 
-new Promise((resolve, reject) =%3E %7B%0A      let body = response.body;%0A      let buffer = '';%0A%0A      body.on('data', data =%3E buffer += data.toString());%0A      body.on('error', reject);%0A      body.on('end', () =%3E %7B%0A        let error = new Error(buffer);%0A%0A        try %7B%0A          error = JSON.parse(buffer);%0A        %7D catch(e) %7B%0A          error.code = response.status;%0A        %7D%0A%0A  
+response.json().then(error =%3E %7B%0A
     
@@ -652,104 +652,32 @@
 se;%0A
-%0A
       
-  reject(
+throw 
 error
-)
 ;%0A    
-  %7D);%0A    %7D);%0A  %7D%0A%0A  parseJSON(response) %7B%0A    return response.json(
+%7D
 );%0A 

Use response.json() for converting errors (#63)