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 🙏

© 2024 – Pkg Stats / Ryan Hefner

zod-server-actions

v0.5.4

Published

Simple utility library to create server actions in Next.js

Downloads

60

Readme

ActionHandler Library

The ActionHandler library provides a typesafe and robust way to manage and execute asynchronous actions with input validation, output validation, and retry logic. This library leverages zod for schema validation and includes functionality to create and handle actions in a structured manner.

Features

  • Input and Output Validation: Define schemas for input and output using zod for type-safe validation.
  • Retry Logic: Automatically retry actions on failure with configurable attempts and delays.
  • Context Management: Optionally provide context for actions using a factory function.
  • Flexible Action Handling: Create handlers for actions with specified input and output types.

Installation

To use the ActionHandler library, first install the required dependencies:

npm install zod

Usage

Creating an Action Handler

Use the createActionHandler function to initialize a new action handler with configuration options.

import { createActionHandler } from 'zod-server-actions';

Example

Here's a step-by-step guide to creating an action handler:

  1. Define Schemas

    Define the input and output schemas using zod.

    import { z } from 'zod';
    
    const InputSchema = z.object({
      name: z.string(),
      age: z.number().min(0),
    });
    
    const OutputSchema = z.object({
      greeting: z.string(),
    });
  2. Create Action Handler

    Use createActionHandler to initialize your action handler.

    const actionHandler = createActionHandler({
      maximumAttempts: 3,
      delay: 1000,
      contextFn: async () => {
        return { user: 'contextData' }; // Example context data
      },
    });
  3. Define the Handler Function

    Create a handler function that will process the input and return the output.

    const action = handler.input(InputSchema).output(OutputSchema).handler(async () => {
       // create user and return data
    })
  4. Execute the Action

    Call the action with the appropriate input.

    const result = await action({ name: 'Alice', age: 30 });
    console.log(result);

API

createActionHandler(config)

Creates a new ActionHandler instance with the specified configuration.

Parameters:

  • config (object): Configuration for the action handler.
    • maximumAttempts (number): Number of retry attempts (optional, default: 0).
    • delay (number): Delay between retries in milliseconds (optional, default: 0).
    • contextFn (function): Asynchronous function that returns context data (optional).

Returns:

  • ActionHandler: An instance of ActionHandler.

ActionHandler

Methods:

  • input<S extends ZodTypeAny>(schema: S): Defines the input schema for the action handler.
  • output<S extends ZodTypeAny>(schema: S): Defines the output schema for the action handler.
  • retry({ maximumAttempts, delay }: RetryProps): Configures retry logic with maximum attempts and delay.
  • handler<R>(callback: HandlerFn<T, R, TContext>): Registers the handler function to process the action.

ActionHandler Properties

  • input: Input schema.
  • output: Output schema.
  • maximumAttempts: Number of retry attempts.
  • delay: Delay between retries.
  • contextFn: Context factory function.

Examples

Basic Example

import { createActionHandler } from 'zod-server-actions';
import { z } from 'zod';

const InputSchema = z.object({
  name: z.string(),
  age: z.number().min(0),
});

const OutputSchema = z.object({
  greeting: z.string(),
});

const actionHandler = createActionHandler({
  maximumAttempts: 3,
  delay: 1000,
});

const handler = async (input, context) => {
  return {
    greeting: `Hello, ${input.name}!`,
  };
};

const action = actionHandler.input(InputSchema).output(OutputSchema).handler(handler);

(async () => {
  const result = await action({ name: 'Bob', age: 25 });
  console.log(result);
})();

Contributing

Feel free to submit issues or pull requests to improve the library. Contributions are welcome!

Contact

For questions or support, please contact [email protected].