-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.ts
79 lines (69 loc) · 1.86 KB
/
util.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { cloneDeep } from 'lodash';
import yaml from 'js-yaml';
import { parse } from './parser';
import { ParserError } from './errors';
import type { AsyncAPIObject } from './spec-types';
/**
* @private
*/
export const toJS = (asyncapiYAMLorJSON: string | object) => {
if (!asyncapiYAMLorJSON) {
throw new ParserError({
type: 'null-or-falsy-document',
title: "Document can't be null or falsey.", // eslint-disable-line
});
}
if (
asyncapiYAMLorJSON.constructor &&
asyncapiYAMLorJSON.constructor.name === 'Object'
) {
return asyncapiYAMLorJSON;
}
if (typeof asyncapiYAMLorJSON !== 'string') {
throw new ParserError({
type: 'invalid-document-type',
title: 'The AsyncAPI document has to be either a string or a JS object.',
});
}
if (JSON.stringify(asyncapiYAMLorJSON).trimStart().startsWith('{')) {
throw new ParserError({
type: 'invalid-yaml',
title: 'The provided yaml is not valid.',
});
}
return yaml.load(asyncapiYAMLorJSON);
};
/**
* @private
*/
export const validate = async (
parsedJSONs: AsyncAPIObject[],
parser: { parse(asyncapi: string | any): Promise<any> }
) => {
for (const parsedJSON of parsedJSONs) {
await parser.parse(cloneDeep(parsedJSON));
}
};
/**
*
* @param {Object} asyncapiDocuments
* @param {Object} options
* @param {boolean} options.referenceIntoComponents
* @returns {Array<Object>}
* @private
*/
export const resolve = async (
asyncapiDocuments: AsyncAPIObject[],
options: any
) => {
const docs = [];
for (const asyncapiDocument of asyncapiDocuments) {
if (options.referenceIntoComponents) {
await parse(asyncapiDocument);
}
const bundledAsyncAPIDocument = await $RefParser.bundle(asyncapiDocument);
docs.push(bundledAsyncAPIDocument);
}
return docs;
};