We are needing log the HTTP request that axios will send to the remote host, along with the response. This data will be stored in a database for a number of days before being automatically pruned.
The lifecycle we are looking at for the logging:
// creates the session that joins both the request and response
function createSession (url: string): string;
// log the request message as sent
function logRequest (sessionId: string, headers: Record<string, any>, body: Record<string, any> | string);
// log the request message as received
function logResponse (sessionId: string, headers: Record<string, any>, body: Record<string, any> | string);
// close the session, recording the final status and any error message
function closeSession(sessionId: string, status: number, error?: string);
We have looked at the request and response interceptors, but the issue we are having is that the request interceptor is before axios adds all the headers is going to send and the response interceptor doesn't seem to have access to the request, to be able to correlate the request and response.
Without needing to channel all this through a proxy to do the logging, how would you suggest doing this with axios?