Created
July 3, 2020 19:05
-
-
Save charlintosh/062c31b8b68377753566eb30e59ad97d to your computer and use it in GitHub Desktop.
Interceptors config file.
This file contains 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 {AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse} from "axios"; | |
const onRequest = (config: AxiosRequestConfig): AxiosRequestConfig => { | |
console.info(`[request] [${JSON.stringify(config)}]`); | |
return config; | |
} | |
const onRequestError = (error: AxiosError): Promise<AxiosError> => { | |
console.error(`[request error] [${JSON.stringify(error)}]`); | |
return Promise.reject(error); | |
} | |
const onResponse = (response: AxiosResponse): AxiosResponse => { | |
console.info(`[response] [${JSON.stringify(response)}]`); | |
return response; | |
} | |
const onResponseError = (error: AxiosError): Promise<AxiosError> => { | |
console.error(`[response error] [${JSON.stringify(error)}]`); | |
return Promise.reject(error); | |
} | |
export function setupInterceptorsTo(axiosInstance: AxiosInstance): AxiosInstance { | |
axiosInstance.interceptors.request.use(onRequest, onRequestError); | |
axiosInstance.interceptors.response.use(onResponse, onResponseError); | |
return axiosInstance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment