contractdiff
Breaking-change diff, lint and code generation for API contracts (OpenAPI, JSON Schema, AsyncAPI, MCP), and the spec back from the code. No dependency outside the burgee family. An openapi-typescript migration path.
contractdiff reads an API contract (OpenAPI, Swagger 2.0, JSON Schema, AsyncAPI or an MCP tool
list) and does four things with it. diff says what a change breaks and for whom, under the
compatibility mode you pick. lint checks the contract's form with rules, Spectral and Redocly
rulesets included. gen writes code from it, one plugin per target: TypeScript types, loadline
validators, fetch and TanStack Query clients, MSW handlers and NestJS, Express, Fastify and Hono
servers. extract writes the contract back from the code. Every finding names its fix, as the same
record for a person, for --json and for a coding agent.
The types target writes what openapi-typescript writes, and contractdiff migrate openapi-typescript turns its flags and redocly.yaml into a contractdiff.config.json, as
Migrating shows.
Install
npm install contractdiff
pnpm add contractdiff
yarn add contractdiff
bun add contractdiffInstall it as a dev dependency (-D) of the project that owns the contract. The generated
validators import loadline, which comes with it.
Quick start
npm install contractdiffimport { mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { gen } from "contractdiff";
const dir = mkdtempSync(join(tmpdir(), "contractdiff-"));
const pet = { type: "object", required: ["name"], properties: { name: { type: "string" } } };
writeFileSync(
join(dir, "openapi.json"),
JSON.stringify({
openapi: "3.1.0",
info: { title: "Pets", version: "1.0.0" },
paths: {
"/pets/{id}": {
get: {
operationId: "getPet",
parameters: [{ name: "id", in: "path", required: true, schema: { type: "string" } }],
responses: { 200: { description: "A pet", content: { "application/json": { schema: pet } } } },
},
},
},
}),
);
writeFileSync(
join(dir, "contractdiff.config.json"),
JSON.stringify({ input: "openapi.json", targets: { types: { generator: "types", out: "src/api" } } }),
);
const report = await gen({ cwd: dir });
for (const file of report.files) console.log(`${file.action} ${file.path}`);write src/api/types.ts
write contractdiff.map.jsonThat is npx contractdiff gen in a project with that config. npx contractdiff diff git:main:openapi.json openapi.json compares the contract on main with the one in the working tree,
and exits 1 when the change breaks a caller.
What it does
diffcompares two versions of a contract, from files orgit:<ref>:<path>, and classifies each change as breaking, dangerous or safe for the side you name, underbackward,forwardorfullcompatibility.--transitivechecks a chain of releases.lintruns rules on a contract: its ownrecommendedandagent-readysets, and Spectral and Redocly rulesets read as they are.genwrites code from the contract, one plugin per target, and records every file and symbol it wrote incontractdiff.map.json, so the next run can tell its own files from yours.extractwrites the contract back from your code, andpropsdraws property tests from it.
Migrating
npx contractdiff migrate openapi-typescript reads the scripts that run openapi-typescript and its
redocly.yaml, writes contractdiff.config.json with a types target in openapi-typescript's
compatibility mode, and rewrites the script:
"scripts": {
- "gen": "openapi-typescript openapi.yaml -o src/api/types.ts"
+ "gen": "contractdiff gen"
}It prints what it changed and what it could not carry, then npm uninstall openapi-typescript.
The same command reads openapi-fetch, Hey API and orval configs.
Compatibility
Not graded: no incumbent's own suite runs against contractdiff through compat-oracle. The types
target is measured against openapi-typescript's output on the corpus in benchmarks/apicontract.
Benchmarks
Every number here is produced by npm run bench and published at burgee.interlace.tools/docs/benchmarks.
No suite is graded against this package yet, so there is no compatibility number to quote.
For agents
contractdiff explain <operation> --jsonprints one operation, message or tool with every$refresolved and its size capped, so an agent calls it from the contract and not from memory.contractdiff lint --ruleset agent-readychecks whether a contract can be used as tools.--json,--schemaand an MCP server come from one declaration on burgee:contractdiff --mcpserves the read-only commands as tools.- Exit codes are the contract.
0ok;1a finding, such as a breaking change;2a usage error;3a config that did not load, with the field to fix. - The docs are machine-readable at burgee.interlace.tools/llms.txt.
API
| Import | What it gives you |
|---|---|
contractdiff | gen(), extract(), configSchema, MAP_FILE and program |
contractdiff/core | the contract model every adapter reads into, and the $ref resolver |
contractdiff/openapi | the OpenAPI adapter |
contractdiff/openapi-typescript | openapi-typescript's API over the types target |
contractdiff/testing | Plugin and the conformance suite a plugin of your own runs |
The contractdiff bin is that API, declared once on burgee. contractdiff plugin new <name>
scaffolds a plugin that passes the conformance suite as written.
Requirements
Node 22.13 or later. git, for diff against a ref. TypeScript in the project, for lint rules on
code and for extract from TypeScript sources.
Where it sits
It hosts no plugin key of its own.
Nothing in this family builds on it yet, and it builds on bellpull, burgee, loadline, seniority.
The family
14 packages, one repository, one release pipeline. Each installs and works on its own, and each owns
one job. Seven of them — bellpull, closeout, linegauge, loadline, paratext, roundel,
seniority — depend on nothing; the others depend only on packages in this table, and every
dependency points one way, down the layers.
No package declares a dependency from outside the family; react and react-reconciler are
optional peers of controlroom, which npm does not install; vitest is an optional peer of
oxmutate, which npm does not install; eslint is an optional peer of truemark, which npm does
not install.
package-shape-lock
holds that for every manifest, and
independence-install-lock
installs each package alone and finds nothing but the family packages it declares.
Releases are published by one workflow through npm trusted publishing: no npm token is used, and
each release carries SLSA provenance, which npm view <package> dist.attestations shows.
trusted-publishing-lock
keeps both true.
| Package | What it is | Migrates from |
|---|---|---|
| burgee | The CLI framework: one declaration, every surface | commander, yargs and meow |
| roundel | Colour: one output policy, semantic tokens, a theme | chalk, colorette, kleur, picocolors, supports-color and yoctocolors |
| flagstaff | The frame loop: spinners, progress, boxes and tables | ora, log-update, boxen and cli-table3 |
| caique | Prompts that are flags first, and never hang | @inquirer/core |
| linegauge | Measuring, wrapping, truncating and slicing styled text | string-width, wrap-ansi, strip-ansi, slice-ansi, cli-truncate and widest-line |
| paratext | Hyperlinks, images, title, clipboard and notifications | ansi-escapes, terminal-link and term-img |
| seniority | Configuration precedence and discovery, with provenance | cosmiconfig, lilconfig, dotenv and rc |
| closeout | Exit handlers, terminal restore and a bounded shutdown | signal-exit, exit-hook, restore-cursor, cli-cursor and onetime |
| bellpull | Subprocesses, and which executable actually ran | cross-spawn and which |
| controlroom | Keyboard-driven terminal screens, inline or full-screen | ink |
| oxmutate | Mutation testing on the oxc engine, inside your own Vitest | Stryker |
| truemark | Lint rules and a pre-push check that keep a test suite honest | @vitest/eslint-plugin |
| contractdiff (this package) | API contracts: breaking-change diff, lint, and code from the spec | openapi-typescript |
| loadline | Validation whose schemas are data, with no eval | zod |
Every migration guide, and the family-wide compatibility and benchmarks pages, are on burgee.interlace.tools.
Contributing
Issues and pull requests are welcome at ofri-peretz/burgee; read CONTRIBUTING.md first. Report a vulnerability privately, as SECURITY.md describes — never in a public issue.
Licence
MIT © Ofri Peretz — see LICENSE.
burgee
A CLI framework built on one declaration: help, --json, --schema, MCP, completions and types are projected from it, with exit codes and fix: lines an agent acts on. No dependency outside the burgee family. Drop-in alternative to commander, yargs and meow.
loadline
A JS/TS validator whose schemas are data: checked with no eval, or compiled ahead of time from a contract. Zero dependencies. A zod migration path.