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