Read: API client and superjson decoder
client.ts and superjson.ts: the GET client for a Seed site's /api endpoints and the hand-written decoder for its superjson envelope.

src/read/client.ts is the only place the read, discuss and profile packages build a read request to a Seed site. src/read/superjson.ts decodes the {json, meta} envelope those endpoints answer with. Both are small and have no state.

Files

File

Role

src/read/client.ts

SeedError, apiUrl, api, siteConfig

src/read/superjson.ts

decodeEnvelope

src/read/__tests__/client.test.ts

4 Bun tests

client.ts

Export

What it does

SeedError

An Error subclass whose message is safe to show to a person. Callers such as Discuss loading show SeedError messages verbatim and replace anything else with a generic line.

apiUrl(server, key, params)

Builds {server}/api/{key}?…. Trailing slashes on server are trimmed. undefined and null parameters are dropped. Strings, numbers and booleans are written as is; any other value is JSON.stringify-ed (this is how Query's includes array and ListComments' targetId object travel).

api(server, key, params)

Fetches that URL and returns decodeEnvelope(body).

siteConfig(server)

GET {server}/hm/api/config, plain JSON, typed as {registeredAccountUid?: string}.

The private getJson (L12-L25) sends Accept: application/json with exactTimeout: 15000 (the Exact fetch deadline, see The data module runtime). Network access is allowed by the net.fetch https: grant in app.ts, see Grants and capabilities.

Error messages

Condition

Message

fetch throws (offline, timeout, refused grant)

Couldn't reach the server.

Status is not 2xx

The server answered {status}.

Body is not JSON, or the envelope fails to decode

The server sent something unreadable.

superjson.ts

The file's header explains why it exists: "Exact rejects superjson's is-what dependency (dynamic Function constructor)." The decoder is hand-written and fails closed: "Unknown types fail closed rather than silently changing the response's meaning" (L1-L3).

decodeEnvelope(body):

    Returns body unchanged when it is not an object with a json key (plain JSON is still accepted).

    Walks meta.values, a tree whose leaves are type annotations, and replaces the value at each path.

    Applies meta.referentialEqualities (an object of source path to alias paths). An array form (cyclic root aliases) throws Unsupported API aliases.

Supported annotations (transform):

Annotation

Result

undefined

undefined

bigint (string value)

BigInt

Date (string value)

Date, invalid dates throw

number with NaN, Infinity, -Infinity, -0

the number

["typed-array", "Uint8Array"] (array of 0 to 255)

Uint8Array

anything else

throws Unsupported API annotation

Paths are dot separated with \. as an escaped dot. __proto__, constructor and prototype segments are rejected, and every step must be an own property (pathOf, at), so metadata cannot reach or modify a prototype.

Tests

client.test.ts covers: decoding dates, bigint, undefined and bytes while still accepting plain JSON; an invalid annotation surfacing as The server sent something unreadable.; root values, escaped names and shared references; and the prototype guards.

Known limits

    GET only. Writes go through a direct fetch in each package, see Publishing signed blobs.

    No retry and no caching here. Caches live in load.ts and discuss/load.ts.

    Me does not use this client: it has its own fetchJson with shorter timeouts.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime