function http_factory(method) {
  return async (url, data) => {
    return new Promise((resolve, reject) => {
      fetch(url, {
        method,
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(data),
      })
        .then((res) => res.json())
        .then((res) => {
          resolve(res.data)
        })
        .catch((e) => {
          console.warn(url, e)
          reject(e)
        })
    })
  }
}

const http_get = http_factory('GET')
const http_post = http_factory('POST')

export { http_get, http_post }
