mirror of
https://github.com/usebruno/bruno.git
synced 2024-12-27 00:59:12 +01:00
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
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 {axios.AxiosInstance}
|
|
*/
|
|
function makeAxiosInstance() {
|
|
/** @type {axios.AxiosInstance} */
|
|
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'];
|
|
error.response.headers['request-duration'] = end - start;
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
return instance;
|
|
}
|
|
|
|
module.exports = {
|
|
makeAxiosInstance
|
|
};
|