next-safe-action
Version:
Type safe and validated Server Actions in your Next.js project.
141 lines (139 loc) • 5 kB
text/typescript
import {
A as InferInputOrDefault,
M as StandardSchemaV1,
O as MaybePromise,
_ as SafeActionResult,
g as SafeActionFn,
k as Prettify,
m as NavigationKind,
y as SafeStateActionFn,
} from "./index.types-CqcWrJ4z.mjs";
//#region src/hooks.types.d.ts
/**
* 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;
//#endregion
export {
HookShorthandStatus as a,
InferUseStateActionHookReturn as c,
UseStateActionHookReturn as d,
HookSafeStateActionFn as i,
UseActionHookReturn as l,
HookCallbacks as n,
InferUseActionHookReturn as o,
HookSafeActionFn as r,
InferUseOptimisticActionHookReturn as s,
HookActionStatus as t,
UseOptimisticActionHookReturn as u,
};
//# sourceMappingURL=hooks.types-BWkq1TFG.d.mts.map