1 | import { reduce } from 'ramda';
|
---|
2 | import { isPrimitiveElement, toValue } from '@swagger-api/apidom-core';
|
---|
3 | import { SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';
|
---|
4 | import * as url from "../../../util/url.mjs";
|
---|
5 | export const resolveSchema$refField = (retrievalURI, schemaElement) => {
|
---|
6 | if (typeof schemaElement.$ref === 'undefined') {
|
---|
7 | return undefined;
|
---|
8 | }
|
---|
9 | const hash = url.getHash(toValue(schemaElement.$ref));
|
---|
10 | const inherited$id = toValue(schemaElement.meta.get('inherited$id'));
|
---|
11 | const $refBaseURI = reduce((acc, uri) => {
|
---|
12 | return url.resolve(acc, url.sanitize(url.stripHash(uri)));
|
---|
13 | }, retrievalURI, [...inherited$id, toValue(schemaElement.$ref)]);
|
---|
14 | return `${$refBaseURI}${hash === '#' ? '' : hash}`;
|
---|
15 | };
|
---|
16 | export const resolveSchema$idField = (retrievalURI, schemaElement) => {
|
---|
17 | if (typeof schemaElement.$id === 'undefined') {
|
---|
18 | return undefined;
|
---|
19 | }
|
---|
20 | const inherited$id = toValue(schemaElement.meta.get('inherited$id'));
|
---|
21 | return reduce((acc, $id) => {
|
---|
22 | return url.resolve(acc, url.sanitize(url.stripHash($id)));
|
---|
23 | }, retrievalURI, [...inherited$id, toValue(schemaElement.$id)]);
|
---|
24 | };
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Cached version of SchemaElement.refract.
|
---|
28 | */
|
---|
29 | export const refractToSchemaElement = element => {
|
---|
30 | if (refractToSchemaElement.cache.has(element)) {
|
---|
31 | return refractToSchemaElement.cache.get(element);
|
---|
32 | }
|
---|
33 | const refracted = SchemaElement.refract(element);
|
---|
34 | refractToSchemaElement.cache.set(element, refracted);
|
---|
35 | return refracted;
|
---|
36 | };
|
---|
37 | refractToSchemaElement.cache = new WeakMap();
|
---|
38 | export const maybeRefractToSchemaElement = element => {
|
---|
39 | /**
|
---|
40 | * Conditional version of refractToSchemaElement, that acts as an identity
|
---|
41 | * function for all non-primitive Element instances.
|
---|
42 | */
|
---|
43 | if (isPrimitiveElement(element)) {
|
---|
44 | return refractToSchemaElement(element);
|
---|
45 | }
|
---|
46 | return element;
|
---|
47 | }; |
---|