source: node_modules/@swagger-api/apidom-reference/es/resolve/strategies/openapi-3-1/util.mjs@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1import { reduce } from 'ramda';
2import { isPrimitiveElement, toValue } from '@swagger-api/apidom-core';
3import { SchemaElement } from '@swagger-api/apidom-ns-openapi-3-1';
4import * as url from "../../../util/url.mjs";
5export 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};
16export 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 */
29export 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};
37refractToSchemaElement.cache = new WeakMap();
38export 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};
Note: See TracBrowser for help on using the repository browser.