UNPKG

@har-sdk/core

Version:

The base package can be used to import specification files (i.e. HAR, OAS and Postman Collection) and detect their type.

54 lines 1.68 kB
import { LoaderFactory } from '../loaders'; export class BaseImporter { constructor() { this.docFormats = ['json', 'yaml']; this.loaderFactory = new LoaderFactory(); this.loaders = new Map(); // noop } async import(content, expectedFormat) { const loadResult = this.load(content.trim(), expectedFormat); if (loadResult && this.isSupported(loadResult.doc)) { const { format, doc } = loadResult; const name = this.fileName({ format, doc }); return { doc, name, format, type: this.type }; } } getErrorDetails(format) { var _a; return (_a = this.getLoader(format)) === null || _a === void 0 ? void 0 : _a.getSyntaxErrorDetails(); } fileName(_) { return; } load(source, format) { this.initLoaders(); return this.docFormats.reduce((res, docFormat) => { if (res || (format && format !== docFormat)) { return res; } try { const doc = this.getLoader(docFormat).load(source); if (doc) { return { doc, format: docFormat }; } } catch (e) { // noop } }, undefined); } initLoaders() { this.loaders.clear(); this.docFormats.forEach((docFormat) => this.loaders.set(docFormat, this.loaderFactory.getLoader(docFormat))); } getLoader(docFormat) { return this.loaders.get(docFormat); } } //# sourceMappingURL=BaseImporter.js.map