Skip to main content

Generics in run

import { run } from "trybox";

type Item = { id: string };
const r = await run<Item>(() => fetch("/api").then((x) => x.json()));

Result helpers in runAll

import {
  runAll,
  isSuccess,
  type SuccessResult,
  type ErrorResult,
} from "trybox";

const rs = await runAll<number>([async () => 1]);
const ok: SuccessResult<number>[] = rs.filter(isSuccess);
const ko: ErrorResult<any>[] = rs.filter((r) => r.status === "error");

Rules and types

import trybox, { errorRule } from "trybox";

type MyError = { code: "HTTP"; message: string; status?: number };

const runner = trybox({
  rules: [
    errorRule
      .when(
        (e): e is { status: number } => typeof (e as any)?.status === "number"
      )
      .toError<MyError>((e) => ({
        code: "HTTP",
        message: "failed",
        status: (e as any).status,
      })),
  ],
});