UNPKG

msal

Version:
61 lines 1.99 kB
// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. /** * 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)); } try { var jsonResponse = JSON.parse(xhr.responseText); } catch (e) { reject(_this.handleError(xhr.responseText)); } resolve(jsonResponse); }; 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; }()); export { XhrClient }; //# sourceMappingURL=XHRClient.js.map