react-form-action
Version:
State management helpers for the react form actions.
69 lines (66 loc) • 3.73 kB
text/typescript
import { A as ActionState, a as InvalidState, b as FailureState, S as SuccessState } from './createFormAction-DjhozgiG.mjs';
export { F as FormAction, I as InitialState, c as createFormAction, i as initial } from './createFormAction-DjhozgiG.mjs';
import { ZodType, z, ZodTuple, ZodObject } from 'zod/v4';
import { $ZodType, $ZodShape, $ZodErrorTree } from 'zod/v4/core';
declare const emptyInput: unique symbol;
type EmptyInput = typeof emptyInput;
type Flatten<T> = Identity<{
[K in keyof T]: T[K];
}>;
type Identity<T> = T;
type ErrorHandler<Context, Err, Args> = (params: {
error: unknown;
ctx: Context;
args: Args;
}) => Promise<Err>;
type InitialContext = Record<"formData", FormData>;
/**
* Action without schema has no input.
*/
type Action<Data, Context, Args extends unknown[]> = (params: {
args: Args;
ctx: Context;
}) => Promise<Data>;
/**
* Action with schema will get parsed formData as the input.
*/
type SchemaAction<Data, Context, Args extends unknown[], Schema extends ZodType> = (params: {
args: Args;
ctx: Context;
input: z.output<Schema>;
}) => Promise<Data>;
type FormActionBuilder<Schema extends ZodType | EmptyInput, Args extends $ZodType[] = [], TErr = unknown, Context = InitialContext, PlainArgs extends unknown[] = z.output<ZodTuple<Args, null>>> = {
args: <T extends [$ZodType, ...$ZodType[]]>(args: T) => FormActionBuilder<Schema, [...Args, ...T], TErr, Context>;
input: <T extends ZodObject>(newInput: T extends ZodObject ? T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends $ZodShape> ? T extends ZodObject<infer O2 extends $ZodShape> ? ZodObject<O1 & O2> : never : T, Args, TErr, Context>;
/**
* A finishing call on the builder completing your action.
* @param action async function to execute. It will receive parsed formData as input, when the builder was initialized with a schema.
* @returns runnable server action exportable from a module.
*/
run: Schema extends ZodType ? <Data>(action: SchemaAction<Data, Flatten<Context>, PlainArgs, Schema>) => (...args: [
...PlainArgs,
state: ActionState<Data, TErr, $ZodErrorTree<z.output<Schema>> & $ZodErrorTree<PlainArgs>>,
payload: FormData
]) => Promise<Flatten<InvalidState<$ZodErrorTree<z.output<Schema>> & $ZodErrorTree<PlainArgs>> | FailureState<TErr> | SuccessState<Data>>> : <Data>(action: Action<Data, Flatten<Context>, PlainArgs>) => (...args: [
...PlainArgs,
state: ActionState<Data, TErr, $ZodErrorTree<PlainArgs>>,
payload: FormData
]) => Promise<Flatten<InvalidState<$ZodErrorTree<PlainArgs>> | FailureState<TErr> | SuccessState<Data>>>;
/**
* A chainable context enhancing helper.
* @param middleware A function which receives the current context, and returns an object which will be added to the context.
* @returns FormActionBuilder
*/
use: <NewContext extends Record<string, unknown>>(middleware: ({ ctx }: {
ctx: Context;
}) => Promise<NewContext>) => FormActionBuilder<Schema, Args, TErr, Context & NewContext>;
/**
* A chainable error handler to handle errors thrown while running the action passed to .run().
* It does not receive errors thrown from the context/middleware.
* @param processError A function which receives action's errors.
* @returns FormActionBuilder
*/
error: <TErr>(processError: ErrorHandler<Context, TErr, PlainArgs>) => FormActionBuilder<Schema, Args, TErr, Context>;
};
declare const formAction: FormActionBuilder<typeof emptyInput, [], unknown, InitialContext, []>;
export { ActionState, FailureState, InvalidState, SuccessState, formAction };