Giter Club home page Giter Club logo

typed-server-action's Introduction

Typed Server Action

This is a tiny type-utility wrapper for Next.js's server action.

Feature

  • Add type and validation to server action function
  • Wrapped the return as Result

Installation

The package require zod as peer dependency.

npm/yarn/pnpm install typed-server-action zod

Usage

// action.ts
import {
	ServerReturnType,
	serverActionResult,
	typedServerAction,
} from 'typed-server-action';
import { z } from 'zod';

const myServerAction = typedServerAction(z.object({
    id: z.number(),
    firstName: z.string(),
    lastName: z.string()
}), async function(data): Promise<ServerReturnType<User>> {
                // ^ { id: number; firstName: string; lastName: string; }
    const { id, firstName, lastName } = data;
    try {
        const updatedUser = await db.update(users).set({
            firstName,
            lastName
        }).where(eq(users.id, id));
        return serverActionResult.success(updatedUser);
    } catch {
        return serverActionResult.fail([
            {
                key: 'general',
                errMsg: 'The server action failed'
            }
        ]);
    }
});

// page.tsx (use with client component if you like)
'use client';

export default function Page() {
    const _updateUser = async (formData: FormData) => {
        const result = await myServerAction(formData);
            // ^ { success: true, data: T } | { success: false, errMsgs: {key: string; errMsg: string; }[] }
    }

    return (
        ...
    );
}

typed-server-action's People

Contributors

github-actions[bot] avatar tom555my avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.