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

fetch-reactive

v1.0.2

Published

Zero-dependency reactive fetch wrapper for JavaScript & TypeScript.

Readme

fetch-reactive

🧠 A zero-dependency reactive fetch wrapper for JavaScript & TypeScript.

Subscribe to loading, error, and data states in a clean, observable way — no frameworks, no fluff.


🚀 Features

  • ✅ Reactive subscription API (subscribe / unsubscribe)
  • 🔁 refetch() & abort() built-in
  • ⏱ Retry with delay on failure
  • 🔧 Easy transformResponse hook
  • 📦 TypeScript support with types included
  • 🪶 Lightweight & framework-agnostic

📦 Install

npm install fetch-reactive

🎯 Basic Example

import { createFetchStore } from "fetch-reactive";

// Create a reactive fetch store
const store = createFetchStore("https://jsonplaceholder.typicode.com/posts");

// Subscribe to state changes
const unsubscribe = store.subscribe(({ loading, results, error }) => {
  if (loading) {
    console.log("⏳ Loading...");
    return;
  }

  if (error) {
    console.error("❌ Error:", error.message);
    return;
  }

  console.log("✅ Data received:", results);
});

// Clean up when done
// unsubscribe();

🔧 Custom Options

const store = createFetchStore("https://api.example.com/data", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: "Bearer your-token",
  },
  body: JSON.stringify({ key: "value" }),
  retries: 3,
  retryDelay: 1000,
  transformResponse: (data) => data.items || data,
  onError: (error) => console.error("Custom error handler:", error),
});

🔄 Refetch Data

const store = createFetchStore("https://api.example.com/data");

// Manually trigger a new request
store.refetch();

// Example: Refetch on button click
button.addEventListener("click", () => {
  store.refetch();
});

🛑 Unsubscribe & Abort

const store = createFetchStore("https://api.example.com/data");

// Subscribe returns an unsubscribe function
const unsubscribe = store.subscribe((state) => {
  console.log(state);
});

// Stop listening to state changes
unsubscribe();

// Abort the ongoing request and cleanup
store.abort();

📋 API Reference

createFetchStore<T>(url: string, options?: FetchOptions): FetchStore<T>

Creates a reactive fetch store for the given URL.

FetchOptions

interface FetchOptions {
  method?: string; // HTTP method (default: 'GET')
  headers?: Record<string, string>; // Request headers
  body?: any; // Request body
  retries?: number; // Number of retry attempts (default: 0)
  retryDelay?: number; // Delay between retries in ms (default: 1000)
  transformResponse?: (data: any) => any; // Transform response data
  onError?: (err: Error) => void; // Custom error handler
}

FetchStore<T>

interface FetchStore<T> {
  subscribe(listener: (state: FetchState<T>) => void): () => void; // Subscribe to state changes
  refetch(): void; // Trigger a new fetch
  abort(): void; // Abort request and cleanup
  readonly state: FetchState<T>; // Current state snapshot
}

FetchState<T>

interface FetchState<T> {
  results: T; // Response data
  loading: boolean; // Loading state
  error: Error | null; // Error state
}

🎨 Advanced Examples

With TypeScript Types

interface User {
  id: number;
  name: string;
  email: string;
}

const userStore = createFetchStore<User[]>("https://api.example.com/users", {
  transformResponse: (data) => data.users,
});

userStore.subscribe(({ loading, results, error }) => {
  if (!loading && results) {
    // results is typed as User[]
    results.forEach((user) => console.log(user.name));
  }
});

Error Handling & Retries

const store = createFetchStore("https://unreliable-api.com/data", {
  retries: 3,
  retryDelay: 2000,
  onError: (error) => {
    // Custom error logging
    console.error("Fetch failed:", error.message);
    // Could send to error tracking service
  },
});

React Integration

import { useEffect, useState } from "react";
import { createFetchStore } from "fetch-reactive";

function useReactiveFetch<T>(url: string, options?: FetchOptions) {
  const [state, setState] = useState<FetchState<T>>({
    results: null,
    loading: true,
    error: null,
  });

  useEffect(() => {
    const store = createFetchStore<T>(url, options);
    const unsubscribe = store.subscribe(setState);

    return () => {
      unsubscribe();
      store.abort();
    };
  }, [url]);

  return state;
}

📝 License

MIT