text: "use strict";
  /*
   * Copyright (c) Microsoft Corporation. All rights reserved.
   * Licensed under the MIT License.
   */
  Object.defineProperty(exports, "__esModule", { value: true });
  /**
   * XHR client for JSON endpoints
   * https://www.npmjs.com/package/async-promise
   * @hidden
   */
  var XhrClient = /** @class */ (function () {
      function XhrClient() {
      }
      XhrClient.prototype.sendRequestAsync = function (url, method, enableCaching) {
          var _this = this;
          return new Promise(function (resolve, reject) {
              var xhr = new XMLHttpRequest();
              xhr.open(method, url, /* async: */ true);
              if (enableCaching) {
                  /*
                   * TODO: (shivb) ensure that this can be cached
                   * xhr.setRequestHeader("Cache-Control", "Public");
                   */
              }
              xhr.onload = function (ev) {
                  if (xhr.status < 200 || xhr.status >= 300) {
                      reject(_this.handleError(xhr.responseText));
                  }
                  var jsonResponse;
                  try {
                      jsonResponse = JSON.parse(xhr.responseText);
                  }
                  catch (e) {
                      reject(_this.handleError(xhr.responseText));
                  }
                  var response = {
                      statusCode: xhr.status,
                      body: jsonResponse
                  };
                  resolve(response);
              };
              xhr.onerror = function (ev) {
                  reject(xhr.status);
              };
              if (method === "GET") {
                  xhr.send();
              }
              else {
                  throw "not implemented";
              }
          });
      };
      XhrClient.prototype.handleError = function (responseText) {
          var jsonResponse;
          try {
              jsonResponse = JSON.parse(responseText);
              if (jsonResponse.error) {
                  return jsonResponse.error;
              }
              else {
                  throw responseText;
              }
          }
          catch (e) {
              return responseText;
          }
      };
      return XhrClient;
  }());
  exports.XhrClient = XhrClient;
  //# sourceMappingURL=XHRClient.js.map