@rodrigoehlers/fetch

1.1.0 • Public • Published

@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

Package Sidebar

Install

npm i @rodrigoehlers/fetch

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

4.5 kB

Total Files

6

Last publish

Collaborators

  • rodrigoehlers