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

naction

v0.1.0

Published

build next actions easily

Readme

naction is a lightweight library for managing server actions and schema-based validation in Next.js projects. It simplifies handling async operations and schema validation with zod, providing a clean and intuitive API.

Features

  • Schema-based validation: Automatically validate input values using Zod schemas.
  • Error handling: Structured and detailed error messages for validation and runtime errors.
  • Type safety: Ensures type-safe parameters and return values.
  • Next.js optimized: Built with Next.js in mind, designed to work seamlessly with its server functions.

Installation

Install naction using npm or yarn:

npm install naction zod

or

yarn add naction zod

Usage

schema

The schema utility allows you to define an action handler with schema validation. The input values are validated against the provided Zod schema before executing the action function.

Example

import { z } from "zod";
import naction from "naction";

// Define a Zod schema
const userSchema = z.object({
  name: z.string(),
  age: z.number().min(18, "Age must be at least 18"),
});

// Create a schema-based action
const createUser = naction.schema(userSchema).action(async (values) => {
  // Business logic here
  return `User ${values.name} created successfully!`;
});

// Usage
(async () => {
  const result = await createUser({ name: "John", age: 25 });

  if (result.success) {
    console.log(result.data); // Output: "User John created successfully!"
  } else {
    console.error(result.error); // Validation or runtime error message
  }
})();

action

The action utility is used for simple action handlers that do not require schema validation.

Example

import naction from "naction";

// Create an action
const fetchData = naction.action(async () => {
  // Simulate a data fetch
  return { id: 1, name: "Sample Data" };
});

// Usage
(async () => {
  const result = await fetchData();

  if (result.success) {
    console.log(result.data); // Output: { id: 1, name: "Sample Data" }
  } else {
    console.error(result.error); // Runtime error message
  }
})();

API Reference

schema

schema<Schema extends z.ZodSchema>(schema: Schema)

Creates a schema object for defining an action handler with schema validation.

Parameters:

  • schema (ZodSchema): The Zod schema to validate the input values.

Returns: An object with the action method.

action<Fn extends (values: z.infer<typeof schema>) => Promise<any>>(fn: Fn)

Defines an action handler that validates input values against the provided schema and executes the given function.

Parameters:

  • fn (Function): An asynchronous function that processes the validated input values.

Returns: An asynchronous function that accepts input values, validates them, and executes the provided function. It returns a result of type ActionResult.

action

action<Fn extends () => Promise<any>>(fn: Fn)

Creates an action handler without schema validation.

Parameters:

  • fn (Function): An asynchronous function to execute.

Returns: An asynchronous function that executes the provided function. It returns a result of type ActionResult.

Types

ActionResult<Result>

A union type that represents the result of an action.

type ActionResult<Result> =
  | { success: true; data: Result }
  | { success: false; error: string };

Error Handling

  • Validation errors: If the input values do not match the schema, naction returns a detailed error message specifying the invalid fields and their issues.
  • Runtime errors: Any error thrown during the execution of the action function is caught and returned with an error message.