UNPKG

@jokoor/sdk

Version:
64 lines 1.86 kB
/** * Result type for API responses following the {data, error} pattern * Similar to Go's error handling approach */ /** * Result type that represents either successful data or an error * Only one of data or error will be present at a time */ export type Result<T> = { data: T; error: null; } | { data: null; error: string; }; /** * Helper function to create a successful result */ export declare function ok<T>(data: T): Result<T>; /** * Helper function to create an error result */ export declare function err<T>(error: string): Result<T>; /** * Type guard to check if a result is successful */ export declare function isOk<T>(result: Result<T>): result is { data: T; error: null; }; /** * Type guard to check if a result is an error */ export declare function isErr<T>(result: Result<T>): result is { data: null; error: string; }; /** * Extract data from a result or throw if it's an error * Useful for migrating from throw-based code */ export declare function unwrap<T>(result: Result<T>): T; /** * Extract data from a result or return a default value */ export declare function unwrapOr<T>(result: Result<T>, defaultValue: T): T; /** * Map a successful result to a new value */ export declare function map<T, U>(result: Result<T>, fn: (value: T) => U): Result<U>; /** * Chain results together - useful for sequential operations */ export declare function chain<T, U>(result: Result<T>, fn: (value: T) => Result<U>): Result<U>; /** * Convert a Promise that might throw into a Result */ export declare function fromPromise<T>(promise: Promise<T>): Promise<Result<T>>; /** * Convert multiple Results into a single Result containing an array * If any result is an error, returns that error */ export declare function all<T>(results: Result<T>[]): Result<T[]>; //# sourceMappingURL=result.d.ts.map