Extensions
Another service, without a package
Extensions
services.md

Another service, without a package

What a JavaScript program would npm install for (a payment service's SDK, a mail client, an S3 client, a model's SDK) is, in Mote, a few fns of ordinary Mote over the back end's own means: net.send and net.stream, secret, crypto, a server route for the service's webhooks, and a job fn for what must not be lost. The extensions that ship with Mote are that, written out; this page is how to write one for a service that is not there, so that extern js is the last resort. Back to the map.

What you would npm install, and what to use#

The pattern#

Examplea whole program
// Acme, a service with an HTTP API: an extension of forty lines.
wants fn acmeDone(job: AcmeJob): bool = true
shape AcmeJob
id: string
status: string
result: string
fn acmeBase(): string = env("ACME_API") == "" ? "https://api.acme.example" : env("ACME_API")
// The one place that talks to Acme: the key as a bearer, tried again on 429 and 5xx with
// the same idempotency key, and its refusal as its own sentence with a kind.
fn acmeCall(method: string, path: string, body: json, key: string): json
let a = net.send(method, `${acmeBase()}${path}`, {auth: {bearer: secret("ACME_KEY")}, body, idempotency: key == "" ? uid() : key, retries: 2, timeout: 30000}) else err => fail("Acme did not answer.", {kind: "offline"})?
if a.ok return a.json
let kind = a.status == 401 ? "auth" : a.status == 429 ? "limited" : a.status >= 500 ? "acme" : "invalid"
fail(`Acme: ${a.json?.error?.message ?? a.status}`, {kind, data: {status: a.status}})?
return none
// What Acme sends, held to the shape: typed, or a failure that says where it differs.
fn acmeStart(input: string, key: string): AcmeJob = check(acmeCall("POST", "/v1/jobs", {input}, key)?, AcmeJob)?
// Acme signs its webhooks: an HMAC-SHA256 of the body under the endpoint's secret, in hex.
// Each event is done once, by a job, however often Acme sends it.
server route POST "/acme/webhook"(req) requires anyone limit 600 per minute by ip
let event = webhook.check(req, "hex", secret("ACME_WEBHOOK_SECRET"), {header: "acme-signature"})?
webhook.once(`acme:${event.id}`)
acmeHeard(check(event, AcmeJob)?)
return {status: 200, json: {received: true}}
job fn acmeHeard(job: AcmeJob) retries=5
acmeDone(job)?
server fn paint(input: string): string requires signedIn limit 10 per hour
return acmeStart(input, "")?.id
test "acme: a job is begun with the key, and Acme's refusal is its words"
given secret "ACME_KEY" is "acme-test-key"
given net "https://api.acme.example/v1/jobs" answers {status: 200, json: {id: "j1", status: "queued", result: ""}}
trusted
expect acmeStart("a lamp", "lamp-1").id == "j1"
expect sent(net)[0].headers["authorization"] == "Bearer acme-test-key"
expect sent(net)[0].headers["idempotency-key"] == "lamp-1"
text "Acme"

A service with an OpenAPI file#

When there is no HTTP API#

esc
1072 sections of 111 pages, ranked as you type.A heading's words count for more than what stands under it.
↑↓move
↵open
ranked by BM25, in the page