⭐️ bestfetch

bestfetch

Initiates an HTTP request.

import { bestfetch } from 'bestfetch';

Arguments

  1. url. The URL of the request.
  2. [options]. An object that allows you to configure the request. No options are required. Supported options are:
    • method: The HTTP method of the request.
    • body: The body of the request; typically a string.
    • headers: An object of HTTP headers to include in the request.
    • dedupe: Whether or not to deduplicate the request. Defaults to true. Learn more here
    • cachePolicy: Determines how the cache will be used, if at all. Defaults to "cache-first". Valid values are "cache-first", "network-only", and "cache-only". Learn more here.
    • responseType: The responseType option of the request. Defaults to "json". Valid values are "json", "text", "formData", "blob", and "arrayBuffer". Learn more here.
    • requestKey: Optional. Learn more here.

    Additional, less commonly used options come from fetch:

    • mode
    • credentials
    • cache (note: this is unrelated to the caching system of bestfetch)
    • redirect
    • referrer
    • referrerPolicy
    • integrity
    • keepalive
    • signal

To learn more about the options that come from fetch, refer to the MDN documentation.

Returns

A Promise that resolves once the response is received from the server or cache. It rejects otherwise.

Example Usage

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

More examples, as well as guidance on usage, can be found in the Making Requests guide.