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
23 lines (22 loc) • 929 B
TypeScript
import type { ActionState, FieldErrors } from "../types";
export type SafeActionType<TInput, TOutput> = (data: TInput) => Promise<ActionState<TInput, TOutput>>;
export type UseActionOptions<TOutput> = {
retries?: number;
onStart?: () => void;
onSuccess?: (data?: TOutput) => void;
onError?: (error: string) => void;
onComplete?: () => void;
toastMessages?: {
loading: string;
success: string;
};
};
export declare const useSafeAction: <TInput, TOutput>(serverAction: SafeActionType<TInput, TOutput>, options?: UseActionOptions<TOutput>) => {
clientAction: (input: TInput) => Promise<void>;
abortAction: () => void | undefined;
error: string | undefined;
data: TOutput | undefined;
isPending: boolean;
fieldErrors: FieldErrors<TInput> | undefined;
setFieldErrors: import("react").Dispatch<import("react").SetStateAction<FieldErrors<TInput> | undefined>>;
};