next-safe-action
Version:
Type safe and validated Server Actions in your Next.js project.
139 lines (136 loc) • 4.91 kB
text/typescript
import {
S as StandardSchemaV1,
I as InferInputOrDefault,
e as SafeActionResult,
t as MaybePromise,
P as Prettify,
N as NavigationKind,
f as SafeActionFn,
g as SafeStateActionFn,
} from "./index.types-CJ06jFQp.mjs";
/**
* Type of hooks callbacks. These are executed when action is in a specific state.
*/
type HookCallbacks<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data> = {
onExecute?: (args: { input: InferInputOrDefault<S, undefined> }) => MaybePromise<unknown>;
onSuccess?: (args: { data?: Data; input: InferInputOrDefault<S, undefined> }) => MaybePromise<unknown>;
onError?: (args: {
error: Prettify<Omit<SafeActionResult<ServerError, S, CVE, Data>, "data">> & {
thrownError?: Error;
};
input: InferInputOrDefault<S, undefined>;
}) => MaybePromise<unknown>;
onNavigation?: (args: {
input: InferInputOrDefault<S, undefined>;
navigationKind: NavigationKind;
}) => MaybePromise<unknown>;
onSettled?: (args: {
result: Prettify<SafeActionResult<ServerError, S, CVE, Data>>;
input: InferInputOrDefault<S, undefined>;
navigationKind?: NavigationKind;
}) => MaybePromise<unknown>;
};
/**
* Type of the safe action function passed to hooks. Same as `SafeActionFn` except it accepts
* just a single input, without bind arguments.
*/
type HookSafeActionFn<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data> = (
input: InferInputOrDefault<S, undefined>
) => Promise<SafeActionResult<ServerError, S, CVE, Data>>;
/**
* Type of the stateful safe action function passed to hooks. Same as `SafeStateActionFn` except it accepts
* just a single input, without bind arguments.
*/
type HookSafeStateActionFn<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data> = (
prevResult: SafeActionResult<ServerError, S, CVE, Data>,
input: InferInputOrDefault<S, undefined>
) => Promise<SafeActionResult<ServerError, S, CVE, Data>>;
/**
* Type of the action status returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
*/
type HookActionStatus = "idle" | "executing" | "transitioning" | "hasSucceeded" | "hasErrored" | "hasNavigated";
/**
* Type of the shorthand status object returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
*/
type HookShorthandStatus = {
isIdle: boolean;
isExecuting: boolean;
isTransitioning: boolean;
isPending: boolean;
hasSucceeded: boolean;
hasErrored: boolean;
hasNavigated: boolean;
};
/**
* Type of the return object of the `useAction` hook.
*/
type UseActionHookReturn<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data> = {
execute: (input: InferInputOrDefault<S, void>) => void;
executeAsync: (input: InferInputOrDefault<S, void>) => Promise<SafeActionResult<ServerError, S, CVE, Data>>;
input: InferInputOrDefault<S, undefined>;
result: Prettify<SafeActionResult<ServerError, S, CVE, Data>>;
reset: () => void;
status: HookActionStatus;
} & HookShorthandStatus;
/**
* Type of the return object of the `useOptimisticAction` hook.
*/
type UseOptimisticActionHookReturn<
ServerError,
S extends StandardSchemaV1 | undefined,
CVE,
Data,
State,
> = UseActionHookReturn<ServerError, S, CVE, Data> &
HookShorthandStatus & {
optimisticState: State;
};
/**
* Type of the return object of the `useStateAction` hook.
*/
type UseStateActionHookReturn<ServerError, S extends StandardSchemaV1 | undefined, CVE, Data> = Omit<
UseActionHookReturn<ServerError, S, CVE, Data>,
"executeAsync" | "reset"
> &
HookShorthandStatus;
/**
* Type of the return object of the `useAction` hook.
*/
type InferUseActionHookReturn<T extends Function> =
T extends SafeActionFn<infer ServerError, infer S extends StandardSchemaV1 | undefined, any[], infer CVE, infer Data>
? UseActionHookReturn<ServerError, S, CVE, Data>
: never;
/**
* Type of the return object of the `useOptimisticAction` hook.
*/
type InferUseOptimisticActionHookReturn<T extends Function, State = any> =
T extends SafeActionFn<infer ServerError, infer S extends StandardSchemaV1 | undefined, any[], infer CVE, infer Data>
? UseOptimisticActionHookReturn<ServerError, S, CVE, Data, State>
: never;
/**
* Type of the return object of the `useStateAction` hook.
* @deprecated The `useStateAction` hook is deprecated. Use React's `useActionState` hook instead.
*/
type InferUseStateActionHookReturn<T extends Function> =
T extends SafeStateActionFn<
infer ServerError,
infer S extends StandardSchemaV1 | undefined,
any[],
infer CVE,
infer Data
>
? UseStateActionHookReturn<ServerError, S, CVE, Data>
: never;
export type {
HookSafeActionFn as H,
InferUseActionHookReturn as I,
UseActionHookReturn as U,
HookCallbacks as a,
UseOptimisticActionHookReturn as b,
HookSafeStateActionFn as c,
HookActionStatus as d,
HookShorthandStatus as e,
UseStateActionHookReturn as f,
InferUseOptimisticActionHookReturn as g,
InferUseStateActionHookReturn as h,
};