Created
December 18, 2024 07:58
-
-
Save Rankarusu/ceb07af138679d1d0fe2ba1fccf62be7 to your computer and use it in GitHub Desktop.
Axios Logger as interceptor
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 } from "axios"; | |
// using react-native-logger here | |
import { Logger } from "@/utils/logger"; | |
export function registerDebugLogger(instance: AxiosInstance) { | |
Logger.debug("registering debug loggers"); | |
instance.interceptors.request.use( | |
(config) => { | |
Logger.debug( | |
`${config.method?.toUpperCase()} ${instance.getUri(config)}`, | |
); | |
return config; | |
}, | |
(error: AxiosError) => { | |
Logger.error(error.toJSON()); | |
return Promise.reject(error); | |
}, | |
); | |
instance.interceptors.response.use( | |
(response) => { | |
Logger.debug(`${response.status} (${instance.getUri(response.config)})`); | |
return response; | |
}, | |
(error: AxiosError) => { | |
Logger.error(error.toJSON(), error.response?.data); | |
return Promise.reject(error); | |
}, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment