diff --git a/src/BaseCommand.ts b/src/BaseCommand.ts index 30cb4e7a..221951e9 100644 --- a/src/BaseCommand.ts +++ b/src/BaseCommand.ts @@ -3,6 +3,7 @@ import { MittwaldAPIV2Client } from "@mittwald/api-client"; import { configureAxiosRetry } from "./lib/api_retry.js"; import { configureConsistencyHandling } from "./lib/api_consistency.js"; import { getTokenFilename, readApiToken } from "./lib/auth/token.js"; +import { configureAxiosLogging } from "./lib/api_logging.js"; export abstract class BaseCommand extends Command { protected authenticationRequired = true; @@ -23,6 +24,7 @@ export abstract class BaseCommand extends Command { this.apiClient.axios.defaults.headers["User-Agent"] = `mittwald-cli/${this.config.version}`; + configureAxiosLogging(this.apiClient.axios); configureAxiosRetry(this.apiClient.axios); configureConsistencyHandling(this.apiClient.axios); } diff --git a/src/lib/api_logging.ts b/src/lib/api_logging.ts new file mode 100644 index 00000000..f73b0385 --- /dev/null +++ b/src/lib/api_logging.ts @@ -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; + }); +}