UNPKG

@synet/patterns

Version:

Robust, battle-tested collection of stable patterns used in Synet packages

105 lines (104 loc) 3.8 kB
/** * A simple and versatile Result pattern implementation * Used for representing the outcome of operations that might fail */ export declare class Result<T> { private readonly _isSuccess; private readonly _value?; private readonly _error?; private constructor(); /** * Returns whether this Result represents a successful operation */ get isSuccess(): boolean; /** * Returns whether this Result represents a failed operation */ get isFailure(): boolean; /** * Returns the success value * @throws Error if called on a failed result */ get value(): T; /** * Returns the error details if this is a failure result */ get error(): { message: string; cause?: Error; data?: unknown[]; } | undefined; /** * Returns the error message if this is a failure result */ get errorMessage(): string | undefined; /** * Returns the error cause if this is a failure result */ get errorCause(): Error | undefined; /** * Creates a successful result with a value * @param value The success value */ static success<T>(value: T): Result<T>; /** * Creates a failure result with a message, optional cause, and optional context data * @param message Error message describing what went wrong * @param cause Optional underlying error that caused the failure * @param data Optional additional context data for debugging */ static fail<T>(message: string, cause?: Error, ...data: unknown[]): Result<T>; /** * Executes the given callback if this is a success result * @param fn Function to execute with the success value * @returns This result, for method chaining */ onSuccess(fn: (value: T) => void): Result<T>; /** * Executes the given callback if this is a failure result * @param fn Function to execute with the error details * @returns This result, for method chaining */ onFailure(fn: (message: string, cause?: Error, data?: unknown[]) => void): Result<T>; /** * Maps a successful result to another type using the provided function * @param fn Mapping function to apply to the success value * @returns A new Result with either the mapped value or the original error */ map<U>(fn: (value: T) => U): Result<U>; /** * Maps a successful result to another Result using the provided function * @param fn Mapping function that returns a new Result * @returns The new Result or the original error */ flatMap<U>(fn: (value: T) => Result<U>): Result<U>; /** * Recovers from a failure by providing a default value * @param defaultValue Value to use if this result is a failure * @returns A success result with either the original or default value */ recover(defaultValue: T): Result<T>; /** * Attempts to recover from a failure by calling a function * @param fn Function that provides a recovery value * @returns A success result with either the original or recovered value */ recoverWith(fn: (error: { message: string; cause?: Error; data?: unknown[]; } | undefined) => T): Result<T>; /** * Ensures a condition is met or fails with the provided message * @param condition Predicate that must be true * @param message Error message if condition fails * @returns This result if successful and condition is met, otherwise a failure */ ensure(condition: (value: T) => boolean, message: string): Result<T>; /** * Checks if the result is null * @returns true if the result is null, false otherwise * */ isNull(): boolean; combine<U>(results: Result<U>[]): Result<U[]>; }