const axios = require('axios'); /** * Function that configures axios with timing interceptors * Important to note here that the timings are not completely accurate. * @see https://github.com/axios/axios/issues/695 * @returns {import('axios').AxiosStatic} */ function makeAxiosInstance() { /** @type {import('axios').AxiosStatic} */ const instance = axios.create(); instance.interceptors.request.use((config) => { config.headers['request-start-time'] = Date.now(); return config; }); instance.interceptors.response.use( (response) => { const end = Date.now(); const start = response.config.headers['request-start-time']; response.headers['request-duration'] = end - start; return response; }, (error) => { if (error.response) { const end = Date.now(); const start = error.config.headers['request-start-time']; if (error.response) { error.response.headers['request-duration'] = end - start; } } return Promise.reject(error); } ); return instance; } module.exports = { makeAxiosInstance };