@awbx/cronix-adapter-fastify
v0.9.1
Published
Fastify adapter for @awbx/cronix-sdk. Lifts cron.handle into a Fastify route handler with a raw-body content-type parser for HMAC verification.
Readme
@awbx/cronix-adapter-fastify
Fastify adapter for @awbx/cronix-sdk. Lifts a Web Fetch handler into a Fastify route handler with a raw-body content-type parser so HMAC verification has the canonical bytes.
Install
pnpm add @awbx/cronix-sdk @awbx/cronix-adapter-fastifyfastify is a peer dep — bring your own.
Usage
import Fastify from "fastify";
import { createCron, MANIFEST_PATH, TRIGGER_PATH_PREFIX } from "@awbx/cronix-sdk";
import { handle, rawBody } from "@awbx/cronix-adapter-fastify";
const cron = createCron({
app: "billing",
baseUrl: "https://billing.internal.example.com",
secret: process.env.CRON_SECRET!,
});
cron.register({ name: "reconcile", schedule: "*/15 * * * *" }, async (ctx) => {
// ...
});
const app = Fastify();
rawBody(app); // ← required, register first
app.all(MANIFEST_PATH, handle((req) => cron.handle(req)));
app.all(`${TRIGGER_PATH_PREFIX}:name`, handle((req) => cron.handle(req)));
await app.listen({ port: 3000 });API
rawBody(app)
Replaces every content-type parser on the Fastify instance with a wildcard parseAs: "buffer" parser, so request bodies arrive as Buffer and HMAC verification sees the bytes-as-sent. Call once per app, before registering cronix routes.
If you need Fastify's built-in JSON parsing for non-cronix routes, register cronix on a sub-app via app.register and call rawBody only on the sub-app.
handle(fn)
Returns a RouteHandlerMethod that:
- Turns the Fastify
reqinto a WebRequest(method, headers, raw body buffer, full URL). - Awaits
fn(request). - Pipes the returned
Responseback to the Fastify reply.
fn is any (req: Request) => Response | Promise<Response> — usually cron.handle, but you can wrap it for logging, auth, fan-out across multiple cron instances, etc.
NestFastify
NestJS apps running on Fastify (@nestjs/platform-fastify) can use this adapter directly — the underlying FastifyRequest / FastifyReply shapes match what the adapter expects.
