fetch-response-handler

1.0.18 • Public • Published

Total alerts Language grade: JavaScript

Fetch Response Handler

Fetch Response Handler is wrapper for the javascript Fetch API which provides expressive and idiomatic handling of network errors, client errors HTTP status = 4**, server errors HTTP status = 5** and successful responses as either the native Response or as a JSON object for responses with Content-Type = application/json. It also provides support for default error handling and cancelling requests using AbortControllers.

It exists to improve the expressiveness and idiomacity of production web applications using the Fetch API.

Getting Started

Install

npm install fetch-response-handler

Import

// Import the fetchResponseHandler function from the fetch-response-handler package in a module
import {fetchResponseHandler} from "fetch-response-handler";

// OR in a script
var fetchResponseHandler = require("./src/fetch-response-handler.js").fetchResponseHandler;

Usage

// Fetch JSON payload from URL
fetchResponseHandler(<URL>)
  .onSuccessJson(json => {<Do something with the received JSON>})
  .fetch();

Background

Cancelling Requests

Simple React Example

Maximise Code Reuse and Idiomacity

Default Response Handlers

Fetch Response Handler API

Contributing

If you'd like to contribute to this project feel free to fork the repository and use a feature branch. Pull requests are warmly welcome

Licensing

The code in this project is under the MIT licence

Package Sidebar

Install

npm i fetch-response-handler

Weekly Downloads

25

Version

1.0.18

License

MIT

Unpacked Size

68.4 kB

Total Files

14

Last publish

Collaborators

  • k2_tools