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