Created
February 22, 2023 17:01
-
-
Save neharkarvishal/c5bc8dd0561eff530fee5d3908aee2d3 to your computer and use it in GitHub Desktop.
Revisions
-
neharkarvishal created this gist
Feb 22, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ import axios from "axios"; import {v4 as uuid} from 'uuid' const API_URL = `${process.env.REACT_APP_URL}/`; const Axios = axios.create({ baseURL: API_URL, }); //API Call Interceptor to add token to the header. Axios.interceptors.request.use( async (config) => { const token = store.getState().auth.token; config.headers["Authorization"] = "Bearer " + token; config.headers["X-Request-ID"] = `${uuid()}` return config; }, (error) => { Promise.reject(error); } ); Axios.interceptors.response.use((response) => { return response }, async function (error) { const originalRequest = error.config; if (error.response.status === 400 && !originalRequest._retry) { originalRequest._retry = true; const token = await getAccessToken(); axios.defaults.headers.common['Authorization'] = 'Bearer ' + token; return Axios(originalRequest); } return Promise.reject(error); }); const options = { headers: { "Content-Type": "application/json", }, };