Skip to content

Instantly share code, notes, and snippets.

@jcgertig
Last active May 3, 2016 21:05
Show Gist options
  • Save jcgertig/ce2044a84a9064e23bead1192c74db9f to your computer and use it in GitHub Desktop.
Save jcgertig/ce2044a84a9064e23bead1192c74db9f to your computer and use it in GitHub Desktop.
Fetchum

Fetchum

Fetchum is a simplified api for making xhr requests. It is a wrapper around the native fetch api implemented in new browsers and has the most well know polyfill for fetch as the fallback for older browsers.

It also allows you to build a request function to call later describing how a call will be made using the generateRequest function. This allows for very descriptive usage of a api and increases readability of how requests are made overall.

Example:

const getUser = generateRequest({
  token: true,
  method: 'GET',
  route: '/v1/users/:id',
});

getUser({id: 1})
  .then((res) => { setUser(res.user); })
  .catch((res) => { setErrors(res.errors); });

By the example here we are able to describe how a api is used and even say that we have a token in localstorage that will be passed in an authorization header with that token for you. There are many other convenience options that are also available to make describing how a endpoint is used concise, easy and reuseable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment