axios - jckkaxios
Mon Apr 21 2025 05:17:36 GMT+0000 (Coordinated Universal Time)
Saved by @codejck
import axios, { AxiosRequestConfig } from 'axios'; import Axios from 'axios'; import { setTokens } from 'screens/SignUp/reducer/signup.reducer'; import { persisterStore, store } from 'services/redux/store'; import { TypeStore } from './redux/store'; import { isTokenExpired } from './utils'; import { CONFIG } from 'constants/config'; // import { useLogout } from 'hooks/useLogout'; // import { NotificationBox } from 'components/notificationBox'; export const injectStore = (_store: TypeStore) => { let store: TypeStore; store = _store; }; let isRefreshing = false; let refreshSubscribers: any[] = []; const processQueue = (error: null | string, token: string | null = null) => { refreshSubscribers.forEach((prom) => { if (error) { prom.reject(error); } else { prom.resolve(token); } }); refreshSubscribers = []; }; export interface ICapiumAuthPayload { email: string; password: string; } const capiumBaseURL = 'https://dev-identity.capium.co.uk/api/Auth/AuthenticateUser'; const googleUserInfoURL = 'https://www.googleapis.com/oauth2/v3/userinfo'; const refreshTokensURL = 'auth/refresh-tokens'; const fetchTokens = (refreshToken: string) => { return axios.post( `${CONFIG.apiUrl}${refreshTokensURL}`, {}, { headers: { Authorization: `Bearer ${refreshToken}`, }, } ); }; const instance = axios.create({ baseURL: CONFIG.apiUrl, timeout: 60000, }); const logOut = () => { // Clear user-related data from localStorage/sessionStorage localStorage.removeItem('authToken'); localStorage.removeItem('refreshToken'); persisterStore.purge(); // Purge persisted Redux store if needed // Optionally dispatch a Redux action to clear user state store.dispatch({ type: 'LOGOUT' }); // Redirect to login page or any other route window.location.href = '/login'; }; export const setInterseptors = () => { instance.interceptors.request.use(async (config: AxiosRequestConfig<any>) => { const token = store.getState().user.token; const refreshToken = store.getState().user.refreshToken; if (!token) { return config; } if (config.headers!.Authorization) { return config; } if (isTokenExpired(token)) { if (isRefreshing) { return new Promise(function (resolve, reject) { refreshSubscribers.push({ resolve, reject }); return config; }) .then((token) => { config.headers && (config.headers['Authorization'] = `Bearer ${token}`); return { ...config, headers: { Authorization: `Bearer ${token}` }, }; }) .catch((error) => { return Promise.reject(error); }); } isRefreshing = true; try { const { data } = await fetchTokens(refreshToken); store.dispatch( setTokens({ accessToken: data.access_token, refreshToken: data.refresh_token, }) ); processQueue(null, data.access_token); isRefreshing = false; return { ...config, headers: { Authorization: `Bearer ${data.access_token}` }, }; } catch (err: any) { store.dispatch({ type: 'LOGOUT' }); processQueue(err, null); isRefreshing = false; return config; } } else { return { ...config, headers: { Authorization: `Bearer ${token}`, }, }; } }); return instance; }; const token = store.getState().user.token; export const apiServices = { postData: async (requestUrl: string, payload: any) => { const token = store.getState().user.token; console.warn('()()post-post', token, payload); // return await instance.post(`${requestUrl}`, payload, { // headers: { Authorization: `Bearer ${token}` }, // }); try { const response = await instance.post(`${requestUrl}`, payload, { headers: { Authorization: `Bearer ${token}` }, }); if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); } //else if (response.status !== 200) { // callNotification(response); // setNotification({yesShow: true, message: 'Something went wrong. Try after sometimes!', type: 'warning'}); // dispatch(setNotification({yesShow: true, message: `${response.status} | Something went wrong. Try after sometimes!`, type: 'warning', position:'75'})) // } else if (response.status > 499 && response.status < 599) { // setNotification({yesShow: true, message: 'Technical Error. Sorry for Inconvience!', type: 'alert'}); // dispatch(setNotification({yesShow: true, message: `${response.status} | Technical Error. Sorry for Inconvience!`, type: 'alert', position:'75'})) // } return response; } catch (error: any) { // <NotificationBox titleText={notification.message || ''} closePopupFc={notificationRevoked} isShowPopup={notification.yesShow} type={notification.type}/> console.error('Error during POST request:', error); return error.response; // Return the successful response // throw error; } }, fetchData: async (requestUrl: string, params?: {}) => { const token = store.getState().user.token; console.warn('()()fetch-get', token); // return instance.get(`${requestUrl}`, { params , headers: { Authorization: `Bearer ${token}` } }); try { // Perform the GET request const response = await instance.get(`${requestUrl}`, { params, headers: { Authorization: `Bearer ${token}` }, }); if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); // Call logOut when the token has expired } return response; // Return the successful response } catch (error: any) { console.error('Error during GET request:', error); // If error response indicates token expiry, log out if (error.response?.data?.message === 'TOKEN IS EXPIRED') { logOut(); // Log out if the token is expired } throw error; // Propagate the error for further handling } }, changeData: async (requestUrl: string, payload: any) => { const token = store.getState().user.token; console.warn('()()change-put', token); try { // Perform the PUT request const response = await instance.put(`${requestUrl}`, payload, { headers: { Authorization: `Bearer ${token}` }, }); // Check if the token has expired if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); // Call logOut when the token has expired } return response; // Return the successful response } catch (error: any) { console.error('Error during PUT request:', error); // If error response indicates token expiry, log out if (error.response?.data?.message === 'TOKEN IS EXPIRED') { logOut(); // Log out if the token is expired } throw error; // Propagate the error for further handling } // return instance.put(`${requestUrl}`, payload, { // headers: { Authorization: `Bearer ${token}` }, // }); }, deleteData: async (requestUrl: string, params?: {}) => { const token = store.getState().user.token; console.warn('()()delete-delete', token); try { // Perform the DELETE request const response = await instance.delete(`${requestUrl}`, { params, headers: { Authorization: `Bearer ${token}` }, }); // Check if the token has expired if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); // Call logOut when the token has expired } return response; // Return the successful response } catch (error: any) { console.error('Error during DELETE request:', error); // If error response indicates token expiry, log out if (error.response?.data?.message === 'TOKEN IS EXPIRED') { logOut(); // Log out if the token is expired } throw error; // Propagate the error for further handling } // return instance.delete(`${requestUrl}`, { params, headers: { Authorization: `Bearer ${token}` } }); }, deleteDataPayload: async (requestUrl: string, payload?: any) => { const token = store.getState().user.token; console.warn('()()delete-delete', token); try { // Perform the DELETE request const response = await Axios.delete(requestUrl, { headers: { Authorization: `Bearer ${token}`, }, data: payload, }); // Check if the token has expired if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); // Call logOut when the token has expired } return response; // Return the successful response } catch (error: any) { console.error('Error during DELETE request:', error); // If error response indicates token expiry, log out if (error.response?.data?.message === 'TOKEN IS EXPIRED') { logOut(); // Log out if the token is expired } throw error; // Propagate the error for further handling } // return instance.delete(`${requestUrl}`, { params, headers: { Authorization: `Bearer ${token}` } }); }, updateData: async (requestUrl: string, payload: any) => { const token = store.getState().user.token; console.warn('()()update-patch', token); try { // Perform the PATCH request const response = await instance.patch(`${requestUrl}`, payload, { headers: { Authorization: `Bearer ${token}` }, }); // Check if the token has expired if (response.data.message === 'TOKEN IS EXPIRED') { logOut(); // Call logOut when the token has expired } return response; // Return the successful response } catch (error: any) { console.error('Error during PATCH request:', error); // If error response indicates token expiry, log out if (error.response?.data?.message === 'TOKEN IS EXPIRED') { logOut(); // Log out if the token is expired } throw error; // Propagate the error for further handling } // return instance.patch(`${requestUrl}`, payload, { // headers: { Authorization: `Bearer ${token}` }, // }); }, capiumFetchData: async (payload: ICapiumAuthPayload) => { const token = store.getState().user.token; console.warn('()()capium-post', token); const data = axios.post(capiumBaseURL, payload, { headers: { Authorization: `Bearer ${token}` }, }); return data; }, getGoogleUserInfo: async (token: string) => { console.warn('()()google-get', token); const data = await axios.get(googleUserInfoURL, { headers: { Authorization: `Bearer ${token}` }, }); return data; }, };
Comments