'use strict';

import {camelizeKeys} from 'humps';
import request        from 'superagent';

var APIUtils = {

  root: '//localhost:3000/api/',

  normalizeResponse(response) {
    return camelizeKeys(response.body);
  },

  get(path) {
    return new Promise((resolve, reject) => {
      request.get(this.root + path)
      .withCredentials()
      .end(res => {
        if ( !res.ok ) {
          reject(this.normalizeResponse(res));
        } else {
          resolve(this.normalizeResponse(res));
        }
      });
    });
  },

  post(path, body) {
    return new Promise((resolve, reject) => {
      request.post(this.root + path, body)
      .withCredentials()
      .end(res => {
        if ( !res.ok ) {
          reject(this.normalizeResponse(res));
        } else {
          resolve(this.normalizeResponse(res));
        }
      });
    });
  },

  patch(path, body) {
    return new Promise((resolve, reject) => {
      request.patch(this.root + path, body)
      .withCredentials()
      .end(res => {
        if ( !res.ok ) {
          reject(this.normalizeResponse(res));
        } else {
          resolve(this.normalizeResponse(res));
        }
      });
    });
  },

  put(path, body) {
    return new Promise((resolve, reject) => {
      request.put(this.root + path, body)
      .withCredentials()
      .end(res => {
        if ( !res.ok ) {
          reject(this.normalizeResponse(res));
        } else {
          resolve(this.normalizeResponse(res));
        }
      });
    });
  },

  del(path) {
    return new Promise((resolve, reject) => {
      request.del(this.root + path)
      .withCredentials()
      .end(res => {
        if ( !res.ok ) {
          reject(this.normalizeResponse(res));
        } else {
          resolve(this.normalizeResponse(res));
        }
      });
    });
  }

};

export default APIUtils;
@@ -340,35 +340,42 @@
 ls()%0A      .end(
+(err, 
 res
+)
  =%3E %7B%0A        if
@@ -368,32 +368,39 @@
 %3E %7B%0A        if (
+ err %7C%7C
  !res.ok ) %7B%0A   
@@ -428,32 +428,39 @@
 rmalizeResponse(
+err %7C%7C 
 res));%0A        %7D
@@ -687,35 +687,42 @@
 ls()%0A      .end(
+(err, 
 res
+)
  =%3E %7B%0A        if
@@ -711,36 +711,74 @@
 s) =%3E %7B%0A        
-if (
+console.log(err, res);%0A        if ( err %7C%7C
  !res.ok ) %7B%0A   
@@ -806,32 +806,39 @@
 rmalizeResponse(
+err %7C%7C 
 res));%0A        %7D
@@ -1067,35 +1067,42 @@
 ls()%0A      .end(
+(err, 
 res
+)
  =%3E %7B%0A        if
@@ -1095,32 +1095,39 @@
 %3E %7B%0A        if (
+ err %7C%7C
  !res.ok ) %7B%0A   
@@ -1155,32 +1155,39 @@
 rmalizeResponse(
+err %7C%7C 
 res));%0A        %7D
@@ -1412,35 +1412,42 @@
 ls()%0A      .end(
+(err, 
 res
+)
  =%3E %7B%0A        if
@@ -1440,32 +1440,39 @@
 %3E %7B%0A        if (
+ err %7C%7C
  !res.ok ) %7B%0A   
@@ -1500,32 +1500,39 @@
 rmalizeResponse(
+err %7C%7C 
 res));%0A        %7D
@@ -1753,19 +1753,26 @@
    .end(
+(err, 
 res
+)
  =%3E %7B%0A  
@@ -1781,16 +1781,23 @@
     if (
+ err %7C%7C
  !res.ok
@@ -1833,32 +1833,39 @@
 rmalizeResponse(
+err %7C%7C 
 res));%0A        %7D

add error handling to API utils