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

swr-resource

v1.0.2

Published

A zero-boilerplate, modern CRUD resource wrapper around SWR for fast React data fetching.

Readme

⚡ swr-resource

A zero-boilerplate, modern resource wrapper around SWR for lightning-fast React data fetching and CRUD operations.

Built for enterprise-scale applications, swr-resource eliminates repetitive API calls, loading state management, and manual cache updates. It keeps your UI synchronized automatically.


✨ Features

  • 🚀 Zero Boilerplate – Perform complete CRUD operations with a single hook.
  • 🔄 Automatic Cache Sync – UI updates automatically after create, update, and remove operations.
  • 🔐 Global Configuration – Configure your API base URL and authentication token once.
  • 📦 Lightweight – Built on top of the powerful swr library.
  • 💙 TypeScript Ready – Fully typed for an excellent developer experience.
  • Optimized Performance – Leverages SWR's caching and revalidation features.

📦 Installation

Install swr-resource along with its peer dependency, SWR.

npm install swr-resource swr

or

yarn add swr-resource swr

or

pnpm add swr-resource swr

🛠️ Setup

Wrap your application with the ApiProvider.

This allows you to configure a global API base URL and automatically inject authentication tokens into every request.

// App.tsx
import React from "react";
import { ApiProvider } from "swr-resource";
import Dashboard from "./Dashboard";

export default function App() {
  return (
    <ApiProvider
      baseURL="https://api.yourdomain.com/v1"
      getAuthToken={() => localStorage.getItem("access_token")}
    >
      <Dashboard />
    </ApiProvider>
  );
}

💻 Usage

Once the provider is configured, use the useResource hook anywhere in your application.

import React, { useState } from "react";
import { useResource } from "swr-resource";

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

export default function UserManagement() {
  const {
    data: users,
    isLoading,
    error,
    create,
    remove,
  } = useResource<User[]>("/users");

  const [newName, setNewName] = useState("");

  if (isLoading) return <p>Loading users...</p>;

  if (error) return <p>Failed to load users.</p>;

  const handleAddUser = async () => {
    await create({
      name: newName,
      email: "[email protected]",
    });

    setNewName("");
  };

  return (
    <div>
      <h2>User Management</h2>

      <div>
        <input
          value={newName}
          onChange={(e) => setNewName(e.target.value)}
          placeholder="Enter user name"
        />

        <button onClick={handleAddUser}>
          Add User
        </button>
      </div>

      <ul>
        {users?.map((user) => (
          <li key={user.id}>
            {user.name} ({user.email})

            <button
              onClick={() => remove(user.id)}
            >
              Delete
            </button>
          </li>
        ))}
      </ul>
    </div>
  );
}

📚 API Reference

<ApiProvider />

Props

| Prop | Type | Description | |------|------|-------------| | baseURL | string | Base URL of your API (e.g. https://api.example.com) | | getAuthToken | () => string \| null | Function that returns the current Bearer token. It is evaluated before every request. |


useResource(endpoint)

Returns the following object:

| Property | Type | Description | |----------|------|-------------| | data | T | Fetched resource data | | isLoading | boolean | true while the initial request is loading | | isValidating | boolean | true whenever SWR is revalidating | | error | Error | Error object if the request fails | | create(payload) | Promise<any> | Sends a POST request and automatically revalidates the cache | | update(id, payload) | Promise<any> | Sends a PUT request to endpoint/:id and revalidates | | remove(id) | Promise<any> | Sends a DELETE request to endpoint/:id and revalidates | | refresh() | () => void | Manually re-fetches the current resource |


Example

const {
  data,
  create,
  update,
  remove,
  refresh,
} = useResource("/products");

Why swr-resource?

Instead of writing this:

const { data, mutate } = useSWR("/users");

await axios.post("/users", payload);

mutate();

Simply write:

const { data, create } = useResource("/users");

await create(payload);

No manual cache updates.

No repetitive Axios calls.

Just clean, declarative CRUD.


Requirements

  • React 18+
  • SWR 2+
  • TypeScript (optional but recommended)

License

MIT