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

yup-server-action

v0.1.3

Published

Server actions powered by Yup

Readme

YSA - Yup Server Actions 🚀

MIT License TypeScript React Yup

A type-safe wrapper for React Server Actions with Yup schema validation.

Features ✨

  • 🛡️ Type-safe: Full TypeScript support with inferred types
  • Validation: Seamless integration with Yup schemas
  • 🧩 Flexible: Support for actions with or without input
  • 🪝 React Hook: Easy integration with React components
  • 📊 Status Tracking: Built-in state management for pending, success, and error states

Installation 📦

npm install yup-server-action
# or
yarn add yup-server-action
# or
pnpm add yup-server-action

Usage 📝

Creating a Server Action with Input Validation

// _action.ts
import { createServerAction } from "yup-server-action";
import * as y from "yup";

const UserSchema = y.object({
  name: y.string().required(),
  email: y.string().email().required(),
  password: y.string().required(),
});

export const saveUserAction = createServerAction()
  .input(UserSchema)
  .handle(async ({ input }) => {
    // Perform your server-side logic here
    // e.g., save to database, call external API, etc.
    
    return {
      user: input,
    };
  });

Using the Server Action in a React Component

// page.tsx
import React from "react";
import { useServerAction } from "yup-server-action";
import { saveUserAction } from "./_action";

export default function CreateUserPage() {
  const { execute, isPending, isSuccess, isError, error, data } =
    useServerAction(saveUserAction, {
      onSuccess(result) {
        alert("User created");
        console.log(result);
      },
      onError(err) {
        alert("Error creating user");
        console.error(err);
      },
    });

  if (isPending) {
    return <button disabled>Creating user...</button>;
  }

  if (isError) {
    return (
      <div>
        <h1>Error creating user</h1>
        <pre>{JSON.stringify(error, null, 2)}</pre>
      </div>
    );
  }

  if (isSuccess) {
    return (
      <div>
        <h1>User created</h1>
        <pre>{JSON.stringify(data, null, 2)}</pre>
      </div>
    );
  }

  return (
    <div>
      <h1>Create user</h1>
      <form
        onSubmit={(e) => {
          e.preventDefault();
          execute({
            name: e.currentTarget.userName.value,
            email: e.currentTarget.email.value,
            password: e.currentTarget.password.value,
          });
        }}
      >
        <label htmlFor="userName">Name</label>
        <input id="userName" name="userName" type="text" />
        <label htmlFor="email">Email</label>
        <input id="email" name="email" type="email" />
        <label htmlFor="password">Password</label>
        <input id="password" name="password" type="password" />
        <button type="submit">Create user</button>
      </form>
    </div>
  );
}

Creating a Server Action without Input

// _action.ts
import { createServerAction } from "yup-server-action";

export const getMyUserAction = createServerAction().handle(async () => {
  // Fetch user data or perform any server-side operation
  return {
    data: {
      // User data here
    },
  };
});

Using a Server Action without Input

// page.tsx
import React from "react";
import { useServerAction } from "yup-server-action";
import { getMyUserAction } from "./_action";

export default function UserProfilePage() {
  const { execute, isPending, isSuccess, isError, error, data } =
    useServerAction(getMyUserAction, {
      onSuccess(result) {
        console.log(result);
      },
    });

  if (isPending) {
    return <div>Loading user data...</div>;
  }

  if (isError) {
    return <div>Error loading user data</div>;
  }

  return (
    <div>
      <h1>User Profile</h1>
      <button onClick={() => execute()}>Load User Data</button>
      
      {isSuccess && (
        <div>
          <h2>User Data</h2>
          <pre>{JSON.stringify(data, null, 2)}</pre>
        </div>
      )}
    </div>
  );
}

API Reference 📚

createServerAction()

Creates a new server action builder.

// Without input schema
createServerAction().handle(handler)

// With input schema
createServerAction(schema).handle(handler)
// or
createServerAction().input(schema).handle(handler)

useServerAction(action, options?)

React hook for using server actions in components.

const {
  execute,   // Function to execute the server action
  isPending, // Boolean indicating if the action is in progress
  isSuccess, // Boolean indicating if the action completed successfully
  isError,   // Boolean indicating if the action resulted in an error
  data,      // The data returned by the action (if successful)
  error      // The error thrown by the action (if failed)
} = useServerAction(action, {
  onSuccess, // Optional callback for successful execution
  onError    // Optional callback for failed execution
});

Why Use YSA? 🤔

  • Simplified Validation: Leverage Yup's powerful schema validation without boilerplate
  • Type Safety: Get full TypeScript support with inferred types from your schemas
  • React Integration: Easily use server actions in your React components with built-in state management
  • Developer Experience: Improve your DX with a clean, fluent API

License 📄

MIT

Author ✍️

Enzo Apolinário