I want to fire an event when there is a new network request. I have overwritten the fetch
by following code,
window.fetch = new Proxy(window.fetch, {
apply(actualFetch, that, args) {
const result = Reflect.apply(actualFetch, that, args);
result.then(() => {
if (args.some(i => {
return (
typeof i === 'string' &&
(i?.includes('/example')
)
})) {
console.log('hello')
}
});
return result;
}
});
It works only when I make a new request using fetch
but when I make an axios
request from different script file (compiled react) it doesn’t fire any event. Any idea?
2
Answers
Axios uses the
XMLHttpRequest
under the hood, instead of thefetch
Axios uses the XMLHttpRequest api.
You could still make it work by using interceptors in the axios library, here’s an example :