1 | "use strict";
|
---|
2 |
|
---|
3 | exports.__esModule = true;
|
---|
4 | exports.default = void 0;
|
---|
5 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
6 | var _empty = require("@swagger-api/apidom-reference/configuration/empty");
|
---|
7 | var _apidomNsOpenapi = require("@swagger-api/apidom-ns-openapi-3-1");
|
---|
8 | /* eslint-disable camelcase */
|
---|
9 |
|
---|
10 | const OpenApiJson3_1Parser = _empty.Parser.compose({
|
---|
11 | props: {
|
---|
12 | name: 'openapi-json-3-1-swagger-client',
|
---|
13 | fileExtensions: ['.json'],
|
---|
14 | mediaTypes: new _apidomNsOpenapi.OpenAPIMediaTypes(..._apidomNsOpenapi.mediaTypes.filterByFormat('generic'), ..._apidomNsOpenapi.mediaTypes.filterByFormat('json')),
|
---|
15 | detectionRegExp: /"openapi"\s*:\s*"(?<version_json>3\.1\.(?:[1-9]\d*|0))"/
|
---|
16 | },
|
---|
17 | methods: {
|
---|
18 | async canParse(file) {
|
---|
19 | const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
|
---|
20 | const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
|
---|
21 | if (!hasSupportedFileExtension) return false;
|
---|
22 | if (hasSupportedMediaType) return true;
|
---|
23 | if (!hasSupportedMediaType) {
|
---|
24 | try {
|
---|
25 | const source = file.toString();
|
---|
26 | JSON.parse(source);
|
---|
27 | return this.detectionRegExp.test(source);
|
---|
28 | } catch (error) {
|
---|
29 | return false;
|
---|
30 | }
|
---|
31 | }
|
---|
32 | return false;
|
---|
33 | },
|
---|
34 | async parse(file) {
|
---|
35 | if (this.sourceMap) {
|
---|
36 | throw new _empty.ParserError("openapi-json-3-1-swagger-client parser plugin doesn't support sourceMaps option");
|
---|
37 | }
|
---|
38 | const parseResultElement = new _apidomCore.ParseResultElement();
|
---|
39 | const source = file.toString();
|
---|
40 |
|
---|
41 | // allow empty files
|
---|
42 | if (this.allowEmpty && source.trim() === '') {
|
---|
43 | return parseResultElement;
|
---|
44 | }
|
---|
45 | try {
|
---|
46 | const pojo = JSON.parse(source);
|
---|
47 | const element = _apidomNsOpenapi.OpenApi3_1Element.refract(pojo, this.refractorOpts);
|
---|
48 | element.classes.push('result');
|
---|
49 | parseResultElement.push(element);
|
---|
50 | return parseResultElement;
|
---|
51 | } catch (error) {
|
---|
52 | throw new _empty.ParserError(`Error parsing "${file.uri}"`, {
|
---|
53 | cause: error
|
---|
54 | });
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 | });
|
---|
59 | var _default = exports.default = OpenApiJson3_1Parser;
|
---|
60 | /* eslint-enable camelcase */ |
---|