Skip to content

Capstone: the webhook endpoint

Relay can now describe a job, move it between states and parse a webhook body. This lesson joins them into the one function an HTTP server calls: receive(store, source, text). It takes the source name and the raw body text, and returns a status and a JSON body.

  1. Parse the text into a JobEvent. A bad source, bad JSON or a bad payload ends here with 404, 400 or 422 (status codes Relay picks for itself).
  2. Look the job up in the store.
  3. If the job is new, only JobQueued makes sense. It creates the job and replies 201. Any other event for an unknown job is a 404.
  4. If the job exists, apply the event with transition. A refused transition is a 409. Otherwise store the new job and reply 200 with it.

Every failure is a value with a _tag, so one statusOf turns any of them into a response, and the compiler checks that none is missing.

  • Lesson 1: Queued, Running, Failed and Succeeded are separate types, so a Running job always has a worker.
  • Lesson 2: Job and JobEvent are discriminated unions, and never makes statusOf exhaustive.
  • Lesson 3: transition is the only place that decides which moves are legal.
  • Lesson 4: JobStore keeps its map private behind get and put.
  • Lesson 5: parseBody turns unknown text into a JobEvent once. receive never looks at raw data.
  • Lesson 6: errors are classes with a _tag, returned as values, and mapped to statuses.

The starter has the types, transition, JobStore, the parse errors and parseBody, all working. You add UnknownJob and InvalidTransition, the ReceiveError union, an exhaustive statusOf and receive. The rules are in the comment at the top.

In the Relay project, receive is the whole handler. The server reads the body and passes it through:

src/server.ts
interface Reply {
readonly status: number
readonly body: string
}
declare class JobStore {}
declare function receive(store: JobStore, source: string, text: string): Reply
const store = new JobStore()
Bun.serve({
port: 3000,
fetch: async (req) => {
const source = new URL(req.url).pathname.split("/").at(-1) ?? ""
const reply = receive(store, source, await req.text())
return new Response(reply.body, {
status: reply.status,
headers: { "content-type": "application/json" },
})
},
})

Send it an event:

Terminal window
bun src/server.ts &
curl -i localhost:3000/relay -d '{"_tag":"JobQueued","jobId":"j1","at":1}'
curl -i localhost:3000/relay -d '{"_tag":"JobStarted","jobId":"j1","worker":"w1","at":2}'
curl -i localhost:3000/relay -d '{"_tag":"JobStarted","jobId":"j1","worker":"w1","at":3}'

The first reply is 201. The second is 200. The third is 409, with {"error":"InvalidTransition","message":"cannot start a running job"}.

  • receive parses once at the edge, then works only with typed values.
  • A new job comes from JobQueued alone. Every other move goes through transition.
  • Errors are tagged classes returned as values. statusOf maps all of them, exhaustively.
  • Pure logic takes its inputs as arguments, so the server is a thin shell around receive.