source: node_modules/@swagger-api/apidom-reference/es/dereference/index.mjs

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

Initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import { isEmpty, propEq } from 'ramda';
2import { isParseResultElement, ParseResultElement, cloneShallow } from '@swagger-api/apidom-core';
3import File from "../util/File.mjs";
4import * as plugins from "../util/plugins.mjs";
5import UnmatchedDereferenceStrategyError from "../errors/UnmatchedDereferenceStrategyError.mjs";
6import DereferenceError from "../errors/DereferenceError.mjs";
7import parse from "../parse/index.mjs";
8import { merge as mergeOptions } from "../options/util.mjs";
9import * as url from "../util/url.mjs";
10/**
11 * Dereferences ApiDOM with all its external references.
12 */
13export const dereferenceApiDOM = async (element, options) => {
14 // @ts-ignore
15 let parseResult = element;
16 let surrogateWrapping = false;
17
18 // wrap element into parse result
19 if (!isParseResultElement(element)) {
20 const elementClone = cloneShallow(element);
21 elementClone.classes.push('result');
22 parseResult = new ParseResultElement([elementClone]);
23 surrogateWrapping = true;
24 }
25 const file = File({
26 uri: options.resolve.baseURI,
27 parseResult,
28 mediaType: options.parse.mediaType
29 });
30 const dereferenceStrategies = await plugins.filter('canDereference', file, options.dereference.strategies);
31
32 // we couldn't find any dereference strategy for this File
33 if (isEmpty(dereferenceStrategies)) {
34 throw new UnmatchedDereferenceStrategyError(file.uri);
35 }
36 try {
37 const {
38 result
39 } = await plugins.run('dereference', [file, options], dereferenceStrategies);
40 // unwrap the element from ParseResult assuming first element is the actual result
41 return surrogateWrapping ? result.get(0) : result;
42 } catch (error) {
43 throw new DereferenceError(`Error while dereferencing file "${file.uri}"`, {
44 cause: error
45 });
46 }
47};
48
49/**
50 * Dereferences a file with all its external references.
51 */
52const dereference = async (uri, options) => {
53 const {
54 refSet
55 } = options.dereference;
56 const sanitizedURI = url.sanitize(uri);
57 let parseResult;
58
59 // if refSet was provided, use it to avoid unnecessary parsing
60 if (refSet !== null && refSet.has(sanitizedURI)) {
61 // @ts-ignore
62 ({
63 value: parseResult
64 } = refSet.find(propEq(sanitizedURI, 'uri')));
65 } else {
66 parseResult = await parse(uri, options);
67 }
68 const mergedOptions = mergeOptions(options, {
69 resolve: {
70 baseURI: sanitizedURI
71 }
72 });
73 return dereferenceApiDOM(parseResult, mergedOptions);
74};
75export default dereference;
Note: See TracBrowser for help on using the repository browser.