@speclynx/apidom-reference 4.0.1 → 4.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/package.json +26 -27
  3. package/src/File.cjs +50 -0
  4. package/src/File.mjs +44 -0
  5. package/src/File.ts +63 -0
  6. package/src/Reference.cjs +31 -0
  7. package/src/Reference.mjs +27 -0
  8. package/src/Reference.ts +38 -0
  9. package/src/ReferenceSet.cjs +60 -0
  10. package/src/ReferenceSet.mjs +57 -0
  11. package/src/ReferenceSet.ts +73 -0
  12. package/src/bundle/index.cjs +61 -0
  13. package/src/bundle/index.mjs +55 -0
  14. package/src/bundle/index.ts +57 -0
  15. package/src/bundle/strategies/BundleStrategy.cjs +20 -0
  16. package/src/bundle/strategies/BundleStrategy.mjs +16 -0
  17. package/src/bundle/strategies/BundleStrategy.ts +27 -0
  18. package/src/bundle/strategies/openapi-3-1/index.cjs +35 -0
  19. package/src/bundle/strategies/openapi-3-1/index.mjs +29 -0
  20. package/src/bundle/strategies/openapi-3-1/index.ts +57 -0
  21. package/src/configuration/empty.cjs +9 -0
  22. package/src/configuration/empty.mjs +1 -0
  23. package/src/configuration/empty.ts +1 -0
  24. package/src/configuration/saturated.cjs +88 -0
  25. package/src/configuration/saturated.mjs +80 -0
  26. package/src/configuration/saturated.ts +72 -0
  27. package/src/dereference/index.cjs +90 -0
  28. package/src/dereference/index.mjs +83 -0
  29. package/src/dereference/index.ts +96 -0
  30. package/src/dereference/strategies/DereferenceStrategy.cjs +20 -0
  31. package/src/dereference/strategies/DereferenceStrategy.mjs +16 -0
  32. package/src/dereference/strategies/DereferenceStrategy.ts +27 -0
  33. package/src/dereference/strategies/apidom/index.cjs +89 -0
  34. package/src/dereference/strategies/apidom/index.mjs +83 -0
  35. package/src/dereference/strategies/apidom/index.ts +128 -0
  36. package/src/dereference/strategies/apidom/selectors/element-id.cjs +47 -0
  37. package/src/dereference/strategies/apidom/selectors/element-id.mjs +41 -0
  38. package/src/dereference/strategies/apidom/selectors/element-id.ts +48 -0
  39. package/src/dereference/strategies/apidom/visitor.cjs +266 -0
  40. package/src/dereference/strategies/apidom/visitor.mjs +259 -0
  41. package/src/dereference/strategies/apidom/visitor.ts +316 -0
  42. package/src/dereference/strategies/arazzo-1/index.cjs +109 -0
  43. package/src/dereference/strategies/arazzo-1/index.mjs +100 -0
  44. package/src/dereference/strategies/arazzo-1/index.ts +158 -0
  45. package/src/dereference/strategies/arazzo-1/selectors/$anchor.cjs +12 -0
  46. package/src/dereference/strategies/arazzo-1/selectors/$anchor.mjs +1 -0
  47. package/src/dereference/strategies/arazzo-1/selectors/$anchor.ts +9 -0
  48. package/src/dereference/strategies/arazzo-1/selectors/uri.cjs +8 -0
  49. package/src/dereference/strategies/arazzo-1/selectors/uri.mjs +1 -0
  50. package/src/dereference/strategies/arazzo-1/selectors/uri.ts +5 -0
  51. package/src/dereference/strategies/arazzo-1/source-descriptions.cjs +248 -0
  52. package/src/dereference/strategies/arazzo-1/source-descriptions.mjs +243 -0
  53. package/src/dereference/strategies/arazzo-1/source-descriptions.ts +317 -0
  54. package/src/dereference/strategies/arazzo-1/util.cjs +37 -0
  55. package/src/dereference/strategies/arazzo-1/util.mjs +29 -0
  56. package/src/dereference/strategies/arazzo-1/util.ts +33 -0
  57. package/src/dereference/strategies/arazzo-1/visitor.cjs +507 -0
  58. package/src/dereference/strategies/arazzo-1/visitor.mjs +500 -0
  59. package/src/dereference/strategies/arazzo-1/visitor.ts +574 -0
  60. package/src/dereference/strategies/asyncapi-2/index.cjs +94 -0
  61. package/src/dereference/strategies/asyncapi-2/index.mjs +88 -0
  62. package/src/dereference/strategies/asyncapi-2/index.ts +133 -0
  63. package/src/dereference/strategies/asyncapi-2/visitor.cjs +501 -0
  64. package/src/dereference/strategies/asyncapi-2/visitor.mjs +494 -0
  65. package/src/dereference/strategies/asyncapi-2/visitor.ts +589 -0
  66. package/src/dereference/strategies/openapi-2/index.cjs +96 -0
  67. package/src/dereference/strategies/openapi-2/index.mjs +90 -0
  68. package/src/dereference/strategies/openapi-2/index.ts +136 -0
  69. package/src/dereference/strategies/openapi-2/visitor.cjs +629 -0
  70. package/src/dereference/strategies/openapi-2/visitor.mjs +622 -0
  71. package/src/dereference/strategies/openapi-2/visitor.ts +745 -0
  72. package/src/dereference/strategies/openapi-3-0/index.cjs +96 -0
  73. package/src/dereference/strategies/openapi-3-0/index.mjs +90 -0
  74. package/src/dereference/strategies/openapi-3-0/index.ts +134 -0
  75. package/src/dereference/strategies/openapi-3-0/visitor.cjs +622 -0
  76. package/src/dereference/strategies/openapi-3-0/visitor.mjs +615 -0
  77. package/src/dereference/strategies/openapi-3-0/visitor.ts +760 -0
  78. package/src/dereference/strategies/openapi-3-1/index.cjs +99 -0
  79. package/src/dereference/strategies/openapi-3-1/index.mjs +90 -0
  80. package/src/dereference/strategies/openapi-3-1/index.ts +141 -0
  81. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.cjs +65 -0
  82. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.mjs +54 -0
  83. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.ts +64 -0
  84. package/src/dereference/strategies/openapi-3-1/selectors/uri.cjs +50 -0
  85. package/src/dereference/strategies/openapi-3-1/selectors/uri.mjs +42 -0
  86. package/src/dereference/strategies/openapi-3-1/selectors/uri.ts +54 -0
  87. package/src/dereference/strategies/openapi-3-1/util.cjs +68 -0
  88. package/src/dereference/strategies/openapi-3-1/util.mjs +59 -0
  89. package/src/dereference/strategies/openapi-3-1/util.ts +83 -0
  90. package/src/dereference/strategies/openapi-3-1/visitor.cjs +874 -0
  91. package/src/dereference/strategies/openapi-3-1/visitor.mjs +867 -0
  92. package/src/dereference/strategies/openapi-3-1/visitor.ts +1053 -0
  93. package/src/dereference/util.cjs +31 -0
  94. package/src/dereference/util.mjs +27 -0
  95. package/src/dereference/util.ts +29 -0
  96. package/src/errors/BundleError.cjs +10 -0
  97. package/src/errors/BundleError.mjs +7 -0
  98. package/src/errors/BundleError.ts +8 -0
  99. package/src/errors/DereferenceError.cjs +10 -0
  100. package/src/errors/DereferenceError.mjs +7 -0
  101. package/src/errors/DereferenceError.ts +8 -0
  102. package/src/errors/EvaluationElementIdError.cjs +10 -0
  103. package/src/errors/EvaluationElementIdError.mjs +7 -0
  104. package/src/errors/EvaluationElementIdError.ts +8 -0
  105. package/src/errors/EvaluationJsonSchema$anchorError.cjs +11 -0
  106. package/src/errors/EvaluationJsonSchema$anchorError.mjs +6 -0
  107. package/src/errors/EvaluationJsonSchema$anchorError.ts +8 -0
  108. package/src/errors/EvaluationJsonSchemaUriError.cjs +11 -0
  109. package/src/errors/EvaluationJsonSchemaUriError.mjs +6 -0
  110. package/src/errors/EvaluationJsonSchemaUriError.ts +8 -0
  111. package/src/errors/InvalidJsonSchema$anchorError.cjs +15 -0
  112. package/src/errors/InvalidJsonSchema$anchorError.mjs +10 -0
  113. package/src/errors/InvalidJsonSchema$anchorError.ts +12 -0
  114. package/src/errors/JsonSchema$anchorError.cjs +10 -0
  115. package/src/errors/JsonSchema$anchorError.mjs +7 -0
  116. package/src/errors/JsonSchema$anchorError.ts +8 -0
  117. package/src/errors/JsonSchemaUriError.cjs +10 -0
  118. package/src/errors/JsonSchemaUriError.mjs +7 -0
  119. package/src/errors/JsonSchemaUriError.ts +8 -0
  120. package/src/errors/MaximumBundleDepthError.cjs +11 -0
  121. package/src/errors/MaximumBundleDepthError.mjs +6 -0
  122. package/src/errors/MaximumBundleDepthError.ts +8 -0
  123. package/src/errors/MaximumDereferenceDepthError.cjs +11 -0
  124. package/src/errors/MaximumDereferenceDepthError.mjs +6 -0
  125. package/src/errors/MaximumDereferenceDepthError.ts +8 -0
  126. package/src/errors/MaximumResolveDepthError.cjs +11 -0
  127. package/src/errors/MaximumResolveDepthError.mjs +6 -0
  128. package/src/errors/MaximumResolveDepthError.ts +8 -0
  129. package/src/errors/ParseError.cjs +10 -0
  130. package/src/errors/ParseError.mjs +7 -0
  131. package/src/errors/ParseError.ts +8 -0
  132. package/src/errors/ParserError.cjs +11 -0
  133. package/src/errors/ParserError.mjs +6 -0
  134. package/src/errors/ParserError.ts +8 -0
  135. package/src/errors/PluginError.cjs +18 -0
  136. package/src/errors/PluginError.mjs +15 -0
  137. package/src/errors/PluginError.ts +15 -0
  138. package/src/errors/ResolveError.cjs +10 -0
  139. package/src/errors/ResolveError.mjs +7 -0
  140. package/src/errors/ResolveError.ts +8 -0
  141. package/src/errors/ResolverError.cjs +11 -0
  142. package/src/errors/ResolverError.mjs +6 -0
  143. package/src/errors/ResolverError.ts +8 -0
  144. package/src/errors/UnmatchedBundleStrategyError.cjs +11 -0
  145. package/src/errors/UnmatchedBundleStrategyError.mjs +6 -0
  146. package/src/errors/UnmatchedBundleStrategyError.ts +8 -0
  147. package/src/errors/UnmatchedDereferenceStrategyError.cjs +11 -0
  148. package/src/errors/UnmatchedDereferenceStrategyError.mjs +6 -0
  149. package/src/errors/UnmatchedDereferenceStrategyError.ts +8 -0
  150. package/src/errors/UnmatchedParserError.cjs +11 -0
  151. package/src/errors/UnmatchedParserError.mjs +6 -0
  152. package/src/errors/UnmatchedParserError.ts +8 -0
  153. package/src/errors/UnmatchedResolveStrategyError.cjs +11 -0
  154. package/src/errors/UnmatchedResolveStrategyError.mjs +6 -0
  155. package/src/errors/UnmatchedResolveStrategyError.ts +8 -0
  156. package/src/errors/UnmatchedResolverError.cjs +11 -0
  157. package/src/errors/UnmatchedResolverError.mjs +6 -0
  158. package/src/errors/UnmatchedResolverError.ts +8 -0
  159. package/src/errors/UnresolvableReferenceError.cjs +11 -0
  160. package/src/errors/UnresolvableReferenceError.mjs +6 -0
  161. package/src/errors/UnresolvableReferenceError.ts +8 -0
  162. package/src/index.cjs +146 -0
  163. package/src/index.mjs +103 -0
  164. package/src/index.ts +135 -0
  165. package/src/options/index.cjs +194 -0
  166. package/src/options/index.mjs +191 -0
  167. package/src/options/index.ts +239 -0
  168. package/src/options/util.cjs +24 -0
  169. package/src/options/util.mjs +19 -0
  170. package/src/options/util.ts +22 -0
  171. package/src/parse/index.cjs +69 -0
  172. package/src/parse/index.mjs +63 -0
  173. package/src/parse/index.ts +67 -0
  174. package/src/parse/parsers/Parser.cjs +62 -0
  175. package/src/parse/parsers/Parser.mjs +58 -0
  176. package/src/parse/parsers/Parser.ts +80 -0
  177. package/src/parse/parsers/apidom-json/index.cjs +70 -0
  178. package/src/parse/parsers/apidom-json/index.mjs +64 -0
  179. package/src/parse/parsers/apidom-json/index.ts +78 -0
  180. package/src/parse/parsers/arazzo-json-1/index.cjs +62 -0
  181. package/src/parse/parsers/arazzo-json-1/index.mjs +56 -0
  182. package/src/parse/parsers/arazzo-json-1/index.ts +76 -0
  183. package/src/parse/parsers/arazzo-json-1/source-descriptions.cjs +221 -0
  184. package/src/parse/parsers/arazzo-json-1/source-descriptions.mjs +214 -0
  185. package/src/parse/parsers/arazzo-json-1/source-descriptions.ts +280 -0
  186. package/src/parse/parsers/arazzo-yaml-1/index.cjs +62 -0
  187. package/src/parse/parsers/arazzo-yaml-1/index.mjs +56 -0
  188. package/src/parse/parsers/arazzo-yaml-1/index.ts +77 -0
  189. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.cjs +12 -0
  190. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.mjs +7 -0
  191. package/src/parse/parsers/arazzo-yaml-1/source-descriptions.ts +16 -0
  192. package/src/parse/parsers/asyncapi-json-2/index.cjs +54 -0
  193. package/src/parse/parsers/asyncapi-json-2/index.mjs +48 -0
  194. package/src/parse/parsers/asyncapi-json-2/index.ts +58 -0
  195. package/src/parse/parsers/asyncapi-yaml-2/index.cjs +54 -0
  196. package/src/parse/parsers/asyncapi-yaml-2/index.mjs +48 -0
  197. package/src/parse/parsers/asyncapi-yaml-2/index.ts +58 -0
  198. package/src/parse/parsers/binary/index-browser.cjs +56 -0
  199. package/src/parse/parsers/binary/index-browser.mjs +50 -0
  200. package/src/parse/parsers/binary/index-browser.ts +60 -0
  201. package/src/parse/parsers/binary/index-node.cjs +51 -0
  202. package/src/parse/parsers/binary/index-node.mjs +45 -0
  203. package/src/parse/parsers/binary/index-node.ts +57 -0
  204. package/src/parse/parsers/json/index.cjs +53 -0
  205. package/src/parse/parsers/json/index.mjs +47 -0
  206. package/src/parse/parsers/json/index.ts +52 -0
  207. package/src/parse/parsers/openapi-json-2/index.cjs +54 -0
  208. package/src/parse/parsers/openapi-json-2/index.mjs +48 -0
  209. package/src/parse/parsers/openapi-json-2/index.ts +58 -0
  210. package/src/parse/parsers/openapi-json-3-0/index.cjs +54 -0
  211. package/src/parse/parsers/openapi-json-3-0/index.mjs +48 -0
  212. package/src/parse/parsers/openapi-json-3-0/index.ts +59 -0
  213. package/src/parse/parsers/openapi-json-3-1/index.cjs +54 -0
  214. package/src/parse/parsers/openapi-json-3-1/index.mjs +48 -0
  215. package/src/parse/parsers/openapi-json-3-1/index.ts +59 -0
  216. package/src/parse/parsers/openapi-yaml-2/index.cjs +54 -0
  217. package/src/parse/parsers/openapi-yaml-2/index.mjs +48 -0
  218. package/src/parse/parsers/openapi-yaml-2/index.ts +58 -0
  219. package/src/parse/parsers/openapi-yaml-3-0/index.cjs +54 -0
  220. package/src/parse/parsers/openapi-yaml-3-0/index.mjs +48 -0
  221. package/src/parse/parsers/openapi-yaml-3-0/index.ts +59 -0
  222. package/src/parse/parsers/openapi-yaml-3-1/index.cjs +54 -0
  223. package/src/parse/parsers/openapi-yaml-3-1/index.mjs +48 -0
  224. package/src/parse/parsers/openapi-yaml-3-1/index.ts +59 -0
  225. package/src/parse/parsers/yaml-1-2/index.cjs +56 -0
  226. package/src/parse/parsers/yaml-1-2/index.mjs +50 -0
  227. package/src/parse/parsers/yaml-1-2/index.ts +60 -0
  228. package/src/resolve/index.cjs +67 -0
  229. package/src/resolve/index.mjs +60 -0
  230. package/src/resolve/index.ts +75 -0
  231. package/src/resolve/resolvers/HTTPResolver.cjs +45 -0
  232. package/src/resolve/resolvers/HTTPResolver.mjs +37 -0
  233. package/src/resolve/resolvers/HTTPResolver.ts +58 -0
  234. package/src/resolve/resolvers/Resolver.cjs +20 -0
  235. package/src/resolve/resolvers/Resolver.mjs +16 -0
  236. package/src/resolve/resolvers/Resolver.ts +25 -0
  237. package/src/resolve/resolvers/file/index-browser.cjs +24 -0
  238. package/src/resolve/resolvers/file/index-browser.mjs +19 -0
  239. package/src/resolve/resolvers/file/index-browser.ts +24 -0
  240. package/src/resolve/resolvers/file/index-node.cjs +49 -0
  241. package/src/resolve/resolvers/file/index-node.mjs +42 -0
  242. package/src/resolve/resolvers/file/index-node.ts +55 -0
  243. package/src/resolve/resolvers/http-axios/cache/MemoryCache.cjs +41 -0
  244. package/src/resolve/resolvers/http-axios/cache/MemoryCache.mjs +37 -0
  245. package/src/resolve/resolvers/http-axios/cache/MemoryCache.ts +46 -0
  246. package/src/resolve/resolvers/http-axios/index.cjs +113 -0
  247. package/src/resolve/resolvers/http-axios/index.mjs +105 -0
  248. package/src/resolve/resolvers/http-axios/index.ts +130 -0
  249. package/src/resolve/strategies/ResolveStrategy.cjs +20 -0
  250. package/src/resolve/strategies/ResolveStrategy.mjs +16 -0
  251. package/src/resolve/strategies/ResolveStrategy.ts +26 -0
  252. package/src/resolve/strategies/apidom/index.cjs +49 -0
  253. package/src/resolve/strategies/apidom/index.mjs +43 -0
  254. package/src/resolve/strategies/apidom/index.ts +78 -0
  255. package/src/resolve/strategies/asyncapi-2/index.cjs +49 -0
  256. package/src/resolve/strategies/asyncapi-2/index.mjs +43 -0
  257. package/src/resolve/strategies/asyncapi-2/index.ts +78 -0
  258. package/src/resolve/strategies/openapi-2/index.cjs +49 -0
  259. package/src/resolve/strategies/openapi-2/index.mjs +43 -0
  260. package/src/resolve/strategies/openapi-2/index.ts +78 -0
  261. package/src/resolve/strategies/openapi-3-0/index.cjs +49 -0
  262. package/src/resolve/strategies/openapi-3-0/index.mjs +43 -0
  263. package/src/resolve/strategies/openapi-3-0/index.ts +78 -0
  264. package/src/resolve/strategies/openapi-3-1/index.cjs +49 -0
  265. package/src/resolve/strategies/openapi-3-1/index.mjs +43 -0
  266. package/src/resolve/strategies/openapi-3-1/index.ts +78 -0
  267. package/src/resolve/util.cjs +37 -0
  268. package/src/resolve/util.mjs +30 -0
  269. package/src/resolve/util.ts +39 -0
  270. package/src/util/plugins.cjs +39 -0
  271. package/src/util/plugins.mjs +34 -0
  272. package/src/util/plugins.ts +37 -0
  273. package/src/util/url.cjs +288 -0
  274. package/src/util/url.mjs +274 -0
  275. package/src/util/url.ts +285 -0
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.resolveSchema$refField = exports.resolveSchema$idField = exports.maybeRefractToSchemaElement = exports.default = void 0;
6
+ var _ramda = require("ramda");
7
+ var _apidomDatamodel = require("@speclynx/apidom-datamodel");
8
+ var _apidomTraverse = require("@speclynx/apidom-traverse");
9
+ var _apidomNsOpenapi = require("@speclynx/apidom-ns-openapi-3-1");
10
+ var _DereferenceStrategy = _interopRequireDefault(require("../DereferenceStrategy.cjs"));
11
+ var _Reference = _interopRequireDefault(require("../../../Reference.cjs"));
12
+ var _ReferenceSet = _interopRequireDefault(require("../../../ReferenceSet.cjs"));
13
+ var _visitor = _interopRequireDefault(require("./visitor.cjs"));
14
+ exports.OpenAPI3_1DereferenceVisitor = _visitor.default;
15
+ var _util = require("./util.cjs");
16
+ exports.resolveSchema$refField = _util.resolveSchema$refField;
17
+ exports.resolveSchema$idField = _util.resolveSchema$idField;
18
+ exports.maybeRefractToSchemaElement = _util.maybeRefractToSchemaElement;
19
+ /**
20
+ * @public
21
+ */
22
+
23
+ /**
24
+ * @public
25
+ */
26
+ class OpenAPI3_1DereferenceStrategy extends _DereferenceStrategy.default {
27
+ constructor(options) {
28
+ super({
29
+ ...(options ?? {}),
30
+ name: 'openapi-3-1'
31
+ });
32
+ }
33
+ canDereference(file) {
34
+ // assert by media type
35
+ if (file.mediaType !== 'text/plain') {
36
+ return _apidomNsOpenapi.mediaTypes.includes(file.mediaType);
37
+ }
38
+
39
+ // assert by inspecting ApiDOM
40
+ return (0, _apidomNsOpenapi.isOpenApi3_1Element)(file.parseResult?.result);
41
+ }
42
+ async dereference(file, options) {
43
+ const immutableRefSet = options.dereference.refSet ?? new _ReferenceSet.default();
44
+ const mutableRefSet = new _ReferenceSet.default();
45
+ let refSet = immutableRefSet;
46
+ let reference;
47
+ if (!immutableRefSet.has(file.uri)) {
48
+ reference = new _Reference.default({
49
+ uri: file.uri,
50
+ value: file.parseResult
51
+ });
52
+ immutableRefSet.add(reference);
53
+ } else {
54
+ // pre-computed refSet was provided as configuration option
55
+ reference = immutableRefSet.find((0, _ramda.propEq)(file.uri, 'uri'));
56
+ }
57
+
58
+ /**
59
+ * Clone refSet due the dereferencing process being mutable.
60
+ * We don't want to mutate the original refSet and the references.
61
+ */
62
+ if (options.dereference.immutable) {
63
+ immutableRefSet.refs.map(ref => new _Reference.default({
64
+ ...ref,
65
+ value: (0, _apidomDatamodel.cloneDeep)(ref.value)
66
+ })).forEach(ref => mutableRefSet.add(ref));
67
+ reference = mutableRefSet.find(ref => ref.uri === file.uri);
68
+ refSet = mutableRefSet;
69
+ }
70
+ const visitor = new _visitor.default({
71
+ reference: reference,
72
+ options
73
+ });
74
+ const dereferencedElement = await (0, _apidomTraverse.traverseAsync)(refSet.rootRef.value, visitor, {
75
+ mutable: true
76
+ });
77
+
78
+ /**
79
+ * If immutable option is set, replay refs from the refSet.
80
+ */
81
+ if (options.dereference.immutable) {
82
+ mutableRefSet.refs.filter(ref => ref.uri.startsWith('immutable://')).map(ref => new _Reference.default({
83
+ ...ref,
84
+ uri: ref.uri.replace(/^immutable:\/\//, '')
85
+ })).forEach(ref => immutableRefSet.add(ref));
86
+ }
87
+
88
+ /**
89
+ * Release all memory if this refSet was not provided as an configuration option.
90
+ * If provided as configuration option, then provider is responsible for cleanup.
91
+ */
92
+ if (options.dereference.refSet === null) {
93
+ immutableRefSet.clean();
94
+ }
95
+ mutableRefSet.clean();
96
+ return dereferencedElement;
97
+ }
98
+ }
99
+ var _default = exports.default = OpenAPI3_1DereferenceStrategy;
@@ -0,0 +1,90 @@
1
+ import { propEq } from 'ramda';
2
+ import { cloneDeep } from '@speclynx/apidom-datamodel';
3
+ import { traverseAsync } from '@speclynx/apidom-traverse';
4
+ import { isOpenApi3_1Element, mediaTypes } from '@speclynx/apidom-ns-openapi-3-1';
5
+ import DereferenceStrategy from "../DereferenceStrategy.mjs";
6
+ import Reference from "../../../Reference.mjs";
7
+ import ReferenceSet from "../../../ReferenceSet.mjs";
8
+ import OpenAPI3_1DereferenceVisitor from "./visitor.mjs";
9
+ /**
10
+ * @public
11
+ */
12
+ /**
13
+ * @public
14
+ */
15
+ class OpenAPI3_1DereferenceStrategy extends DereferenceStrategy {
16
+ constructor(options) {
17
+ super({
18
+ ...(options ?? {}),
19
+ name: 'openapi-3-1'
20
+ });
21
+ }
22
+ canDereference(file) {
23
+ // assert by media type
24
+ if (file.mediaType !== 'text/plain') {
25
+ return mediaTypes.includes(file.mediaType);
26
+ }
27
+
28
+ // assert by inspecting ApiDOM
29
+ return isOpenApi3_1Element(file.parseResult?.result);
30
+ }
31
+ async dereference(file, options) {
32
+ const immutableRefSet = options.dereference.refSet ?? new ReferenceSet();
33
+ const mutableRefSet = new ReferenceSet();
34
+ let refSet = immutableRefSet;
35
+ let reference;
36
+ if (!immutableRefSet.has(file.uri)) {
37
+ reference = new Reference({
38
+ uri: file.uri,
39
+ value: file.parseResult
40
+ });
41
+ immutableRefSet.add(reference);
42
+ } else {
43
+ // pre-computed refSet was provided as configuration option
44
+ reference = immutableRefSet.find(propEq(file.uri, 'uri'));
45
+ }
46
+
47
+ /**
48
+ * Clone refSet due the dereferencing process being mutable.
49
+ * We don't want to mutate the original refSet and the references.
50
+ */
51
+ if (options.dereference.immutable) {
52
+ immutableRefSet.refs.map(ref => new Reference({
53
+ ...ref,
54
+ value: cloneDeep(ref.value)
55
+ })).forEach(ref => mutableRefSet.add(ref));
56
+ reference = mutableRefSet.find(ref => ref.uri === file.uri);
57
+ refSet = mutableRefSet;
58
+ }
59
+ const visitor = new OpenAPI3_1DereferenceVisitor({
60
+ reference: reference,
61
+ options
62
+ });
63
+ const dereferencedElement = await traverseAsync(refSet.rootRef.value, visitor, {
64
+ mutable: true
65
+ });
66
+
67
+ /**
68
+ * If immutable option is set, replay refs from the refSet.
69
+ */
70
+ if (options.dereference.immutable) {
71
+ mutableRefSet.refs.filter(ref => ref.uri.startsWith('immutable://')).map(ref => new Reference({
72
+ ...ref,
73
+ uri: ref.uri.replace(/^immutable:\/\//, '')
74
+ })).forEach(ref => immutableRefSet.add(ref));
75
+ }
76
+
77
+ /**
78
+ * Release all memory if this refSet was not provided as an configuration option.
79
+ * If provided as configuration option, then provider is responsible for cleanup.
80
+ */
81
+ if (options.dereference.refSet === null) {
82
+ immutableRefSet.clean();
83
+ }
84
+ mutableRefSet.clean();
85
+ return dereferencedElement;
86
+ }
87
+ }
88
+ export { OpenAPI3_1DereferenceVisitor };
89
+ export { resolveSchema$refField, resolveSchema$idField, maybeRefractToSchemaElement } from "./util.mjs";
90
+ export default OpenAPI3_1DereferenceStrategy;
@@ -0,0 +1,141 @@
1
+ import { propEq } from 'ramda';
2
+ import { Element, cloneDeep } from '@speclynx/apidom-datamodel';
3
+ import { traverseAsync } from '@speclynx/apidom-traverse';
4
+ import { isOpenApi3_1Element, mediaTypes } from '@speclynx/apidom-ns-openapi-3-1';
5
+
6
+ import DereferenceStrategy, { DereferenceStrategyOptions } from '../DereferenceStrategy.ts';
7
+ import File from '../../../File.ts';
8
+ import Reference from '../../../Reference.ts';
9
+ import ReferenceSet from '../../../ReferenceSet.ts';
10
+ import OpenAPI3_1DereferenceVisitor from './visitor.ts';
11
+ import type { ReferenceOptions } from '../../../options/index.ts';
12
+
13
+ export type {
14
+ default as DereferenceStrategy,
15
+ DereferenceStrategyOptions,
16
+ } from '../DereferenceStrategy.ts';
17
+ export type { default as File, FileOptions } from '../../../File.ts';
18
+ export type { default as Reference, ReferenceOptions } from '../../../Reference.ts';
19
+ export type { default as ReferenceSet, ReferenceSetOptions } from '../../../ReferenceSet.ts';
20
+ export type { OpenAPI3_1DereferenceVisitorOptions } from './visitor.ts';
21
+ export type {
22
+ ReferenceOptions as ApiDOMReferenceOptions,
23
+ ReferenceBundleOptions as ApiDOMReferenceBundleOptions,
24
+ ReferenceDereferenceOptions as ApiDOMReferenceDereferenceOptions,
25
+ ReferenceParseOptions as ApiDOMReferenceParseOptions,
26
+ ReferenceResolveOptions as ApiDOMReferenceResolveOptions,
27
+ } from '../../../options/index.ts';
28
+ export type { default as Parser, ParserOptions } from '../../../parse/parsers/Parser.ts';
29
+ export type { default as Resolver, ResolverOptions } from '../../../resolve/resolvers/Resolver.ts';
30
+ export type {
31
+ default as ResolveStrategy,
32
+ ResolveStrategyOptions,
33
+ } from '../../../resolve/strategies/ResolveStrategy.ts';
34
+ export type {
35
+ default as BundleStrategy,
36
+ BundleStrategyOptions,
37
+ } from '../../../bundle/strategies/BundleStrategy.ts';
38
+ export type { AncestorLineage } from '../../util.ts';
39
+
40
+ /**
41
+ * @public
42
+ */
43
+ export interface OpenAPI3_1DereferenceStrategyOptions extends Omit<
44
+ DereferenceStrategyOptions,
45
+ 'name'
46
+ > {}
47
+
48
+ /**
49
+ * @public
50
+ */
51
+ class OpenAPI3_1DereferenceStrategy extends DereferenceStrategy {
52
+ constructor(options?: OpenAPI3_1DereferenceStrategyOptions) {
53
+ super({ ...(options ?? {}), name: 'openapi-3-1' });
54
+ }
55
+
56
+ canDereference(file: File): boolean {
57
+ // assert by media type
58
+ if (file.mediaType !== 'text/plain') {
59
+ return mediaTypes.includes(file.mediaType);
60
+ }
61
+
62
+ // assert by inspecting ApiDOM
63
+ return isOpenApi3_1Element(file.parseResult?.result);
64
+ }
65
+
66
+ async dereference(file: File, options: ReferenceOptions): Promise<Element> {
67
+ const immutableRefSet = options.dereference.refSet ?? new ReferenceSet();
68
+ const mutableRefSet = new ReferenceSet();
69
+ let refSet = immutableRefSet;
70
+ let reference;
71
+
72
+ if (!immutableRefSet.has(file.uri)) {
73
+ reference = new Reference({ uri: file.uri, value: file.parseResult! });
74
+ immutableRefSet.add(reference);
75
+ } else {
76
+ // pre-computed refSet was provided as configuration option
77
+ reference = immutableRefSet.find(propEq(file.uri, 'uri'));
78
+ }
79
+
80
+ /**
81
+ * Clone refSet due the dereferencing process being mutable.
82
+ * We don't want to mutate the original refSet and the references.
83
+ */
84
+ if (options.dereference.immutable) {
85
+ immutableRefSet.refs
86
+ .map(
87
+ (ref) =>
88
+ new Reference({
89
+ ...ref,
90
+ value: cloneDeep(ref.value),
91
+ }),
92
+ )
93
+ .forEach((ref) => mutableRefSet.add(ref));
94
+ reference = mutableRefSet.find((ref) => ref.uri === file.uri);
95
+ refSet = mutableRefSet;
96
+ }
97
+
98
+ const visitor = new OpenAPI3_1DereferenceVisitor({ reference: reference!, options });
99
+
100
+ const dereferencedElement = await traverseAsync(refSet.rootRef!.value, visitor, {
101
+ mutable: true,
102
+ });
103
+
104
+ /**
105
+ * If immutable option is set, replay refs from the refSet.
106
+ */
107
+ if (options.dereference.immutable) {
108
+ mutableRefSet.refs
109
+ .filter((ref) => ref.uri.startsWith('immutable://'))
110
+ .map(
111
+ (ref) =>
112
+ new Reference({
113
+ ...ref,
114
+ uri: ref.uri.replace(/^immutable:\/\//, ''),
115
+ }),
116
+ )
117
+ .forEach((ref) => immutableRefSet.add(ref));
118
+ }
119
+
120
+ /**
121
+ * Release all memory if this refSet was not provided as an configuration option.
122
+ * If provided as configuration option, then provider is responsible for cleanup.
123
+ */
124
+ if (options.dereference.refSet === null) {
125
+ immutableRefSet.clean();
126
+ }
127
+
128
+ mutableRefSet.clean();
129
+
130
+ return dereferencedElement;
131
+ }
132
+ }
133
+
134
+ export { OpenAPI3_1DereferenceVisitor };
135
+ export {
136
+ resolveSchema$refField,
137
+ resolveSchema$idField,
138
+ maybeRefractToSchemaElement,
139
+ } from './util.ts';
140
+
141
+ export default OpenAPI3_1DereferenceStrategy;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.uriToAnchor = exports.parse = exports.isAnchor = exports.evaluate = exports.JsonSchema$anchorError = void 0;
6
+ var _ramdaAdjunct = require("ramda-adjunct");
7
+ var _apidomCore = require("@speclynx/apidom-core");
8
+ var _apidomTraverse = require("@speclynx/apidom-traverse");
9
+ var _apidomNsJsonSchema = require("@speclynx/apidom-ns-json-schema-2020-12");
10
+ var _url = require("../../../../util/url.cjs");
11
+ var _EvaluationJsonSchema$anchorError = _interopRequireDefault(require("../../../../errors/EvaluationJsonSchema$anchorError.cjs"));
12
+ exports.EvaluationJsonSchema$anchorError = _EvaluationJsonSchema$anchorError.default;
13
+ var _InvalidJsonSchema$anchorError = _interopRequireDefault(require("../../../../errors/InvalidJsonSchema$anchorError.cjs"));
14
+ exports.InvalidJsonSchema$anchorError = _InvalidJsonSchema$anchorError.default;
15
+ var _JsonSchema$anchorError = _interopRequireDefault(require("../../../../errors/JsonSchema$anchorError.cjs"));
16
+ exports.JsonSchema$anchorError = _JsonSchema$anchorError.default;
17
+ /**
18
+ * @public
19
+ */
20
+ const isAnchor = uri => {
21
+ /**
22
+ * MUST start with a letter ([A-Za-z]) or underscore ("_"), followed by any number of letters,
23
+ * digits ([0-9]), hyphens ("-"), underscores ("_"), and periods (".").
24
+ *
25
+ * https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.2
26
+ */
27
+ return /^[A-Za-z_][A-Za-z_0-9.-]*$/.test(uri);
28
+ };
29
+
30
+ /**
31
+ * @public
32
+ */
33
+ exports.isAnchor = isAnchor;
34
+ const uriToAnchor = uri => {
35
+ const hash = (0, _url.getHash)(uri);
36
+ return (0, _ramdaAdjunct.trimCharsStart)('#', hash);
37
+ };
38
+
39
+ /**
40
+ * @public
41
+ */
42
+ exports.uriToAnchor = uriToAnchor;
43
+ const parse = anchor => {
44
+ if (!isAnchor(anchor)) {
45
+ throw new _InvalidJsonSchema$anchorError.default(anchor);
46
+ }
47
+ return anchor;
48
+ };
49
+
50
+ /**
51
+ * Evaluates JSON Schema $anchor against ApiDOM fragment.
52
+ * @public
53
+ */
54
+ exports.parse = parse;
55
+ const evaluate = (anchor, element) => {
56
+ const token = parse(anchor);
57
+ const resultPath = (0, _apidomTraverse.find)(element,
58
+ // @ts-ignore
59
+ path => (0, _apidomNsJsonSchema.isJSONSchemaElement)(path.node) && (0, _apidomCore.toValue)(path.node.$anchor) === token);
60
+ if ((0, _ramdaAdjunct.isUndefined)(resultPath)) {
61
+ throw new _EvaluationJsonSchema$anchorError.default(`Evaluation failed on token: "${token}"`);
62
+ }
63
+ return resultPath.node;
64
+ };
65
+ exports.evaluate = evaluate;
@@ -0,0 +1,54 @@
1
+ import { trimCharsStart, isUndefined } from 'ramda-adjunct';
2
+ import { toValue } from '@speclynx/apidom-core';
3
+ import { find } from '@speclynx/apidom-traverse';
4
+ import { isJSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
5
+ import { getHash } from "../../../../util/url.mjs";
6
+ import EvaluationJsonSchema$anchorError from "../../../../errors/EvaluationJsonSchema$anchorError.mjs";
7
+ import InvalidJsonSchema$anchorError from "../../../../errors/InvalidJsonSchema$anchorError.mjs";
8
+ /**
9
+ * @public
10
+ */
11
+ export const isAnchor = uri => {
12
+ /**
13
+ * MUST start with a letter ([A-Za-z]) or underscore ("_"), followed by any number of letters,
14
+ * digits ([0-9]), hyphens ("-"), underscores ("_"), and periods (".").
15
+ *
16
+ * https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.2
17
+ */
18
+ return /^[A-Za-z_][A-Za-z_0-9.-]*$/.test(uri);
19
+ };
20
+
21
+ /**
22
+ * @public
23
+ */
24
+ export const uriToAnchor = uri => {
25
+ const hash = getHash(uri);
26
+ return trimCharsStart('#', hash);
27
+ };
28
+
29
+ /**
30
+ * @public
31
+ */
32
+ export const parse = anchor => {
33
+ if (!isAnchor(anchor)) {
34
+ throw new InvalidJsonSchema$anchorError(anchor);
35
+ }
36
+ return anchor;
37
+ };
38
+
39
+ /**
40
+ * Evaluates JSON Schema $anchor against ApiDOM fragment.
41
+ * @public
42
+ */
43
+ export const evaluate = (anchor, element) => {
44
+ const token = parse(anchor);
45
+ const resultPath = find(element,
46
+ // @ts-ignore
47
+ path => isJSONSchemaElement(path.node) && toValue(path.node.$anchor) === token);
48
+ if (isUndefined(resultPath)) {
49
+ throw new EvaluationJsonSchema$anchorError(`Evaluation failed on token: "${token}"`);
50
+ }
51
+ return resultPath.node;
52
+ };
53
+ export { EvaluationJsonSchema$anchorError, InvalidJsonSchema$anchorError };
54
+ export { default as JsonSchema$anchorError } from "../../../../errors/JsonSchema$anchorError.mjs";
@@ -0,0 +1,64 @@
1
+ import { trimCharsStart, isUndefined } from 'ramda-adjunct';
2
+ import { Element } from '@speclynx/apidom-datamodel';
3
+ import { toValue } from '@speclynx/apidom-core';
4
+ import { find } from '@speclynx/apidom-traverse';
5
+ import { isJSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
6
+
7
+ import { getHash } from '../../../../util/url.ts';
8
+ import EvaluationJsonSchema$anchorError from '../../../../errors/EvaluationJsonSchema$anchorError.ts';
9
+ import InvalidJsonSchema$anchorError from '../../../../errors/InvalidJsonSchema$anchorError.ts';
10
+
11
+ /**
12
+ * @public
13
+ */
14
+ export const isAnchor = (uri: string) => {
15
+ /**
16
+ * MUST start with a letter ([A-Za-z]) or underscore ("_"), followed by any number of letters,
17
+ * digits ([0-9]), hyphens ("-"), underscores ("_"), and periods (".").
18
+ *
19
+ * https://json-schema.org/draft/2020-12/json-schema-core.html#rfc.section.8.2.2
20
+ */
21
+ return /^[A-Za-z_][A-Za-z_0-9.-]*$/.test(uri);
22
+ };
23
+
24
+ /**
25
+ * @public
26
+ */
27
+ export const uriToAnchor = (uri: string): string => {
28
+ const hash = getHash(uri);
29
+ return trimCharsStart('#', hash);
30
+ };
31
+
32
+ /**
33
+ * @public
34
+ */
35
+ export const parse = (anchor: string): string => {
36
+ if (!isAnchor(anchor)) {
37
+ throw new InvalidJsonSchema$anchorError(anchor);
38
+ }
39
+
40
+ return anchor;
41
+ };
42
+
43
+ /**
44
+ * Evaluates JSON Schema $anchor against ApiDOM fragment.
45
+ * @public
46
+ */
47
+ export const evaluate = <T extends Element>(anchor: string, element: T): Element | undefined => {
48
+ const token = parse(anchor);
49
+
50
+ const resultPath = find(
51
+ element,
52
+ // @ts-ignore
53
+ (path) => isJSONSchemaElement(path.node) && toValue(path.node.$anchor) === token,
54
+ );
55
+
56
+ if (isUndefined(resultPath)) {
57
+ throw new EvaluationJsonSchema$anchorError(`Evaluation failed on token: "${token}"`);
58
+ }
59
+
60
+ return resultPath.node;
61
+ };
62
+
63
+ export { EvaluationJsonSchema$anchorError, InvalidJsonSchema$anchorError };
64
+ export { default as JsonSchema$anchorError } from '../../../../errors/JsonSchema$anchorError.ts';
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
5
+ exports.__esModule = true;
6
+ exports.evaluate = exports.JsonSchemaUriError = void 0;
7
+ var _ramdaAdjunct = require("ramda-adjunct");
8
+ var _apidomTraverse = require("@speclynx/apidom-traverse");
9
+ var _apidomNsJsonSchema = require("@speclynx/apidom-ns-json-schema-2020-12");
10
+ var _apidomJsonPointer = require("@speclynx/apidom-json-pointer");
11
+ var url = _interopRequireWildcard(require("../../../../util/url.cjs"));
12
+ var _EvaluationJsonSchemaUriError = _interopRequireDefault(require("../../../../errors/EvaluationJsonSchemaUriError.cjs"));
13
+ exports.EvaluationJsonSchemaUriError = _EvaluationJsonSchemaUriError.default;
14
+ var _$anchor = require("./$anchor.cjs");
15
+ var _util = require("../util.cjs");
16
+ var _JsonSchemaUriError = _interopRequireDefault(require("../../../../errors/JsonSchemaUriError.cjs"));
17
+ exports.JsonSchemaUriError = _JsonSchemaUriError.default;
18
+ /**
19
+ * Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
20
+ * @public
21
+ */
22
+ const evaluate = (uri, element) => {
23
+ const {
24
+ cache
25
+ } = evaluate;
26
+ const uriStrippedHash = url.stripHash(uri);
27
+ const isJSONSchemaElementWith$id = path => (0, _apidomNsJsonSchema.isJSONSchemaElement)(path.node) && typeof path.node.$id !== 'undefined';
28
+
29
+ // warm the cache
30
+ if (!cache.has(element)) {
31
+ const schemaObjectPaths = (0, _apidomTraverse.filter)(element, isJSONSchemaElementWith$id);
32
+ cache.set(element, schemaObjectPaths.map(path => path.node));
33
+ }
34
+
35
+ // search for the matching schema
36
+ const result = cache.get(element).find(e => {
37
+ const $idBaseURI = (0, _util.resolveSchema$idField)(uriStrippedHash, e);
38
+ return $idBaseURI === uriStrippedHash;
39
+ });
40
+ if ((0, _ramdaAdjunct.isUndefined)(result)) {
41
+ throw new _EvaluationJsonSchemaUriError.default(`Evaluation failed on URI: "${uri}"`);
42
+ }
43
+ if ((0, _$anchor.isAnchor)((0, _$anchor.uriToAnchor)(uri))) {
44
+ // we're dealing with JSON Schema $anchor here
45
+ return (0, _$anchor.evaluate)((0, _$anchor.uriToAnchor)(uri), result);
46
+ }
47
+ return (0, _apidomJsonPointer.evaluate)(result, _apidomJsonPointer.URIFragmentIdentifier.fromURIReference(uri));
48
+ };
49
+ exports.evaluate = evaluate;
50
+ evaluate.cache = new WeakMap();
@@ -0,0 +1,42 @@
1
+ import { isUndefined } from 'ramda-adjunct';
2
+ import { filter } from '@speclynx/apidom-traverse';
3
+ import { isJSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
4
+ import { URIFragmentIdentifier, evaluate as jsonPointerEvaluate } from '@speclynx/apidom-json-pointer';
5
+ import * as url from "../../../../util/url.mjs";
6
+ import EvaluationJsonSchemaUriError from "../../../../errors/EvaluationJsonSchemaUriError.mjs";
7
+ import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from "./$anchor.mjs";
8
+ import { resolveSchema$idField } from "../util.mjs";
9
+ /**
10
+ * Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
11
+ * @public
12
+ */
13
+ export const evaluate = (uri, element) => {
14
+ const {
15
+ cache
16
+ } = evaluate;
17
+ const uriStrippedHash = url.stripHash(uri);
18
+ const isJSONSchemaElementWith$id = path => isJSONSchemaElement(path.node) && typeof path.node.$id !== 'undefined';
19
+
20
+ // warm the cache
21
+ if (!cache.has(element)) {
22
+ const schemaObjectPaths = filter(element, isJSONSchemaElementWith$id);
23
+ cache.set(element, schemaObjectPaths.map(path => path.node));
24
+ }
25
+
26
+ // search for the matching schema
27
+ const result = cache.get(element).find(e => {
28
+ const $idBaseURI = resolveSchema$idField(uriStrippedHash, e);
29
+ return $idBaseURI === uriStrippedHash;
30
+ });
31
+ if (isUndefined(result)) {
32
+ throw new EvaluationJsonSchemaUriError(`Evaluation failed on URI: "${uri}"`);
33
+ }
34
+ if (isAnchor(uriToAnchor(uri))) {
35
+ // we're dealing with JSON Schema $anchor here
36
+ return $anchorEvaluate(uriToAnchor(uri), result);
37
+ }
38
+ return jsonPointerEvaluate(result, URIFragmentIdentifier.fromURIReference(uri));
39
+ };
40
+ evaluate.cache = new WeakMap();
41
+ export { EvaluationJsonSchemaUriError };
42
+ export { default as JsonSchemaUriError } from "../../../../errors/JsonSchemaUriError.mjs";
@@ -0,0 +1,54 @@
1
+ import { isUndefined } from 'ramda-adjunct';
2
+ import { Element } from '@speclynx/apidom-datamodel';
3
+ import { filter, type Path } from '@speclynx/apidom-traverse';
4
+ import { isJSONSchemaElement, JSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
5
+ import {
6
+ URIFragmentIdentifier,
7
+ evaluate as jsonPointerEvaluate,
8
+ } from '@speclynx/apidom-json-pointer';
9
+
10
+ import * as url from '../../../../util/url.ts';
11
+ import EvaluationJsonSchemaUriError from '../../../../errors/EvaluationJsonSchemaUriError.ts';
12
+ import { isAnchor, uriToAnchor, evaluate as $anchorEvaluate } from './$anchor.ts';
13
+ import { resolveSchema$idField } from '../util.ts';
14
+
15
+ /**
16
+ * Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
17
+ * @public
18
+ */
19
+ export const evaluate = <T extends Element>(uri: string, element: T): Element | undefined => {
20
+ const { cache } = evaluate;
21
+ const uriStrippedHash = url.stripHash(uri);
22
+ const isJSONSchemaElementWith$id = (path: Path<Element>) =>
23
+ isJSONSchemaElement(path.node) && typeof (path.node as any).$id !== 'undefined';
24
+
25
+ // warm the cache
26
+ if (!cache.has(element)) {
27
+ const schemaObjectPaths = filter(element, isJSONSchemaElementWith$id);
28
+ cache.set(
29
+ element,
30
+ schemaObjectPaths.map((path) => path.node),
31
+ );
32
+ }
33
+
34
+ // search for the matching schema
35
+ const result = cache.get(element).find((e: JSONSchemaElement) => {
36
+ const $idBaseURI = resolveSchema$idField(uriStrippedHash, e);
37
+ return $idBaseURI === uriStrippedHash;
38
+ });
39
+
40
+ if (isUndefined(result)) {
41
+ throw new EvaluationJsonSchemaUriError(`Evaluation failed on URI: "${uri}"`);
42
+ }
43
+
44
+ if (isAnchor(uriToAnchor(uri))) {
45
+ // we're dealing with JSON Schema $anchor here
46
+ return $anchorEvaluate(uriToAnchor(uri), result);
47
+ }
48
+
49
+ return jsonPointerEvaluate(result, URIFragmentIdentifier.fromURIReference(uri));
50
+ };
51
+ evaluate.cache = new WeakMap();
52
+
53
+ export { EvaluationJsonSchemaUriError };
54
+ export { default as JsonSchemaUriError } from '../../../../errors/JsonSchemaUriError.ts';