Skip to main content

Types

/

SafeActionClientOpts

Type of options when creating a new safe action client.

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

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<NextCtx> = SafeActionResult<any, unknown, NextCtx> & {
parsedInput?: unknown;
ctx?: unknown;
success: boolean;
};

MiddlewareFn

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

export type MiddlewareFn<ClientInput, Ctx, NextCtx> = {
(opts: {
clientInput: ClientInput;
ctx: Ctx;
metadata: ActionMetadata;
next: {
<const NC>(opts: { ctx: NC }): Promise<MiddlewareResult<NC>>;
};
}): Promise<MiddlewareResult<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>>>;

SafeActionResult

Type of the result of a safe action.

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

SafeActionFn

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

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

/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.

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

HookCallbacks

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

type HookCallbacks<S extends Schema, Data> = {
onExecute?: (input: InferIn<S>) => MaybePromise<void>;
onSuccess?: (data: Data, input: InferIn<S>, reset: () => void) => MaybePromise<void>;
onError?: (
error: Omit<HookResult<S, Data>, "data">,
input: InferIn<S>,
reset: () => void
) => MaybePromise<void>;
onSettled?: (
result: HookResult<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.