apiDiff.js
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
* API 对比工具
*
* 功能:
* 1. 对比两个 OpenAPI 文档的差异
* 2. 检测破坏性变更
* 3. 生成详细的变更报告
*
* 使用方式:
* node scripts/apiDiff.js <oldPath> <newPath>
*/
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
/**
* 从 Markdown 文件中提取 YAML
*/
function extractYAMLFromMarkdown(content) {
const yamlRegex = /```yaml\s*\n([\s\S]*?)\n```/;
const match = content.match(yamlRegex);
return match ? match[1] : null;
}
/**
* 解析 OpenAPI 文档(支持 .md 和目录)
*/
function parseOpenAPIPath(filePath) {
const stat = fs.statSync(filePath);
if (stat.isFile()) {
// 单个文件
if (filePath.endsWith('.md')) {
const content = fs.readFileSync(filePath, 'utf8');
const yamlContent = extractYAMLFromMarkdown(content);
if (!yamlContent) {
throw new Error(`文件 ${filePath} 中未找到 YAML 代码块`);
}
return [yaml.load(yamlContent)];
} else if (filePath.endsWith('.js')) {
// TODO: 支持对比生成的 JS 文件(需要解析 AST)
throw new Error('暂不支持对比生成的 JS 文件,请对比 OpenAPI 文档');
} else {
throw new Error(`不支持的文件类型: ${filePath}`);
}
} else if (stat.isDirectory()) {
// 目录,读取所有 .md 文件
const files = fs.readdirSync(filePath).filter(f => f.endsWith('.md'));
const docs = [];
files.forEach(file => {
const fullPath = path.join(filePath, file);
const content = fs.readFileSync(fullPath, 'utf8');
const yamlContent = extractYAMLFromMarkdown(content);
if (yamlContent) {
const doc = yaml.load(yamlContent);
// 保存文件名用于标识
doc._fileName = path.basename(file, '.md');
docs.push(doc);
}
});
return docs;
}
}
/**
* 从 OpenAPI 文档提取 API 信息
*/
function extractAPIInfo(openapiDoc) {
const path = Object.keys(openapiDoc.paths)[0];
const method = Object.keys(openapiDoc.paths[path])[0];
const apiInfo = openapiDoc.paths[path][method];
// 提取参数
const queryParams = (apiInfo.parameters || [])
.filter(p => p.in === 'query' && p.name !== 'a' && p.name !== 'f')
.map(p => ({
name: p.name,
type: p.schema?.type || 'any',
required: p.required || false,
description: p.description || '',
}));
// 提取 body 参数
const bodyParams = [];
if (apiInfo.requestBody && apiInfo.requestBody.content) {
const content = apiInfo.requestBody.content['application/x-www-form-urlencoded'] ||
apiInfo.requestBody.content['application/json'];
if (content && content.schema && content.schema.properties) {
Object.entries(content.schema.properties).forEach(([key, value]) => {
if (key !== 'a' && key !== 'f') {
bodyParams.push({
name: key,
type: value.type || 'any',
required: content.schema.required?.includes(key) || false,
description: value.description || '',
});
}
});
}
}
// 提取响应结构
const responseSchema = apiInfo.responses?.['200']?.content?.['application/json']?.schema;
return {
name: openapiDoc._fileName || 'unknown',
path,
method: method.toUpperCase(),
queryParams: new Set(queryParams.map(p => p.name)),
bodyParams: new Set(bodyParams.map(p => p.name)),
requiredQueryParams: new Set(queryParams.filter(p => p.required).map(p => p.name)),
requiredBodyParams: new Set(bodyParams.filter(p => p.required).map(p => p.name)),
allQueryParams: queryParams,
allBodyParams: bodyParams,
responseSchema,
summary: apiInfo.summary || '',
};
}
/**
* 对比两个 API 信息
*/
function compareAPI(oldAPI, newAPI) {
const changes = {
breaking: [],
nonBreaking: [],
};
// 检查 HTTP 方法变更
if (oldAPI.method !== newAPI.method) {
changes.breaking.push(`HTTP 方法变更: ${oldAPI.method} → ${newAPI.method}`);
}
// 检查 GET 参数变更
oldAPI.allQueryParams.forEach(oldParam => {
const newParam = newAPI.allQueryParams.find(p => p.name === oldParam.name);
if (!newParam) {
// 参数被删除
if (oldAPI.requiredQueryParams.has(oldParam.name)) {
changes.breaking.push(`删除必填 query 参数: ${oldParam.name}`);
} else {
changes.nonBreaking.push(`删除可选 query 参数: ${oldParam.name}`);
}
} else {
// 参数类型变更
if (oldParam.type !== newParam.type) {
changes.breaking.push(`query 参数类型变更: ${oldParam.name} (${oldParam.type} → ${newParam.type})`);
}
// 可选 → 必填
if (!oldParam.required && newParam.required) {
changes.breaking.push(`query 参数变为必填: ${newParam.name}`);
}
// 必填 → 可选
if (oldParam.required && !newParam.required) {
changes.nonBreaking.push(`query 参数变为可选: ${newParam.name}`);
}
}
});
// 检查新增 GET 参数
newAPI.allQueryParams.forEach(newParam => {
const oldParam = oldAPI.allQueryParams.find(p => p.name === newParam.name);
if (!oldParam) {
if (newParam.required) {
changes.breaking.push(`新增必填 query 参数: ${newParam.name}`);
} else {
changes.nonBreaking.push(`新增可选 query 参数: ${newParam.name}`);
}
}
});
// 检查 POST body 参数变更
oldAPI.allBodyParams.forEach(oldParam => {
const newParam = newAPI.allBodyParams.find(p => p.name === oldParam.name);
if (!newParam) {
// 参数被删除
if (oldAPI.requiredBodyParams.has(oldParam.name)) {
changes.breaking.push(`删除必填 body 参数: ${oldParam.name}`);
} else {
changes.nonBreaking.push(`删除可选 body 参数: ${oldParam.name}`);
}
} else {
// 参数类型变更
if (oldParam.type !== newParam.type) {
changes.breaking.push(`body 参数类型变更: ${oldParam.name} (${oldParam.type} → ${newParam.type})`);
}
// 可选 → 必填
if (!oldParam.required && newParam.required) {
changes.breaking.push(`body 参数变为必填: ${newParam.name}`);
}
// 必填 → 可选
if (oldParam.required && !newParam.required) {
changes.nonBreaking.push(`body 参数变为可选: ${newParam.name}`);
}
}
});
// 检查新增 body 参数
newAPI.allBodyParams.forEach(newParam => {
const oldParam = oldAPI.allBodyParams.find(p => p.name === newParam.name);
if (!oldParam) {
if (newParam.required) {
changes.breaking.push(`新增必填 body 参数: ${newParam.name}`);
} else {
changes.nonBreaking.push(`新增可选 body 参数: ${newParam.name}`);
}
}
});
return changes;
}
/**
* 生成变更报告
*/
function generateReport(oldDocs, newDocs, format = 'text') {
const oldAPIs = oldDocs.map(extractAPIInfo);
const newAPIs = newDocs.map(extractAPIInfo);
const oldAPIsMap = new Map(oldAPIs.map(api => [api.name, api]));
const newAPIsMap = new Map(newAPIs.map(api => [api.name, api]));
const addedAPIs = [];
const removedAPIs = [];
const modifiedAPIs = [];
// 检测新增接口
newAPIs.forEach(api => {
if (!oldAPIsMap.has(api.name)) {
addedAPIs.push(api);
}
});
// 检测删除接口
oldAPIs.forEach(api => {
if (!newAPIsMap.has(api.name)) {
removedAPIs.push(api);
}
});
// 检测修改接口
newAPIs.forEach(api => {
const oldAPI = oldAPIsMap.get(api.name);
if (oldAPI) {
const changes = compareAPI(oldAPI, api);
if (changes.breaking.length > 0 || changes.nonBreaking.length > 0) {
modifiedAPIs.push({
name: api.name,
summary: api.summary,
changes,
});
}
}
});
// 统计
const totalBreaking = modifiedAPIs.reduce(
(sum, api) => sum + api.changes.breaking.length,
0
);
// 生成文本报告
if (format === 'text') {
const lines = [];
lines.push('=== API 变更检测报告 ===\n');
lines.push(`📦 对比范围: ${oldAPIs.length} 个旧接口 → ${newAPIs.length} 个新接口`);
lines.push('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
if (addedAPIs.length > 0) {
lines.push(`✅ 新增接口 (${addedAPIs.length}):`);
addedAPIs.forEach(api => {
lines.push(` + ${api.name} - ${api.summary}`);
});
lines.push('');
}
if (modifiedAPIs.length > 0) {
lines.push(`⚠️ 修改接口 (${modifiedAPIs.length}):`);
modifiedAPIs.forEach(api => {
lines.push(` ↪ ${api.name} - ${api.summary}`);
api.changes.breaking.forEach(change => {
lines.push(` ✗ [破坏性] ${change}`);
});
api.changes.nonBreaking.forEach(change => {
lines.push(` ✓ [非破坏性] ${change}`);
});
});
lines.push('');
}
if (removedAPIs.length > 0) {
lines.push(`❌ 删除接口 (${removedAPIs.length}):`);
removedAPIs.forEach(api => {
lines.push(` - ${api.name} - ${api.summary}`);
});
lines.push('');
}
lines.push('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
lines.push(`总计: ${addedAPIs.length} 新增, ${modifiedAPIs.length} 修改, ${removedAPIs.length} 删除`);
if (totalBreaking > 0) {
lines.push(`⚠️ 检测到 ${totalBreaking} 个破坏性变更,请仔细检查业务逻辑!`);
} else if (addedAPIs.length > 0 || modifiedAPIs.length > 0 || removedAPIs.length > 0) {
lines.push('✅ 未检测到破坏性变更');
} else {
lines.push('✅ 无接口变更');
}
return lines.join('\n');
}
// 生成 JSON 报告
if (format === 'json') {
return JSON.stringify({
summary: {
added: addedAPIs.length,
modified: modifiedAPIs.length,
removed: removedAPIs.length,
breakingChanges: totalBreaking,
},
added: addedAPIs.map(api => ({
name: api.name,
summary: api.summary,
method: api.method,
path: api.path,
})),
modified: modifiedAPIs.map(api => ({
name: api.name,
summary: api.summary,
breakingChanges: api.changes.breaking,
nonBreakingChanges: api.changes.nonBreaking,
})),
removed: removedAPIs.map(api => ({
name: api.name,
summary: api.summary,
method: api.method,
path: api.path,
})),
}, null, 2);
}
}
/**
* 主函数
*/
function main() {
const args = process.argv.slice(2);
if (args.length < 2) {
console.error('用法: node scripts/apiDiff.js <oldPath> <newPath>');
console.error('示例:');
console.error(' node scripts/apiDiff.js docs/api-specs/user/ docs/api-specs/user-new/');
console.error(' node scripts/apiDiff.js docs/api-specs/user/api1.md docs/api-specs/user/api1-new.md');
process.exit(1);
}
const [oldPath, newPath] = args;
if (!fs.existsSync(oldPath)) {
console.error(`❌ 旧路径不存在: ${oldPath}`);
process.exit(1);
}
if (!fs.existsSync(newPath)) {
console.error(`❌ 新路径不存在: ${newPath}`);
process.exit(1);
}
try {
const oldDocs = parseOpenAPIPath(oldPath);
const newDocs = parseOpenAPIPath(newPath);
const format = process.env.API_DIFF_FORMAT || 'text';
const report = generateReport(oldDocs, newDocs, format);
console.log(report);
// 如果有破坏性变更,返回退出码 1
const oldAPIs = oldDocs.map(extractAPIInfo);
const newAPIs = newDocs.map(extractAPIInfo);
const oldAPIsMap = new Map(oldAPIs.map(api => [api.name, api]));
const newAPIsMap = new Map(newAPIs.map(api => [api.name, api]));
let totalBreaking = 0;
newAPIs.forEach(api => {
const oldAPI = oldAPIsMap.get(api.name);
if (oldAPI) {
const changes = compareAPI(oldAPI, api);
totalBreaking += changes.breaking.length;
}
});
// 严格模式:任何变更都返回 1
const strictMode = process.env.API_DIFF_STRICT === 'true';
const hasChanges = oldAPIs.length !== newAPIs.length ||
newAPIs.some(api => !oldAPIsMap.has(api.name)) ||
oldAPIs.some(api => !newAPIsMap.has(api.name));
if (totalBreaking > 0 || (strictMode && hasChanges)) {
process.exit(1);
} else {
process.exit(0);
}
} catch (error) {
console.error(`❌ 对比失败: ${error.message}`);
process.exit(1);
}
}
// 如果直接运行此脚本
if (require.main === module) {
main();
}
module.exports = {
compareAPI,
generateReport,
parseOpenAPIPath,
extractAPIInfo,
};