import config from './config'

const wrapFetch = function (request) {
  let status = 200
  return new Promise((resolve, reject) => {
    fetch(request).then(res => {
      if (res.ok) return res.json()
      status = res.status
      throw {
        status,
        msg: config.errorStatusMsg[res.status] || res
      }
      // return res.text()
    }).then(body => {
      if (body.code === 0) {
        resolve(body.data)
      } else {
        throw {
          status,
          code: body.code,
          msg: config.errorMsg[body.code] || body.msg || body.error || 'Unknown Error'
        }
      }
    }).catch(reject)
  })
}
let urlPrefix = '/api', CSRFHeader = 'X-CSRFToken', CSRFValue = 'csrf'
const requestUrl = function (url) {
  if (url.startsWith('/') || url.startsWith('http')) return url
  return `${urlPrefix}/${url}`
}

export default {
  prefix(prefix) {
    urlPrefix = prefix
  },
  setCSRF(header, value) {
    CSRFHeader = header
    CSRFValue = value
  },
  get(url) {
    return wrapFetch(new Request(requestUrl(url), {
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'x-access-token': localStorage.token
      }
    }))
  },
  post(url, params) {
    return wrapFetch(new Request(requestUrl(url), {
      method: 'post',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'x-access-token': localStorage.token,
        [CSRFHeader]: sessionStorage[CSRFValue]
      },
      body: JSON.stringify(params)
    }))
  },
  form(url, params) {
    return wrapFetch(new Request(requestUrl(url), {
      method: 'post',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'x-access-token': localStorage.token,
        [CSRFHeader]: sessionStorage[CSRFValue]
      },
      body: params
    }))
  },
  put(url, params) {
    return wrapFetch(new Request(requestUrl(url), {
      method: 'put',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'x-access-token': localStorage.token,
        [CSRFHeader]: sessionStorage[CSRFValue]
      },
      body: JSON.stringify(params)
    }))
  },
  putForm(url, params) {
    return wrapFetch(new Request(requestUrl(url), {
      method: 'put',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'x-access-token': localStorage.token,
        [CSRFHeader]: sessionStorage[CSRFValue]
      },
      body: params
    }))
  },
  delete(url) {
    return wrapFetch(new Request(requestUrl(url), {
      method: 'delete',
      credentials: 'same-origin',
      headers: {
        'Accept': 'application/json',
        'x-access-token': localStorage.token,
        [CSRFHeader]: sessionStorage[CSRFValue]
      }
    }))
  }
}

@@ -627,16 +627,17 @@
 %0A  %7D)%0A%7D%0A
+%0A
 let urlP
@@ -654,56 +654,27 @@
 api'
-, CSRFHeader = 'X-CSRFToken', CSRFValue = 'csrf'
+%0Aconst headers = %7B%7D
 %0Acon
@@ -877,19 +877,18 @@
  set
-CSRF(h
+H
 eader
+(key
 , va
@@ -902,41 +902,20 @@
     
-CSRFHeader = header%0A    CSRFValue
+headers%5Bkey%5D
  = v
@@ -1090,44 +1090,18 @@
     
-'x-access-token': localStorage.token
+...headers
 %0A   
@@ -1359,93 +1359,18 @@
     
-'x-access-token': localStorage.token,%0A        %5BCSRFHeader%5D: sessionStorage%5BCSRFValue%5D
+...headers
 %0A   
@@ -1620,93 +1620,18 @@
     
-'x-access-token': localStorage.token,%0A        %5BCSRFHeader%5D: sessionStorage%5BCSRFValue%5D
+...headers
 %0A   
@@ -1907,93 +1907,18 @@
     
-'x-access-token': localStorage.token,%0A        %5BCSRFHeader%5D: sessionStorage%5BCSRFValue%5D
+...headers
 %0A   
@@ -2170,93 +2170,18 @@
     
-'x-access-token': localStorage.token,%0A        %5BCSRFHeader%5D: sessionStorage%5BCSRFValue%5D
+...headers
 %0A   
@@ -2411,93 +2411,18 @@
     
-'x-access-token': localStorage.token,%0A        %5BCSRFHeader%5D: sessionStorage%5BCSRFValue%5D
+...headers
 %0A   

Support setting headers in fetch