-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
89 lines (79 loc) · 3.37 KB
/
index.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {
OpenApi3_0Element,
mediaTypes as openAPI3_0MediaTypes,
} from '@swagger-api/apidom-ns-openapi-3-0';
import {
isOpenApi3_1Element,
mediaTypes as openAPI3_1MediaTypes,
keyMap,
getNodeType,
} from '@swagger-api/apidom-ns-openapi-3-1';
import {
ParseResultElement,
dispatchRefractorPlugins,
AnnotationElement,
cloneDeep,
} from '@swagger-api/apidom-core';
import ConvertStrategy, { IFile } from '../ConvertStrategy';
import openAPIVersionRefractorPlugin from './refractor-plugins/openapi-version';
import webhooksRefractorPlugin from './refractor-plugins/webhooks';
import securitySchemeTypeRefractorPlugin from './refractor-plugins/security-scheme-type';
import securityRequirementsEmptyRolesRefractorPlugin from './refractor-plugins/security-requirements-empty-roles';
import type { ConverterOptions } from '../../options';
import createToolbox from './toolbox';
import infoSummaryRefractorPlugin from './refractor-plugins/info-summary';
import licenseIdentifierRefractorPlugin from './refractor-plugins/license-identifier';
import referenceDescriptionRefractorPlugin from './refractor-plugins/reference-description';
import referenceSummaryRefractorPlugin from './refractor-plugins/reference-summary';
// eslint-disable-next-line @typescript-eslint/naming-convention
const openAPI3_0_3MediaTypes = [
openAPI3_0MediaTypes.findBy('3.0.3', 'generic'),
openAPI3_0MediaTypes.findBy('3.0.3', 'json'),
openAPI3_0MediaTypes.findBy('3.0.3', 'yaml'),
];
/* eslint-disable class-methods-use-this */
class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy {
constructor() {
super({ name: 'openapi-3-1-to-openapi-3-0-3' });
}
canConvert(file: IFile, options: ConverterOptions): boolean {
let hasRecognizedSourceMediaType = false;
const hasRecognizedTargetMediaType = openAPI3_0_3MediaTypes.includes(
options.convert.targetMediaType,
);
// source detection
if (openAPI3_1MediaTypes.includes(options.convert.sourceMediaType)) {
hasRecognizedSourceMediaType = true;
} else if (file.mediaType !== 'text/plain') {
hasRecognizedSourceMediaType = openAPI3_1MediaTypes.includes(file.mediaType);
} else if (isOpenApi3_1Element(file.parseResult?.result)) {
hasRecognizedSourceMediaType = true;
}
return hasRecognizedSourceMediaType && hasRecognizedTargetMediaType;
}
async convert(file: IFile): Promise<ParseResultElement> {
const annotations: AnnotationElement[] = [];
const parseResultElement = dispatchRefractorPlugins(
cloneDeep(file.parseResult),
[
openAPIVersionRefractorPlugin(),
webhooksRefractorPlugin({ annotations }),
securitySchemeTypeRefractorPlugin({ annotations }),
securityRequirementsEmptyRolesRefractorPlugin({ annotations }),
infoSummaryRefractorPlugin({ annotations }),
licenseIdentifierRefractorPlugin({ annotations }),
referenceDescriptionRefractorPlugin({ annotations }),
referenceSummaryRefractorPlugin({ annotations }),
],
{
toolboxCreator: createToolbox,
visitorOptions: { keyMap, nodeTypeGetter: getNodeType },
},
);
annotations.forEach((a) => parseResultElement.push(a));
parseResultElement.replaceResult(OpenApi3_0Element.refract(parseResultElement.api));
return parseResultElement;
}
}
/* eslint-enable class-methods-use-this */
export default OpenAPI31ToOpenAPI30ConvertStrategy;