A tiny ES6 fetch() wrapper that makes your life easier without drastic changes to the API.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Actually add response to error Apr 2, 2017
src Actually add response to error Apr 2, 2017
.gitignore Initial commit Nov 2, 2016
LICENSE Initial commit Nov 2, 2016
README.md Update README Nov 2, 2016
package.json 1.1.2 Apr 2, 2017

README.md

better-fetch

A tiny ES6 fetch() wrapper that makes your life easier.

Without changing the API, better-fetch automatically includes cookies, which would have saved me a very frustrating amount of time yesterday, lets you add default headers, and you can pass request body as a plain JS object, none of that FormData nonsense.

better-fetch works the same as fetch(), but is less cumbersome to use.

In practice better-fetch looks like this:

You install with npm. Or whatever you use to install packages from npmjs.org. Yarn maybe?

$ npm install --save better-fetch

Then you set up headers that every one of your fetch() calls needs. My backend requires an Authorization, and an Accept header from all calls.

// top of project
// src/index.js

import fetch from 'better-fetch';

fetch.setDefaultHeaders({
    Authorization: `Token token=${GlobalTokenValue}`,
    Accept: "application/json.v2"
});

// ^ this is optional and depends on your use-case ^

You can then use better-fetch anywhere in your code as you normally would with fetch(). The API feels the same and promises work just like you'd expect.

// any file
import fetch from 'better-fetch';

fetch('/api/some/thing')
  .then(response => response.json())
	.then(json => {
		// do stuff
	});

This code fetches JSON document with a GET request to the /api/some/thing URL. Any default headers are set in the request and cookies are sent as well.

POST-ing is also made less cumbersome:

// any file
import fetch from 'better-fetch';

const data = {
	key: 'value',
	key2: 'value2'
};

fetch('/api/save_response', {method: 'POST',
                             body: data})
          .then(response => response.json())
          .then(json => {
              console.log(json);
          });

A dictionary body is automatically transformed into a FormData object, strings and FormData objects are let through. This gives you flexibility to work with any API backend, but still makes your life easier.

Similarly, you can specify headers as either a Headers object or a dictionary - better-fetch has you covered.

Happy hacking 🤓