I'm trying to monkey patch window.fetch inside a class to be able to fetch request and response body.
I currently have the following implementation:
export class NetworkService implements Listener {
originalFetch: typeof window.fetch
constructor() {
this.originalFetch = window.fetch
}
async listenFetch(): Promise<void> {
if (typeof window.fetch !== 'function') return
window.fetch = async (input: RequestInfo, init?: RequestInit) => {
const response = await this.originalFetch(input, init);
try {
response
.clone()
.text()
.then(body => console.log(body))
.catch(err => console.error(err));
} catch (e) {
console.log('Error reading response: ' + e)
}
return response;
};
}
registerListeners(): void {
this.listenFetch()
}
unregisterListeners(): void {
if (typeof fetch !== 'function') return
window.fetch = this.originalFetch
}
}
But whenever I make a simple fetch request from my page I get Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
.
The following question Fetch TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation and tried
constructor(
fetch: FetchFunction = (...args) => window.fetch(...args)
) {
this.originalFetch = fetch;
}
But this leads to a never ending recursion.
How can I monkey patch window.fetch
in a class? More importantly, why am I getting Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
?
Edit
Thank you for the replies. I have transformed my arrow function into an a regular one:
async listenFetch(): Promise<void> {
if (typeof fetch !== 'function') return
const self = this
window.fetch = async function (input: RequestInfo, init?: RequestInit) {
const response = await self.originalFetch(input, init);
try {
response
.clone()
.text()
.then(body => console.log(body))
.catch(err => console.error(err));
} catch (e) {
console.log('Error reading response: ' + e)
}
return response;
};
}
I'm still getting Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation