@axman/remote
v0.0.2
Published
Remote data helper types and utilities for TypeScript
Readme
@axman/remote
Typed helpers for remote data state: discriminated Remote<T> union with pending, error, success, plus match, all and map utilities.
Install
npm i @axman/remote
# or
pnpm add @axman/remote
# or
bun add @axman/remoteRemote type
Remote<T> is a discriminated union with status ∈ "pending" | "error" | "success" and convenient flags isPending/isError/isSuccess.
import { type Remote, error, pending, success } from "@axman/remote";
const a: Remote<number> = pending();
const b: Remote<number> = error(new Error("boom"));
const c: Remote<number> = success(42);match(remote, cases)
Pattern-match on Remote<T> in a type-safe way.
import { error, match, pending, success } from "@axman/remote";
const out = match(success({ id: 1 }), {
pending: () => "loading",
error: (e) => `error: ${e.message}`,
success: (data) => `ok: ${data.id}`,
});
// => 'ok: 1'all(...remotes)
Combine multiple Remotes into one. Precedence: any error → error; else if any pending → pending; otherwise success with a tuple of data.
import { all, error, pending, success } from "@axman/remote";
all(error(new Error("x")), pending(), success(1));
// => { status: 'error', ... }
all(pending(), success(1));
// => { status: 'pending', ... }
const r = all(success(1), success("x" as const));
// r.data has type [number, 'x']map(remote, fn)
Map data of a successful Remote<T> to Remote<R>; passes through pending and error unchanged.
import { type Remote, error, map, pending, success } from "@axman/remote";
map(success(2), (n) => n * 10);
// => { status: 'success', data: 20, ... }
map(pending() as Remote<number>, (n) => n * 10);
// => { status: 'pending', ... }
map(error(new Error("x")) as Remote<number>, (n) => n * 10);
// => { status: 'error', ... }License
MIT
