UNPKG

react-form-action

Version:

State management helpers for the react form actions.

68 lines (65 loc) 4.08 kB
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 { z, ZodTypeAny, AnyZodTuple, ZodTuple, ZodType, ZodObject, ZodRawShape, objectUtil, ZodSchema, ZodEffects } from 'zod'; 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> = (params: { error: unknown; ctx: Context; }) => Promise<Err>; type InitialContext = Record<"formData", FormData>; /** * Action without schema has no input. */ type Action<Data, Context, Args extends AnyZodTuple> = (params: { args: z.infer<Args>; ctx: Context; }) => Promise<Data>; /** * Action with schema will get parsed formData as the input. */ type SchemaAction<Data, Context, Args extends AnyZodTuple, Schema extends ZodSchema> = (params: { args: z.infer<Args>; ctx: Context; input: z.infer<Schema>; }) => Promise<Data>; type AnyZodEffects = ZodEffects<any, any, any>; type FormActionBuilder<Schema extends ZodTypeAny | EmptyInput, TErr = unknown, Context = InitialContext, Args extends AnyZodTuple = ZodTuple<[], null>> = { args: <T extends [ZodTypeAny, ...ZodTypeAny[]]>(args: T) => FormActionBuilder<Schema, TErr, Context, ZodTuple<T, null>>; input: <T extends ZodTypeAny>(newInput: T extends ZodType<infer Out extends Record<string, unknown>> ? Schema extends AnyZodEffects ? "Extending schema with effect is not possible." : Schema extends EmptyInput ? T : Schema extends ZodTypeAny ? T extends AnyZodEffects ? "Your input contains effect which prevents merging it with the previous inputs." : T : T : "The schema output must be an object.") => FormActionBuilder<Schema extends ZodObject<infer O1 extends ZodRawShape> ? T extends ZodObject<infer O2 extends ZodRawShape> ? ZodObject<objectUtil.extendShape<O1, O2>> : never : T, TErr, Context, Args>; /** * 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 ZodTypeAny ? <Data>(action: SchemaAction<Data, Flatten<Context>, Args, Schema>) => (...args: [ ...z.infer<Args>, state: ActionState<Data, TErr, z.inferFormattedError<Schema> & z.inferFormattedError<Args>>, payload: FormData ]) => Promise<Flatten<InvalidState<z.inferFormattedError<Schema> & z.inferFormattedError<Args>> | FailureState<TErr> | SuccessState<Data>>> : <Data>(action: Action<Data, Flatten<Context>, Args>) => (...args: [ ...z.infer<Args>, state: ActionState<Data, TErr, z.inferFormattedError<Args>>, payload: FormData ]) => Promise<Flatten<InvalidState<z.inferFormattedError<Args>> | 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, TErr, Context & NewContext, Args>; /** * 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>) => FormActionBuilder<Schema, TErr, Context, Args>; }; declare const formAction: FormActionBuilder<typeof emptyInput, unknown, InitialContext, z.ZodTuple<[], null>>; export { ActionState, FailureState, InvalidState, SuccessState, formAction };