fetch-response-handler
v1.0.18
Published
A function that wraps up the fetch API with opinionated handling of the Response Promise which effectively removes all boiler plate from making REST requests to back-end APIs
Downloads
11
Maintainers
Readme
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