UNPKG

@kellanjs/actioncraft

Version:

Fluent, type-safe builder for Next.js server actions.

40 lines (39 loc) 2.22 kB
import type { CraftedAction } from "./types/actions.js"; import type { BaseError } from "./types/errors.js"; import type { InferResult } from "./types/inference.js"; import type { Result } from "./types/result.js"; import type { ApiResult, StatefulApiResult, ThrowableAction } from "./types/shared.js"; /** * Unwraps an Actioncraft result, returning the data or throwing an error. * Supports both async and sync usage patterns. * Thrown errors automatically include action ID for verification when available. */ export declare function unwrap<TData, TError extends BaseError>(promiseResult: Promise<ApiResult<TData, TError> | StatefulApiResult<TData, TError> | Result<TData, TError>>): Promise<TData>; export declare function unwrap<TData, TError extends BaseError>(result: ApiResult<TData, TError> | StatefulApiResult<TData, TError> | Result<TData, TError>): TData; /** * Creates a throwable version of an Actioncraft action. * The returned function throws ActioncraftErrors with automatic action ID verification support. * Errors thrown by this function can be verified with isActioncraftError(error, action). */ export declare function throwable<TAction extends CraftedAction<any, any, any, any>>(action: TAction): ThrowableAction<TAction>; /** * Creates an appropriate initial state for any action based on its configuration. * The initial state uses the action's real ID for consistency with actual results. * * For useActionState actions: returns StatefulApiResult with error and values * For functional format actions: returns Result.err() with error * For regular actions: returns ApiResult with error * * Usage: * - useActionState: const [state, action] = useActionState(myAction, initial(myAction)) * - useState: const [state, setState] = useState(initial(myAction)) */ export declare function initial<TAction>(action: TAction): InferResult<TAction>; /** * Utility to extract the action ID from a crafted action. * Useful for debugging and logging purposes. * * @param action - The crafted action * @returns The action ID if available, undefined otherwise */ export declare function getActionId<TAction extends CraftedAction<any, any, any, any>>(action: TAction): string | undefined;