Created
September 7, 2017 01:48
-
-
Save RaymondYe/5ebe1c796f46ef51b86fccdc8d81c73b to your computer and use it in GitHub Desktop.
axios extend
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
| // Add a request interceptor | |
| axios.interceptors.request.use(function (config) { | |
| // Do something before request is sent | |
| return config; | |
| }, function (error) { | |
| // Do something with request error | |
| return Promise.reject(error); | |
| }); | |
| // Add a response interceptor | |
| axios.interceptors.response.use(function (response) { | |
| // Do something with response data | |
| return response; | |
| }, function (error) { | |
| let originalRequest = error.config | |
| if (error.response !== undefined && error.response.status === 401 && !originalRequest._retry) { | |
| return Login(loggedUser).then((loginresponse) => { | |
| const params = loginresponse; | |
| originalRequest.headers['Authorization'] = 'Bearer ' + params.access_token; | |
| return axios(originalRequest); | |
| }); | |
| } | |
| // Do something with response error | |
| return Promise.reject(error); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment