extended-fetch
![Module type: CJS](https://img.shields.io/badge/module%20type-cjs-brightgreen)
Tiny window.fetch JavaScript implementation without over XMLHttpRequest with additional features
Can be used ds drop in replacement for fetch
Installation
npm install @akiyamka/extended-fetch
Usage
Works just like native fetch, but with few additional features:
Catch timeout error
Fetch does not allow the user to know if his request was failed due to a 504 error.
Instead it throws common TypeError: Failed to fetch
But extended-fetch
throw 'Timeout Error' error for that case
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:
Also you can hook XMLHttpRequest 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