Created
February 22, 2023 17:01
-
-
Save neharkarvishal/c5bc8dd0561eff530fee5d3908aee2d3 to your computer and use it in GitHub Desktop.
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 characters
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", | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment