Skip to main content

Types

/

SafeActionClientOpts

Type of options when creating a new safe action client.

export type SafeActionClientOpts<ServerError> = {
handleServerErrorLog?: (e: Error) => MaybePromise<void>;
handleReturnedServerError?: (e: Error) => MaybePromise<ServerError>;
};

SafeActionResult

Type of the result of a safe action.

export type SafeActionResult<ServerError, S extends Schema, Data, NextCtx = unknown> = {
data?: Data;
serverError?: ServerError;
validationErrors?: ValidationErrors<S>;
};

SafeActionFn

Type of the function called from components with typesafe input data.

export type SafeActionFn<ServerError, S extends Schema, Data> = (
input: InferIn<S>
) => Promise<SafeActionResult<ServerError, S, Data>>;

ActionMetadata

Type of meta options to be passed when defining a new safe action.

export type ActionMetadata = {
actionName?: string;
};

MiddlewareResult

Type of the result of a middleware function. It extends the result of a safe action with parsedInput and ctx optional properties.

export type MiddlewareResult<ServerError, NextCtx> = SafeActionResult<
ServerError,
any,
unknown,
NextCtx
> & {
parsedInput?: unknown;
ctx?: unknown;
success: boolean;
};

MiddlewareFn

Type of the middleware function passed to a safe action client.

export type MiddlewareFn<ServerError, ClientInput, Ctx, NextCtx> = {
(opts: {
clientInput: ClientInput;
ctx: Ctx;
metadata: ActionMetadata;
next: {
<const NC>(opts: { ctx: NC }): Promise<MiddlewareResult<ServerError, NC>>;
};
}): Promise<MiddlewareResult<ServerError, NextCtx>>;
};

ServerCodeFn

Type of the function that executes server code when defining a new safe action.

export type ServerCodeFn<S extends Schema, Data, Context> = (
parsedInput: Infer<S>,
utils: { ctx: Context; metadata: ActionMetadata }
) => Promise<Data>;

ValidationErrors

Type of the returned object when input validation fails.

export type ValidationErrors<S extends Schema> = Extend<ErrorList & SchemaErrors<Infer<S>>>;

/hooks

HookResult

Type of result object returned by useAction and useOptimisticAction hooks.

If a server-client communication error occurs, fetchError will be set to the error message.

export type HookResult<ServerError, S extends Schema, Data> = SafeActionResult<
ServerError,
S,
Data
> & {
fetchError?: string;
};

HookCallbacks

Type of hooks callbacks. These are executed when action is in a specific state.

export type HookCallbacks<ServerError, S extends Schema, Data> = {
onExecute?: (input: InferIn<S>) => MaybePromise<void>;
onSuccess?: (data: Data, input: InferIn<S>, reset: () => void) => MaybePromise<void>;
onError?: (
error: Omit<HookResult<ServerError, S, Data>, "data">,
input: InferIn<S>,
reset: () => void
) => MaybePromise<void>;
onSettled?: (
result: HookResult<ServerError, S, Data>,
input: InferIn<S>,
reset: () => void
) => MaybePromise<void>;
};

HookActionStatus

Type of the action status returned by useAction and useOptimisticAction hooks.

type HookActionStatus = "idle" | "executing" | "hasSucceeded" | "hasErrored";

Utility types

MaybePromise

Returns type or promise of type.

export type MaybePromise<T> = Promise<T> | T;

Extend

Extends an object without printing "&".

export type Extend<S> = S extends infer U ? { [K in keyof U]: U[K] } : never;

ErrorList

Object with an optional list of validation errors. Used in ValidationErrors type.

export type ErrorList = { _errors?: string[] } & {};

SchemaErrors

Creates nested schema validation errors type using recursion. Used in ValidationErrors type.

export type SchemaErrors<S> = {
[K in keyof S]?: S[K] extends object | null | undefined
? Extend<ErrorList & SchemaErrors<S[K]>>
: ErrorList;
} & {};

TypeSchema library

Infer, InferIn, Schema types come from TypeSchema library.