-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resolve): add ApiDOM JSON parser (#2739)
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { from, ParseResultElement } from '@swagger-api/apidom-core'; | ||
import { ParserError } from '@swagger-api/apidom-reference/configuration/empty'; | ||
|
||
const JsonParser = { | ||
name: 'json-swagger-client', | ||
fileExtensions: [], | ||
mediaTypes: ['application/json'], | ||
|
||
async canParse(file) { | ||
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) { | ||
try { | ||
JSON.parse(file.toString()); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
return false; | ||
}, | ||
|
||
async parse(file) { | ||
if (this.sourceMap) { | ||
// eslint-disable-next-line no-console | ||
console.warn("json-swagger-client parser plugin doesn't support sourceMaps option"); | ||
} | ||
|
||
const source = file.toString(); | ||
|
||
try { | ||
const element = from(JSON.parse(source)); | ||
const parseResultElement = new ParseResultElement(); | ||
|
||
element.classes.push('result'); | ||
parseResultElement.push(element); | ||
return parseResultElement; | ||
} catch (error) { | ||
throw new ParserError(`Error parsing "${file.uri}"`, error); | ||
} | ||
}, | ||
}; | ||
|
||
export default JsonParser; |