A thin wrapper around fetch to take care of some common use cases in the browser.
NB This does not handle polyfilling fetch. If you are using this in a browser that does not implement HTML5 Fetch API
then you will need to polyfill with something like
whatwg-fetch or it will not work.
npm i fetch-nice
The examples below are written using ES7 async/await for clarity. Of course you can still use fetch-nice without async/await since it returns a promise as per HTML5 fetch.
fetchNice assumes that your request is for JSON and sets headers accordingly. It also defaults to using fetch's
credentials: 'include' setting so that any cookies are sent in order to make authenticated API requests.
The response object is JSON parsed automatically. If the response was a 204 (no content), fetch-nice returns
fetch-nice returns a rejected promise for error responses. If the error response contained a JSON body, this is added to the rejected Error.
boom for it's errors.
;// make a GET requestconst result = await ;// make a POST requestconst body = name: 'Onion McOnionFace' ;const result = await fetchNice;// PUT and DELETE follow the same pattern.