forked from asyncapi/parser-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-unique-ids.ts
38 lines (35 loc) · 1.26 KB
/
apply-unique-ids.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
import { xParserObjectUniqueId } from '../constants';
/**
* This function applies unique ids for objects whose key's function as ids, ensuring that the key is part of the value.
*
* For v3; Apply unique ids to all channel's, operations, and message's.
*/
export function applyUniqueIds(structure: any) {
const asyncapiVersion = structure.asyncapi.charAt(0);
switch (asyncapiVersion) {
case '3':
applyUniqueIdToChannels(structure.channels);
applyUniqueIdToObjects(structure.operations);
if (structure.components) {
applyUniqueIdToObjects(structure.components.messages);
applyUniqueIdToObjects(structure.components.operations);
applyUniqueIdToChannels(structure.components.channels);
}
break;
}
}
function applyUniqueIdToChannels(channels: any) {
for (const [channelId, channel] of Object.entries((channels ?? {}) as Record<string, any>)) {
if (!channel[xParserObjectUniqueId]) {
channel[xParserObjectUniqueId] = channelId;
}
applyUniqueIdToObjects(channel.messages);
}
}
function applyUniqueIdToObjects(objects: any) {
for (const [objectKey, object] of Object.entries((objects ?? {}) as Record<string, any>)) {
if (!object[xParserObjectUniqueId]) {
object[xParserObjectUniqueId] = objectKey;
}
}
}