-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.js
78 lines (68 loc) · 2.17 KB
/
combine.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
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
/**
* @copyright Copyright 2016-2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
* @module procore-docs-to-openapi
*/
import assert from 'node:assert';
/** Combines OpenAPI Objects created by {@see
* ProcoreApiDocToOpenApiTransformer}.
*
* @param {!Array<!object>} openapiDocs Array of OpenAPI Objects produced
* by {@see ProcoreApiDocToOpenApiTransformer}.
* @returns {!object} OpenAPI Object with all information from openapiDocs.
* @throws {Error} If any object in openapiDocs contains properties not
* produced by {@see ProcoreApiDocToOpenApiTransformer}.
* @throws {Error} If the same path appears in multiple objects in openapiDocs.
*/
export default function combineOpenapi(openapiDocs) {
const combinedPaths = Object.create(null);
const tagsByName = new Map();
let firstVer;
for (const openapiDoc of openapiDocs) {
const {
openapi,
tags,
paths,
...unrecognized
} = openapiDoc;
if (!openapi.startsWith('3.')) {
throw new Error(`Unsupported OpenAPI version: ${openapi}`);
}
if (firstVer === undefined) {
firstVer = openapi;
} else if (openapi !== firstVer) {
// TODO: Ignore patch version and just compare major.minor?
throw new Error(`Can not combine different OpenAPI versions: ${
openapi} != ${firstVer}`);
}
const unrecognizedKeys = Object.keys(unrecognized);
if (unrecognizedKeys.length > 0) {
throw new Error(`Unsupported OpenAPI properties: ${unrecognizedKeys}`);
}
if (tags) {
for (const tag of tags) {
const tagName = tag.name;
const oldTag = tagsByName.get(tagName);
if (oldTag) {
assert.deepStrictEqual(tag, oldTag, `Tag ${tagName} must match`);
} else {
tagsByName.set(tagName, tag);
}
}
}
for (const [pathStr, pathObj] of Object.entries(paths)) {
if (hasOwnProperty.call(combinedPaths, pathStr)) {
throw new Error(`Duplicate path ${pathStr}`);
}
combinedPaths[pathStr] = pathObj;
}
}
if (firstVer === undefined) {
return undefined;
}
return {
openapi: firstVer,
tags: [...tagsByName.values()],
paths: combinedPaths,
};
}