@invariant-app/runtime-node
v0.4.0
Published
The runtime in any Node server: Express, Koa, Fastify, NestJS, Next.js or plain node:http, through one request listener or Connect middleware
Readme
@invariant-app/runtime-node
The Invariant runtime in any Node server: Express 4 and 5, Koa, Fastify 4 and 5, NestJS 10 to 12 on Express, a Next.js 14 to 16 custom server, or plain node:http. One conformance suite holds each of them to the same behaviour.
import { createServer } from "node:http";
import { createRuntime } from "@invariant-app/runtime";
import { adaptListener } from "@invariant-app/runtime-node";
import program from "./invariant/compiled/program.json" with { type: "json" };
const runtime = createRuntime({ program });
createServer(adaptListener(app, { runtime })).listen(3000); // Express: app; Koa: app.callback()Fastify: Fastify({ serverFactory: (handler) => createServer(adaptListener(handler, { runtime })) }).
adaptListener runs before anything in the application. Where a middleware
checks a signature over the request body, mount adaptMiddleware({ runtime })
after that check instead, so it sees the bytes the caller signed.
How a request names its contract comes with the program, from invariant.yaml.
Part of Invariant, which lets an API provider change their API without breaking the integrations built against it. Start with the quickstart.
Licensed under the Apache License, Version 2.0.
