safe-actions-state
Version:
A lightweight, type-safe utility for Next.js server & client actions with built-in authentication and RBAC(role based access control) checks, Zod validation, auto retries if server action fails, and real-time toast feedback out of the box. Just write your
34 lines (33 loc) • 1.09 kB
TypeScript
export type SessionObject = {
authenticated: boolean;
role?: string;
};
import type { z } from "zod";
import type { ActionState } from "../types";
type PublicAction = {
isPrivate: false;
};
type PrivateAction = {
isPrivate: true;
};
type RoleBasedAction = {
isPrivate: true;
allowedRoles: string[];
};
type ActionType = PublicAction | PrivateAction | RoleBasedAction;
type ActionWithInputs<TInput, TOutput> = {
withInputs: true;
handler: (validatedData: TInput) => Promise<ActionState<TInput, TOutput>>;
schema: z.Schema<TInput>;
};
type ActionWithoutInputs<TOutput> = {
withInputs: false;
handler: () => Promise<ActionState<undefined, TOutput>>;
};
type Action<TInput, TOutput> = ActionWithInputs<TInput, TOutput> | ActionWithoutInputs<TOutput>;
type SafeActionProps<TInput, TOutput> = {
action: Action<TInput, TOutput>;
actionType: ActionType;
};
export declare const createSafeAction: <TInput, TOutput>({ action, actionType, }: SafeActionProps<TInput, TOutput>) => (data: TInput) => Promise<ActionState<TInput, TOutput>>;
export {};