Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

fetchres

1.7.2 • Public • Published

fetchres Build Status

Some useful response handlers for using Fetch API in the real world.

Warning: doesn't come with Fetch or Promise, so you'll need to bring your own polyfill (unless you're using this module within Service Workers, where both fetch and Promise are already defined).

Installation

npm install --save fetchres

Or

bower install --save fetchres

Usage (node)

var fetchres = require('fetchres');
 
fetch('https://mattandre.ws/my-json-endpoint')
    .then(fetchres.json)
    .then(function(data) {
        // `data` is an object containing the data
        // from the response, not a Response object
        console.log(data);
    })
    .catch(function(err) {
        if (err.name === fetchres.BadServerResponseError.name) {
            // just a dodgy response
            console.error(err);
        } else if (err.name === fetchres.InvalidJsonError.name) {
            // malformed json, probably
            console.error('malformed json returned');
        } else {
            // something more serious could be wrong.
            // probably want to investigate / send to
            // error aggregation service
            debugger;
        }
    });

Keywords

none

Install

npm i fetchres

DownloadsWeekly Downloads

1,803

Version

1.7.2

License

none

Last publish

Collaborators

  • avatar
  • avatar