-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add OpenAPI 2.0 YAML parser plugin
Refs #3100
- Loading branch information
Showing
8 changed files
with
1,016 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/apidom-reference/src/parse/parsers/openapi-yaml-2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import stampit from 'stampit'; | ||
import { pick } from 'ramda'; | ||
import { ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { parse, mediaTypes, detect } from '@swagger-api/apidom-parser-adapter-openapi-yaml-2'; | ||
|
||
import ParserError from '../../../errors/ParserError'; | ||
import { File as IFile, Parser as IParser } from '../../../types'; | ||
import Parser from '../Parser'; | ||
|
||
const OpenApiYaml2Parser: stampit.Stamp<IParser> = stampit(Parser, { | ||
props: { | ||
name: 'openapi-yaml-2', | ||
fileExtensions: ['.yaml', '.yml'], | ||
mediaTypes, | ||
}, | ||
methods: { | ||
async canParse(file: IFile): 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: IFile): Promise<ParseResultElement> { | ||
const source = file.toString(); | ||
|
||
try { | ||
const parserOpts = pick(['sourceMap', 'refractorOpts'], this); | ||
return await parse(source, parserOpts); | ||
} catch (error: any) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, { cause: error }); | ||
} | ||
}, | ||
}, | ||
}); | ||
|
||
export default OpenApiYaml2Parser; |
Oops, something went wrong.