Skip to content

Provides helper functions to define a type-safe React server action for forms, validating form data with Zod and handling errors internally

License

Notifications You must be signed in to change notification settings

omermecitoglu/next-form-actions

Repository files navigation

next-form-actions

npm version npm downloads codecov License: MIT GitHub last commit GitHub issues GitHub stars

Overview

Provides helper functions to define a type-safe React server action for forms, validating form data with Zod and handling errors internally.

Installation

To install this package

npm install @omer-x/next-form-actions

Usage

"use server";
import { defineFormAction, displayMessage } from "@omer-x/next-form-actions";
import z from "zod";
import { authMiddleware } from "./middlewares/session";

export const createUser = defineFormAction({
  middlewares: [authMiddleware],
  schema: z.object({
    foo: z.string(),
  }),
  action: input => (async () => {
    console.log(input); // { foo: "bar" }
    try {
      // try to create a user

      displayMessage("success", "ok!");
    } catch (error) {
      if (error === "BAD_REQUEST") {
        displayMessage("warning", "you are doing something wrong");
      }
      displayMessage("error", "unknown error");
    }
  }),
});

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Provides helper functions to define a type-safe React server action for forms, validating form data with Zod and handling errors internally

Topics

Resources

License

Stars

Watchers

Forks