1.1.0 • Published 6 years ago
@rodrigoehlers/fetch v1.1.0
@rodrigoehlers/fetch
Just an extension to fetch which makes it reject on HTTP error codes and throw an appropiate error.
Installation
Install with npm i @rodrigoehlers/fetch.
Usage
Simply import / require the module in your code and use it as you'd use fetch with the simple difference that the returned Promise will reject if the response contains an HTTP status in the range of 4XX - 5XX.
import fetch from '@rodrigoehlers/fetch';
fetch('https://httpstat.us/500')
.then(() => console.log('successful'))
.catch(error => console.log(error));You'll see this in the console:
{
code: 500,
status: 'Bad Request',
body: ... // Optional response body
}Compatibility
This module depends on fetch and therefore won't natively work in nodejs.
You can check browser support here (MDN) or here (caniuse.com).
License
MIT - Made by Rodrigo Ehlers