Skip to Content

Fastify

Fastify ships as one plugin. Unlike Express it registers the socket itself, so there is a single call rather than two.

Install

npm install @luno-oss/core @luno-oss/fastify fastify

Mount it

import Fastify from 'fastify' import { createLuno, memoryStore } from '@luno-oss/core' import { lunoFastify } from '@luno-oss/fastify' const luno = createLuno({ store: memoryStore(), secret: process.env.LUNO_SECRET! }) const app = Fastify() await app.register(lunoFastify, { luno }) await app.listen({ port: 3000 })

That registers POST /enroll, POST /enroll/status and WS /ws.

API

interface LunoFastifyOptions { luno: Luno paths?: { enroll?: string; enrollStatus?: string; ws?: string } }

Mount under a prefix using Fastify’s own option:

await app.register(lunoFastify, { luno }, { prefix: '/api/luno' })

The plugin registers @fastify/websocket for you if it is not already registered, so there is nothing to wire up by hand. If you register it yourself first — with your own options — the plugin detects that and leaves it alone.

The body shim

Fastify parses a JSON body ahead of the handler, so the HttpRequest shim just hands it back:

{ method: request.method, url: request.url, json: async () => request.body ?? {} }

Compare with Express, which may have to read the stream itself. The core accepting a structural { method, url, json() } is what lets both be three lines.

Next