@apidiff/core 1.0.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/dist/ast/hash.d.ts +2 -0
- package/dist/ast/hash.d.ts.map +1 -0
- package/dist/ast/index.d.ts +3 -0
- package/dist/ast/index.d.ts.map +1 -0
- package/dist/ast/traverse.d.ts +13 -0
- package/dist/ast/traverse.d.ts.map +1 -0
- package/dist/config/index.d.ts +9 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/diff/auth-differ.d.ts +4 -0
- package/dist/diff/auth-differ.d.ts.map +1 -0
- package/dist/diff/endpoint-differ.d.ts +3 -0
- package/dist/diff/endpoint-differ.d.ts.map +1 -0
- package/dist/diff/index.d.ts +3 -0
- package/dist/diff/index.d.ts.map +1 -0
- package/dist/diff/schema-differ.d.ts +3 -0
- package/dist/diff/schema-differ.d.ts.map +1 -0
- package/dist/diff/server-differ.d.ts +3 -0
- package/dist/diff/server-differ.d.ts.map +1 -0
- package/dist/index.cjs +2902 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2855 -0
- package/dist/loader/file-loader.d.ts +5 -0
- package/dist/loader/file-loader.d.ts.map +1 -0
- package/dist/loader/git-loader.d.ts +5 -0
- package/dist/loader/git-loader.d.ts.map +1 -0
- package/dist/loader/index.d.ts +7 -0
- package/dist/loader/index.d.ts.map +1 -0
- package/dist/loader/url-loader.d.ts +5 -0
- package/dist/loader/url-loader.d.ts.map +1 -0
- package/dist/output/html.d.ts +3 -0
- package/dist/output/html.d.ts.map +1 -0
- package/dist/output/index.d.ts +3 -0
- package/dist/output/index.d.ts.map +1 -0
- package/dist/output/json.d.ts +3 -0
- package/dist/output/json.d.ts.map +1 -0
- package/dist/output/markdown.d.ts +3 -0
- package/dist/output/markdown.d.ts.map +1 -0
- package/dist/output/terminal.d.ts +3 -0
- package/dist/output/terminal.d.ts.map +1 -0
- package/dist/parsers/base.d.ts +8 -0
- package/dist/parsers/base.d.ts.map +1 -0
- package/dist/parsers/graphql/index.d.ts +13 -0
- package/dist/parsers/graphql/index.d.ts.map +1 -0
- package/dist/parsers/index.d.ts +7 -0
- package/dist/parsers/index.d.ts.map +1 -0
- package/dist/parsers/openapi2/index.d.ts +9 -0
- package/dist/parsers/openapi2/index.d.ts.map +1 -0
- package/dist/parsers/openapi3/index.d.ts +9 -0
- package/dist/parsers/openapi3/index.d.ts.map +1 -0
- package/dist/parsers/openapi3/ref-resolver.d.ts +2 -0
- package/dist/parsers/openapi3/ref-resolver.d.ts.map +1 -0
- package/dist/parsers/openapi3/schema-normalizer.d.ts +3 -0
- package/dist/parsers/openapi3/schema-normalizer.d.ts.map +1 -0
- package/dist/parsers/openapi3/security-normalizer.d.ts +3 -0
- package/dist/parsers/openapi3/security-normalizer.d.ts.map +1 -0
- package/dist/parsers/protobuf/index.d.ts +14 -0
- package/dist/parsers/protobuf/index.d.ts.map +1 -0
- package/dist/rules/auth/oauth-scope-removed.d.ts +9 -0
- package/dist/rules/auth/oauth-scope-removed.d.ts.map +1 -0
- package/dist/rules/auth/security-added.d.ts +9 -0
- package/dist/rules/auth/security-added.d.ts.map +1 -0
- package/dist/rules/auth/security-removed.d.ts +9 -0
- package/dist/rules/auth/security-removed.d.ts.map +1 -0
- package/dist/rules/auth/security-scheme-type-changed.d.ts +9 -0
- package/dist/rules/auth/security-scheme-type-changed.d.ts.map +1 -0
- package/dist/rules/base.d.ts +19 -0
- package/dist/rules/base.d.ts.map +1 -0
- package/dist/rules/endpoint/endpoint-added.d.ts +9 -0
- package/dist/rules/endpoint/endpoint-added.d.ts.map +1 -0
- package/dist/rules/endpoint/endpoint-deprecated.d.ts +9 -0
- package/dist/rules/endpoint/endpoint-deprecated.d.ts.map +1 -0
- package/dist/rules/endpoint/endpoint-removed.d.ts +9 -0
- package/dist/rules/endpoint/endpoint-removed.d.ts.map +1 -0
- package/dist/rules/endpoint/http-method-changed.d.ts +9 -0
- package/dist/rules/endpoint/http-method-changed.d.ts.map +1 -0
- package/dist/rules/endpoint/path-changed.d.ts +9 -0
- package/dist/rules/endpoint/path-changed.d.ts.map +1 -0
- package/dist/rules/index.d.ts +4 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/meta/server-removed.d.ts +9 -0
- package/dist/rules/meta/server-removed.d.ts.map +1 -0
- package/dist/rules/param/param-added.d.ts +9 -0
- package/dist/rules/param/param-added.d.ts.map +1 -0
- package/dist/rules/param/param-deprecated.d.ts +9 -0
- package/dist/rules/param/param-deprecated.d.ts.map +1 -0
- package/dist/rules/param/param-enum-value-added.d.ts +9 -0
- package/dist/rules/param/param-enum-value-added.d.ts.map +1 -0
- package/dist/rules/param/param-enum-value-removed.d.ts +9 -0
- package/dist/rules/param/param-enum-value-removed.d.ts.map +1 -0
- package/dist/rules/param/param-location-changed.d.ts +9 -0
- package/dist/rules/param/param-location-changed.d.ts.map +1 -0
- package/dist/rules/param/param-removed.d.ts +9 -0
- package/dist/rules/param/param-removed.d.ts.map +1 -0
- package/dist/rules/param/param-required-added.d.ts +9 -0
- package/dist/rules/param/param-required-added.d.ts.map +1 -0
- package/dist/rules/param/param-required-true-to-false.d.ts +9 -0
- package/dist/rules/param/param-required-true-to-false.d.ts.map +1 -0
- package/dist/rules/param/param-type-changed.d.ts +9 -0
- package/dist/rules/param/param-type-changed.d.ts.map +1 -0
- package/dist/rules/request/request-body-added-required.d.ts +9 -0
- package/dist/rules/request/request-body-added-required.d.ts.map +1 -0
- package/dist/rules/request/request-body-removed.d.ts +9 -0
- package/dist/rules/request/request-body-removed.d.ts.map +1 -0
- package/dist/rules/request/request-content-type-added.d.ts +9 -0
- package/dist/rules/request/request-content-type-added.d.ts.map +1 -0
- package/dist/rules/request/request-content-type-removed.d.ts +9 -0
- package/dist/rules/request/request-content-type-removed.d.ts.map +1 -0
- package/dist/rules/request/request-field-added-required.d.ts +9 -0
- package/dist/rules/request/request-field-added-required.d.ts.map +1 -0
- package/dist/rules/request/request-field-removed.d.ts +9 -0
- package/dist/rules/request/request-field-removed.d.ts.map +1 -0
- package/dist/rules/request/request-field-type-changed.d.ts +9 -0
- package/dist/rules/request/request-field-type-changed.d.ts.map +1 -0
- package/dist/rules/request/request-required-false-to-true.d.ts +9 -0
- package/dist/rules/request/request-required-false-to-true.d.ts.map +1 -0
- package/dist/rules/response/response-field-added.d.ts +9 -0
- package/dist/rules/response/response-field-added.d.ts.map +1 -0
- package/dist/rules/response/response-field-removed.d.ts +9 -0
- package/dist/rules/response/response-field-removed.d.ts.map +1 -0
- package/dist/rules/response/response-field-type-changed.d.ts +9 -0
- package/dist/rules/response/response-field-type-changed.d.ts.map +1 -0
- package/dist/rules/response/response-header-added-required.d.ts +9 -0
- package/dist/rules/response/response-header-added-required.d.ts.map +1 -0
- package/dist/rules/response/response-header-removed.d.ts +9 -0
- package/dist/rules/response/response-header-removed.d.ts.map +1 -0
- package/dist/rules/response/response-media-type-added.d.ts +9 -0
- package/dist/rules/response/response-media-type-added.d.ts.map +1 -0
- package/dist/rules/response/response-media-type-removed.d.ts +9 -0
- package/dist/rules/response/response-media-type-removed.d.ts.map +1 -0
- package/dist/rules/response/response-status-added.d.ts +9 -0
- package/dist/rules/response/response-status-added.d.ts.map +1 -0
- package/dist/rules/response/response-status-removed.d.ts +9 -0
- package/dist/rules/response/response-status-removed.d.ts.map +1 -0
- package/dist/types/ast.d.ts +140 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/config.d.ts +17 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/diff.d.ts +43 -0
- package/dist/types/diff.d.ts.map +1 -0
- package/dist/types/errors.d.ts +21 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/semantic.d.ts +45 -0
- package/dist/types/semantic.d.ts.map +1 -0
- package/package.json +31 -0
- package/src/ast/hash.ts +26 -0
- package/src/ast/index.ts +2 -0
- package/src/ast/traverse.ts +59 -0
- package/src/config/index.ts +44 -0
- package/src/diff/auth-differ.ts +72 -0
- package/src/diff/endpoint-differ.ts +144 -0
- package/src/diff/index.ts +13 -0
- package/src/diff/schema-differ.ts +70 -0
- package/src/diff/server-differ.ts +22 -0
- package/src/index.ts +92 -0
- package/src/loader/file-loader.ts +19 -0
- package/src/loader/git-loader.ts +22 -0
- package/src/loader/index.ts +32 -0
- package/src/loader/url-loader.ts +25 -0
- package/src/output/html.ts +177 -0
- package/src/output/index.ts +16 -0
- package/src/output/json.ts +5 -0
- package/src/output/markdown.ts +29 -0
- package/src/output/terminal.ts +37 -0
- package/src/parsers/base.ts +8 -0
- package/src/parsers/graphql/index.ts +181 -0
- package/src/parsers/index.ts +61 -0
- package/src/parsers/openapi2/index.ts +218 -0
- package/src/parsers/openapi3/index.ts +223 -0
- package/src/parsers/openapi3/ref-resolver.ts +101 -0
- package/src/parsers/openapi3/schema-normalizer.ts +52 -0
- package/src/parsers/openapi3/security-normalizer.ts +18 -0
- package/src/parsers/protobuf/index.ts +208 -0
- package/src/rules/auth/oauth-scope-removed.ts +34 -0
- package/src/rules/auth/security-added.ts +32 -0
- package/src/rules/auth/security-removed.ts +47 -0
- package/src/rules/auth/security-scheme-type-changed.ts +30 -0
- package/src/rules/base.ts +29 -0
- package/src/rules/endpoint/endpoint-added.ts +26 -0
- package/src/rules/endpoint/endpoint-deprecated.ts +29 -0
- package/src/rules/endpoint/endpoint-removed.ts +26 -0
- package/src/rules/endpoint/http-method-changed.ts +29 -0
- package/src/rules/endpoint/path-changed.ts +29 -0
- package/src/rules/index.ts +83 -0
- package/src/rules/meta/server-removed.ts +26 -0
- package/src/rules/param/param-added.ts +52 -0
- package/src/rules/param/param-deprecated.ts +34 -0
- package/src/rules/param/param-enum-value-added.ts +32 -0
- package/src/rules/param/param-enum-value-removed.ts +32 -0
- package/src/rules/param/param-location-changed.ts +43 -0
- package/src/rules/param/param-removed.ts +52 -0
- package/src/rules/param/param-required-added.ts +34 -0
- package/src/rules/param/param-required-true-to-false.ts +34 -0
- package/src/rules/param/param-type-changed.ts +32 -0
- package/src/rules/request/request-body-added-required.ts +33 -0
- package/src/rules/request/request-body-removed.ts +30 -0
- package/src/rules/request/request-content-type-added.ts +31 -0
- package/src/rules/request/request-content-type-removed.ts +31 -0
- package/src/rules/request/request-field-added-required.ts +41 -0
- package/src/rules/request/request-field-removed.ts +35 -0
- package/src/rules/request/request-field-type-changed.ts +37 -0
- package/src/rules/request/request-required-false-to-true.ts +56 -0
- package/src/rules/response/response-field-added.ts +34 -0
- package/src/rules/response/response-field-removed.ts +34 -0
- package/src/rules/response/response-field-type-changed.ts +37 -0
- package/src/rules/response/response-header-added-required.ts +47 -0
- package/src/rules/response/response-header-removed.ts +32 -0
- package/src/rules/response/response-media-type-added.ts +32 -0
- package/src/rules/response/response-media-type-removed.ts +32 -0
- package/src/rules/response/response-status-added.ts +31 -0
- package/src/rules/response/response-status-removed.ts +31 -0
- package/src/types/ast.ts +164 -0
- package/src/types/config.ts +31 -0
- package/src/types/diff.ts +49 -0
- package/src/types/errors.ts +26 -0
- package/src/types/index.ts +5 -0
- package/src/types/semantic.ts +60 -0
- package/test/integration/stripe-v1.yaml +36 -0
- package/test/integration/stripe-v2.yaml +35 -0
- package/tests/ast.test.ts +60 -0
- package/tests/config.test.ts +43 -0
- package/tests/diff/auth-differ.test.ts +55 -0
- package/tests/diff/endpoint-differ.test.ts +42 -0
- package/tests/diff/schema-differ.test.ts +46 -0
- package/tests/diff/server-differ.test.ts +23 -0
- package/tests/diff.test.ts +116 -0
- package/tests/fixtures/openapi3/auth-added/v1.yaml +11 -0
- package/tests/fixtures/openapi3/auth-added/v2.yaml +18 -0
- package/tests/integration.test.ts +21 -0
- package/tests/loader-more.test.ts +75 -0
- package/tests/loader.test.ts +61 -0
- package/tests/output.test.ts +58 -0
- package/tests/parsers/openapi3-coverage.test.ts +77 -0
- package/tests/parsers/openapi3.test.ts +41 -0
- package/tests/parsers/parsers-other.test.ts +135 -0
- package/tests/parsers/ref-resolver.test.ts +78 -0
- package/tests/parsers/schema-normalizer.test.ts +30 -0
- package/tests/rules/auth-meta.test.ts +87 -0
- package/tests/rules/base.test.ts +44 -0
- package/tests/rules/endpoint.test.ts +122 -0
- package/tests/rules/param.test.ts +242 -0
- package/tests/rules/request.test.ts +147 -0
- package/tests/rules/response.test.ts +161 -0
- package/tests/rules/rules-coverage.test.ts +64 -0
- package/tests/types-errors.test.ts +22 -0
- package/tsconfig.json +8 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { SemanticChange } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
export function formatHtml(changes: SemanticChange[]): string {
|
|
4
|
+
const breaking = changes.filter(c => c.severity === 'breaking');
|
|
5
|
+
const warning = changes.filter(c => c.severity === 'warning');
|
|
6
|
+
const info = changes.filter(c => c.severity === 'info');
|
|
7
|
+
|
|
8
|
+
return `
|
|
9
|
+
<!DOCTYPE html>
|
|
10
|
+
<html lang="en">
|
|
11
|
+
<head>
|
|
12
|
+
<meta charset="UTF-8">
|
|
13
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
14
|
+
<title>Semantic API Diff Report</title>
|
|
15
|
+
<style>
|
|
16
|
+
body {
|
|
17
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
18
|
+
line-height: 1.6;
|
|
19
|
+
color: #333;
|
|
20
|
+
max-width: 900px;
|
|
21
|
+
margin: 0 auto;
|
|
22
|
+
padding: 2rem;
|
|
23
|
+
background: #f9f9f9;
|
|
24
|
+
}
|
|
25
|
+
h1 {
|
|
26
|
+
color: #222;
|
|
27
|
+
border-bottom: 2px solid #eaeaea;
|
|
28
|
+
padding-bottom: 0.5rem;
|
|
29
|
+
}
|
|
30
|
+
.summary {
|
|
31
|
+
display: flex;
|
|
32
|
+
gap: 1rem;
|
|
33
|
+
margin: 2rem 0;
|
|
34
|
+
}
|
|
35
|
+
.stat-card {
|
|
36
|
+
background: white;
|
|
37
|
+
padding: 1.5rem;
|
|
38
|
+
border-radius: 8px;
|
|
39
|
+
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
|
|
40
|
+
flex: 1;
|
|
41
|
+
text-align: center;
|
|
42
|
+
border-top: 4px solid #ccc;
|
|
43
|
+
}
|
|
44
|
+
.stat-card.breaking { border-top-color: #e53e3e; }
|
|
45
|
+
.stat-card.warning { border-top-color: #d69e2e; }
|
|
46
|
+
.stat-card.info { border-top-color: #3182ce; }
|
|
47
|
+
.stat-card .value {
|
|
48
|
+
font-size: 2rem;
|
|
49
|
+
font-weight: bold;
|
|
50
|
+
margin: 0.5rem 0;
|
|
51
|
+
}
|
|
52
|
+
.stat-card .label {
|
|
53
|
+
text-transform: uppercase;
|
|
54
|
+
font-size: 0.8rem;
|
|
55
|
+
letter-spacing: 0.05em;
|
|
56
|
+
color: #666;
|
|
57
|
+
}
|
|
58
|
+
.change-list {
|
|
59
|
+
list-style: none;
|
|
60
|
+
padding: 0;
|
|
61
|
+
}
|
|
62
|
+
.change-item {
|
|
63
|
+
background: white;
|
|
64
|
+
margin-bottom: 1rem;
|
|
65
|
+
border-radius: 6px;
|
|
66
|
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
|
67
|
+
overflow: hidden;
|
|
68
|
+
}
|
|
69
|
+
.change-header {
|
|
70
|
+
padding: 1rem;
|
|
71
|
+
cursor: pointer;
|
|
72
|
+
display: flex;
|
|
73
|
+
justify-content: space-between;
|
|
74
|
+
align-items: center;
|
|
75
|
+
background: #fff;
|
|
76
|
+
border-left: 4px solid #ccc;
|
|
77
|
+
}
|
|
78
|
+
.change-item.breaking .change-header { border-left-color: #e53e3e; }
|
|
79
|
+
.change-item.warning .change-header { border-left-color: #d69e2e; }
|
|
80
|
+
.change-item.info .change-header { border-left-color: #3182ce; }
|
|
81
|
+
|
|
82
|
+
.change-title {
|
|
83
|
+
font-weight: 600;
|
|
84
|
+
}
|
|
85
|
+
.change-body {
|
|
86
|
+
padding: 1rem;
|
|
87
|
+
border-top: 1px solid #eaeaea;
|
|
88
|
+
background: #fafafa;
|
|
89
|
+
display: none;
|
|
90
|
+
}
|
|
91
|
+
.open .change-body {
|
|
92
|
+
display: block;
|
|
93
|
+
}
|
|
94
|
+
.badge {
|
|
95
|
+
padding: 0.25rem 0.5rem;
|
|
96
|
+
border-radius: 9999px;
|
|
97
|
+
font-size: 0.75rem;
|
|
98
|
+
font-weight: bold;
|
|
99
|
+
background: #eee;
|
|
100
|
+
}
|
|
101
|
+
.badge.breaking { background: #fee2e2; color: #991b1b; }
|
|
102
|
+
.badge.warning { background: #fef3c7; color: #92400e; }
|
|
103
|
+
.badge.info { background: #dbeafe; color: #1e40af; }
|
|
104
|
+
|
|
105
|
+
.detail-row {
|
|
106
|
+
margin-bottom: 0.5rem;
|
|
107
|
+
}
|
|
108
|
+
.detail-label {
|
|
109
|
+
font-weight: 600;
|
|
110
|
+
color: #555;
|
|
111
|
+
width: 100px;
|
|
112
|
+
display: inline-block;
|
|
113
|
+
}
|
|
114
|
+
code {
|
|
115
|
+
background: #edf2f7;
|
|
116
|
+
padding: 0.1rem 0.3rem;
|
|
117
|
+
border-radius: 3px;
|
|
118
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
|
|
119
|
+
font-size: 0.9em;
|
|
120
|
+
}
|
|
121
|
+
</style>
|
|
122
|
+
<script>
|
|
123
|
+
function toggleChange(el) {
|
|
124
|
+
el.parentElement.classList.toggle('open');
|
|
125
|
+
}
|
|
126
|
+
</script>
|
|
127
|
+
</head>
|
|
128
|
+
<body>
|
|
129
|
+
|
|
130
|
+
<h1>API Diff Report</h1>
|
|
131
|
+
|
|
132
|
+
<div class="summary">
|
|
133
|
+
<div class="stat-card breaking">
|
|
134
|
+
<div class="label">Breaking</div>
|
|
135
|
+
<div class="value">${breaking.length}</div>
|
|
136
|
+
</div>
|
|
137
|
+
<div class="stat-card warning">
|
|
138
|
+
<div class="label">Warnings</div>
|
|
139
|
+
<div class="value">${warning.length}</div>
|
|
140
|
+
</div>
|
|
141
|
+
<div class="stat-card info">
|
|
142
|
+
<div class="label">Info</div>
|
|
143
|
+
<div class="value">${info.length}</div>
|
|
144
|
+
</div>
|
|
145
|
+
</div>
|
|
146
|
+
|
|
147
|
+
<h2>All Changes</h2>
|
|
148
|
+
<ul class="change-list">
|
|
149
|
+
${changes.map(renderChange).join('\n')}
|
|
150
|
+
</ul>
|
|
151
|
+
|
|
152
|
+
</body>
|
|
153
|
+
</html>
|
|
154
|
+
`.trim();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function renderChange(c: SemanticChange): string {
|
|
158
|
+
let locStr = `<code>${c.location.method} ${c.location.path}</code>`;
|
|
159
|
+
if (c.location.paramName) locStr += ` (param: <code>${c.location.paramName}</code>)`;
|
|
160
|
+
if (c.location.field) locStr += ` (field: <code>${c.location.field}</code>)`;
|
|
161
|
+
|
|
162
|
+
return `
|
|
163
|
+
<li class="change-item ${c.severity}">
|
|
164
|
+
<div class="change-header" onclick="toggleChange(this)">
|
|
165
|
+
<div class="change-title">${c.message}</div>
|
|
166
|
+
<span class="badge ${c.severity}">${c.severity}</span>
|
|
167
|
+
</div>
|
|
168
|
+
<div class="change-body">
|
|
169
|
+
<div class="detail-row"><span class="detail-label">Location:</span> ${locStr}</div>
|
|
170
|
+
<div class="detail-row"><span class="detail-label">Category:</span> ${c.category}</div>
|
|
171
|
+
<div class="detail-row"><span class="detail-label">Rule:</span> <code>${c.ruleId}</code></div>
|
|
172
|
+
${c.consequence ? `<div class="detail-row"><span class="detail-label">Impact:</span> ${c.consequence}</div>` : ''}
|
|
173
|
+
${c.migration ? `<div class="detail-row"><span class="detail-label">Migration:</span> ${c.migration}</div>` : ''}
|
|
174
|
+
</div>
|
|
175
|
+
</li>
|
|
176
|
+
`;
|
|
177
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { SemanticChange, ApidiffConfig } from '../types/index.js';
|
|
2
|
+
import { formatTerminal } from './terminal.js';
|
|
3
|
+
import { formatJson } from './json.js';
|
|
4
|
+
import { formatMarkdown } from './markdown.js';
|
|
5
|
+
import { formatHtml } from './html.js';
|
|
6
|
+
|
|
7
|
+
export function formatOutput(changes: SemanticChange[], format: ApidiffConfig['output']['format']): string {
|
|
8
|
+
switch (format) {
|
|
9
|
+
case 'json': return formatJson(changes);
|
|
10
|
+
case 'markdown': return formatMarkdown(changes);
|
|
11
|
+
case 'html': return formatHtml(changes);
|
|
12
|
+
case 'terminal':
|
|
13
|
+
default:
|
|
14
|
+
return formatTerminal(changes);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { SemanticChange } from '../types/index.js';
|
|
2
|
+
|
|
3
|
+
const SEVERITY_EMOJI = {
|
|
4
|
+
breaking: '❌',
|
|
5
|
+
warning: '⚠️',
|
|
6
|
+
info: 'ℹ️'
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function formatMarkdown(changes: SemanticChange[]): string {
|
|
10
|
+
if (changes.length === 0) {
|
|
11
|
+
return 'No changes detected.\n';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let md = '## API Changes\n\n';
|
|
15
|
+
|
|
16
|
+
for (const c of changes) {
|
|
17
|
+
const emoji = SEVERITY_EMOJI[c.severity] || SEVERITY_EMOJI.info;
|
|
18
|
+
let locStr = `**${c.location.method}** \`${c.location.path}\``;
|
|
19
|
+
if (c.location.paramName) locStr += ` (param: \`${c.location.paramName}\`)`;
|
|
20
|
+
if (c.location.field) locStr += ` (field: \`${c.location.field}\`)`;
|
|
21
|
+
|
|
22
|
+
md += `### ${emoji} ${c.message}\n`;
|
|
23
|
+
md += `- **Location:** ${locStr}\n`;
|
|
24
|
+
md += `- **Consequence:** ${c.consequence}\n`;
|
|
25
|
+
md += `- **Migration:** ${c.migration}\n\n`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return md;
|
|
29
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import type { SemanticChange } from '../types/index.js';
|
|
3
|
+
|
|
4
|
+
const SEVERITY_ICONS = {
|
|
5
|
+
breaking: chalk.red('❌'),
|
|
6
|
+
warning: chalk.yellow('⚠️ '),
|
|
7
|
+
info: chalk.blue('ℹ '),
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const SEVERITY_COLORS = {
|
|
11
|
+
breaking: chalk.red,
|
|
12
|
+
warning: chalk.yellow,
|
|
13
|
+
info: chalk.blue,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function formatTerminal(changes: SemanticChange[]): string {
|
|
17
|
+
if (changes.length === 0) {
|
|
18
|
+
return chalk.green('No changes detected.\n');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let output = '';
|
|
22
|
+
for (const c of changes) {
|
|
23
|
+
const icon = SEVERITY_ICONS[c.severity] || SEVERITY_ICONS.info;
|
|
24
|
+
const color = SEVERITY_COLORS[c.severity] || SEVERITY_COLORS.info;
|
|
25
|
+
|
|
26
|
+
let locStr = `${c.location.method} ${c.location.path}`;
|
|
27
|
+
if (c.location.paramName) locStr += ` (param: ${c.location.paramName})`;
|
|
28
|
+
if (c.location.field) locStr += ` (field: ${c.location.field})`;
|
|
29
|
+
|
|
30
|
+
output += `${icon} ${color(c.message)}\n`;
|
|
31
|
+
output += ` Location: ${locStr}\n`;
|
|
32
|
+
output += ` Consequence: ${c.consequence}\n`;
|
|
33
|
+
output += ` Migration: ${c.migration}\n\n`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return output;
|
|
37
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { NormalizedAST, SpecFormat } from '../types/index.js';
|
|
2
|
+
import type { RawSpec } from '../loader/index.js';
|
|
3
|
+
|
|
4
|
+
export interface ISpecParser {
|
|
5
|
+
readonly format: SpecFormat;
|
|
6
|
+
canParse(raw: RawSpec): boolean;
|
|
7
|
+
parse(raw: RawSpec): Promise<NormalizedAST>;
|
|
8
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { parse as parseGraphql, DocumentNode, ObjectTypeDefinitionNode, FieldDefinitionNode, InputObjectTypeDefinitionNode, TypeNode, EnumTypeDefinitionNode } from 'graphql';
|
|
2
|
+
import type { NormalizedAST, Endpoint, Parameter, RequestBody, ResponseDef, ComponentMap, Schema } from '../../types/index.js';
|
|
3
|
+
import type { RawSpec } from '../../loader/index.js';
|
|
4
|
+
import type { ISpecParser } from '../base.js';
|
|
5
|
+
import { ParseError } from '../../types/errors.js';
|
|
6
|
+
|
|
7
|
+
export class GraphqlParser implements ISpecParser {
|
|
8
|
+
readonly format = 'graphql';
|
|
9
|
+
|
|
10
|
+
canParse(raw: RawSpec): boolean {
|
|
11
|
+
return raw.format === 'graphql' || raw.content.includes('type Query') || raw.content.includes('type Mutation');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async parse(raw: RawSpec): Promise<NormalizedAST> {
|
|
15
|
+
let ast: DocumentNode;
|
|
16
|
+
try {
|
|
17
|
+
ast = parseGraphql(raw.content);
|
|
18
|
+
} catch (err: any) {
|
|
19
|
+
throw new ParseError('Failed to parse GraphQL spec', err.locations?.[0]?.line, err.locations?.[0]?.column, raw.sourcePath, err);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const meta = {
|
|
23
|
+
title: 'GraphQL API',
|
|
24
|
+
version: '1.0.0',
|
|
25
|
+
format: 'graphql' as const,
|
|
26
|
+
rawVersion: '1'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const components: ComponentMap = {
|
|
30
|
+
schemas: {},
|
|
31
|
+
securitySchemes: {},
|
|
32
|
+
parameters: {},
|
|
33
|
+
responses: {},
|
|
34
|
+
headers: {},
|
|
35
|
+
requestBodies: {}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const endpoints: Endpoint[] = [];
|
|
39
|
+
|
|
40
|
+
// First pass: extract schemas
|
|
41
|
+
for (const def of ast.definitions) {
|
|
42
|
+
if (def.kind === 'ObjectTypeDefinition' || def.kind === 'InputObjectTypeDefinition' || def.kind === 'InterfaceTypeDefinition') {
|
|
43
|
+
components.schemas[def.name.value] = this.typeDefToSchema(def as any);
|
|
44
|
+
} else if (def.kind === 'EnumTypeDefinition') {
|
|
45
|
+
components.schemas[def.name.value] = this.enumDefToSchema(def);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Second pass: extract endpoints from Query, Mutation, Subscription
|
|
50
|
+
for (const def of ast.definitions) {
|
|
51
|
+
if (def.kind === 'ObjectTypeDefinition' && ['Query', 'Mutation', 'Subscription'].includes(def.name.value)) {
|
|
52
|
+
const typeName = def.name.value;
|
|
53
|
+
if (def.fields) {
|
|
54
|
+
for (const field of def.fields) {
|
|
55
|
+
endpoints.push(this.fieldToEndpoint(field, typeName));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
meta,
|
|
63
|
+
servers: [],
|
|
64
|
+
endpoints,
|
|
65
|
+
components,
|
|
66
|
+
security: []
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private typeDefToSchema(def: ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode): Schema {
|
|
71
|
+
const properties: Record<string, Schema> = {};
|
|
72
|
+
const required: string[] = [];
|
|
73
|
+
|
|
74
|
+
if (def.fields) {
|
|
75
|
+
for (const field of def.fields) {
|
|
76
|
+
const fieldName = field.name.value;
|
|
77
|
+
properties[fieldName] = this.resolveTypeNode(field.type);
|
|
78
|
+
if (field.type.kind === 'NonNullType') {
|
|
79
|
+
required.push(fieldName);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const schema: Schema = {
|
|
85
|
+
type: 'object',
|
|
86
|
+
properties
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
if (required.length > 0) {
|
|
90
|
+
schema.required = required;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return schema;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private enumDefToSchema(def: EnumTypeDefinitionNode): Schema {
|
|
97
|
+
const values = def.values?.map(v => v.name.value) || [];
|
|
98
|
+
return {
|
|
99
|
+
type: 'string',
|
|
100
|
+
enum: values
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private resolveTypeNode(typeNode: TypeNode): Schema {
|
|
105
|
+
if (typeNode.kind === 'NonNullType') {
|
|
106
|
+
return this.resolveTypeNode(typeNode.type);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeNode.kind === 'ListType') {
|
|
110
|
+
return {
|
|
111
|
+
type: 'array',
|
|
112
|
+
items: this.resolveTypeNode(typeNode.type)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// NamedType
|
|
117
|
+
const typeName = typeNode.name.value;
|
|
118
|
+
switch (typeName) {
|
|
119
|
+
case 'Int':
|
|
120
|
+
return { type: 'integer' };
|
|
121
|
+
case 'Float':
|
|
122
|
+
return { type: 'number' };
|
|
123
|
+
case 'String':
|
|
124
|
+
case 'ID':
|
|
125
|
+
return { type: 'string' };
|
|
126
|
+
case 'Boolean':
|
|
127
|
+
return { type: 'boolean' };
|
|
128
|
+
default:
|
|
129
|
+
// Reference to another schema
|
|
130
|
+
return {
|
|
131
|
+
type: 'object',
|
|
132
|
+
$ref: `#/components/schemas/${typeName}`
|
|
133
|
+
} as any;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private fieldToEndpoint(field: FieldDefinitionNode, parentType: string): Endpoint {
|
|
138
|
+
const path = `${parentType}.${field.name.value}`;
|
|
139
|
+
const id = `POST:${path.toLowerCase()}`;
|
|
140
|
+
|
|
141
|
+
const parameters: Parameter[] = [];
|
|
142
|
+
if (field.arguments) {
|
|
143
|
+
for (const arg of field.arguments) {
|
|
144
|
+
parameters.push({
|
|
145
|
+
name: arg.name.value,
|
|
146
|
+
in: 'query', // Map GraphQL args to 'query'
|
|
147
|
+
required: arg.type.kind === 'NonNullType',
|
|
148
|
+
deprecated: !!arg.directives?.some(d => d.name.value === 'deprecated'),
|
|
149
|
+
description: arg.description?.value,
|
|
150
|
+
schema: this.resolveTypeNode(arg.type)
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const responses: ResponseDef[] = [
|
|
156
|
+
{
|
|
157
|
+
statusCode: '200',
|
|
158
|
+
description: 'Successful response',
|
|
159
|
+
content: {
|
|
160
|
+
'application/graphql': {
|
|
161
|
+
schema: this.resolveTypeNode(field.type)
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
headers: {}
|
|
165
|
+
}
|
|
166
|
+
];
|
|
167
|
+
|
|
168
|
+
return {
|
|
169
|
+
id,
|
|
170
|
+
path,
|
|
171
|
+
method: 'POST',
|
|
172
|
+
summary: field.description?.value,
|
|
173
|
+
tags: [parentType],
|
|
174
|
+
deprecated: !!field.directives?.some(d => d.name.value === 'deprecated'),
|
|
175
|
+
security: [],
|
|
176
|
+
parameters,
|
|
177
|
+
responses,
|
|
178
|
+
extensions: {}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import yaml from 'js-yaml';
|
|
2
|
+
import { ParseError, FormatError } from '../types/errors.js';
|
|
3
|
+
import type { NormalizedAST, SpecFormat } from '../types/index.js';
|
|
4
|
+
import type { RawSpec } from '../loader/index.js';
|
|
5
|
+
import type { ISpecParser } from './base.js';
|
|
6
|
+
|
|
7
|
+
import { OpenApi3Parser } from './openapi3/index.js';
|
|
8
|
+
import { OpenApi2Parser } from './openapi2/index.js';
|
|
9
|
+
import { ProtobufParser } from './protobuf/index.js';
|
|
10
|
+
import { GraphqlParser } from './graphql/index.js';
|
|
11
|
+
|
|
12
|
+
const parsers: ISpecParser[] = [
|
|
13
|
+
new OpenApi3Parser(),
|
|
14
|
+
new OpenApi2Parser(),
|
|
15
|
+
new ProtobufParser(),
|
|
16
|
+
new GraphqlParser()
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export function registerParser(parser: ISpecParser) {
|
|
20
|
+
parsers.push(parser);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function detectFormat(raw: RawSpec): SpecFormat {
|
|
24
|
+
if (raw.format) return raw.format;
|
|
25
|
+
|
|
26
|
+
const content = raw.content.trim();
|
|
27
|
+
|
|
28
|
+
if (content.startsWith('syntax = "proto')) return 'protobuf';
|
|
29
|
+
if (content.includes('type Query') || content.includes('type Mutation')) return 'graphql';
|
|
30
|
+
|
|
31
|
+
let parsed: any;
|
|
32
|
+
try {
|
|
33
|
+
parsed = JSON.parse(content);
|
|
34
|
+
} catch {
|
|
35
|
+
try {
|
|
36
|
+
parsed = yaml.load(content);
|
|
37
|
+
} catch {
|
|
38
|
+
// not yaml or json
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (parsed && typeof parsed === 'object') {
|
|
43
|
+
if (parsed.openapi && typeof parsed.openapi === 'string' && parsed.openapi.startsWith('3.')) {
|
|
44
|
+
return 'openapi3';
|
|
45
|
+
}
|
|
46
|
+
if (parsed.swagger && typeof parsed.swagger === 'string' && parsed.swagger.startsWith('2.')) {
|
|
47
|
+
return 'openapi2';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
throw new FormatError('Could not detect format. Use --format openapi3|openapi2|protobuf|graphql');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function parseSpec(raw: RawSpec): Promise<NormalizedAST> {
|
|
55
|
+
const format = detectFormat(raw);
|
|
56
|
+
const parser = parsers.find(p => p.format === format);
|
|
57
|
+
if (!parser) {
|
|
58
|
+
throw new FormatError(`Parser for format '${format}' not implemented yet.`);
|
|
59
|
+
}
|
|
60
|
+
return parser.parse(raw);
|
|
61
|
+
}
|