UNPKG

@sisu-llc/pki-suit

Version:

Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.

97 lines (86 loc) 3.32 kB
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Utility class to handle fetch requests. */ var FetchUtils = function () { function FetchUtils() { _classCallCheck(this, FetchUtils); } /** * Make a fetch call. * It ensures that all requests behave the same including handling SAML errors when a user's session times out. */ FetchUtils.fetch = function (_fetch) { function fetch(_x, _x2, _x3, _x4, _x5) { return _fetch.apply(this, arguments); } fetch.toString = function () { return _fetch.toString(); }; return fetch; }(function (uri, payload, callback, method, errorMesssage) { var headers = new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }); var body = payload ? JSON.stringify(payload) : null; var params = { method: method, headers: headers, body: body, credentials: 'include' }; var fetchRequest = new Request(uri, params); fetch(fetchRequest).then(function (response) { if (response.ok) { var contentType = response.headers.get('content-type'); if (!contentType || contentType.indexOf('text/html') === -1) { response.json().then(function (jsonResponse) { callback(jsonResponse, null); }).catch(function (error) { // Catch errors from converting the response's JSON callback(null, FetchUtils.getErrorMessage(error, errorMesssage)); }); } else { // If the response content-type is HTML then reload the page because user's session likely timedout. // SAML tries to redirect but we can't deal with that in Ajax. window.location.reload(); } } else { // The request came back other than a 200-type response code // There should be JSON describing it... response.json().then(function (searchException) { var exceptionMessasge = searchException.message ? searchException.message : ''; var exceptionCode = searchException.errorCode ? ' (' + searchException.errorCode + ')' : ''; var finalExceptionMessage = errorMesssage + ' ' + exceptionMessasge + exceptionCode; callback(null, finalExceptionMessage); }).catch(function (badJsonError) { callback(null, FetchUtils.getErrorMessage(badJsonError, errorMesssage)); }); } }, function (error) { // Catch network-type errors from the main fetch() call callback(null, FetchUtils.getErrorMessage(error, errorMesssage)); }).catch(function (error) { // Catch exceptions from the main "then" function callback(null, FetchUtils.getErrorMessage(error, errorMesssage)); }); }); /** * Get the error message out of the error object. * * @param error the error recieved * @return a string represening the error object */ FetchUtils.getErrorMessage = function getErrorMessage(error, errorMesssage) { var message = void 0; if (error && error.message) { message = error.message; } else { message = errorMesssage; } return message; }; return FetchUtils; }(); export { FetchUtils as default };