A small in-memory notes API that shows how the pieces of pure-fx fit together:
Serverfor routing (GET,POST,PATCH,DELETEwith typed:idparams).Schemato validate request bodies, with the failing field path in the error message.ErrTypefor typed errors (ValidationFailedmaps to 400,NoteNotFoundmaps to 404).Resultfor lookups (Result.fromNullable) andTaskfor the async body read and the handler pipelines.
createApp() is exported and builds a fresh store on every call. The server only starts
listening when the file is run directly.
Build the library first, because the example imports @igorjs/pure-fx from the local dist/
through the package self-reference. No install step is needed inside this folder.
pnpm run build
node examples/rest-api/server.mjs
bun examples/rest-api/server.mjsThe server prints listening on port <n>. Set PORT=3000 to pick the port, otherwise a free
port is chosen.
BASE=http://127.0.0.1:3000
curl -i -X POST $BASE/notes -H 'content-type: application/json' -d '{"title":"first"}'
curl $BASE/notes
curl $BASE/notes/1
curl -X PATCH $BASE/notes/1 -H 'content-type: application/json' -d '{"body":"hello"}'
curl -i -X DELETE $BASE/notes/1
# Typed errors
curl -i $BASE/notes/99 # 404 NOTE_NOT_FOUND
curl -i -X POST $BASE/notes -H 'content-type: application/json' -d '{"title":5}' # 400 VALIDATION_FAILEDError bodies look like {"error":{"tag":"NoteNotFound","code":"NOTE_NOT_FOUND","message":"..."}}.
pnpm run test:examplesThis starts the example on Node and on Bun, runs CRUD and invalid input requests against each,
and stops it. A missing runtime is skipped locally and fails the run with --ci or CI=1.