if (!window.SequraFE) { window.SequraFE = {}; } if (!window.SequraFE.customHeader) { window.SequraFE.customHeader = {}; } /** * @typedef AjaxServiceType * @property {(url:string, errorCallback?: (error?: Record) => Promise | any, fallthrough?: * boolean) => Promise} get * @property {(url:string, data?: any, customHeader?: Record, errorCallback: (error?: * Record) => Promise) => Promise} post * @property {(url:string, data?: any, customHeader?: Record, errorCallback: (error?: * Record) => Promise) => Promise} put * @property {(url:string, data?: any, errorCallback?: (error?: Record) => Promise) => * Promise} delete */ (function () { /** * Ajax/API service. * * @returns {AjaxServiceType} */ const AjaxService = () => { /** * Handles the server response. * @param {Response} response * @param {(error: Record) => Promise?} errorCallback * @returns {Record} */ const handleResponse = (response, errorCallback) => { if (!errorCallback) { errorCallback = SequraFE.responseService.errorHandler; } try { if (response.ok) { return response.json(); } if (response.status === 401 || response.status === 403) { return response.json().then(SequraFE.responseService.unauthorizedHandler); } if (response.status === 400) { return response.json().then(errorCallback); } } catch (e) { } return errorCallback({ status: response.status, error: response.statusMessage }); }; /** * Performs GET ajax request. * * @param {string} url The URL to call. * @param {(error: Record) => Promise?} errorCallback * @param {Record?} customHeader */ const get = (url, errorCallback, customHeader = {}) => call('GET', url, null, errorCallback, customHeader); /** * Performs POST ajax request. * * @param {string} url The URL to call. * @param {Record?} data * @param {Record?} customHeader * @param {(error: Record) => Promise?} errorCallback */ const post = (url, data, customHeader, errorCallback) => call('POST', url, data, errorCallback, customHeader); /** * Performs PUT ajax request. * * @param {string} url The URL to call. * @param {Record} data * @param {Record?} customHeader * @param {(error: Record) => Promise?} errorCallback */ const put = (url, data, customHeader, errorCallback) => call('PUT', url, data, errorCallback, customHeader); /** * Performs DELETE ajax request. * * @param {string} url The URL to call. * @param {Record?} data * @param {Record?} customHeader * @param {(error: Record) => Promise?} errorCallback */ const del = (url, data, customHeader, errorCallback) => call('DELETE', url, data, errorCallback, customHeader); /** * Performs ajax call. * * @param {'GET' | 'POST' | 'PUT' | 'DELETE'} method The HTTP method. * @param {string} url The URL to call. * @param {Record?} data The data to send. * @param {(error: Record) => Promise?} errorCallback An error callback. If not set, the * default one will be used. * @param {Record?} customHeader * @returns {Promise>} */ const call = (method, url, data, errorCallback, customHeader) => { const callUUID = SequraFE.StateUUIDService.getStateUUID(); return new Promise((resolve, reject) => { url = url.replace('https:', ''); url = url.replace('http:', ''); let authHeader = {}; if (SequraFE?.integration?.authToken) { authHeader = { 'Authorization': `Bearer ${SequraFE.integration.authToken}` } } const headers = { 'Content-Type': 'application/json', ...authHeader, ...(customHeader || {}) }; if (headers['Content-Type'] === 'multipart/form-data') { delete headers['Content-Type']; } const body = data ? headers['Content-Type'] === 'application/json' ? JSON.stringify(data) : data : undefined; fetch(url, { method, headers, body }).then((response) => { if (callUUID !== SequraFE.StateUUIDService.getStateUUID()) { // Obsolete request. The app has changed the original state (page) that issued the call. // console.log('cancelling an obsolete request', url); return; } handleResponse(response, errorCallback).then(resolve).catch(reject); }).catch(reject); }); }; return { get, post, put, delete: del, call, handleResponse }; }; SequraFE.ajaxService = AjaxService(); })();