TypeScript SDK (@devitek/mailsink)
@devitek/mailsink is the typed client of every Mailsink HTTP surface a test or a tool
needs: Admin API, Capture API, overrides, suppressions, webhooks, logs and the live
stream. It is the base of the Playwright and Vitest helpers and of the CLI. Install, quick
start and recipes (magic link, one-time code, bounce): the
package README. Decisions: D235 to D239.
Package
Section titled “Package”- Lives in
packages/sdk, published as@devitek/mailsink(#128 publishes it), with the version of the server: the rootpackage.json(D181), bumped inpackages/sdk/package.jsonby release-please (extra-files). - ESM only, plain
fetch, no Node-only API: Node 20+ and browsers.src/is typechecked without Node typings (tsconfig.json); tests usetsconfig.test.json. pnpm --filter @devitek/mailsink build(tsdown) writesdist/index.jsanddist/index.d.ts. The private@mailsink/contractsis bundled into both (tsconfig.build.jsonlets the declaration build see it);zodis the only runtime dependency. In the monorepo,exportspoints tosrc/index.ts;publishConfig.exportspoints the published package todist/.
Client
Section titled “Client”createMailsink({ url, // base URL; a path prefix is kept (https://ci.example.com/mailsink) key, // mskw_ workspace key or msk_ connection key: workspace calls adminKey, // admin calls; workspace calls too, with `workspace` workspace, // id or slug, sent as X-Mailsink-Workspace fetch, // custom fetch (default: globalThis.fetch) headers, // extra headers on every call validate, // parse every answer with its contract (default false)})client.with(patch) returns a client with some options changed, typically
admin.with({ key: created.workspaceKey }) after creating a workspace. A call without the
credential it needs fails with unauthorized before any request.
Types: request bodies are the z.input of their contract, answers the z.output. List
queries take the output type with every key optional; since, until and before also
take a Date, arrays are sent comma-separated. Every type is exported (MessageDetail,
StreamEvent, CreateWorkspaceInput, ErrorCode…) as a plain object type: the package
declarations carry no Zod schema (D260). In this repository, they are generated from the
contracts by pnpm --filter @devitek/mailsink generate:types; pnpm typecheck fails when
a contract changed without a regeneration.
client.now() is the current time of the server, estimated from the Date header of the
answers received so far (see « Waiting for a message »).
Namespaces
Section titled “Namespaces”| Namespace | Calls | Route |
|---|---|---|
admin.workspaces |
create, list, get, update, delete, rotateKey |
/admin/workspaces[/:ref[/key/rotate]] |
admin.connections |
create(ref, input), update(ref, id, patch), delete, rotate |
/admin/workspaces/:ref/connections[/:id[/rotate]] |
admin.templates |
list(ref), create, get(ref, templateId), update, delete |
/admin/workspaces/:ref/templates[/:templateId] |
admin.logs |
list, get |
/admin/logs/api[/:id] |
messages |
list, get, waitFor, raw, attachment, purge, triggerEvent, seedDemo |
/capture/messages…, /capture/demo |
overrides |
get, activate(config, { connectionId?, ttl? }), reset({ connectionId? }), with(config, fn, options?) |
/capture/override[/reset] |
suppressions |
list, delete(email), clear() |
/capture/suppressions |
webhooks |
list, create, get, update, delete, preview, test; deliveries.list, deliveries.get, deliveries.replay |
/capture/webhooks… |
logs |
api.list, api.get, smtp.list, smtp.get |
/capture/logs/{api,smtp}[/:id] |
stream |
subscribe(handler, options?) |
/capture/stream |
ref is a workspace id or slug. messages.raw(id) answers the EML as text;
messages.attachment(id, attachmentId) answers { filename, contentType, data }
(Uint8Array). overrides.with resets the same scope in finally, so a failing callback
never leaves an override behind.
Waiting for a message
Section titled “Waiting for a message”messages.waitFor(filters, { timeout = 10_000, since, signal }) long polls
GET /capture/messages?wait=true and resolves the full MessageDetail of the earliest
matching message of the page. Filters are those of messages.list (to, from,
subject, tag, status, connectionId, q…).
sincedefaults to the time of the call: a message stored beforewaitForis called is not seen. Start the wait before the action and await it after, or takesincebefore the action withmailsink.now().sinceis read on the server clock. Every answer carries aDateheader (one second precision);mailsink.now()corrects the client clock by the skew those headers prove, and only by it (D261). Before the first answer, or in a browser calling another origin (the header is not exposed to cross-origin scripts), it is the client clock: against a remote server with a skewed clock, make one call first (anylist) or passsince.- A
timeoutover 30 s (the server maximum) is split into successive long polls. - No match:
MailsinkErrorwait_timeout, its message anddetailscarry the filters.
Errors
Section titled “Errors”MailsinkError carries code, status, requestId and details. Codes: the server
ErrorCode (D40: unauthorized, forbidden, validation_error, not_found,
conflict, idempotency_mismatch, rate_limited, payload_too_large, chaos_injected,
wait_timeout, internal_error), plus network_error (no answer, status null) and
invalid_response (not JSON, or a failed validate). An error answer that is not the
contract body (a proxy page) gets the code of its status. isMailsinkError(error, code?)
narrows.
Stream
Section titled “Stream”const subscription = mailsink.stream.subscribe(handler, { types, // StreamEventType[]; workspace.deleted always comes connectionId, // keep one connection's message and log frames lastEventId, // resume after an evt_ id retryMs, // reconnection delay (default: the server retry, 3 s) signal, onOpen, onError,})await subscription.ready // first connection opensubscription.lastEventId // last evt_ id receivedsubscription.close()await subscription.doneThe SDK reads the SSE body over fetch with its own parser (no EventSource, so the
Authorization header works in browsers too). The handler gets each StreamEvent and its
frame { id } (null for derived frames: statuses, logs, deliveries, overrides). When
the connection drops, it reconnects with Last-Event-ID; the server replays the frames
with an id missed meanwhile (message.created, message.event, up to 500). A 400, 401,
403, 404 or 422 stops it (ready and done reject); a workspace.deleted frame ends it;
handler errors go to onError and the stream goes on.
Helpers
Section titled “Helpers”extractLinks(message): every http(s) link, once each, in order: the links the server extracted, thehrefs of the HTML body (entities decoded), the URLs of the text body.findLink(message, matcher): the first link matching a substring, aRegExpor a predicate on the parsedURL;nullwhen none does.extractCode(message, { length = 6, pattern? }): the first standalone run oflengthdigits in the subject, the text body, then the visible text of the HTML body (URLs skipped); withpattern, its first capture group (or whole match).nullwhen none.
They accept any object with subject, text, html and links, so a MessageDetail
fits.
packages/sdk/test runs the SDK against the real routes of the app: test/server.ts
imports every apps/mailsink/src/routes/{admin,capture,v1}.*.ts file, derives its path
from the file name and serves the handlers on a loopback port, so the SDK goes through
fetch, Node streams and real sockets (the stream test drops them to check the
reconnection). The runtime is the app test runtime on both drivers (describeDb); the
recipe suite also runs the delivery simulation worker.