/* eslint-disable no-empty-function, no-process-env */
const request = require('@sanity/request') // `request` in node, `xhr` in browsers
const queryString = require('./queryString')

const debug = (process.env.DEBUG || '').indexOf('sanity') !== -1

let log = () => {}
if (process.env.NODE_ENV !== 'production') {
  log = require('debug')('sanity:client')
}

module.exports = function httpRequest(options, callback) {
  if (options.query) {
    options.uri += `?${queryString.stringify(options.query)}`
  }

  if (debug) {
    log('HTTP %s %s', options.method || 'GET', options.uri)
    if (options.method === 'POST' && options.json) {
      log('Request body: %s', JSON.stringify(options.json, null, 2))
    }
  }

  const Promised = options.promise
  return new Promised((resolve, reject) => {
    request(options, (err, res, body) => {
      if (err) {
        return reject(err)
      }

      log('Response code: %s', res.statusCode)
      if (debug && body) {
        log('Response body: %s', stringifyBody(body, res))
      }

      const isHttpError = res.statusCode >= 400

      if (isHttpError && body) {
        const msg = (body.errors ? body.errors.map(error => error.message) : [])
          .concat([body.error, body.message]).filter(Boolean).join('\n')

        return reject(new Error(msg))
      } else if (isHttpError) {
        return reject(new Error(
          `Server responded with HTTP ${res.statusCode} ${res.statusMessage || ''}, no description`
        ))
      }

      return resolve(body)
    })
  })
}

function stringifyBody(body, res) {
  const contentType = (res.headers['content-type'] || '').toLowerCase()
  const isJson = contentType.indexOf('application/json') !== -1
  return isJson ? JSON.stringify(body, null, 2) : body
}

@@ -1276,112 +1276,304 @@
     
-return reject(new Error(msg))%0A      %7D else if (isHttpError) %7B%0A        return reject(new Error(%0A         
+const error = new Error(msg %7C%7C httpError(res))%0A        error.responseBody = stringifyBody(body, res)%0A        return reject(error)%0A      %7D else if (isHttpError) %7B%0A        return reject(new Error(httpError(res)))%0A      %7D%0A%0A      return resolve(body)%0A    %7D)%0A  %7D)%0A%7D%0A%0Afunction httpError(res) %7B%0A  return
  %60Se
@@ -1663,67 +1663,8 @@
 on%60%0A
-        ))%0A      %7D%0A%0A      return resolve(body)%0A    %7D)%0A  %7D)%0A
 %7D%0A%0Af

Improve handling of non-JSON HTTP errors