@kellanjs/actioncraft
Version:
Fluent, type-safe builder for Next.js server actions.
67 lines (66 loc) • 2.52 kB
TypeScript
/**
* This is the functional Result type that the library uses for all internal logic.
* The ApiResult format is returned to the client by default, but the action can be
* configured to return the functional Result type if desired.
*/
/** A successful result containing a value of type T */
export type Ok<T> = {
readonly type: "ok";
readonly value: T;
readonly __ac_id: string;
};
/** A failed result containing an error of type E */
export type Err<E> = {
readonly type: "err";
readonly error: E;
readonly __ac_id: string;
};
/**
* A Result represents an operation that can either succeed (Ok) or fail (Err).
* This is a serializable alternative to throwing exceptions.
*/
export type Result<T, E> = Ok<T> | Err<E>;
/**
* Creates a successful result.
* @param value The success value (optional)
* @param actionId The action ID that created this result (optional, will be set to "unknown" if not provided)
* @returns Ok result containing the value
*/
export declare function ok<T>(value?: T, actionId?: string): Ok<T>;
/**
* Creates a failed result.
* @param error The error value (optional)
* @param actionId The action ID that created this result (optional, will be set to "unknown" if not provided)
* @returns Err result containing the error
*/
export declare function err<E>(error?: E, actionId?: string): Err<E>;
/**
* Tests if a Result is successful.
* @param result The Result to check
* @returns true if result is Ok, false if Err
*/
export declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
/**
* Tests if a Result is failed.
* @param result The Result to check
* @returns true if result is Err, false if Ok
*/
export declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
/**
* Tests if an unknown value is a valid Result.
* @param value The value to check
* @returns true if value is a Result (Ok or Err), false otherwise
*/
export declare function isResult<T = unknown, E = unknown>(value: unknown): value is Result<T, E>;
/**
* Tests if an unknown value is a valid Ok Result.
* @param value The value to check
* @returns true if value is an Ok Result, false otherwise
*/
export declare function isResultOk<_T = unknown, _E = unknown>(value: unknown): value is Ok<_T>;
/**
* Tests if an unknown value is a valid Err Result.
* @param value The value to check
* @returns true if value is an Err Result, false otherwise
*/
export declare function isResultErr<_T = unknown, E = unknown>(value: unknown): value is Err<E>;