UNPKG

bc-webclient-mcp

Version:

Model Context Protocol (MCP) server for Microsoft Dynamics 365 Business Central via WebUI protocol. Enables AI assistants to interact with BC through the web client protocol, supporting Card, List, and Document pages with full line item support and server

284 lines 8.02 kB
/** * Result<T, E> Type for Functional Error Handling * * Rust-inspired Result type that forces explicit error handling without exceptions. * All operations that can fail return Result<T, E> instead of throwing. * * @see https://doc.rust-lang.org/std/result/ */ // ============================================================================ // Constructor Functions // ============================================================================ export function ok(value) { return { ok: true, value }; } export function err(error) { return { ok: false, error }; } // ============================================================================ // Type Guards // ============================================================================ export function isOk(result) { return result.ok === true; } export function isErr(result) { return result.ok === false; } // ============================================================================ // Extraction Functions // ============================================================================ /** * Unwraps a Result, returning the value or throwing the error. * Use only when you're certain the result is Ok. * * @throws {E} The error if result is Err */ export function unwrap(result) { if (isOk(result)) { return result.value; } throw result.error; } /** * Unwraps a Result, returning the value or a default value. * Safe alternative to unwrap(). */ export function unwrapOr(result, defaultValue) { return isOk(result) ? result.value : defaultValue; } /** * Unwraps a Result, returning the value or computing a default from the error. */ export function unwrapOrElse(result, fn) { return isOk(result) ? result.value : fn(result.error); } /** * Returns the error if Result is Err, otherwise throws. * Use only when you're certain the result is Err. * * @throws {Error} If result is Ok */ export function unwrapErr(result) { if (isErr(result)) { return result.error; } throw new Error('Called unwrapErr on an Ok value'); } // ============================================================================ // Transformation Functions // ============================================================================ /** * Maps a Result<T, E> to Result<U, E> by applying a function to the Ok value. */ export function map(result, fn) { return isOk(result) ? ok(fn(result.value)) : result; } /** * Maps a Result<T, E> to Result<T, F> by applying a function to the Err value. */ export function mapErr(result, fn) { return isErr(result) ? err(fn(result.error)) : result; } /** * Applies a function to the Ok value and flattens the result. * Also known as 'flatMap' or 'bind'. */ export function andThen(result, fn) { return isOk(result) ? fn(result.value) : result; } /** * Chains operations, replacing Err with a new Result computed from the error. */ export function orElse(result, fn) { return isErr(result) ? fn(result.error) : result; } // ============================================================================ // Combining Functions // ============================================================================ /** * Combines multiple Results into a single Result containing an array of values. * If any Result is Err, returns the first error encountered. */ export function all(results) { const values = []; for (const result of results) { if (isErr(result)) { return result; } values.push(result.value); } return ok(values); } /** * Returns the first Ok result, or the last Err if all are Err. */ export function any(results) { if (results.length === 0) { throw new Error('Cannot call any() on empty array'); } let lastErr; for (const result of results) { if (isOk(result)) { return result; } lastErr = result; } return lastErr; } /** * Combines two Results into a Result containing a tuple. */ export function combine(r1, r2) { if (isErr(r1)) return r1; if (isErr(r2)) return r2; return ok([r1.value, r2.value]); } /** * Combines three Results into a Result containing a tuple. */ export function combine3(r1, r2, r3) { if (isErr(r1)) return r1; if (isErr(r2)) return r2; if (isErr(r3)) return r3; return ok([r1.value, r2.value, r3.value]); } // ============================================================================ // Async Functions // ============================================================================ /** * Maps an async Result with an async function. */ export async function mapAsync(result, fn) { return isOk(result) ? ok(await fn(result.value)) : result; } /** * Chains async operations. */ export async function andThenAsync(result, fn) { return isOk(result) ? fn(result.value) : result; } /** * Wraps an async operation that might throw into a Result. */ export async function fromPromise(promise) { try { const value = await promise; return ok(value); } catch (error) { return err(error instanceof Error ? error : new Error(String(error))); } } /** * Wraps an async operation with a custom error mapper. */ export async function fromPromiseWith(promise, errorMapper) { try { const value = await promise; return ok(value); } catch (error) { return err(errorMapper(error)); } } // ============================================================================ // Utility Functions // ============================================================================ /** * Wraps a function that might throw into a Result. */ export function fromThrowable(fn) { try { return ok(fn()); } catch (error) { return err(error instanceof Error ? error : new Error(String(error))); } } /** * Wraps a function with a custom error mapper. */ export function fromThrowableWith(fn, errorMapper) { try { return ok(fn()); } catch (error) { return err(errorMapper(error)); } } /** * Executes a function on the Ok value without transforming the Result. * Useful for side effects like logging. */ export function inspect(result, fn) { if (isOk(result)) { fn(result.value); } return result; } /** * Executes a function on the Err value without transforming the Result. * Useful for error logging. */ export function inspectErr(result, fn) { if (isErr(result)) { fn(result.error); } return result; } /** * Matches on a Result, executing one of two functions. * Forces explicit handling of both Ok and Err cases. */ export function match(result, handlers) { return isOk(result) ? handlers.ok(result.value) : handlers.err(result.error); } /** * Async version of match. */ export async function matchAsync(result, handlers) { return isOk(result) ? handlers.ok(result.value) : handlers.err(result.error); } // ============================================================================ // Partition Functions // ============================================================================ /** * Partitions an array of Results into two arrays: one with Ok values, one with Err values. */ export function partition(results) { const okValues = []; const errValues = []; for (const result of results) { if (isOk(result)) { okValues.push(result.value); } else { errValues.push(result.error); } } return { ok: okValues, err: errValues }; } /** * Filters an array of Results, keeping only Ok values. */ export function filterOk(results) { return results .filter((r) => isOk(r)) .map(r => r.value); } /** * Filters an array of Results, keeping only Err values. */ export function filterErr(results) { return results .filter((r) => isErr(r)) .map(r => r.error); } //# sourceMappingURL=result.js.map