"use strict";exports.__esModule = true;exports.fetch = fetch;var _globalObject = require("@skbkontur/global-object");















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.globalObject;
  if (globalWithXDomainRequest.XDomainRequest) {
    return new globalWithXDomainRequest.XDomainRequest();
  }
  if (_globalObject.globalObject.XMLHttpRequest) {
    return new _globalObject.globalObject.XMLHttpRequest();
  }
}