Skip to main content
Glama
result.ts1.68 kB
/** * Result Pattern Utilities * Helper functions for working with Result<T,E> types */ import { type DomainError, Result, ok, err } from "../types/domain.js"; /** * Maps a Result's success value through a transformation function. */ export function mapResult<T, U, E>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> { if (result.ok) { return ok(fn(result.value)); } return result; } /** * Chains Result operations together (flatMap/bind). */ export function chainResult<T, U, E>( result: Result<T, E>, fn: (value: T) => Result<U, E> ): Result<U, E> { if (result.ok) { return fn(result.value); } return result; } /** * Unwraps a Result, throwing if error. */ export function unwrap<T, E>(result: Result<T, E>): T { if (result.ok) { return result.value; } throw result.error; } /** * Unwraps a Result with a default value if error. */ export function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T { return result.ok ? result.value : defaultValue; } /** * Wraps a potentially throwing function in a Result. */ export function tryCatch<T>(fn: () => T): Result<T, DomainError> { try { return ok(fn()); } catch (error) { const message = error instanceof Error ? error.message : String(error); return err({ type: "Internal", message }); } } /** * Wraps an async function in a Result. */ export async function tryCatchAsync<T>(fn: () => Promise<T>): Promise<Result<T, DomainError>> { try { const value = await fn(); return ok(value); } catch (error) { const message = error instanceof Error ? error.message : String(error); return err({ type: "Internal", message }); } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hummbl-dev/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server