1 | /* eslint-disable camelcase */
|
---|
2 | import { dispatchRefractorPlugins, isObjectElement, toValue } from '@swagger-api/apidom-core';
|
---|
3 | import { refractorPluginNormalizeOperationIds, refractorPluginNormalizeParameters, refractorPluginNormalizeSecurityRequirements, refractorPluginNormalizeServers, refractorPluginNormalizeParameterExamples, refractorPluginNormalizeHeaderExamples, createToolbox, keyMap, getNodeType, OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1';
|
---|
4 | import opId from '../../../helpers/op-id.js';
|
---|
5 | const normalize = element => {
|
---|
6 | if (!isObjectElement(element)) return element;
|
---|
7 | if (element.hasKey('$$normalized')) return element;
|
---|
8 | const plugins = [refractorPluginNormalizeOperationIds({
|
---|
9 | operationIdNormalizer: (operationId, path, method) => opId({
|
---|
10 | operationId
|
---|
11 | }, path, method, {
|
---|
12 | v2OperationIdCompatibilityMode: false
|
---|
13 | })
|
---|
14 | }), refractorPluginNormalizeParameters(), refractorPluginNormalizeSecurityRequirements(), refractorPluginNormalizeServers(), refractorPluginNormalizeParameterExamples(), refractorPluginNormalizeHeaderExamples()];
|
---|
15 | const normalized = dispatchRefractorPlugins(element, plugins, {
|
---|
16 | toolboxCreator: createToolbox,
|
---|
17 | visitorOptions: {
|
---|
18 | keyMap,
|
---|
19 | nodeTypeGetter: getNodeType
|
---|
20 | }
|
---|
21 | });
|
---|
22 | normalized.set('$$normalized', true);
|
---|
23 | return normalized;
|
---|
24 | };
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * This adapter allow to perform normalization on Plain Old JavaScript Objects.
|
---|
28 | * The function adapts the `normalize` function interface and is able to accept
|
---|
29 | * Plain Old JavaScript Objects and returns Plain Old JavaScript Objects.
|
---|
30 | */
|
---|
31 | export const pojoAdapter = normalizeFn => spec => {
|
---|
32 | if (spec !== null && spec !== void 0 && spec.$$normalized) return spec;
|
---|
33 | if (pojoAdapter.cache.has(spec)) return pojoAdapter.cache.get(spec);
|
---|
34 | const openApiElement = OpenApi3_1Element.refract(spec);
|
---|
35 | const normalized = normalizeFn(openApiElement);
|
---|
36 | const value = toValue(normalized);
|
---|
37 | pojoAdapter.cache.set(spec, value);
|
---|
38 | return value;
|
---|
39 | };
|
---|
40 | pojoAdapter.cache = new WeakMap();
|
---|
41 | export default normalize;
|
---|
42 | /* eslint-enable camelcase */ |
---|