"use strict";
/**
 * Dependency on the CORS module.
 * @type {object}
 */
var createCORSRequest = require('./cors');
var cancellablePromiseBuilder = require('./cancellable-promise-builder');
var uuid = require('uuid').v4;
var dispatcher = require('../dispatcher');


/**
 * Create a pending status.
 * @return {object} The instanciated request status.
 */
function createRequestStatus(){
  return {
    id : uuid(),
    status: 'pending'
  };
}
/**
 * Update the request status.
 * @param  {object} request - The request to treat.
 * @return {[object} - The request to dispatch.
 */
function updateRequestStatus(request){
  if(!request || !request.id || !request.status){return;}
  dispatcher.handleViewAction({
    data: {request: request},
    type: 'update'
  });
  return request;

}

function jsonParser(req){
  return JSON.parse(req.responseText);
};

/**
 * Fetch function to ease http request.
 * @param  {object} obj - method: http verb, url: http url, data:The json to save.
 * @param  {object} options - The options object.
 * @return {CancellablePromise} The promise of the execution of the HTTP request.
 */
function fetch(obj, options) {
  options = options || {};
  options.parser = options.parser || jsonParser;
  options.errorParser = options.errorParser ||  jsonParser;
  var request = createCORSRequest(obj.method, obj.url, options);
  var requestStatus = createRequestStatus();
  var config = require('./config').get();
  if (!request) {
    throw new Error('You cannot perform ajax request on other domains.');
  }

  return cancellablePromiseBuilder(function promiseFn(success, failure) {
    //Request error handler
    request.onerror = function (error) {
      updateRequestStatus({id: requestStatus.id, status: 'error'});
      failure(error);
    };
    //Request success handler
    request.onload = function () {
      var status = request.status;
      var err = JSON.parse(request.response);
      if (status < 200 || status >= 300 ) {
        var err = JSON.parse(request.response);
        err.status = status;
        if(config.xhrErrors[status]){
          config.xhrErrors[status](request.response);
        }
        updateRequestStatus({id: requestStatus.id, status: 'error'});
        return failure(err);
      }
      var contentType = request.getResponseHeader('content-type');
      var data;
      if (contentType && contentType.indexOf("application/json") !== -1) {
        data = options.parser(request);
      } else {
        data = request.responseText;
      }
      updateRequestStatus({id: requestStatus.id, status: 'success'});
      return success(data);
    };
    updateRequestStatus({id: requestStatus.id, status: 'pending'});
    //Execute the request.
    request.send(JSON.stringify(obj.data));
  }, function cancelHandler() { // Promise cancel handler
    if (request.status === 0) { // request has not yet ended
      updateRequestStatus({id: requestStatus.id, status: 'cancelled'});
      request.abort();
      return true;
    } else { // trying to abort an ended request, send a warning to the console
      console.warn('Tried to abort an already ended request', request);
      return false;
    }
  });
}

module.exports = fetch;

@@ -1,18 +1,4 @@
-%22use strict%22;%0A
 /**%0A
@@ -53,19 +53,19 @@
 ct%7D%0A */%0A
-var
+let
  createC
@@ -96,19 +96,19 @@
 cors');%0A
-var
+let
  cancell
@@ -170,19 +170,19 @@
 lder');%0A
-var
+let
  uuid = 
@@ -201,19 +201,19 @@
 d').v4;%0A
-var
+let
  dispatc
@@ -244,16 +244,63 @@
 cher');%0A
+let isObject = require('lodash/lang/isObject');
 %0A%0A/**%0A *
@@ -431,17 +431,16 @@
 %7B%0A    id
- 
 : uuid()
@@ -568,17 +568,16 @@
 return %7B
-%5B
 object%7D 
@@ -702,16 +702,17 @@
 %7Breturn;
+ 
 %7D%0A  disp
@@ -817,76 +817,579 @@
 %0A%0A%7D%0A
-%0Afunction jsonParser(req)%7B%0A  return JSON.parse(req.responseText);%0A%7D;
+/**%0A * Parse the response.%0A * @param  %7Bobject%7D req - The requets object send back from the xhr.%0A * @return %7Bobject%7D     - The parsed object.%0A */%0Afunction jsonParser(req)%7B%0A  if(req.responseText === null %7C%7C req.responseText === null %7C%7C req.responseText === '')%7B%0A    console.warn('The response of your request was empty');%0A    return null;%0A  %7D%0A  let parsedObject = JSON.parse(req.responseText);%0A  if(!isObject(parsedObject))%7B%0A    //Maybe this check should read the header content-type%0A    console.warn('The response did not sent a JSON object');%0A  %7D%0A  return parsedObject;%0A%7D
 %0A%0A/*
@@ -1802,17 +1802,16 @@
 arser %7C%7C
- 
  jsonPar
@@ -2408,54 +2408,8 @@
 us;%0A
-      var err = JSON.parse(request.response);%0A
     
@@ -2460,28 +2460,37 @@
     
-var
+let
  err = 
-JSON.p
+options.errorP
 arse
+r
 (req
@@ -2871,17 +2871,17 @@
 indexOf(
-%22
+'
 applicat
@@ -2892,9 +2892,9 @@
 json
-%22
+'
 ) !=

Fix error parser and add check on the response content, only with console.warn, fix style.