@apidiffguard/cli 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +39 -0
- package/bin/apidiff.js +209 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 APIDiffGuard contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# `@apidiffguard/cli`
|
|
2
|
+
|
|
3
|
+
Command-line JSON / API response diff for CI and local checks. Uses the same engine as APIDiffGuard Cloud (`@apidiffguard/diff`).
|
|
4
|
+
|
|
5
|
+
## Install (from this repo)
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
cd packages/diff-engine && npm run build && cd -
|
|
9
|
+
cd packages/cli && npm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Or from the repo root after workspaces are linked:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npx apidiff check --baseline old.json --current new.json --fail-on breaking
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# Compare two files
|
|
22
|
+
apidiff check --baseline baseline.json --current live.json --fail-on breaking
|
|
23
|
+
|
|
24
|
+
# Compare a baseline file to a live URL
|
|
25
|
+
apidiff check --baseline baseline.json --url https://httpbin.org/json --fail-on warning
|
|
26
|
+
|
|
27
|
+
# Machine-readable output
|
|
28
|
+
apidiff check --baseline a.json --current b.json --json
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Exit codes
|
|
32
|
+
|
|
33
|
+
| Code | Meaning |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| 0 | No changes at or above `--fail-on` |
|
|
36
|
+
| 1 | Failing severity found |
|
|
37
|
+
| 2 | Usage / runtime error |
|
|
38
|
+
|
|
39
|
+
`--fail-on` accepts `breaking` (default), `warning`, or `info`.
|
package/bin/apidiff.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync, existsSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { compareJson, summarizeChanges } from "@apidiffguard/diff";
|
|
5
|
+
|
|
6
|
+
const VERSION = "0.3.0";
|
|
7
|
+
|
|
8
|
+
function printHelp() {
|
|
9
|
+
console.log(`apidiff ${VERSION} — APIDiffGuard CLI
|
|
10
|
+
|
|
11
|
+
Usage:
|
|
12
|
+
apidiff check --baseline <file> --current <file> [options]
|
|
13
|
+
apidiff check --baseline <file> --url <https://...> [options]
|
|
14
|
+
apidiff --version
|
|
15
|
+
apidiff --help
|
|
16
|
+
|
|
17
|
+
Options:
|
|
18
|
+
--fail-on breaking|warning|info Severity threshold (default: breaking)
|
|
19
|
+
--schema-only Ignore leaf value churn
|
|
20
|
+
--header "Name: value" Repeatable request header for --url
|
|
21
|
+
--json Print structured JSON
|
|
22
|
+
|
|
23
|
+
Exit codes:
|
|
24
|
+
0 no changes at or above --fail-on severity
|
|
25
|
+
1 failing severity found
|
|
26
|
+
2 usage / runtime error
|
|
27
|
+
`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function parseArgs(argv) {
|
|
31
|
+
const out = {
|
|
32
|
+
cmd: null,
|
|
33
|
+
baseline: null,
|
|
34
|
+
current: null,
|
|
35
|
+
url: null,
|
|
36
|
+
failOn: "breaking",
|
|
37
|
+
schemaOnly: false,
|
|
38
|
+
headers: /** @type {Record<string, string>} */ ({}),
|
|
39
|
+
json: false,
|
|
40
|
+
help: false,
|
|
41
|
+
version: false,
|
|
42
|
+
};
|
|
43
|
+
const args = [...argv];
|
|
44
|
+
if (args[0] && !args[0].startsWith("-")) {
|
|
45
|
+
out.cmd = args.shift();
|
|
46
|
+
}
|
|
47
|
+
while (args.length) {
|
|
48
|
+
const a = args.shift();
|
|
49
|
+
if (a === "--help" || a === "-h") out.help = true;
|
|
50
|
+
else if (a === "--version" || a === "-v") out.version = true;
|
|
51
|
+
else if (a === "--baseline" || a === "--old") out.baseline = args.shift() ?? null;
|
|
52
|
+
else if (a === "--current" || a === "--new") out.current = args.shift() ?? null;
|
|
53
|
+
else if (a === "--url") out.url = args.shift() ?? null;
|
|
54
|
+
else if (a === "--fail-on") out.failOn = (args.shift() ?? "breaking").toLowerCase();
|
|
55
|
+
else if (a === "--schema-only") out.schemaOnly = true;
|
|
56
|
+
else if (a === "--header" || a === "-H") {
|
|
57
|
+
const raw = args.shift();
|
|
58
|
+
if (!raw) throw new Error("--header requires \"Name: value\"");
|
|
59
|
+
const idx = raw.indexOf(":");
|
|
60
|
+
if (idx <= 0) throw new Error(`Invalid --header (expected Name: value): ${raw}`);
|
|
61
|
+
const name = raw.slice(0, idx).trim();
|
|
62
|
+
const value = raw.slice(idx + 1).trim();
|
|
63
|
+
if (!name) throw new Error(`Invalid --header name: ${raw}`);
|
|
64
|
+
out.headers[name] = value;
|
|
65
|
+
} else if (a === "--json") out.json = true;
|
|
66
|
+
else {
|
|
67
|
+
throw new Error(`Unknown argument: ${a}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readJsonFile(path) {
|
|
74
|
+
const abs = resolve(path);
|
|
75
|
+
if (!existsSync(abs)) throw new Error(`File not found: ${abs}`);
|
|
76
|
+
try {
|
|
77
|
+
return JSON.parse(readFileSync(abs, "utf8"));
|
|
78
|
+
} catch (err) {
|
|
79
|
+
throw new Error(`Invalid JSON in ${abs}: ${err instanceof Error ? err.message : err}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function fetchJson(url, headers = {}) {
|
|
84
|
+
let parsed;
|
|
85
|
+
try {
|
|
86
|
+
parsed = new URL(url);
|
|
87
|
+
} catch {
|
|
88
|
+
throw new Error(`Invalid URL: ${url}`);
|
|
89
|
+
}
|
|
90
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
91
|
+
throw new Error("Only http/https URLs are allowed.");
|
|
92
|
+
}
|
|
93
|
+
const res = await fetch(url, {
|
|
94
|
+
headers: {
|
|
95
|
+
Accept: "application/json, text/plain, */*",
|
|
96
|
+
...headers,
|
|
97
|
+
},
|
|
98
|
+
redirect: "follow",
|
|
99
|
+
signal: AbortSignal.timeout(30_000),
|
|
100
|
+
});
|
|
101
|
+
const text = await res.text();
|
|
102
|
+
try {
|
|
103
|
+
return JSON.parse(text);
|
|
104
|
+
} catch {
|
|
105
|
+
throw new Error(
|
|
106
|
+
`URL did not return JSON (HTTP ${res.status}). Body starts with: ${text.slice(0, 120)}`
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const RANK = { info: 0, warning: 1, breaking: 2 };
|
|
112
|
+
|
|
113
|
+
async function runCheck(opts) {
|
|
114
|
+
if (!opts.baseline) throw new Error("--baseline <file> is required");
|
|
115
|
+
if (!opts.current && !opts.url) {
|
|
116
|
+
throw new Error("Provide --current <file> or --url <https://...>");
|
|
117
|
+
}
|
|
118
|
+
if (opts.current && opts.url) {
|
|
119
|
+
throw new Error("Use either --current or --url, not both");
|
|
120
|
+
}
|
|
121
|
+
if (!["breaking", "warning", "info"].includes(opts.failOn)) {
|
|
122
|
+
throw new Error("--fail-on must be breaking, warning, or info");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const baseline = readJsonFile(opts.baseline);
|
|
126
|
+
const current = opts.url
|
|
127
|
+
? await fetchJson(opts.url, opts.headers)
|
|
128
|
+
: readJsonFile(opts.current);
|
|
129
|
+
|
|
130
|
+
const changes = compareJson(baseline, current, {
|
|
131
|
+
schemaOnly: opts.schemaOnly,
|
|
132
|
+
arrayIdentity: true,
|
|
133
|
+
});
|
|
134
|
+
const summary = summarizeChanges(changes);
|
|
135
|
+
const threshold = RANK[opts.failOn];
|
|
136
|
+
const failing = changes.filter((c) => RANK[c.severity] >= threshold);
|
|
137
|
+
|
|
138
|
+
if (opts.json) {
|
|
139
|
+
console.log(
|
|
140
|
+
JSON.stringify(
|
|
141
|
+
{
|
|
142
|
+
summary,
|
|
143
|
+
failOn: opts.failOn,
|
|
144
|
+
schemaOnly: opts.schemaOnly,
|
|
145
|
+
failed: failing.length > 0,
|
|
146
|
+
changes,
|
|
147
|
+
},
|
|
148
|
+
null,
|
|
149
|
+
2
|
|
150
|
+
)
|
|
151
|
+
);
|
|
152
|
+
} else {
|
|
153
|
+
console.log(
|
|
154
|
+
`breaking=${summary.breakingCount} warning=${summary.warningCount} info=${summary.infoCount}${opts.schemaOnly ? " (schema-only)" : ""}`
|
|
155
|
+
);
|
|
156
|
+
for (const c of changes.slice(0, 50)) {
|
|
157
|
+
const tag = c.severity.toUpperCase().padEnd(8);
|
|
158
|
+
console.log(`${tag} ${c.type.padEnd(13)} ${c.path}`);
|
|
159
|
+
}
|
|
160
|
+
if (changes.length > 50) {
|
|
161
|
+
console.log(`… ${changes.length - 50} more`);
|
|
162
|
+
}
|
|
163
|
+
if (failing.length) {
|
|
164
|
+
console.log(
|
|
165
|
+
`\nFailed: ${failing.length} change(s) at or above ${opts.failOn}.`
|
|
166
|
+
);
|
|
167
|
+
} else {
|
|
168
|
+
console.log(`\nPassed: no changes at or above ${opts.failOn}.`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return failing.length ? 1 : 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function main() {
|
|
176
|
+
let opts;
|
|
177
|
+
try {
|
|
178
|
+
opts = parseArgs(process.argv.slice(2));
|
|
179
|
+
} catch (err) {
|
|
180
|
+
console.error(err instanceof Error ? err.message : err);
|
|
181
|
+
printHelp();
|
|
182
|
+
process.exit(2);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (opts.version) {
|
|
186
|
+
console.log(VERSION);
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (opts.help || !opts.cmd) {
|
|
190
|
+
printHelp();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (opts.cmd !== "check") {
|
|
195
|
+
console.error(`Unknown command: ${opts.cmd}`);
|
|
196
|
+
printHelp();
|
|
197
|
+
process.exit(2);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
const code = await runCheck(opts);
|
|
202
|
+
process.exit(code);
|
|
203
|
+
} catch (err) {
|
|
204
|
+
console.error(err instanceof Error ? err.message : err);
|
|
205
|
+
process.exit(2);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@apidiffguard/cli",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "CLI for APIDiffGuard — diff API JSON responses and fail CI on breaking changes.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"apidiff": "./bin/apidiff.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"apidiff": "node ./bin/apidiff.js"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@apidiffguard/diff": "^0.1.0"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"keywords": [
|
|
24
|
+
"api",
|
|
25
|
+
"diff",
|
|
26
|
+
"breaking-change",
|
|
27
|
+
"ci",
|
|
28
|
+
"json",
|
|
29
|
+
"apidiffguard"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/orvi2014/apidiffguard.git",
|
|
35
|
+
"directory": "packages/cli"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/orvi2014/apidiffguard/tree/main/packages/cli",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/orvi2014/apidiffguard/issues"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=18"
|
|
43
|
+
}
|
|
44
|
+
}
|