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.
What the endpoint does
Section titled “What the endpoint does”- 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). - Look the job up in the store.
- If the job is new, only
JobQueuedmakes sense. It creates the job and replies 201. Any other event for an unknown job is a 404. - 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.
How the pieces fit
Section titled “How the pieces fit”- Lesson 1:
Queued,Running,FailedandSucceededare separate types, so aRunningjob always has aworker. - Lesson 2:
JobandJobEventare discriminated unions, andnevermakesstatusOfexhaustive. - Lesson 3:
transitionis the only place that decides which moves are legal. - Lesson 4:
JobStorekeeps its map private behindgetandput. - Lesson 5:
parseBodyturnsunknowntext into aJobEventonce.receivenever looks at raw data. - Lesson 6: errors are classes with a
_tag, returned as values, and mapped to statuses.
Exercise
Section titled “Exercise”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.
Run it locally
Section titled “Run it locally”In the Relay project, receive is the whole handler. The server reads the body and passes it
through:
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:
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"}.
receiveparses once at the edge, then works only with typed values.- A new job comes from
JobQueuedalone. Every other move goes throughtransition. - Errors are tagged classes returned as values.
statusOfmaps all of them, exhaustively. - Pure logic takes its inputs as arguments, so the server is a thin shell around
receive.
Further reading
Section titled “Further reading”- Bun docs: Server:
Bun.serveand its handlers. - TypeScript 4.4 release notes:
unknownin catch variables.