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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-fetch-client

v1.0.2

Published

Lightweight React hooks for data fetching with native fetch - simple factory-based API, TypeScript support, caching, and request cancellation

Readme

react-fetch-client

Lightweight React data fetching built on the native fetch API, with hooks, cache support, retry helpers.

Demo

Visit Demo link for working example of react-fetch-client package in action.

Install

npm install react-fetch-client

Quick start

import { createFetchClient, createFetchHooks } from 'react-fetch-client';

const client = createFetchClient({ baseURL: 'https://api.example.com' });
const { useGet, usePost } = createFetchHooks(client);

Features, examples, and usage

1) Factory HTTP client (createFetchClient)

Example

import { createFetchClient } from 'react-fetch-client';

type User = { id: string; name: string };

const client = createFetchClient({
  baseURL: 'https://api.example.com',
  timeout: 30_000,
  headers: { 'Content-Type': 'application/json' },
});

const users = await client.get<User[]>('/users');
const created = await client.post<User>('/users', { name: 'Ada' });

Usage

  • Use client.get/post/put/patch/delete for typed requests.
  • Add onRequest, onResponse, onError in client config for global intercept logic.
  • Use client.instance.request({...}) for fully custom request config.

2) React hooks (createFetchHooks)

Example

import { createFetchClient, createFetchHooks } from 'react-fetch-client';

type User = { id: string; name: string };
type CreateUserInput = { name: string };

const client = createFetchClient({ baseURL: '/api' });
const { useGet, usePost } = createFetchHooks(client);

export function Users() {
  const { data, loading, error, refetch, responseTimeInMillis } = useGet<User[]>('/users');
  const { mutate: createUser, loading: creating } = usePost<User, CreateUserInput>('/users');

  if (loading) return <p>Loading...</p>;
  if (error) return <p>{error.message}</p>;

  return (
    <div>
      <button disabled={creating} onClick={() => void createUser({ name: 'Grace' })}>
        Add user
      </button>
      <button onClick={() => void refetch()}>Refresh</button>
      <p>Last response: {responseTimeInMillis ?? 0}ms</p>
      <ul>{data?.map((u) => <li key={u.id}>{u.name}</li>)}</ul>
    </div>
  );
}

Usage

  • useGet supports immediate, deps, params, retry, retryDelay, cachePolicy, and responseType.
  • Mutation hooks are usePost, usePatch, usePut, useDelete.
  • Use abort({ clear: false }) in useGet to cancel without clearing previous data.

3) Request cancellation (isCanceledError)

Example

import { createFetchClient, isCanceledError } from 'react-fetch-client';

const client = createFetchClient({ baseURL: '/api' });
const controller = new AbortController();

const req = client.get('/users', { signal: controller.signal });
controller.abort();

try {
  await req;
} catch (err) {
  if (isCanceledError(err)) {
    console.log('Request canceled safely');
  }
}

Usage

  • Pass signal in request config.
  • Use isCanceledError to ignore user-triggered aborts in UI error handling.

4) Cache policies + cache adapters

Example

import {
  createFetchClient,
  createFetchHooks,
  CachePolicy,
  createLocalStorageCache,
} from 'react-fetch-client';

const client = createFetchClient({
  baseURL: '/api',
  cache: createLocalStorageCache(),
  defaultCachePolicy: CachePolicy.NetworkFirst,
  defaultCacheTTL: 60_000,
  cachePolicies: {
    '/products/*': CachePolicy.CacheFirst,
  },
  cacheTTLs: {
    '/products/*': 5 * 60_000,
  },
});

const { useGet } = createFetchHooks(client);
// Uses cache policy + TTL from client config:
const productsQuery = useGet('/products', { params: { page: 1 } });

Usage

  • Available policies: NoCache, CacheFirst, NetworkFirst, NetworkOnly.
  • Adapters: memory (createMemoryCache), localStorage, sessionStorage, IndexedDB.
  • Override per hook with cachePolicy, cacheTTL, cacheKey, cacheAdapter.

API overview

| Export | Description | | -------------------------------- | ------------------------------------------------------------------- | | createFetchClient | Factory client with get/post/put/patch/delete | | createFetchHooks | React hooks: useGet, usePost, usePatch, usePut, useDelete | | CachePolicy | NoCache, CacheFirst, NetworkFirst, NetworkOnly | | FetchError / isCanceledError | Structured errors + cancel detection | | Cache adapters | memory, localStorage, sessionStorage, IndexedDB | | Utilities | cache, retry, dedupe |

Development

npm install
npm run typecheck
npm test
npm run build

License

MIT - see LICENSE.