-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
47 lines (38 loc) · 1.61 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
import { Mixin } from 'ts-mixer';
import { ObjectElement } from '@swagger-api/apidom-core';
import {
isJSONReferenceLikeElement,
isJSONReferenceElement,
JSONReferenceElement,
} from '@swagger-api/apidom-ns-json-schema-draft-4';
import DefinitionsElement from '../../../../elements/Definitions';
import MapVisitor, { MapVisitorOptions, SpecPath } from '../../generics/MapVisitor';
import FallbackVisitor, { FallbackVisitorOptions } from '../../FallbackVisitor';
export interface DefinitionsVisitorOptions extends MapVisitorOptions, FallbackVisitorOptions {}
class DefinitionsVisitor extends Mixin(MapVisitor, FallbackVisitor) {
public declare readonly element: DefinitionsElement;
protected declare readonly specPath: SpecPath<
['document', 'objects', 'JSONReference'] | ['document', 'objects', 'Schema']
>;
constructor(options: DefinitionsVisitorOptions) {
super(options);
this.element = new DefinitionsElement();
this.specPath = (element: unknown) => {
return isJSONReferenceLikeElement(element)
? ['document', 'objects', 'JSONReference']
: ['document', 'objects', 'Schema'];
};
}
ObjectElement(objectElement: ObjectElement) {
const result = MapVisitor.prototype.ObjectElement.call(this, objectElement);
// decorate every JSONReferenceElement with metadata about their referencing type
this.element
.filter(isJSONReferenceElement)
// @ts-ignore
.forEach((referenceElement: JSONReferenceElement) => {
referenceElement.setMetaProperty('referenced-element', 'schema');
});
return result;
}
}
export default DefinitionsVisitor;