-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.ts
More file actions
57 lines (47 loc) · 1.73 KB
/
index.ts
File metadata and controls
57 lines (47 loc) · 1.73 KB
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
import { pick } from 'ramda';
import { ParseResultElement } from '@swagger-api/apidom-core';
import {
parse,
mediaTypes as AsyncAPI3MediaTypes,
detect,
} from '@swagger-api/apidom-parser-adapter-asyncapi-yaml-3';
import ParserError from '../../../errors/ParserError.ts';
import Parser, { ParserOptions } from '../Parser.ts';
import File from '../../../File.ts';
export type { default as Parser, ParserOptions } from '../Parser.ts';
export type { default as File, FileOptions } from '../../../File.ts';
/**
* @public
*/
export interface AsyncAPIYAML3ParserOptions extends Omit<ParserOptions, 'name'> {}
/**
* @public
*/
class AsyncAPIYAML3Parser extends Parser {
public refractorOpts!: object;
constructor(options?: AsyncAPIYAML3ParserOptions) {
const { fileExtensions = [], mediaTypes = AsyncAPI3MediaTypes, ...rest } = options ?? {};
super({ ...rest, name: 'asyncapi-yaml-3', fileExtensions, mediaTypes });
}
async canParse(file: File): Promise<boolean> {
const hasSupportedFileExtension =
this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
if (!hasSupportedFileExtension) return false;
if (hasSupportedMediaType) return true;
if (!hasSupportedMediaType) {
return detect(file.toString());
}
return false;
}
async parse(file: File): Promise<ParseResultElement> {
const source = file.toString();
try {
const parserOpts = pick(['sourceMap', 'refractorOpts'], this);
return await parse(source, parserOpts);
} catch (error: unknown) {
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
}
}
}
export default AsyncAPIYAML3Parser;