npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ehynds/use-fetch

v1.0.3

Published

A small, low-level React hook that wraps the Fetch API

Downloads

13

Readme

use-fetch

build status

A lightweight, type-safe wrapper around the fetch API in the form of a React hook.

Install

$ npm i --save @ehynds/use-fetch

# or

$ yarn add @ehynds/use-fetch

Usage

Perform a GET request:

const { get } = useFetch();

// Optional query params
const params = {
  limit: 1
};

// Optional options to pass to the `fetch` request
const options = {
  headers: {
    'Authorization': 'Access-Token foo'
  }
};

const { json } = await get('/api', params, options);

Each verb method returns an object with an abort function and the request:

const { abort, req } = get('/api');

req.then(({ json, res }) => {
  // `json` is the parsed response body.
  // This lib assumes that you're interfacing with a
  // JSON-based API. If that's not the case, use `res`
  // to handle your response:
  // - res.text()
  // - res.blob()
  console.log('Response body:', json);

  // `res` is the raw fetch response.
  // See the response docs for the full API:
  // https://developer.mozilla.org/en-US/docs/Web/API/Response
  console.log('Response status:', res.status);
  console.log('Response headers', res.headers);
});

// abort the request
abort();

The return value of an HTTP verb method is not chainable for async/await compatibility. If you don't care about aborting and want to chain off the promise, you can write:

get('/api').req.then(({ json }) => {
  console.log('Result:', json);
});

// or async/await, which is _not_ cancellable:

const { json } = await get('/api');
console.log('Result:', json);

useFetch exports an object with the following properties:

const {
  get,
  post,
  put,
  del,
  head,
  request // Escape hatch for direct access to `fetch`
} = useFetch();

Examples

Basic GET request when a component mounts:

import { useFetch } from '@ehynds/use-fetch';

const SomeComponent = () => {
  const { get } = useFetch();

  useEffect() => {
    const params = {
      limit: 2
    };

    get('/api', params)
      .req
      .then(({ json }) => console.log('Result:', json))
      .catch((err) => console.error('Error:', err));
  }, []);
};

Cancel a request on cleanup:

const SomeComponent = () => {
  const { get } = useFetch();

  useEffect(() => {
    const { abort, req } = get('/api');

    req.then(({ json }) => {
      console.log('Result:', json);
    });

    return () => {
      abort();
    };
  }, []);
};

Request a blob:

const SomeComponent = () => {
  const { get } = useFetch();

  useEffect(() => {
    get('image.png')
      .req
      .then(({ res }) => res.blob())
      .then((imageBlob) => {
        const imageUrl = URL.createObjectURL(imageBlob);
        someImage.src = imageUrl;
      });
  }, []);
};

Type your response JSON:

import { useFetch } from '@ehynds/use-fetch';

type Book = {
  title: string;
  author: string;
};

const BookList = () => {
  const [ books, setBooks ] = useState<Book[]>([]);
  const { get } = useFetch();

  useEffect(() => {
    const params = {
      limit: 1
    };
    get<Book[]>('/books/list', params)
      .req
      .then(({ json: books }) => setBooks(books));
  }, []);

  ...
}

Manage loading & error states with the useFetchState helper:

import { useFetch, useFetchState } from '@ehynds/use-fetch';

type Book = {
  title: string;
  author: string;
};

const useBooks = () => {
  const { loading, data, error, setLoading, setData, setError } = useFetchState<Book[]>();
  const { get } = useFetch();

  useEffect(() => {
    setLoading(true);

    const { abort, req } = get<Book[]>('https://my-api.com/books/list', {
      limit: 2
    });

    req
      .then(({ json: books }) => setData(books))
      .catch(err => setError(err));

    return () => {
      abort();
    };
  }, []);

  return {
    loading,
    data,
    error
  };
}

const BookList = () => {
  const { loading, data, error } = useBooks();

  if (loading) {
    return <div>Loading...</div>
  }

  if (error) {
    return <div>An error occurred: {error.message}</div>;
  }

  return (
    <>
      <h1>My books:</h1>

      <ul>
        {data.map((book) => (
          <li key={book.title}>
            {book.title} by {book.author}
          </li>
        ))}
      </ul>
    </>
  );
};