@omer-x/bs-ui-kit
Version:
Bootstrap UI Kit for React.js
35 lines (34 loc) • 1.75 kB
TypeScript
import type { ZodObject, ZodType } from "zod/v4";
/**
* Describes the definition of a type-safe server action for forms.
*/
type ActionDefinition<Name, Shape extends Record<string, ZodType>, SchemaI, SchemaO, Args extends unknown[]> = {
/**
* The unique name of the action.
*/
name: Name;
/**
* An array of middleware functions to execute before the action. (Optional)
*/
middlewares?: (() => void | Promise<void>)[];
/**
* The Zod schema used to validate form data.
*/
schema: ZodType<SchemaO, SchemaI> & ZodObject<Shape>;
/**
* The function to execute if validation succeeds. Receives parsed data and returns a function for additional arguments.
*/
action: (input: SchemaO) => ((...args: [...Args]) => Promise<Partial<Record<string, string>>>);
};
/**
* Defines a type-safe React server action for forms, validating form data with Zod and handling errors internally.
*
* @param definition - The form action definition object.
* @param definition.name - The unique name of the action.
* @param definition.middleware - An array of middleware functions to execute before the action. (Optional)
* @param definition.schema - The Zod schema used to validate form data.
* @param definition.action - The function to execute if validation succeeds.
* @returns An object containing the server action handler keyed by the action name.
*/
export declare function defineFormAction<Name extends string, Shape extends Record<string, ZodType>, SchemaI, SchemaO, Args extends unknown[]>(definition: ActionDefinition<Name, Shape, SchemaI, SchemaO, Args>): Record<Name, (...allParams: [...Args, initialState: unknown, formData: FormData]) => Promise<Record<string, string>>>;
export {};