⭐️ bestfetch

Getting Started

bestfetch is a lightweight HTTP request library that improves your application's performance by minimizing the number of requests that go over the network.

Installation

Install using npm:

npm install bestfetch

or yarn:

yarn add bestfetch

Prerequisites

A global fetch method is required to use bestfetch. If you need to run bestfetch in a browser without fetch, then we recommend using GitHub's fetch polyfill.

Basic Usage

Get started by importing the bestfetch function. The following example demonstrates importing it using the ES2015 module syntax.

import { bestfetch } from 'bestfetch';

Use this function to make requests. In the following example, a request is made to fetch a todo item from an API.

💁‍♀️ Heads up! You can copy and paste the following code snippet into your browser's developer tools to try it out!
bestfetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(res => {
    console.log('Got some data', res.data);
  });

If you run that code snippet several times you'll see that a single network request is made; subsequent requests hit the cache and resolve immediately.

If you're familiar with fetch, you may have noticed that we're not calling .json()on the response. Typical fetch usage would look like the following:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(res => res.json())
  .then(data => {
    console.log('Got some data', data);
  });

When you call .json() you're reading the response body from the server and parsing it as JSON. When you use bestfetch, the request body is read and parsed as JSON automatically for you (although this can be configured).

Next Steps

Now that you've seen the basic usage of bestfetch, read the Making Requests guide to learn more.