forked from ljacobsson/evb-cli
/
schema-browser.js
117 lines (103 loc) · 3.18 KB
/
schema-browser.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
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
const inputUtil = require("./input-util");
const YAML = require("json-to-pretty-yaml");
const { DescribeSchemaCommand, ListSchemasCommand, SchemasClient } = require("@aws-sdk/client-schemas");
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
function deepMerge(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} });
deepMerge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
}
}
return deepMerge(target, ...sources);
}
function outputPattern(output, format) {
console.log("Generated output:");
if (!format || format === "json") {
console.log(JSON.stringify(output, null, 2));
} else {
console.log(YAML.stringify(output, null, 2));
}
}
async function* ListSchemas(schemas, params) {
let token;
let errorOccurred = false;
do {
const listSchemasParams = {
...params,
NextToken: token,
};
const response = await schemas.send(new ListSchemasCommand(listSchemasParams));
if (!response.$metadata.httpStatusCode || response.$metadata.httpStatusCode !== 200) {
console.error("Error listing schemas:", response);
errorOccurred = true;
break; // Exit the loop in case of an error
}
yield response.Schemas;
({ NextToken: token } = response);
} while (token && !errorOccurred);
}
async function getSchema() {
const schemas = new SchemasClient();
const { registry, schemaName, sourceName } = await getSchemaName();
const describeSchemaResponse = await schemas.send(
new DescribeSchemaCommand({
RegistryName: registry.id,
SchemaName: schemaName,
})
);
const schema = JSON.parse(describeSchemaResponse.Content);
return { sourceName, schema };
}
async function getSchemaName() {
const schemas = new SchemasClient();
const registry = await inputUtil.getRegistry(schemas);
const schemaList = [];
for await (schemaItem of ListSchemas(schemas, {
RegistryName: registry.id,
})) {
schemaList.push(...schemaItem);
}
const sourceName = await inputUtil.getSourceName(schemaList);
const detailTypeName = await inputUtil.getDetailTypeName(
schemaList,
sourceName
);
const schemaName = `${sourceName}@${detailTypeName}`.replace(/\//g, "-");
return { registry, schemaName, sourceName };
}
function findCurrent(path, schema) {
const pathArray = path.split("/");
pathArray.shift(); // Remove leading #
let current = schema;
for (var node of pathArray) {
current = current[node];
}
return current;
}
async function getForPattern(pattern) {
const schemas = new SchemasClient();
const registry = await inputUtil.getRegistry();
const params = {
SchemaName: `${pattern.source[0]}@${pattern["detail-type"][0].replace(/ /g, "")}`,
RegistryName: registry.id,
};
const schema = await schemas.send(new DescribeSchemaCommand(params));
return schema;
}
module.exports = {
deepMerge,
getSchema,
outputPattern,
findCurrent,
getSchemaName,
getForPattern
};