-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #476 from mittwald/task/request-debugging
Add debug logging to Axios requests
- Loading branch information
Showing
2 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import debug from "debug"; | ||
import { AxiosInstance } from "@mittwald/api-client-commons"; | ||
|
||
/** | ||
* Configure logging for Axios requests and responses using the `debug` module. | ||
* | ||
* Run the CLI with `DEBUG=mw:api:client:*` to see the logs. Keep in mind that | ||
* this will also log sensitive information in request bodies or headers. | ||
*/ | ||
export function configureAxiosLogging(axios: AxiosInstance) { | ||
const baseDebugger = debug("mw:api:client"); | ||
const reqDebugger = baseDebugger.extend("request"); | ||
const resDebugger = baseDebugger.extend("response"); | ||
|
||
axios.interceptors.request.use((config) => { | ||
reqDebugger( | ||
"%s %s requested with %O", | ||
config.method?.toUpperCase(), | ||
config.url, | ||
config.data, | ||
); | ||
return config; | ||
}); | ||
|
||
axios.interceptors.response.use((response) => { | ||
resDebugger( | ||
"%s %s responded with %o %O", | ||
response.config.method?.toUpperCase(), | ||
response.config.url, | ||
response.status + " " + response.statusText, | ||
response.data, | ||
); | ||
return response; | ||
}); | ||
} |