@speclynx/apidom-reference 4.0.2 → 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 +6 -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,69 @@
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.default = void 0;
7
+ var _ramda = require("ramda");
8
+ var url = _interopRequireWildcard(require("../util/url.cjs"));
9
+ var _File = _interopRequireDefault(require("../File.cjs"));
10
+ var plugins = _interopRequireWildcard(require("../util/plugins.cjs"));
11
+ var _ParseError = _interopRequireDefault(require("../errors/ParseError.cjs"));
12
+ var _UnmatchedParserError = _interopRequireDefault(require("../errors/UnmatchedParserError.cjs"));
13
+ var _util = require("../resolve/util.cjs");
14
+ /**
15
+ * Parses the given file's contents, using the configured parser plugins.
16
+ */
17
+ const parseFile = async (file, options) => {
18
+ const optsBoundParsers = options.parse.parsers.map(parser => {
19
+ const clonedParser = Object.create(parser);
20
+ return Object.assign(clonedParser, options.parse.parserOpts);
21
+ });
22
+ const parsers = await plugins.filter('canParse', [file, options], optsBoundParsers);
23
+
24
+ // we couldn't find any parser for this File
25
+ if ((0, _ramda.isEmpty)(parsers)) {
26
+ throw new _UnmatchedParserError.default(`Could not find a parser that can parse the file "${file.uri}"`);
27
+ }
28
+ try {
29
+ const {
30
+ plugin,
31
+ result
32
+ } = await plugins.run('parse', [file, options], parsers);
33
+
34
+ // empty files handling
35
+ if (!plugin.allowEmpty && result.isEmpty) {
36
+ return Promise.reject(new _ParseError.default(`Error while parsing file "${file.uri}": file is empty`));
37
+ }
38
+ return result;
39
+ } catch (error) {
40
+ throw new _ParseError.default(`Error while parsing file "${file.uri}"`, {
41
+ cause: error
42
+ });
43
+ }
44
+ };
45
+
46
+ /**
47
+ * Parses a file into ApiDOM.
48
+ */
49
+ const parse = async (uri, options) => {
50
+ /**
51
+ * If the path is a filesystem path, then convert it to a URL.
52
+ *
53
+ * NOTE: According to the JSON Reference spec, these should already be URLs,
54
+ * but, in practice, many people use local filesystem paths instead.
55
+ * So we're being generous here and doing the conversion automatically.
56
+ * This is not intended to be a 100% bulletproof solution.
57
+ * If it doesn't work for your use-case, then use a URL instead.
58
+ */
59
+ const file = new _File.default({
60
+ uri: url.sanitize(url.stripHash(uri)),
61
+ mediaType: options.parse.mediaType
62
+ });
63
+ const data = await (0, _util.readFile)(file, options);
64
+ return parseFile(new _File.default({
65
+ ...file,
66
+ data
67
+ }), options);
68
+ };
69
+ var _default = exports.default = parse;
@@ -0,0 +1,63 @@
1
+ import { isEmpty } from 'ramda';
2
+ import * as url from "../util/url.mjs";
3
+ import File from "../File.mjs";
4
+ import * as plugins from "../util/plugins.mjs";
5
+ import ParseError from "../errors/ParseError.mjs";
6
+ import UnmatchedParserError from "../errors/UnmatchedParserError.mjs";
7
+ import { readFile } from "../resolve/util.mjs";
8
+ /**
9
+ * Parses the given file's contents, using the configured parser plugins.
10
+ */
11
+ const parseFile = async (file, options) => {
12
+ const optsBoundParsers = options.parse.parsers.map(parser => {
13
+ const clonedParser = Object.create(parser);
14
+ return Object.assign(clonedParser, options.parse.parserOpts);
15
+ });
16
+ const parsers = await plugins.filter('canParse', [file, options], optsBoundParsers);
17
+
18
+ // we couldn't find any parser for this File
19
+ if (isEmpty(parsers)) {
20
+ throw new UnmatchedParserError(`Could not find a parser that can parse the file "${file.uri}"`);
21
+ }
22
+ try {
23
+ const {
24
+ plugin,
25
+ result
26
+ } = await plugins.run('parse', [file, options], parsers);
27
+
28
+ // empty files handling
29
+ if (!plugin.allowEmpty && result.isEmpty) {
30
+ return Promise.reject(new ParseError(`Error while parsing file "${file.uri}": file is empty`));
31
+ }
32
+ return result;
33
+ } catch (error) {
34
+ throw new ParseError(`Error while parsing file "${file.uri}"`, {
35
+ cause: error
36
+ });
37
+ }
38
+ };
39
+
40
+ /**
41
+ * Parses a file into ApiDOM.
42
+ */
43
+ const parse = async (uri, options) => {
44
+ /**
45
+ * If the path is a filesystem path, then convert it to a URL.
46
+ *
47
+ * NOTE: According to the JSON Reference spec, these should already be URLs,
48
+ * but, in practice, many people use local filesystem paths instead.
49
+ * So we're being generous here and doing the conversion automatically.
50
+ * This is not intended to be a 100% bulletproof solution.
51
+ * If it doesn't work for your use-case, then use a URL instead.
52
+ */
53
+ const file = new File({
54
+ uri: url.sanitize(url.stripHash(uri)),
55
+ mediaType: options.parse.mediaType
56
+ });
57
+ const data = await readFile(file, options);
58
+ return parseFile(new File({
59
+ ...file,
60
+ data
61
+ }), options);
62
+ };
63
+ export default parse;
@@ -0,0 +1,67 @@
1
+ import { isEmpty } from 'ramda';
2
+ import { ParseResultElement } from '@speclynx/apidom-datamodel';
3
+
4
+ import * as url from '../util/url.ts';
5
+ import File from '../File.ts';
6
+ import * as plugins from '../util/plugins.ts';
7
+ import Parser from './parsers/Parser.ts';
8
+ import ParseError from '../errors/ParseError.ts';
9
+ import UnmatchedParserError from '../errors/UnmatchedParserError.ts';
10
+ import { readFile } from '../resolve/util.ts';
11
+ import type { ReferenceOptions } from '../options/index.ts';
12
+
13
+ /**
14
+ * Parses the given file's contents, using the configured parser plugins.
15
+ */
16
+ const parseFile = async (file: File, options: ReferenceOptions): Promise<ParseResultElement> => {
17
+ const optsBoundParsers = options.parse.parsers.map((parser) => {
18
+ const clonedParser = Object.create(parser);
19
+ return Object.assign(clonedParser, options.parse.parserOpts);
20
+ });
21
+
22
+ const parsers: Parser[] = await plugins.filter('canParse', [file, options], optsBoundParsers);
23
+
24
+ // we couldn't find any parser for this File
25
+ if (isEmpty(parsers)) {
26
+ throw new UnmatchedParserError(`Could not find a parser that can parse the file "${file.uri}"`);
27
+ }
28
+
29
+ try {
30
+ const { plugin, result } = await plugins.run('parse', [file, options], parsers);
31
+
32
+ // empty files handling
33
+ if (!plugin.allowEmpty && result.isEmpty) {
34
+ return Promise.reject(
35
+ new ParseError(`Error while parsing file "${file.uri}": file is empty`),
36
+ );
37
+ }
38
+
39
+ return result;
40
+ } catch (error: any) {
41
+ throw new ParseError(`Error while parsing file "${file.uri}"`, { cause: error });
42
+ }
43
+ };
44
+
45
+ /**
46
+ * Parses a file into ApiDOM.
47
+ */
48
+ const parse = async (uri: string, options: ReferenceOptions): Promise<ParseResultElement> => {
49
+ /**
50
+ * If the path is a filesystem path, then convert it to a URL.
51
+ *
52
+ * NOTE: According to the JSON Reference spec, these should already be URLs,
53
+ * but, in practice, many people use local filesystem paths instead.
54
+ * So we're being generous here and doing the conversion automatically.
55
+ * This is not intended to be a 100% bulletproof solution.
56
+ * If it doesn't work for your use-case, then use a URL instead.
57
+ */
58
+ const file = new File({
59
+ uri: url.sanitize(url.stripHash(uri)),
60
+ mediaType: options.parse.mediaType,
61
+ });
62
+ const data = await readFile(file, options);
63
+
64
+ return parseFile(new File({ ...file, data }), options);
65
+ };
66
+
67
+ export default parse;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ /**
6
+ * @public
7
+ */
8
+
9
+ /**
10
+ * @public
11
+ */
12
+ class Parser {
13
+ name;
14
+
15
+ /**
16
+ * Whether to allow "empty" files. This includes zero-byte files.
17
+ */
18
+ allowEmpty;
19
+
20
+ /**
21
+ * Whether to generate source map during parsing.
22
+ */
23
+ sourceMap;
24
+
25
+ /**
26
+ * Whether to capture format-specific style information for round-trip preservation.
27
+ */
28
+ style;
29
+
30
+ /**
31
+ * Whether to use strict parsing (native JSON.parse/YAML instead of tree-sitter).
32
+ */
33
+ strict;
34
+
35
+ /**
36
+ * List of supported file extensions.
37
+ */
38
+ fileExtensions;
39
+
40
+ /**
41
+ * List of supported media types.
42
+ */
43
+ mediaTypes;
44
+ constructor({
45
+ name,
46
+ allowEmpty = true,
47
+ sourceMap = false,
48
+ style = false,
49
+ strict = true,
50
+ fileExtensions = [],
51
+ mediaTypes = []
52
+ }) {
53
+ this.name = name;
54
+ this.allowEmpty = allowEmpty;
55
+ this.sourceMap = sourceMap;
56
+ this.style = style;
57
+ this.strict = strict;
58
+ this.fileExtensions = fileExtensions;
59
+ this.mediaTypes = mediaTypes;
60
+ }
61
+ }
62
+ var _default = exports.default = Parser;
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @public
3
+ */
4
+
5
+ /**
6
+ * @public
7
+ */
8
+ class Parser {
9
+ name;
10
+
11
+ /**
12
+ * Whether to allow "empty" files. This includes zero-byte files.
13
+ */
14
+ allowEmpty;
15
+
16
+ /**
17
+ * Whether to generate source map during parsing.
18
+ */
19
+ sourceMap;
20
+
21
+ /**
22
+ * Whether to capture format-specific style information for round-trip preservation.
23
+ */
24
+ style;
25
+
26
+ /**
27
+ * Whether to use strict parsing (native JSON.parse/YAML instead of tree-sitter).
28
+ */
29
+ strict;
30
+
31
+ /**
32
+ * List of supported file extensions.
33
+ */
34
+ fileExtensions;
35
+
36
+ /**
37
+ * List of supported media types.
38
+ */
39
+ mediaTypes;
40
+ constructor({
41
+ name,
42
+ allowEmpty = true,
43
+ sourceMap = false,
44
+ style = false,
45
+ strict = true,
46
+ fileExtensions = [],
47
+ mediaTypes = []
48
+ }) {
49
+ this.name = name;
50
+ this.allowEmpty = allowEmpty;
51
+ this.sourceMap = sourceMap;
52
+ this.style = style;
53
+ this.strict = strict;
54
+ this.fileExtensions = fileExtensions;
55
+ this.mediaTypes = mediaTypes;
56
+ }
57
+ }
58
+ export default Parser;
@@ -0,0 +1,80 @@
1
+ import { ParseResultElement } from '@speclynx/apidom-datamodel';
2
+
3
+ import File from '../../File.ts';
4
+ import type { ReferenceOptions } from '../../options/index.ts';
5
+
6
+ /**
7
+ * @public
8
+ */
9
+ export interface ParserOptions {
10
+ readonly name: string;
11
+ readonly allowEmpty?: boolean;
12
+ readonly sourceMap?: boolean;
13
+ readonly style?: boolean;
14
+ readonly strict?: boolean;
15
+ readonly fileExtensions?: string[];
16
+ readonly mediaTypes?: string[];
17
+ }
18
+
19
+ /**
20
+ * @public
21
+ */
22
+ abstract class Parser {
23
+ public readonly name: string;
24
+
25
+ /**
26
+ * Whether to allow "empty" files. This includes zero-byte files.
27
+ */
28
+ public allowEmpty: boolean;
29
+
30
+ /**
31
+ * Whether to generate source map during parsing.
32
+ */
33
+ public sourceMap: boolean;
34
+
35
+ /**
36
+ * Whether to capture format-specific style information for round-trip preservation.
37
+ */
38
+ public style: boolean;
39
+
40
+ /**
41
+ * Whether to use strict parsing (native JSON.parse/YAML instead of tree-sitter).
42
+ */
43
+ public strict: boolean;
44
+
45
+ /**
46
+ * List of supported file extensions.
47
+ */
48
+ public fileExtensions: string[];
49
+
50
+ /**
51
+ * List of supported media types.
52
+ */
53
+ public mediaTypes: string[];
54
+
55
+ constructor({
56
+ name,
57
+ allowEmpty = true,
58
+ sourceMap = false,
59
+ style = false,
60
+ strict = true,
61
+ fileExtensions = [],
62
+ mediaTypes = [],
63
+ }: ParserOptions) {
64
+ this.name = name;
65
+ this.allowEmpty = allowEmpty;
66
+ this.sourceMap = sourceMap;
67
+ this.style = style;
68
+ this.strict = strict;
69
+ this.fileExtensions = fileExtensions;
70
+ this.mediaTypes = mediaTypes;
71
+ }
72
+
73
+ abstract canParse(file: File, options?: ReferenceOptions): boolean | Promise<boolean>;
74
+ abstract parse(
75
+ file: File,
76
+ options?: ReferenceOptions,
77
+ ): ParseResultElement | Promise<ParseResultElement>;
78
+ }
79
+
80
+ export default Parser;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.default = void 0;
6
+ var _apidomDatamodel = require("@speclynx/apidom-datamodel");
7
+ var _ParserError = _interopRequireDefault(require("../../../errors/ParserError.cjs"));
8
+ var _Parser = _interopRequireDefault(require("../Parser.cjs"));
9
+ /**
10
+ * @public
11
+ */
12
+
13
+ /**
14
+ * @public
15
+ */
16
+ class ApiDOMJSONParser extends _Parser.default {
17
+ namespace;
18
+ ['apidom-json'];
19
+ constructor(options) {
20
+ const {
21
+ fileExtensions = [],
22
+ mediaTypes = ['application/vnd.apidom', 'application/vnd.apidom+json'],
23
+ namespace = new _apidomDatamodel.Namespace(),
24
+ ...rest
25
+ } = options ?? {};
26
+ super({
27
+ ...rest,
28
+ name: 'apidom-json',
29
+ fileExtensions,
30
+ mediaTypes
31
+ });
32
+ this.namespace = namespace;
33
+ }
34
+ canParse(file) {
35
+ const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
36
+ const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
37
+ if (!hasSupportedFileExtension) return false;
38
+ if (hasSupportedMediaType) return true;
39
+ if (!hasSupportedMediaType) {
40
+ try {
41
+ return this.namespace.fromRefract(JSON.parse(file.toString())) && true;
42
+ } catch {
43
+ return false;
44
+ }
45
+ }
46
+ return false;
47
+ }
48
+ parse(file) {
49
+ const source = file.toString();
50
+ const namespace = this['apidom-json']?.namespace ?? this.namespace;
51
+
52
+ // allow empty files
53
+ if (this.allowEmpty && source.trim() === '') {
54
+ return new _apidomDatamodel.ParseResultElement();
55
+ }
56
+ try {
57
+ const element = namespace.fromRefract(JSON.parse(source));
58
+ if (!(0, _apidomDatamodel.isParseResultElement)(element)) {
59
+ element.classes.push('result');
60
+ return new _apidomDatamodel.ParseResultElement([element]);
61
+ }
62
+ return element;
63
+ } catch (error) {
64
+ throw new _ParserError.default(`Error parsing "${file.uri}"`, {
65
+ cause: error
66
+ });
67
+ }
68
+ }
69
+ }
70
+ var _default = exports.default = ApiDOMJSONParser;
@@ -0,0 +1,64 @@
1
+ import { Namespace, ParseResultElement, isParseResultElement } from '@speclynx/apidom-datamodel';
2
+ import ParserError from "../../../errors/ParserError.mjs";
3
+ import Parser from "../Parser.mjs";
4
+ /**
5
+ * @public
6
+ */
7
+ /**
8
+ * @public
9
+ */
10
+ class ApiDOMJSONParser extends Parser {
11
+ namespace;
12
+ ['apidom-json'];
13
+ constructor(options) {
14
+ const {
15
+ fileExtensions = [],
16
+ mediaTypes = ['application/vnd.apidom', 'application/vnd.apidom+json'],
17
+ namespace = new Namespace(),
18
+ ...rest
19
+ } = options ?? {};
20
+ super({
21
+ ...rest,
22
+ name: 'apidom-json',
23
+ fileExtensions,
24
+ mediaTypes
25
+ });
26
+ this.namespace = namespace;
27
+ }
28
+ canParse(file) {
29
+ const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
30
+ const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
31
+ if (!hasSupportedFileExtension) return false;
32
+ if (hasSupportedMediaType) return true;
33
+ if (!hasSupportedMediaType) {
34
+ try {
35
+ return this.namespace.fromRefract(JSON.parse(file.toString())) && true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ }
40
+ return false;
41
+ }
42
+ parse(file) {
43
+ const source = file.toString();
44
+ const namespace = this['apidom-json']?.namespace ?? this.namespace;
45
+
46
+ // allow empty files
47
+ if (this.allowEmpty && source.trim() === '') {
48
+ return new ParseResultElement();
49
+ }
50
+ try {
51
+ const element = namespace.fromRefract(JSON.parse(source));
52
+ if (!isParseResultElement(element)) {
53
+ element.classes.push('result');
54
+ return new ParseResultElement([element]);
55
+ }
56
+ return element;
57
+ } catch (error) {
58
+ throw new ParserError(`Error parsing "${file.uri}"`, {
59
+ cause: error
60
+ });
61
+ }
62
+ }
63
+ }
64
+ export default ApiDOMJSONParser;
@@ -0,0 +1,78 @@
1
+ import { Namespace, ParseResultElement, isParseResultElement } from '@speclynx/apidom-datamodel';
2
+
3
+ import ParserError from '../../../errors/ParserError.ts';
4
+ import Parser, { ParserOptions } from '../Parser.ts';
5
+ import File from '../../../File.ts';
6
+
7
+ export type { default as Parser, ParserOptions } from '../Parser.ts';
8
+ export type { default as File, FileOptions } from '../../../File.ts';
9
+
10
+ /**
11
+ * @public
12
+ */
13
+ export interface ApiDOMJSONParserOptions extends Omit<ParserOptions, 'name'> {
14
+ readonly namespace?: Namespace;
15
+ }
16
+
17
+ /**
18
+ * @public
19
+ */
20
+ class ApiDOMJSONParser extends Parser {
21
+ public namespace: Namespace;
22
+
23
+ public ['apidom-json']!: { namespace?: Namespace };
24
+
25
+ constructor(options?: ApiDOMJSONParserOptions) {
26
+ const {
27
+ fileExtensions = [],
28
+ mediaTypes = ['application/vnd.apidom', 'application/vnd.apidom+json'],
29
+ namespace = new Namespace(),
30
+ ...rest
31
+ } = options ?? {};
32
+
33
+ super({ ...rest, name: 'apidom-json', fileExtensions, mediaTypes });
34
+ this.namespace = namespace;
35
+ }
36
+
37
+ canParse(file: File): boolean {
38
+ const hasSupportedFileExtension =
39
+ this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
40
+ const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
41
+
42
+ if (!hasSupportedFileExtension) return false;
43
+ if (hasSupportedMediaType) return true;
44
+ if (!hasSupportedMediaType) {
45
+ try {
46
+ return this.namespace.fromRefract(JSON.parse(file.toString())) && true;
47
+ } catch {
48
+ return false;
49
+ }
50
+ }
51
+ return false;
52
+ }
53
+
54
+ parse(file: File): ParseResultElement {
55
+ const source = file.toString();
56
+ const namespace = this['apidom-json']?.namespace ?? this.namespace;
57
+
58
+ // allow empty files
59
+ if (this.allowEmpty && source.trim() === '') {
60
+ return new ParseResultElement();
61
+ }
62
+
63
+ try {
64
+ const element = namespace.fromRefract(JSON.parse(source));
65
+
66
+ if (!isParseResultElement(element)) {
67
+ element.classes.push('result');
68
+ return new ParseResultElement([element]);
69
+ }
70
+
71
+ return element;
72
+ } catch (error: unknown) {
73
+ throw new ParserError(`Error parsing "${file.uri}"`, { cause: error });
74
+ }
75
+ }
76
+ }
77
+
78
+ export default ApiDOMJSONParser;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4
+ exports.__esModule = true;
5
+ exports.default = void 0;
6
+ var _ramda = require("ramda");
7
+ var _apidomParserAdapterArazzoJson = require("@speclynx/apidom-parser-adapter-arazzo-json-1");
8
+ var _ParserError = _interopRequireDefault(require("../../../errors/ParserError.cjs"));
9
+ var _Parser = _interopRequireDefault(require("../Parser.cjs"));
10
+ var _sourceDescriptions = require("./source-descriptions.cjs");
11
+ exports.parseSourceDescriptions = _sourceDescriptions.parseSourceDescriptions;
12
+ /**
13
+ * @public
14
+ */
15
+
16
+ /**
17
+ * @public
18
+ */
19
+ class ArazzoJSON1Parser extends _Parser.default {
20
+ refractorOpts;
21
+ constructor(options) {
22
+ const {
23
+ fileExtensions = [],
24
+ mediaTypes = _apidomParserAdapterArazzoJson.mediaTypes,
25
+ ...rest
26
+ } = options ?? {};
27
+ super({
28
+ ...rest,
29
+ name: 'arazzo-json-1',
30
+ fileExtensions,
31
+ mediaTypes
32
+ });
33
+ }
34
+ async canParse(file) {
35
+ const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
36
+ const hasSupportedMediaType = this.mediaTypes.includes(file.mediaType);
37
+ if (!hasSupportedFileExtension) return false;
38
+ if (hasSupportedMediaType) return true;
39
+ if (!hasSupportedMediaType) {
40
+ return (0, _apidomParserAdapterArazzoJson.detect)(file.toString());
41
+ }
42
+ return false;
43
+ }
44
+ async parse(file, options) {
45
+ const source = file.toString();
46
+ try {
47
+ const parserOpts = (0, _ramda.pick)(['sourceMap', 'style', 'strict', 'refractorOpts'], this);
48
+ const parseResult = await (0, _apidomParserAdapterArazzoJson.parse)(source, parserOpts);
49
+ const shouldParseSourceDescriptions = options?.parse?.parserOpts?.[this.name]?.sourceDescriptions ?? options?.parse?.parserOpts?.sourceDescriptions;
50
+ if (shouldParseSourceDescriptions) {
51
+ const sourceDescriptions = await (0, _sourceDescriptions.parseSourceDescriptions)(parseResult, file.uri, options, this.name);
52
+ parseResult.push(...sourceDescriptions);
53
+ }
54
+ return parseResult;
55
+ } catch (error) {
56
+ throw new _ParserError.default(`Error parsing "${file.uri}"`, {
57
+ cause: error
58
+ });
59
+ }
60
+ }
61
+ }
62
+ var _default = exports.default = ArazzoJSON1Parser;