/* eslint-disable */

// A simple wrapper for XHR.
export function req(conf) {
  var req = new XMLHttpRequest(),
    aborted = false;
  var result = new Promise(function (success, failure) {
    req.open(conf.method, conf.url, true);
    req.addEventListener('load', function () {
      if (aborted) return;
      if (req.status < 400) {
        success(req.responseText);
      } else {
        var text = req.responseText;
        if (text && /html/.test(req.getResponseHeader('content-type'))) text = makePlain(text);
        var err = new Error('Request failed: ' + req.statusText + (text ? '\n\n' + text : ''));
        err.status = req.status;
        failure(err);
      }
    });
    req.addEventListener('error', function () {
      if (!aborted) failure(new Error('Network error'));
    });
    if (conf.headers) for (var header in conf.headers) req.setRequestHeader(header, conf.headers[header]);
    req.send(conf.body || null);
  });
  result.abort = function () {
    if (!aborted) {
      req.abort();
      aborted = true;
    }
  };
  return result;
}
function makePlain(html) {
  var elt = document.createElement('div');
  elt.innerHTML = html;
  return elt.textContent.replace(/\n[^]*|\s+$/g, '');
}
export function GET(url) {
  return req({
    url: url,
    method: 'GET'
  });
}
export function POST(url, body, type) {
  return req({
    url: url,
    method: 'POST',
    body: body,
    headers: {
      'Content-Type': type
    }
  });
}
export function PUT(url, body, type) {
  return req({
    url: url,
    method: 'PUT',
    body: body,
    headers: {
      'Content-Type': type
    }
  });
}

// [FS] IRAD-1128 2021-02-03
// http DELETE request overrided
export function DELETE(url, type) {
  return req({
    url: url,
    method: 'DELETE',
    headers: {
      'Content-Type': type
    }
  });
}

// [FS] IRAD-1128 2021-02-03
// http PATCH request overrided
export function PATCH(url, body, type) {
  return req({
    url: url,
    method: 'PATCH',
    body: body,
    headers: {
      'Content-Type': type
    }
  });
}