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

@tahanabavi/typeaction

v1.0.0

Published

A lightweight React hook system for Next.js server actions with caching, retries, optimistic updates, and type safety.

Downloads

4

Readme

@tahanabavi/typeaction

npm version npm version

A lightweight React utility for type-safe server actions. It simplifies working with async functions by providing useTransition-powered loading state, cache support, and type inference — all designed for modern React apps with Server Components and server actions.


Table of contents


Why use this

If you use Next.js Server Actions or any async functions in React, you often need boilerplate for:

  • Loading state
  • Caching results
  • Reusing results across components

This package provides a minimal but extensible hook system for calling actions with isPending, mutate, and cache support.


Features

  • ✅ Type-safe request/response with TypeScript inference
  • ✅ Automatic loading state via useTransition
  • ✅ Simple cache API (invalidate, prefetch, getCache)
  • ✅ Works with Next.js Server Actions ("use server")
  • ✅ Minimal, composable design — extend as needed

Install

npm install @tahanabavi/typeaction
# or
yarn add @tahanabavi/typeaction

Quick start

  • Server action
"use server";
export type RequestData = { id: number };
export type ResponseData = { message: string };

export async function fetchMessage(input: RequestData): Promise<ResponseData> {
  await new Promise((resolve) => setTimeout(resolve, 500)); // simulate server delay

  if (input.id < 0) throw new Error("Invalid ID");

  return { message: `Hello ${input.id}` };
}
  • Hook
"use client";

import { createAction } from "@tahanabavi/typeaction";
import { fetchMessage, RequestData, ResponseData } from "./action";

// Wrap the server action with createAction
export const useFetchMessage = createAction<RequestData, ResponseData>(fetchMessage);
  • Usage
"use client";

import { useFetchMessage } from "./hook";

export default function App() {
  const { mutate, data, error, isPending, reset, abort } = useFetchMessage({
    key: "message",
    optimisticUpdate: (prev: any, input: any) => ({
      message: `Optimistic ${input.id}`,
    }),
  });

  return (
    <div>
      <button onClick={() => mutate({ id: 1 })}>Fetch id=1</button>
      <button onClick={() => mutate({ id: -1 })}>Fetch Invalid</button>
      <button onClick={abort}>Abort</button>
      <button onClick={reset}>Reset</button>

      <div>Status: {isPending ? "Loading..." : "Idle"}</div>
      <div>Data: {data?.message ?? "No data"}</div>
      <div>Error: {error ? (error as Error).message : "No error"}</div>
    </div>
  );
}

API reference

createAction(fn, options)

Wraps an async function or server action.

| Parameter | Type | Description | | ------------- | ------------------------- | ------------------------------- | | fn | (data: T) => Promise<R> | Async function or server action | | options.key | string (optional) | Unique key for caching results |

Returned API

const action = createAction(fn) returns an object with:

| Method | Description | | -------------- | -------------------------------------------------------- | | useAction() | Hook that exposes isPending, mutate, data, error | | invalidate() | Clears cached data for this action | | prefetch(d) | Runs action in background and caches it |

Inside useAction()

| Property | Type | Description | | ----------- | ------------ | ------------------------------- | | isPending | boolean | True while action is running | | mutate(d) | Promise<R> | Runs the action with input d | | data | R \| null | Last successful result (cached) | | error | any | Last error if call failed |


Testing / Development

Run unit tests with Jest:

npm test

Build TypeScript:

npm run build

Local develop/test with another app:

npm link

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feat/my-feature
  3. Run tests and linters locally
  4. Open a PR with a clear description and changelog entry (if applicable)

Please include tests for new features and follow the established code style.


Maintainers & Support

Maintained by @tahanabavi.

For issues, please open a GitHub issue in this repository. For questions or suggestions, create an issue or reach out on GitHub Discussions.


Thank you for using @tahanabavi/typeaction — feedback and contributions are highly appreciated!