import { globalObject } from '@skbkontur/global-object';
export function fetch(uri, options) {
  if (options === void 0) {
    options = {};
  }
  var method = options.method || 'GET';
  var xhr = createXHR();
  var promise = new Promise(function (resolve, reject) {
    if (xhr) {
      xhr.onerror = reject;
      xhr.ontimeout = reject;
      xhr.onload = function () {
        resolve({
          ok: xhr.status >= 200 && xhr.status < 300,
          status: xhr.status,
          statusText: xhr.statusText,
          text: function text() {
            return Promise.resolve(xhr.responseText);
          },
          json: function json() {
            return Promise.resolve(JSON.parse(xhr.responseText));
          }
        });
      };
    }
  });
  if (xhr) {
    xhr.open(method, uri);
    xhr.send(options.body);
  }
  return promise;
}
function createXHR() {
  var globalWithXDomainRequest = globalObject;
  if (globalWithXDomainRequest.XDomainRequest) {
    return new globalWithXDomainRequest.XDomainRequest();
  }
  if (globalObject.XMLHttpRequest) {
    return new globalObject.XMLHttpRequest();
  }
}