-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathparser.ts
73 lines (69 loc) · 2.41 KB
/
parser.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
import $RefParser from '@apidevtools/json-schema-ref-parser';
import type { ParserOptions as $RefParserOptions } from '@apidevtools/json-schema-ref-parser';
import type { AsyncAPIObject, Options as BundlerOptions } from './spec-types';
let RefParserOptions: $RefParserOptions;
/**
* Function fully dereferences the provided AsyncAPI Document.
* @param {Object[]} JSONSchema
* @param {number} specVersion
* @param {Object} options
* @private
*/
export async function parse(
JSONSchema: AsyncAPIObject,
specVersion: number,
options: BundlerOptions = {}
) {
/* eslint-disable indent */
// It is assumed that there will be major Spec versions 4, 5 and on.
switch (specVersion) {
case 2:
RefParserOptions = {
dereference: {
circular: false,
// prettier-ignore
excludedPathMatcher: (path: string): any => { // eslint-disable-line
return;
},
onDereference: (path: string, value: AsyncAPIObject) => {
if (options.xOrigin === true) {
value['x-origin'] = path;
}
},
},
};
break;
case 3:
RefParserOptions = {
dereference: {
circular: false,
excludedPathMatcher: (path: string): any => {
return (
// prettier-ignore
(/#\/channels\/.*\/servers/).test(path) ||
(/#\/operations\/.*\/channel/).test(path) ||
(/#\/operations\/.*\/messages/).test(path) ||
(/#\/operations\/.*\/reply\/channel/).test(path) ||
(/#\/operations\/.*\/reply\/messages/).test(path) ||
(/#\/components\/channels\/.*\/servers/).test(path) ||
(/#\/components\/operations\/.*\/channel/).test(path) ||
(/#\/components\/operations\/.*\/messages/).test(path) ||
(/#\/components\/operations\/.*\/reply\/channel/).test(path) ||
(/#\/components\/operations\/.*\/reply\/messages/).test(path)
);
},
onDereference: (path: string, value: AsyncAPIObject) => {
if (options.xOrigin === true) {
value['x-origin'] = path;
}
},
},
};
break;
default:
console.error(
`There is no support for AsyncAPI Specification v${specVersion}.`
);
}
return await $RefParser.dereference(JSONSchema, RefParserOptions) as AsyncAPIObject;
}