source: node_modules/@swagger-api/apidom-reference/cjs/parse/index.cjs@ 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: 2.5 KB
RevLine 
[d24f17c]1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4var _interopRequireWildcard = require("@babel/runtime-corejs3/helpers/interopRequireWildcard").default;
5exports.__esModule = true;
6exports.default = void 0;
7var _ramda = require("ramda");
8var url = _interopRequireWildcard(require("../util/url.cjs"));
9var _File = _interopRequireDefault(require("../util/File.cjs"));
10var plugins = _interopRequireWildcard(require("../util/plugins.cjs"));
11var _ParseError = _interopRequireDefault(require("../errors/ParseError.cjs"));
12var _UnmatchedResolverError = _interopRequireDefault(require("../errors/UnmatchedResolverError.cjs"));
13var _util = require("../resolve/util.cjs");
14/**
15 * Parses the given file's contents, using the configured parser plugins.
16 */
17const 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, optsBoundParsers);
23
24 // we couldn't find any parser for this File
25 if ((0, _ramda.isEmpty)(parsers)) {
26 throw new _UnmatchedResolverError.default(file.uri);
27 }
28 try {
29 const {
30 plugin,
31 result
32 } = await plugins.run('parse', [file], 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 */
49const 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 = (0, _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((0, _File.default)({
65 ...file,
66 data
67 }), options);
68};
69var _default = exports.default = parse;
Note: See TracBrowser for help on using the repository browser.