mirror of
https://github.com/usebruno/bruno.git
synced 2025-06-21 04:08:01 +02:00
fix(network): use interceptors when measuring request duration
This commit is contained in:
parent
e1bf475f1f
commit
6ef7891f8a
@ -1,10 +1,8 @@
|
|||||||
export const sendNetworkRequest = async (item, collection, environment, collectionVariables) => {
|
export const sendNetworkRequest = async (item, collection, environment, collectionVariables) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (['http-request', 'graphql-request'].includes(item.type)) {
|
if (['http-request', 'graphql-request'].includes(item.type)) {
|
||||||
const timeStart = Date.now();
|
|
||||||
sendHttpRequest(item, collection, environment, collectionVariables)
|
sendHttpRequest(item, collection, environment, collectionVariables)
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const timeEnd = Date.now();
|
|
||||||
resolve({
|
resolve({
|
||||||
state: 'success',
|
state: 'success',
|
||||||
data: response.data,
|
data: response.data,
|
||||||
@ -12,7 +10,7 @@ export const sendNetworkRequest = async (item, collection, environment, collecti
|
|||||||
size: response.headers['content-length'] || 0,
|
size: response.headers['content-length'] || 0,
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
duration: timeEnd - timeStart
|
duration: response.duration
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((err) => reject(err));
|
.catch((err) => reject(err));
|
||||||
|
38
packages/bruno-electron/src/ipc/network/axios-instance.js
Normal file
38
packages/bruno-electron/src/ipc/network/axios-instance.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
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) => {
|
||||||
|
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
|
||||||
|
};
|
@ -15,6 +15,7 @@ const { sortFolder, getAllRequestsInFolderRecursively } = require('./helper');
|
|||||||
const { getPreferences } = require('../../store/preferences');
|
const { getPreferences } = require('../../store/preferences');
|
||||||
const { getProcessEnvVars } = require('../../store/process-env');
|
const { getProcessEnvVars } = require('../../store/process-env');
|
||||||
const { getBrunoConfig } = require('../../store/bruno-config');
|
const { getBrunoConfig } = require('../../store/bruno-config');
|
||||||
|
const { makeAxiosInstance } = require('./axios-instance');
|
||||||
|
|
||||||
// override the default escape function to prevent escaping
|
// override the default escape function to prevent escaping
|
||||||
Mustache.escape = function (value) {
|
Mustache.escape = function (value) {
|
||||||
@ -240,7 +241,10 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios(request);
|
const axiosInstance = makeAxiosInstance();
|
||||||
|
|
||||||
|
/** @type {import('axios').AxiosResponse} */
|
||||||
|
const response = await axiosInstance(request);
|
||||||
|
|
||||||
// run post-response vars
|
// run post-response vars
|
||||||
const postResponseVars = get(request, 'vars.res', []);
|
const postResponseVars = get(request, 'vars.res', []);
|
||||||
@ -343,12 +347,16 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deleteCancelToken(cancelTokenUid);
|
deleteCancelToken(cancelTokenUid);
|
||||||
|
// Prevents the duration on leaking to the actual result
|
||||||
|
const requestDuration = response.headers.get('request-duration');
|
||||||
|
response.headers.delete('request-duration');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: response.status,
|
status: response.status,
|
||||||
statusText: response.statusText,
|
statusText: response.statusText,
|
||||||
headers: response.headers,
|
headers: response.headers,
|
||||||
data: response.data
|
data: response.data,
|
||||||
|
duration: requestDuration
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// todo: better error handling
|
// todo: better error handling
|
||||||
@ -416,11 +424,15 @@ const registerNetworkIpc = (mainWindow) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevents the duration from leaking to the actual result
|
||||||
|
const requestDuration = error.response.headers.get('request-duration');
|
||||||
|
error.response.headers.delete('request-duration');
|
||||||
return {
|
return {
|
||||||
status: error.response.status,
|
status: error.response.status,
|
||||||
statusText: error.response.statusText,
|
statusText: error.response.statusText,
|
||||||
headers: error.response.headers,
|
headers: error.response.headers,
|
||||||
data: error.response.data
|
data: error.response.data,
|
||||||
|
duration: requestDuration ?? 0
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user