@redocly/openapi-core
Version:
See https://github.com/Redocly/redocly-cli
74 lines • 2.71 kB
JavaScript
import { BaseResolver, makeDocumentFromString } from '../resolve.js';
import { walkDocument } from '../walk.js';
import { detectSpec } from '../detect-spec.js';
import { getTypes } from '../oas-types.js';
import { NormalizedConfigTypes } from '../types/redocly-yaml.js';
import { configBundlerVisitor, pluginsCollectorVisitor } from '../config/visitors.js';
import { CONFIG_BUNDLER_VISITOR_ID, PLUGINS_COLLECTOR_VISITOR_ID } from '../config/constants.js';
import { bundleDocument } from './bundle-document.js';
export function collectConfigPlugins(document, resolvedRefMap, rootConfigDir) {
const visitorsData = { plugins: [], rootConfigDir };
const ctx = {
problems: [],
specVersion: 'oas3_0', // TODO: change it to a config-specific type
refTypes: new Map(),
visitorsData: {
[PLUGINS_COLLECTOR_VISITOR_ID]: visitorsData,
},
};
walkDocument({
document,
rootType: NormalizedConfigTypes.ConfigRoot,
normalizedVisitors: pluginsCollectorVisitor,
resolvedRefMap,
ctx,
});
return visitorsData.plugins;
}
export function bundleConfig(document, resolvedRefMap, plugins) {
const visitorsData = { plugins };
const ctx = {
problems: [],
specVersion: 'oas3_0', // TODO: change it to a config-specific type
refTypes: new Map(),
visitorsData: {
[CONFIG_BUNDLER_VISITOR_ID]: visitorsData,
},
};
walkDocument({
document,
rootType: NormalizedConfigTypes.ConfigRoot,
normalizedVisitors: configBundlerVisitor,
resolvedRefMap,
ctx,
});
return document.parsed ?? {};
}
export async function bundle(opts) {
const { ref, doc, externalRefResolver = new BaseResolver(opts.config.resolve), base = null, } = opts;
if (!(ref || doc)) {
throw new Error('Document or reference is required.\n');
}
const document = doc === undefined ? await externalRefResolver.resolveDocument(base, ref, true) : doc;
if (document instanceof Error) {
throw document;
}
opts.collectSpecData?.(document.parsed);
return bundleDocument({
document,
...opts,
externalRefResolver,
types: getTypes(detectSpec(document.parsed)),
});
}
export async function bundleFromString(opts) {
const { source, absoluteRef, externalRefResolver = new BaseResolver(opts.config.resolve) } = opts;
const document = makeDocumentFromString(source, absoluteRef || '/');
return bundleDocument({
document,
...opts,
externalRefResolver,
types: getTypes(detectSpec(document.parsed)),
});
}
//# sourceMappingURL=bundle.js.map