UNPKG

mnotify-ts-sdk

Version:

Modern, zero-dependency TypeScript SDK for mNotify BMS API - Type-safe SMS, contacts, and account management with Railway-Oriented Programming

118 lines (117 loc) 3.42 kB
/** * Railway-oriented programming Result type for functional error handling * * This type represents the result of an operation that can either succeed with a value * or fail with an error, following the principles of railway-oriented programming * inspired by Rust's Result<T, E> type. * * @template T - The type of the success value * @template E - The type of the error value (defaults to Error) * * @example * ```typescript * // Success case * const success: Result<number> = ok(42); * * // Error case * const failure: Result<number> = err(new Error('Something went wrong')); * * // Pattern matching * const value = success.match({ * ok: (value) => value * 2, * err: (error) => 0 * }); * ``` */ export type Result<T, E = Error> = Ok<T, E> | Err<T, E>; /** * Success variant of Result */ export interface Ok<T, E> { readonly success: true; readonly value: T; /** * Returns true if the result is Ok */ isOk(): this is Ok<T, E>; /** * Returns true if the result is Err */ isErr(): this is Err<T, E>; /** * Maps the Ok value to a new value */ map<U>(fn: (value: T) => U): Result<U, E>; /** * Maps the error value (no-op for Ok) */ mapErr<F>(fn: (error: E) => F): Result<T, F>; /** * Chains another Result-returning operation */ andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>; /** * Chains another async Result-returning operation */ andThenAsync<U>(fn: (value: T) => Promise<Result<U, E>>): Promise<Result<U, E>>; /** * Returns the Ok value or throws the Err */ unwrap(): T; /** * Returns the Ok value or a default value */ unwrapOr(defaultValue: T): T; /** * Returns the Ok value or computes it from a function */ unwrapOrElse(fn: (error: E) => T): T; /** * Pattern match on the Result */ match<U>(matcher: { ok: (value: T) => U; err: (error: E) => U; }): U; } /** * Error variant of Result */ export interface Err<T, E> { readonly success: false; readonly error: E; isOk(): this is Ok<T, E>; isErr(): this is Err<T, E>; map<U>(fn: (value: T) => U): Result<U, E>; mapErr<F>(fn: (error: E) => F): Result<T, F>; andThen<U>(fn: (value: T) => Result<U, E>): Result<U, E>; andThenAsync<U>(_fn: (value: T) => Promise<Result<U, E>>): Promise<Result<U, E>>; unwrap(): T; unwrapOr(defaultValue: T): T; unwrapOrElse(fn: (error: E) => T): T; match<U>(matcher: { ok: (value: T) => U; err: (error: E) => U; }): U; } /** * Creates a successful Result */ export declare function ok<T, E = Error>(value: T): Result<T, E>; /** * Creates an error Result */ export declare function err<T, E = Error>(error: E): Result<T, E>; /** * Wraps a function that might throw in a Result */ export declare function tryCatch<T, E = Error>(fn: () => T, errorHandler: (error: unknown) => E): Result<T, E>; /** * Wraps an async function that might throw in a Result */ export declare function tryCatchAsync<T, E = Error>(fn: () => Promise<T>, errorHandler: (error: unknown) => E): Promise<Result<T, E>>; /** * Combines multiple Results into a single Result * Returns Ok with array of values if all are Ok, or the first Err encountered */ export declare function combine<T, E>(results: Result<T, E>[]): Result<T[], E>;