1 | import { visit, dereference, refract as baseRefract, dispatchRefractorPlugins } from '@swagger-api/apidom-core';
|
---|
2 | import { path } from 'ramda';
|
---|
3 | import specification from "./specification.mjs";
|
---|
4 | import { keyMap, getNodeType } from "../traversal/visitor.mjs";
|
---|
5 | import createToolbox from "./toolbox.mjs";
|
---|
6 | const refract = (value, {
|
---|
7 | specPath = ['visitors', 'document', 'objects', 'OpenApi', '$visitor'],
|
---|
8 | plugins = []
|
---|
9 | } = {}) => {
|
---|
10 | const element = baseRefract(value);
|
---|
11 | const resolvedSpec = dereference(specification);
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * This is where generic ApiDOM becomes semantic (namespace applied).
|
---|
15 | * We don't allow consumers to hook into this translation.
|
---|
16 | * Though we allow consumers to define their onw plugins on already transformed ApiDOM.
|
---|
17 | */
|
---|
18 | const RootVisitorClass = path(specPath, resolvedSpec);
|
---|
19 | const rootVisitor = new RootVisitorClass({
|
---|
20 | specObj: resolvedSpec
|
---|
21 | });
|
---|
22 | visit(element, rootVisitor);
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Running plugins visitors means extra single traversal === performance hit.
|
---|
26 | */
|
---|
27 | return dispatchRefractorPlugins(rootVisitor.element, plugins, {
|
---|
28 | toolboxCreator: createToolbox,
|
---|
29 | visitorOptions: {
|
---|
30 | keyMap,
|
---|
31 | nodeTypeGetter: getNodeType
|
---|
32 | }
|
---|
33 | });
|
---|
34 | };
|
---|
35 | export const createRefractor = specPath => (value, options = {}) => refract(value, {
|
---|
36 | specPath,
|
---|
37 | ...options
|
---|
38 | });
|
---|
39 | export default refract; |
---|