next-action-forge
Version:
A simple, type-safe toolkit for Next.js server actions with Zod validation
111 lines (105 loc) • 4.71 kB
text/typescript
import { A as ActionResult, b as ServerAction, a as ServerActionResponse, S as ServerActionError } from '../index-CeOJE6T8.mjs';
import { FieldValues, DefaultValues, UseFormProps, UseFormReturn } from 'react-hook-form';
import { z } from 'zod';
interface UseServerActionOptions<TOutput> {
onSuccess?: (data: TOutput) => void | Promise<void>;
onError?: (error: ActionResult<TOutput>) => void | Promise<void>;
successMessage?: string | ((data: TOutput) => string);
errorMessage?: string | ((error: ActionResult<TOutput>) => string);
showSuccessToast?: boolean;
showErrorToast?: boolean;
redirectOnAuthError?: boolean;
preventRedirect?: boolean;
redirectDelay?: number;
}
interface UseServerActionReturn<TInput, TOutput> {
execute: TInput extends void ? () => Promise<ActionResult<TOutput>> : (input: TInput) => Promise<ActionResult<TOutput>>;
result: ActionResult<TOutput> | undefined;
isExecuting: boolean;
isRedirecting: boolean;
hasSucceeded: boolean;
hasErrored: boolean;
reset: () => void;
}
declare function useServerAction<TOutput>(action: ServerAction<void, TOutput>, options?: UseServerActionOptions<TOutput>): UseServerActionReturn<void, TOutput>;
declare function useServerAction<TInput, TOutput>(action: ServerAction<TInput, TOutput>, options?: UseServerActionOptions<TOutput>): UseServerActionReturn<TInput, TOutput>;
interface UseOptimisticActionOptions<TInput, TOutput, TOptimistic> extends UseServerActionOptions<TOutput> {
/**
* Function to update the optimistic state
*/
updateFn: TInput extends void ? (current: TOptimistic) => TOptimistic : (current: TOptimistic, input: TInput) => TOptimistic;
}
interface UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {
/**
* The optimistic state
*/
optimisticState: TOptimistic;
/**
* Execute the action with optimistic update
*/
execute: TInput extends void ? () => Promise<ActionResult<TOutput>> : (input: TInput) => Promise<ActionResult<TOutput>>;
/**
* Loading state
*/
isExecuting: boolean;
/**
* Success state
*/
hasSucceeded: boolean;
/**
* Error state
*/
hasErrored: boolean;
/**
* Last result
*/
result: ActionResult<TOutput> | undefined;
/**
* Reset state
*/
reset: () => void;
}
/**
* Hook for server actions with optimistic updates
*
* @example
* ```typescript
* const { optimisticState, execute } = useOptimisticAction(
* currentTodos,
* addTodoAction,
* {
* updateFn: (todos, newTodo) => [...todos, newTodo],
* onSuccess: (savedTodo) => {
* // Update with server response if needed
* }
* }
* );
* ```
*/
declare function useOptimisticAction<TInput, TOutput, TOptimistic>(initialState: TOptimistic, action: ServerAction<TInput, TOutput>, options: UseOptimisticActionOptions<TInput, TOutput, TOptimistic>): UseOptimisticActionReturn<TInput, TOutput, TOptimistic>;
interface UseFormActionOptions<TFieldValues extends FieldValues, TOutput> {
action: (prevState: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>>;
schema?: z.ZodTypeAny;
defaultValues?: DefaultValues<TFieldValues>;
mode?: UseFormProps<TFieldValues>["mode"];
transformData?: (data: TFieldValues) => FormData;
onSuccess?: (data: TOutput) => void | Promise<void>;
onError?: (error: ServerActionError) => void;
resetOnSuccess?: boolean;
showSuccessToast?: boolean | string | ((data: TOutput) => string);
showErrorToast?: boolean | string | ((error: ServerActionError) => string);
preventRedirect?: boolean;
redirectDelay?: number;
}
interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {
form: UseFormReturn<TFieldValues>;
onSubmit: (e?: React.BaseSyntheticEvent) => void;
isSubmitting: boolean;
isRedirecting: boolean;
actionState: ServerActionResponse<TOutput>;
reset: () => void;
handleSubmit: (data: TFieldValues) => Promise<void>;
}
declare function useFormAction<TFieldValues extends FieldValues = FieldValues, TOutput = void>({ action, schema, defaultValues, mode, transformData, onSuccess, onError, resetOnSuccess, showSuccessToast, showErrorToast, preventRedirect, redirectDelay, }: UseFormActionOptions<TFieldValues, TOutput>): UseFormActionReturn<TFieldValues, TOutput>;
declare function ToastRestorer(): null;
export { ToastRestorer, type UseFormActionOptions, type UseFormActionReturn, type UseOptimisticActionOptions, type UseOptimisticActionReturn, type UseServerActionOptions, type UseServerActionReturn, useFormAction, useOptimisticAction, useServerAction };