/**
 * HTTP client for ajax, like vue-resource, but smaller and stand alone
 * @param {Object} options
 * @param {Function} options.onerror especially for network error
 * @param {Function} options.onfail for server error
 */
export default ({
  type,
  url,
  data,
  headers = {},
  withCredentials = false,

  timeout,
  ontimeout,
  onerror,
  onfail
}) => {
  return new Promise((resolve, reject) => {
    let req = new XMLHttpRequest()

    type = type.toUpperCase()

    req.open(type, url, true)
    req.timeout = timeout
    req.withCredentials = withCredentials

    for (let key in headers) {
      req.setRequestHeader(key, headers[key])
    }

    function rejectWith(fn) {
      return function() {
        fn(req)
        reject(req)
      }
    }

    // network error
    req.onerror = rejectWith(onerror)

    // timeout error
    req.ontimeout = rejectWith(ontimeout)

    // xhr.readystate = 4
    req.onload = function() {
      // success
      if (req.status < 400) resolve(req)
      // server error
      else rejectWith(onfail)()
    }

    req.send(data)
  }).then(req => {
    // resolve result
    let result = req.responseText

    if (/json/.test(req.getResponseHeader('Content-Type'))) {
      result = JSON.parse(req.responseText || null)
    }

    return result
  })
}

@@ -166,61 +166,8 @@
 ror%0A
- * @param %7BFunction%7D options.onfail for server error%0A
  */%0A
@@ -288,18 +288,8 @@
 rror
-,%0A  onfail
 %0A%7D) 
@@ -834,17 +834,17 @@
 hr.ready
-s
+S
 tate = 4
@@ -884,215 +884,65 @@
     
-// success%0A      if (req.status %3C 400) resolve(req)%0A      // server error%0A      else rejectWith(onfail)()%0A    %7D%0A%0A    req.send(data)%0A  %7D).then(req =%3E %7B%0A    // resolve result%0A    let result = req.responseText%0A
+resolve(req)%0A    %7D%0A%0A    req.send(data)%0A  %7D).then(req =%3E %7B
 %0A   
@@ -1008,20 +1008,22 @@
       re
-sult
+q.data
  = JSON.
@@ -1078,12 +1078,9 @@
 n re
-sult
+q
 %0A  %7D

remove http onfail