@attivio/suit
Version:
Attivio SUIT, the Search UI Toolkit, is a library for creating search clients for searching the Attivio platform.
112 lines (101 loc) • 3.93 kB
JavaScript
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.
* This method 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, errorMessage) {
var headers = new Headers({
Accept: 'application/json',
'Content-Type': 'application/json'
});
var body = payload ? JSON.stringify(payload) : undefined;
var params = {
method: method,
headers: headers,
body: body,
credentials: 'include',
mode: 'cors'
};
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, errorMessage));
});
} else {
response.text().then(function (text) {
if (text.indexOf('SAML') !== -1) {
// If the response content-type is HTML and it mentions SAML, it's 99.9% likely
// that it's a redirect to a login page. If so, then we then reload the whole
// page to let the user log in again.
window.location.reload();
} else if (text && text.includes('j_security_check')) {
window.location.reload();
} else {
// We've received some other sort of error, so let's just log it and stop.
var msg = 'REST call to ' + uri + ' failed to return JSON.';
console.error(msg);
callback(null, msg);
}
}).catch(function (error) {
var msg = 'Results of REST call to ' + uri + ' couldn\'t be parsed.';
console.error(msg, error);
callback(null, msg);
});
}
} else {
// The request came back other than a 200-type response code
// There should be JSON describing it...
response.json().then(function (searchException) {
var exceptionMessage = searchException.message ? searchException.message : '';
var exceptionCode = searchException.errorCode ? ' (' + searchException.errorCode + ')' : '';
var finalExceptionMessage = errorMessage + ' ' + exceptionMessage + exceptionCode;
callback(null, finalExceptionMessage);
}).catch(function (badJsonError) {
callback(null, FetchUtils.getErrorMessage(badJsonError, errorMessage));
});
}
}).catch(function (error) {
// Catch exceptions from the main "then" function
callback(null, FetchUtils.getErrorMessage(error, errorMessage));
});
});
/**
* Get the error message out of the error object.
*
* @param error the error received
* @return a string representing the error object
*/
FetchUtils.getErrorMessage = function getErrorMessage(error, errorMessage) {
var message = void 0;
if (error && error.message) {
message = error.message;
} else {
message = errorMessage;
}
return message;
};
return FetchUtils;
}();
export { FetchUtils as default };