All Projects → Swizec → Better Fetch

Swizec / Better Fetch

Licence: mit
A tiny ES6 fetch() wrapper that makes your life easier without drastic changes to the API.

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects

Labels

Projects that are alternatives of or similar to Better Fetch

React Native App Boilerplate
A simple and scalable boiler plate code for React Native App using React Native Navigation by WiX and Saga .
Stars: ✭ 9 (-88.31%)
Mutual labels:  fetch
Frisbee
🐕 Modern fetch-based alternative to axios/superagent/request. Great for React Native.
Stars: ✭ 1,038 (+1248.05%)
Mutual labels:  fetch
Redux Api Call
One declarative API to create reducers, action creators and selectors for any API calls
Stars: ✭ 63 (-18.18%)
Mutual labels:  fetch
React Native Background Task
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
Stars: ✭ 873 (+1033.77%)
Mutual labels:  fetch
Create React Redux App
This project was bootstrapped with Create React App and Redux, Sass Structure.
Stars: ✭ 46 (-40.26%)
Mutual labels:  fetch
Redux Query
A library for managing network state in Redux
Stars: ✭ 1,055 (+1270.13%)
Mutual labels:  fetch
Smalldots
Stars: ✭ 905 (+1075.32%)
Mutual labels:  fetch
Freshfetch
A fresh take on neofetch
Stars: ✭ 73 (-5.19%)
Mutual labels:  fetch
Macchina
A system information fetcher, with a focus on performance and minimalism.
Stars: ✭ 45 (-41.56%)
Mutual labels:  fetch
Dva Generator
dva code generator, including perfect-fetch
Stars: ✭ 59 (-23.38%)
Mutual labels:  fetch
Reactspa
combination of react teconology stack
Stars: ✭ 911 (+1083.12%)
Mutual labels:  fetch
Wretch Middlewares
Collection of middlewares for the Wretch library. 🎁
Stars: ✭ 42 (-45.45%)
Mutual labels:  fetch
Cross Fetch
Universal WHATWG Fetch API for Node, Browsers and React Native.
Stars: ✭ 1,063 (+1280.52%)
Mutual labels:  fetch
Xhr.js
🌎 xhr.js is a library(< 2Kb) to make AJAX/HTTP requests with XMLHttpRequest.
Stars: ✭ 12 (-84.42%)
Mutual labels:  fetch
Npmf
Fetch quick info of a npm pacakge using terminal
Stars: ✭ 64 (-16.88%)
Mutual labels:  fetch
Composable Fetch
A library that brings composition to http requests & solves most common tasks
Stars: ✭ 23 (-70.13%)
Mutual labels:  fetch
React Async Fetcher
React component for asynchronous loading/fetch online data
Stars: ✭ 50 (-35.06%)
Mutual labels:  fetch
Github Files Fetcher
Download a specific folder or file from a GitHub repo through command line
Stars: ✭ 73 (-5.19%)
Mutual labels:  fetch
Vue Fetch Data
A simple and declarative way to fetch data for Vue components.
Stars: ✭ 65 (-15.58%)
Mutual labels:  fetch
React M
react-mobile
Stars: ✭ 56 (-27.27%)
Mutual labels:  fetch

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 🤓

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].