/
adapter.ts
36 lines (29 loc) · 1.42 KB
/
adapter.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
import { omit, propOr } from 'ramda';
import { isNotUndefined } from 'ramda-adjunct';
import { ParseResultElement, createNamespace } from '@swagger-api/apidom-core';
import {
parse as parseYAML,
detect as detectYAML,
} from '@swagger-api/apidom-parser-adapter-yaml-1-2';
import asyncApiNamespace, { AsyncApi2Element } from '@swagger-api/apidom-ns-asyncapi-2';
export { default as mediaTypes } from './media-types';
export const detectionRegExp =
/(?<YAML>^(["']?)asyncapi\2\s*:\s*(["']?)(?<version_yaml>2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?<JSON>"asyncapi"\s*:\s*"(?<version_json>2\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m;
export const detect = async (source: string): Promise<boolean> =>
detectionRegExp.test(source) && (await detectYAML(source));
export const parse = async (
source: string,
options: Record<string, unknown> = {},
): Promise<ParseResultElement> => {
const refractorOpts: Record<string, unknown> = propOr({}, 'refractorOpts', options);
const parserOpts = omit(['refractorOpts'], options);
const parseResultElement = await parseYAML(source, parserOpts);
const { result } = parseResultElement;
if (isNotUndefined(result)) {
const asyncApiElement = AsyncApi2Element.refract(result, refractorOpts);
asyncApiElement.classes.push('result');
parseResultElement.replaceResult(asyncApiElement);
}
return parseResultElement;
};
export const namespace = createNamespace(asyncApiNamespace);