@antithrow/std
v2.0.0
Published
Non-throwing wrappers around standard globals using antithrow Result types
Maintainers
Readme
Why
Standard APIs like JSON.parse, fetch, and atob communicate failure by throwing.
@antithrow/std re-exports them as thin wrappers that return Settled or Result, so error handling stays type-safe and composable.
import { JSON, fetch, Response } from "@antithrow/std";
const config = JSON.parse<Config>(text)
.mapErr(() => new AppError("invalid config"));
const body = await fetch("https://api.example.com/data")
.andThen((res) => Response.json<Data>(res));Installation
bun add @antithrow/stdantithrow is a required peer/runtime dependency and will be installed automatically.
Usage
Every export mirrors the name of the global it wraps. If you need both versions, rename the import:
import { JSON as SafeJSON } from "@antithrow/std";
SafeJSON.parse("..."); // Settled<unknown, SyntaxError>
JSON.parse("..."); // throws on invalid inputJSON
import { JSON } from "@antithrow/std";
const parsed = JSON.parse<{ id: number }>('{"id": 1}');
// ok({ id: 1 })
const str = JSON.stringify({ id: 1 });
// ok('{"id":1}')
const circular: Record<string, unknown> = {};
circular["self"] = circular;
JSON.stringify(circular);
// err(TypeError)fetch
Non-2xx responses are Ok — only network errors and rejections become Err.
import { fetch } from "@antithrow/std";
const result = await fetch("https://example.com/api");
// Ok<Response> or Err<DOMException | TypeError>Response
Wraps Response body-reading methods (json, text, arrayBuffer, blob, formData).
import { fetch, Response } from "@antithrow/std";
const user = await fetch("https://api.example.com/user")
.andThen((res) => Response.json<User>(res));structuredClone
import { structuredClone } from "@antithrow/std";
structuredClone({ a: 1 }); // ok({ a: 1 })
structuredClone(() => {}); // err(DOMException)URI encoding / decoding
import { encodeURIComponent, decodeURIComponent } from "@antithrow/std";
encodeURIComponent("hello world"); // ok("hello%20world")
decodeURIComponent("%E0%A4%A"); // err(URIError)Base64
import { atob, btoa } from "@antithrow/std";
btoa("hello"); // ok("aGVsbG8=")
atob("!!!"); // err(DOMException)Reference
Full reference: https://antithrow.dev/docs/reference/std
