"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.req = req;
exports.GET = GET;
exports.POST = POST;
exports.DELETE = DELETE;
exports.PATCH = PATCH;

/* eslint-disable */
// A simple wrapper for XHR.
function req(conf) {
  let req = new XMLHttpRequest(),
      aborted = false;
  let result = new Promise((success, failure) => {
    req.open(conf.method, conf.url, true);
    req.addEventListener('load', () => {
      if (aborted) return;

      if (req.status < 400) {
        success(req.responseText);
      } else {
        let text = req.responseText;
        if (text && /html/.test(req.getResponseHeader('content-type'))) text = makePlain(text);
        let err = new Error('Request failed: ' + req.statusText + (text ? '\n\n' + text : ''));
        err.status = req.status;
        failure(err);
      }
    });
    req.addEventListener('error', () => {
      if (!aborted) failure(new Error('Network error'));
    });
    if (conf.headers) for (let header in conf.headers) req.setRequestHeader(header, conf.headers[header]);
    req.send(conf.body || null);
  });

  result.abort = () => {
    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, '');
}

function GET(url) {
  return req({
    url,
    method: 'GET'
  });
}

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


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


function PATCH(url, body, type) {
  return req({
    url,
    method: 'PATCH',
    body,
    headers: {
      'Content-Type': type
    }
  });
}