@golemcloud/golem-ts
Version:
A library that help writing Golem programs by providing higher level wrappers for Golem's runtime APIs, including functions for defining and performing operations transactionally.
240 lines (239 loc) • 7.52 kB
JavaScript
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Upstream docs about the result type https://bytecodealliance.github.io/jco/wit-type-representations.html#result-result
const prototype = {
/**
* Returns `true` if the result is successful, `false` otherwise
* @example.
* Result.ok(123).isOk() // true
* @example
* Result.err('error').isOk() // false
*/
isOk,
/**
* Returns `true` if the result is an error, `false` otherwise
* @example
* Result.err('error').isOk() // false
* @example.
* Result.ok(123).isOk() // true
*/
isErr,
/**
* Returns the successful value of the result or throws the error value.
* This should be used when returning from a component function that expects to receive a result.
* See https://bytecodealliance.github.io/jco/wit-type-representations.html#results-in-context-function-return-values for more details.
* @example Returns the payload of a successful result.
* Result.ok(123).unwrapForWit() // 123
* @example Throws the payload of a failed result.
* Result.err('error').unwrapForWit() // throws 'error'
*/
unwrapForWit,
/**
* Returns `this.value` if `this` is a successful result, otherwise throws a TypeError.
* @example Returns the payload of a successful result.
* Result.ok(123).unwrap() // 123
* @example Throws a TypeError for a failed result.
* Result.err('error').unwrap() // throws TypeError
*/
unwrap,
/**
*
* Returns `this.error` if `this` is a failed result, otherwise throws a TypeError.
* @example Throw a TypeError for a successful result.
* Result.ok(123).unwrapErr() // throws TypeError
* @example Returns the payload of a failed result
* Result.err('error').unwrap() // 'error'
*/
unwrapErr,
/**
* Returns the payload of the result.
* @example Returns the payload of a successful result.
* Result.ok(123).toUnion() // 123
* @example Returns the payload of a failed result.
* Result.err('error').toUnion() // 'error'
*/
toUnion,
/**
* Return the result of applying one of the given functions to the payload.
* @example
* Result.ok(123).match((x) => x * 2, (x: string) => x + '!') // 246
* Result.err('error').match((x: number) => x * 2, (x) => x + '!') // 'error!'
*/
match,
/**
* Creates a Result value by modifying the payload of the successful result using the given function.
* @example
* Result.ok(123).map((x) => x * 2) // Result.ok(246)
* Result.err('error').map((x: number) => x * 2) // Result.err('error')
*/
map,
/**
* Creates a Result value by modifying the payload of the failed result using the given function.
* @example
* Result.ok(123).mapError((x: string) => x + '!') // Result.ok(123)
* Result.err('error').mapError((x) => x + '!') // Result.err('error!')
*/
mapError,
/**
* Calls the given function with the payload of the result and returns the result unchanged.
*/
tap,
/**
* Maps the payload of the successful result and flattens the nested Result type.
* @example
* Result.ok(123).flatMap((x) => Result.ok(x * 2)) // Result.ok(246)
* Result.ok(123).flatMap((x) => Result.err('error')) // Result.err('error')
* Result.err('error').flatMap((x: number) => Result.ok(x * 2)) // Result.err('error')
* Result.err('error').flatMap((x) => Result.err('failure')) // Result.err('error')
*/
flatMap,
/**
* Flattens the nested Result type.
* @example
* Result.ok(Result.ok(123)).flatten() // Result.ok(123)
* Result.ok(Result.err('error')).flatten() // Result.err('error')
* Result.err('error').flatten() // Result.err('error')
*/
flatten,
/**
* Perform a safe cast of the error type to the given class. If the payload of the failed result is not instance of constructor, throws TypeError.
* @example
* const result: Result<number, Error> = Result.tryCatch(() => {
* if (Math.random() >= 0) {
* throw new Error('error')
* } else {
* return 123
* }
* }).assertErrorInstanceOf(Error)
*/
assertErrorInstanceOf,
};
export var Result;
(function (Result) {
/**
* Create a successful result.
*/
function ok(value) {
return withPrototype({ tag: "ok", val: value }, prototype);
}
Result.ok = ok;
/**
* Create an error result.
*/
function err(error) {
return withPrototype({ tag: "err", val: error }, prototype);
}
Result.err = err;
/**
* Create a result from a function that may throw an error.
*/
function tryCatch(f) {
try {
return ok(f());
}
catch (error) {
return err(error);
}
}
Result.tryCatch = tryCatch;
function fromNullish(value) {
return value != null ? Result.ok(value) : Result.err(value);
}
Result.fromNullish = fromNullish;
function all(results) {
const values = [];
for (const result of results) {
if (result.isErr())
return result;
values.push(result.val);
}
return Result.ok(values);
}
Result.all = all;
})(Result || (Result = {}));
function withPrototype(target, prototype) {
return Object.assign(Object.create(prototype), target);
}
function isOk() {
return this.tag === "ok";
}
function isErr() {
return this.tag === "err";
}
function unwrapForWit() {
if (this.isOk())
return this.val;
else
throw this.val;
}
function unwrap() {
if (this.isOk())
return this.val;
else
throw new TypeError(`unwrap·called·on·Err·result:·${this.val}`);
}
function unwrapErr() {
if (this.isOk())
throw new TypeError(`unwrapErr·called·on·Ok·result:·${this.val}`);
else
return this.val;
}
function toUnion() {
if (this.isOk())
return this.val;
else
return this.val;
}
function match(f, g) {
if (this.isOk())
return f(this.val);
else
return g(this.val);
}
function map(f) {
if (this.isErr())
return this;
else
return Result.ok(f(this.val));
}
function mapError(f) {
if (this.isOk())
return this;
else
return Result.err(f(this.val));
}
function tap(f) {
if (this.isOk())
f(this.val);
return this;
}
function flatMap(f) {
if (this.isErr())
return this;
else
return f(this.val);
}
function flatten() {
if (this.isErr())
return this;
else
return this.val;
}
function assertErrorInstanceOf(constructor) {
if (this.isOk())
return this;
if (this.val instanceof constructor)
return this;
throw new TypeError(`Assertion failed: Expected error to be an instance of ${constructor.name}.`);
}