extended-fetch
A window.fetch JavaScript implementation with additional features
Usage
Works just like whatwg fetch but you can
Catch timeout error
import { fetch, isTimeoutError } from 'extended-fetch';
fetch('/users', {
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
}).catch((error) => {
console.assert(error.message, 'Timeout Error');
console.assert(isTimeoutError(error), true);
});
Subscribe to xhr events:
fetch(
'/users',
{
method: 'POST',
body: JSON.stringify({ foo: 'bar' }),
},
{
eventListener: (event) => {
if (event.type === 'progress') {
console.log(`Progress changed to ${event.payload}`);
}
},
}
).catch((error) => {
console.assert(error.message, 'Timeout Error');
console.assert(isTimeoutError(error), true);
});
Credits
Inspired by https://github.com/JakeChampion/fetch