source: node_modules/@swagger-api/apidom-reference/es/bundle/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: 1.6 KB
Line 
1import { isEmpty, propEq } from 'ramda';
2import File from "../util/File.mjs";
3import * as plugins from "../util/plugins.mjs";
4import UnmatchedBundleStrategyError from "../errors/UnmatchedBundleStrategyError.mjs";
5import BundleError from "../errors/BundleError.mjs";
6import parse from "../parse/index.mjs";
7import { merge as mergeOptions } from "../options/util.mjs";
8import * as url from "../util/url.mjs";
9/**
10 * Bundle a file with all its external references to a compound document.
11 */
12const bundle = async (uri, options) => {
13 const {
14 refSet
15 } = options.bundle;
16 const sanitizedURI = url.sanitize(uri);
17 const mergedOptions = mergeOptions(options, {
18 resolve: {
19 baseURI: sanitizedURI
20 }
21 });
22 let parseResult;
23
24 // if refSet was provided, use it to avoid unnecessary parsing
25 if (refSet !== null && refSet.has(sanitizedURI)) {
26 // @ts-ignore
27 ({
28 value: parseResult
29 } = refSet.find(propEq(sanitizedURI, 'uri')));
30 } else {
31 parseResult = await parse(uri, mergedOptions);
32 }
33 const file = File({
34 uri: mergedOptions.resolve.baseURI,
35 parseResult,
36 mediaType: mergedOptions.parse.mediaType
37 });
38 const bundleStrategies = await plugins.filter('canBundle', file, mergedOptions.bundle.strategies);
39
40 // we couldn't find any bundle strategy for this File
41 if (isEmpty(bundleStrategies)) {
42 throw new UnmatchedBundleStrategyError(file.uri);
43 }
44 try {
45 const {
46 result
47 } = await plugins.run('bundle', [file, mergedOptions], bundleStrategies);
48 return result;
49 } catch (error) {
50 throw new BundleError(`Error while bundling file "${file.uri}"`, {
51 cause: error
52 });
53 }
54};
55export default bundle;
Note: See TracBrowser for help on using the repository browser.