1fded0d11SDerick Montagueimport Axios from 'axios'; 26de03414SPaul Fertserimport router from '../router'; 301492c3dSEd Tanousimport { setupCache, buildWebStorage } from 'axios-cache-interceptor'; 401492c3dSEd Tanous 58263d85cSYoshie Muranaka//Do not change store import. 68263d85cSYoshie Muranaka//Exact match alias set to support 78263d85cSYoshie Muranaka//dotenv customizations. 87d6b44cbSEd Tanousimport store from '.'; 974c24f15SYoshie Muranaka 1080d697d8SEd TanousAxios.defaults.headers.common['Accept'] = 'application/json'; 1180d697d8SEd TanousAxios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; 1280d697d8SEd Tanous 1301492c3dSEd Tanousconst axiosInstance = Axios.create({ 14602e98aaSDerick Montague withCredentials: true, 156ce1a07cSYoshie Muranaka}); 1674c24f15SYoshie Muranaka 1701492c3dSEd Tanousconst api = setupCache(axiosInstance, { 1801492c3dSEd Tanous debug: console.log, 1901492c3dSEd Tanous methods: ['get'], 2001492c3dSEd Tanous interpretHeader: false, 2101492c3dSEd Tanous etag: true, 2201492c3dSEd Tanous modifiedSince: false, 2301492c3dSEd Tanous staleIfError: false, 2401492c3dSEd Tanous ttl: 0, 2501492c3dSEd Tanous storage: buildWebStorage(localStorage, 'webui-vue-cache:'), 2601492c3dSEd Tanous}); 2701492c3dSEd Tanous 28602e98aaSDerick Montagueapi.interceptors.response.use(undefined, (error) => { 29227c41a9SDerick Montague let response = error.response; 30126eaabeSDerick Montague 31227c41a9SDerick Montague // TODO: Provide user with a notification and way to keep system active 32227c41a9SDerick Montague if (response.status == 401) { 33676f2fcaSDerick Montague if (response.config.url != '/login') { 34227c41a9SDerick Montague window.location = '/login'; 3568069e19SYoshie Muranaka // Commit logout to remove XSRF-TOKEN cookie 3668069e19SYoshie Muranaka store.commit('authentication/logout'); 37227c41a9SDerick Montague } 38676f2fcaSDerick Montague } 39126eaabeSDerick Montague 40dd6aa0aaSSukanya Pandey // Check if action is unauthorized. 416de03414SPaul Fertser if (response.status == 403) { 42ce7db82cSPaul Fertser if (isPasswordExpired(response.data)) { 436de03414SPaul Fertser router.push('/change-password'); 446de03414SPaul Fertser } else { 456de03414SPaul Fertser // Toast error message will appear on screen. 46dd6aa0aaSSukanya Pandey store.commit('global/setUnauthorized'); 47126eaabeSDerick Montague } 486de03414SPaul Fertser } 49676f2fcaSDerick Montague 50676f2fcaSDerick Montague return Promise.reject(error); 51227c41a9SDerick Montague}); 52227c41a9SDerick Montague 5374c24f15SYoshie Muranakaexport default { 542a87a2e4Syubowei982 get(path, config) { 552a87a2e4Syubowei982 return api.get(path, config); 5674c24f15SYoshie Muranaka }, 572a87a2e4Syubowei982 delete(path, config) { 582a87a2e4Syubowei982 return api.delete(path, config); 5974c24f15SYoshie Muranaka }, 6037393810SYoshie Muranaka post(path, payload, config) { 6137393810SYoshie Muranaka return api.post(path, payload, config); 6274c24f15SYoshie Muranaka }, 632a87a2e4Syubowei982 patch(path, payload, config) { 642a87a2e4Syubowei982 return api.patch(path, payload, config); 6574c24f15SYoshie Muranaka }, 662a87a2e4Syubowei982 put(path, payload, config) { 672a87a2e4Syubowei982 return api.put(path, payload, config); 6874c24f15SYoshie Muranaka }, 6974c24f15SYoshie Muranaka all(promises) { 7074c24f15SYoshie Muranaka return Axios.all(promises); 71183c2754SYoshie Muranaka }, 72183c2754SYoshie Muranaka spread(callback) { 73183c2754SYoshie Muranaka return Axios.spread(callback); 74602e98aaSDerick Montague }, 7509a3b9e0SPaul Fertser set_auth_token(token) { 7609a3b9e0SPaul Fertser axiosInstance.defaults.headers.common['X-Auth-Token'] = token; 7709a3b9e0SPaul Fertser }, 7874c24f15SYoshie Muranaka}; 79be3af336SYoshie Muranaka 80602e98aaSDerick Montagueexport const getResponseCount = (responses) => { 81be3af336SYoshie Muranaka let successCount = 0; 82be3af336SYoshie Muranaka let errorCount = 0; 83be3af336SYoshie Muranaka 84602e98aaSDerick Montague responses.forEach((response) => { 85be3af336SYoshie Muranaka if (response instanceof Error) errorCount++; 86be3af336SYoshie Muranaka else successCount++; 87be3af336SYoshie Muranaka }); 88be3af336SYoshie Muranaka 89be3af336SYoshie Muranaka return { 90be3af336SYoshie Muranaka successCount, 91602e98aaSDerick Montague errorCount, 92be3af336SYoshie Muranaka }; 93be3af336SYoshie Muranaka}; 946de03414SPaul Fertser 95ce7db82cSPaul Fertserexport const isPasswordExpired = (data) => { 96*c1567299Sjason westover return !!findMessageId(data, 'PasswordChangeRequired'); 97*c1567299Sjason westover}; 98*c1567299Sjason westover 99*c1567299Sjason westover/** 100*c1567299Sjason westover * Returns the first ExtendedInfo.Message to start with the 101*c1567299Sjason westover * Registry Name (Default: "Base") and end with the given key 102*c1567299Sjason westover * Ignore versions (.<X>.<Y>) --or-- (.<X>.<Y>.<Z>.), 103*c1567299Sjason westover * but adhere to Registry namespace 104*c1567299Sjason westover * @param {object} data - AxiosResponse.data 105*c1567299Sjason westover * @param { {MessageKey: string}} key - key into the message registry 106*c1567299Sjason westover * @param { {MessageRegistryPrefix: string}} [registry=Base] - the name of the 107*c1567299Sjason westover * message registry, undefined param defaults to "Base" 108*c1567299Sjason westover * @returns {ExtendedInfo.Message} ExtendedInfo.Message | undefined 109*c1567299Sjason westover */ 110*c1567299Sjason westoverexport const findMessageId = (data, key, registry = 'Base') => { 111ce7db82cSPaul Fertser let extInfoMsgs = data?.['@Message.ExtendedInfo']; 112*c1567299Sjason westover 1136de03414SPaul Fertser return ( 1146de03414SPaul Fertser extInfoMsgs && 115*c1567299Sjason westover extInfoMsgs.find((i) => { 116*c1567299Sjason westover const words = i.MessageId.split('.'); 117*c1567299Sjason westover return words[words.length - 1] === key && words[0] === registry; 118*c1567299Sjason westover }) 1196de03414SPaul Fertser ); 1206de03414SPaul Fertser}; 121