import { Schema, Infer, InferIn, ValidationAdapter, InferInArray, InferArray } from "./adapters/types.mjs"; type Prettify = { [K in keyof T]: T[K]; } & {}; type MaybePromise = Promise | T; type VEList = Prettify<{ _errors?: string[]; }>; type SchemaErrors = { [K in keyof S]?: S[K] extends object | null | undefined ? Prettify> : VEList; } & {}; /** * Type of the returned object when validation fails. */ type ValidationErrors = S extends Schema ? Infer extends object ? Prettify>> : VEList : undefined; /** * Type of the array of bind arguments validation errors. */ type BindArgsValidationErrors = { [K in keyof BAS]: ValidationErrors; }; /** * Type of flattened validation errors. `formErrors` contains global errors, and `fieldErrors` * contains errors for each field, one level deep. */ type FlattenedValidationErrors> = Prettify<{ formErrors: string[]; fieldErrors: { [K in keyof Omit]?: string[]; }; }>; /** * Type of flattened bind arguments validation errors. */ type FlattenedBindArgsValidationErrors[]> = { [K in keyof BAVE]: FlattenedValidationErrors; }; /** * Type of the function used to format validation errors. */ type HandleValidationErrorsShapeFn< S extends Schema | undefined, BAS extends readonly Schema[], MD, Ctx extends object, CVE, > = ( validationErrors: ValidationErrors, utils: { clientInput: S extends Schema ? InferIn : undefined; bindArgsClientInputs: BAS; metadata: MD; ctx: Prettify; } ) => CVE; /** * Type of the function used to format bind arguments validation errors. */ type HandleBindArgsValidationErrorsShapeFn< S extends Schema | undefined, BAS extends readonly Schema[], MD, Ctx extends object, CBAVE, > = ( bindArgsValidationErrors: BindArgsValidationErrors, utils: { clientInput: S extends Schema ? InferIn : undefined; bindArgsClientInputs: BAS; metadata: MD; ctx: Prettify; } ) => CBAVE; declare class SafeActionClient< ServerError, ODVES extends DVES | undefined, // override default validation errors shape MetadataSchema extends Schema | undefined = undefined, MD = MetadataSchema extends Schema ? Infer : undefined, // metadata type (inferred from metadata schema) Ctx extends object = {}, ISF extends (() => Promise) | undefined = undefined, // input schema function IS extends Schema | undefined = ISF extends Function ? Awaited> : undefined, // input schema OS extends Schema | undefined = undefined, // output schema const BAS extends readonly Schema[] = [], CVE = undefined, const CBAVE = undefined, > { #private; constructor( opts: { middlewareFns: MiddlewareFn[]; metadataSchema: MetadataSchema; metadata: MD; inputSchemaFn: ISF; outputSchema: OS; bindArgsSchemas: BAS; validationAdapter: ValidationAdapter; handleValidationErrorsShape: HandleValidationErrorsShapeFn; handleBindArgsValidationErrorsShape: HandleBindArgsValidationErrorsShapeFn; ctxType: Ctx; } & Required< Pick< SafeActionClientOpts, "handleServerError" | "defaultValidationErrorsShape" | "throwValidationErrors" > > ); /** * Use a middleware function. * @param middlewareFn Middleware function * * {@link https://next-safe-action.dev/docs/define-actions/instance-methods#use See docs for more information} */ use( middlewareFn: MiddlewareFn ): SafeActionClient; /** * Define metadata for the action. * @param data Metadata with the same type as the return value of the [`defineMetadataSchema`](https://next-safe-action.dev/docs/define-actions/create-the-client#definemetadataschema) optional initialization function * * {@link https://next-safe-action.dev/docs/define-actions/instance-methods#metadata See docs for more information} */ metadata(data: MD): SafeActionClient; /** * Define the input validation schema for the action. * @param inputSchema Input validation schema * @param utils Optional utils object * * {@link https://next-safe-action.dev/docs/define-actions/create-the-client#inputschema See docs for more information} */ schema< OIS extends Schema | ((prevSchema: IS) => Promise), // override input schema AIS extends Schema = OIS extends (prevSchema: IS) => Promise ? Awaited> : OIS, // actual input schema OCVE = ODVES extends "flattened" ? FlattenedValidationErrors> : ValidationErrors, >( inputSchema: OIS, utils?: { handleValidationErrorsShape?: HandleValidationErrorsShapeFn; } ): SafeActionClient; /** * Define the bind args input validation schema for the action. * @param bindArgsSchemas Bind args input validation schemas * @param utils Optional utils object * * {@link https://next-safe-action.dev/docs/define-actions/instance-methods#bindargsschemas See docs for more information} */ bindArgsSchemas< const OBAS extends readonly Schema[], OCBAVE = ODVES extends "flattened" ? FlattenedBindArgsValidationErrors> : BindArgsValidationErrors, >( bindArgsSchemas: OBAS, utils?: { handleBindArgsValidationErrorsShape?: HandleBindArgsValidationErrorsShapeFn; } ): SafeActionClient; /** * Define the output data validation schema for the action. * @param schema Output data validation schema * * {@link https://next-safe-action.dev/docs/define-actions/create-the-client#outputschema See docs for more information} */ outputSchema( dataSchema: OOS ): SafeActionClient; /** * Define the action. * @param serverCodeFn Code that will be executed on the **server side** * @param [cb] Optional callbacks that will be called after action execution, on the server. * * {@link https://next-safe-action.dev/docs/define-actions/instance-methods#action--stateaction See docs for more information} */ action : any>( serverCodeFn: ServerCodeFn, utils?: SafeActionUtils ): SafeActionFn; /** * Define the stateful action. * To be used with the [`useStateAction`](https://next-safe-action.dev/docs/execute-actions/hooks/usestateaction) hook. * @param serverCodeFn Code that will be executed on the **server side** * @param [cb] Optional callbacks that will be called after action execution, on the server. * * {@link https://next-safe-action.dev/docs/define-actions/instance-methods#action--stateaction See docs for more information} */ stateAction : any>( serverCodeFn: StateServerCodeFn, utils?: SafeActionUtils ): SafeStateActionFn; schemas(): Promise<{ metadata: MD; inputSchema: IS; outputSchema: OS; }>; } /** * Type of the default validation errors shape passed to `createSafeActionClient` via `defaultValidationErrorsShape` * property. */ type DVES = "formatted" | "flattened"; /** * Type of the util properties passed to server error handler functions. */ type ServerErrorFunctionUtils = { clientInput: unknown; bindArgsClientInputs: unknown[]; ctx: object; metadata: MetadataSchema extends Schema ? Infer : undefined; }; /** * Type of options when creating a new safe action client. */ type SafeActionClientOpts = { validationAdapter?: ValidationAdapter; defineMetadataSchema?: () => MetadataSchema; handleServerError?: (error: Error, utils: ServerErrorFunctionUtils) => MaybePromise; throwValidationErrors?: boolean; defaultValidationErrorsShape?: ODVES; }; /** * Type of the result of a safe action. */ type SafeActionResult< ServerError, S extends Schema | undefined, BAS extends readonly Schema[], CVE = ValidationErrors, CBAVE = BindArgsValidationErrors, Data = unknown, NextCtx = object, > = { data?: Data; serverError?: ServerError; validationErrors?: CVE; bindArgsValidationErrors?: CBAVE; }; /** * Type of the function called from components with type safe input data. */ type SafeActionFn = ( ...clientInputs: [...bindArgsInputs: InferInArray, input: S extends Schema ? InferIn : void] ) => Promise | undefined>; /** * Type of the stateful function called from components with type safe input data. */ type SafeStateActionFn = ( ...clientInputs: [ ...bindArgsInputs: InferInArray, prevResult: Prettify>, input: S extends Schema ? InferIn : void, ] ) => Promise>; /** * Type of the result of a middleware function. It extends the result of a safe action with * information about the action execution. */ type MiddlewareResult = SafeActionResult< ServerError, any, any, any, any, any, NextCtx > & { parsedInput?: unknown; bindArgsParsedInputs?: unknown[]; ctx?: object; success: boolean; }; /** * Type of the middleware function passed to a safe action client. */ type MiddlewareFn = { (opts: { clientInput: unknown; bindArgsClientInputs: unknown[]; ctx: Prettify; metadata: MD; next: { (opts?: { ctx?: NC }): Promise>; }; }): Promise>; }; /** * Type of the function that executes server code when defining a new safe action. */ type ServerCodeFn = (args: { parsedInput: S extends Schema ? Infer : undefined; bindArgsParsedInputs: InferArray; ctx: Prettify; metadata: MD; }) => Promise; /** * Type of the function that executes server code when defining a new stateful safe action. */ type StateServerCodeFn< ServerError, MD, Ctx extends object, S extends Schema | undefined, BAS extends readonly Schema[], CVE, CBAVE, Data, > = ( args: { parsedInput: S extends Schema ? Infer : undefined; bindArgsParsedInputs: InferArray; ctx: Prettify; metadata: MD; }, utils: { prevResult: Prettify>; } ) => Promise; /** * Type of action execution utils. It includes action callbacks and other utils. */ type SafeActionUtils< ServerError, MD, Ctx extends object, S extends Schema | undefined, BAS extends readonly Schema[], CVE, CBAVE, Data, > = { throwServerError?: boolean; throwValidationErrors?: boolean; onSuccess?: (args: { data?: Data; metadata: MD; ctx?: Prettify; clientInput: S extends Schema ? InferIn : undefined; bindArgsClientInputs: InferInArray; parsedInput: S extends Schema ? Infer : undefined; bindArgsParsedInputs: InferArray; hasRedirected: boolean; hasNotFound: boolean; }) => MaybePromise; onError?: (args: { error: Prettify, "data">>; metadata: MD; ctx?: Prettify; clientInput: S extends Schema ? InferIn : undefined; bindArgsClientInputs: InferInArray; }) => MaybePromise; onSettled?: (args: { result: Prettify>; metadata: MD; ctx?: Prettify; clientInput: S extends Schema ? InferIn : undefined; bindArgsClientInputs: InferInArray; hasRedirected: boolean; hasNotFound: boolean; }) => MaybePromise; }; /** * Infer input types of a safe action. */ type InferSafeActionFnInput = T extends | SafeActionFn | SafeStateActionFn ? S extends Schema ? { clientInput: InferIn; bindArgsClientInputs: InferInArray; parsedInput: Infer; bindArgsParsedInputs: InferArray; } : { clientInput: undefined; bindArgsClientInputs: InferInArray; parsedInput: undefined; bindArgsParsedInputs: InferArray; } : never; /** * Infer the result type of a safe action. */ type InferSafeActionFnResult = T extends | SafeActionFn< infer ServerError, infer S extends Schema | undefined, infer BAS extends readonly Schema[], infer CVE, infer CBAVE, infer Data > | SafeStateActionFn< infer ServerError, infer S extends Schema | undefined, infer BAS extends readonly Schema[], infer CVE, infer CBAVE, infer Data > ? SafeActionResult : never; /** * Infer the next context type returned by a middleware function using the `next` function. */ type InferMiddlewareFnNextCtx = T extends MiddlewareFn ? NextCtx : never; /** * Infer the context type of a safe action client or middleware function. */ type InferCtx = T extends | SafeActionClient | MiddlewareFn ? Ctx : never; /** * Infer the metadata type of a safe action client or middleware function. */ type InferMetadata = T extends | SafeActionClient | MiddlewareFn ? MD : never; /** * Infer the server error type from a safe action client or a middleware function or a safe action function. */ type InferServerError = T extends | SafeActionClient | MiddlewareFn | SafeActionFn | SafeStateActionFn ? ServerError : never; export { type BindArgsValidationErrors as B, type DVES as D, type FlattenedBindArgsValidationErrors as F, type HandleValidationErrorsShapeFn as H, type InferSafeActionFnInput as I, type MiddlewareFn as M, type Prettify as P, type SafeActionClientOpts as S, type ValidationErrors as V, SafeActionClient as a, type ServerErrorFunctionUtils as b, type SafeActionResult as c, type SafeActionFn as d, type SafeStateActionFn as e, type MiddlewareResult as f, type ServerCodeFn as g, type StateServerCodeFn as h, type SafeActionUtils as i, type InferSafeActionFnResult as j, type InferMiddlewareFnNextCtx as k, type InferCtx as l, type InferMetadata as m, type InferServerError as n, type FlattenedValidationErrors as o, type HandleBindArgsValidationErrorsShapeFn as p, type MaybePromise as q, };