-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.ts
executable file
·64 lines (57 loc) · 1.8 KB
/
filter.ts
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
import { OpenAPIV3_1 } from "openapi-types";
async function readStdin() {
return new Promise<string>((resolve, reject) => {
let data = "";
process.stdin.setEncoding("utf8");
process.stdin.on("error", (err) => {
reject(err);
});
process.stdin.on("data", (chunk) => {
data += chunk;
});
process.stdin.on("end", () => {
resolve(data);
});
});
}
function filterOpenapi(openapi: OpenAPIV3_1.Document): OpenAPIV3_1.Document {
const allowedOperations = [
"listProjects",
"getProject",
"createProject",
"listClusters",
"getCluster",
"createCluster",
"deleteCluster",
"listClustersForAllProjects",
"createDatabaseUser",
"deleteDatabaseUser",
"listDatabaseUsers",
"listProjectIpAccessLists",
"createProjectIpAccessList",
"deleteProjectIpAccessList",
];
const filteredPaths = {};
for (const path in openapi.paths) {
const filteredMethods = {} as OpenAPIV3_1.PathItemObject;
for (const method in openapi.paths[path]) {
if (allowedOperations.includes(openapi.paths[path][method].operationId)) {
filteredMethods[method] = openapi.paths[path][method];
}
}
if (Object.keys(filteredMethods).length > 0) {
filteredPaths[path] = filteredMethods;
}
}
return { ...openapi, paths: filteredPaths };
}
async function main() {
const openapiText = await readStdin();
const openapi = JSON.parse(openapiText) as OpenAPIV3_1.Document;
const filteredOpenapi = filterOpenapi(openapi);
console.log(JSON.stringify(filteredOpenapi));
}
main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});