generated from n8n-io/n8n-nodes-starter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
merge-openapi.js
46 lines (36 loc) · 1.04 KB
/
merge-openapi.js
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
const { merge, isErrorResult } = require('openapi-merge');
const fs = require('fs');
const yaml = require('js-yaml');
function loadJsonFile(filePath) {
const content = fs.readFileSync(filePath, 'utf8');
return JSON.parse(content);
}
function parseYmlStr(ymlStr) {
// parse string to object
return yaml.load(ymlStr);
}
function parseOpenApiList(list) {
const openApiList = list.map((item) => {
return parseYmlStr(item.yml);
});
return openApiList;
}
function main() {
const list = loadJsonFile('./api/v2/api.json');
const openApiList = parseOpenApiList(list[0].data);
const openApiOptions = openApiList.map((item) => {
return {
oas: item,
}
});
const mergeResult = merge(openApiOptions);
if (isErrorResult(mergeResult)) {
// Oops, something went wrong
console.error(`${mergeResult.message} (${mergeResult.type})`);
} else {
console.log(`Merge successful!`);
}
// Save the merged OpenAPI document to openapi.yml
fs.writeFileSync('./openapi.yml', yaml.dump(mergeResult.output));
}
main();