Skip to content

Instantly share code, notes, and snippets.

@charlintosh
Created July 3, 2020 19:05
Show Gist options
  • Save charlintosh/062c31b8b68377753566eb30e59ad97d to your computer and use it in GitHub Desktop.
Save charlintosh/062c31b8b68377753566eb30e59ad97d to your computer and use it in GitHub Desktop.
Interceptors config file.
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