fetch-utils
A collection of utilities for working with the Fetch API.
Installation
npm install @ultraq/fetch-utils
API
checkStatus(reponse)
Make sure the response from the server is an OK one, throwing an error if it isn't. Usually chained right after the fetch request, eg:
fetch(/* ... */)
.then(checkStatus)
.then(response => {
// ...
});
responseAsJson(response)
Assumes a JSON response and attempts to parse and return it as a JavaScript object. Usually chained right after the response is deemed OK, eg:
fetch(/* ... */)
.then(checkStatus)
.then(responseAsJson)
.then(object => {
// ...
});
responseAsText(response)
Process the response body as a string. Usually chained right after the response is deemed OK, eg:
fetch(/* ... */)
.then(checkStatus)
.then(responseAsText)
.then(string => {
// ...
});