@microsoft.azure/openapi-validator-core
Version:
Azure OpenAPI Validator
83 lines • 3.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerInventory = void 0;
const document_1 = require("./document");
const jsonParser_1 = require("./jsonParser");
const utils_1 = require("./utils");
const DepGraph = require("dependency-graph").DepGraph;
class SwaggerInventory {
constructor(fileSystem = utils_1.defaultFileSystem) {
this.fileSystem = fileSystem;
this.inventory = new DepGraph({ circular: true });
this.referenceCache = new Map();
this.allDocs = new Map();
this.docRecords = undefined;
}
getSingleDocument(specPath) {
var _a;
return (_a = this.getInternalDocument(specPath)) === null || _a === void 0 ? void 0 : _a.getObj();
}
getDocumentContent(specPath) {
var _a;
return (_a = this.getInternalDocument(specPath)) === null || _a === void 0 ? void 0 : _a.getContent();
}
getInternalDocument(specPath) {
const urlPath = (0, utils_1.normalizePath)(specPath);
if (this.referenceCache.has(urlPath)) {
return this.referenceCache.get(urlPath);
}
throw new Error(`No cached file:${specPath}`);
}
referencesOf(specPath) {
const result = {};
const references = this.inventory.dependantsOf((0, utils_1.normalizePath)(specPath));
for (const ref of references) {
result[ref] = this.getSingleDocument(ref);
}
return result;
}
getDocuments(docPath) {
if (docPath) {
return this.getSingleDocument(docPath);
}
if (!this.docRecords) {
this.docRecords = {};
for (const [key, value] of this.allDocs.entries()) {
this.docRecords[key] = value;
}
}
return this.docRecords;
}
async loadDocument(specPath) {
const urlPath = (0, utils_1.normalizePath)(specPath);
let cache = this.referenceCache.get(urlPath);
if (cache) {
return cache;
}
cache = await this.cacheDocument(urlPath);
return cache;
}
async cacheDocument(specPath) {
const cache = this.allDocs.get(specPath);
if (cache) {
return cache;
}
const parser = new jsonParser_1.JsonParser();
const document = new document_1.OpenapiDocument(specPath, parser, this.fileSystem);
await document.resolve();
this.referenceCache.set(specPath, document);
this.allDocs.set(specPath, document.getObj());
this.inventory.addNode(specPath);
const references = document.getReferences();
for (const ref of references) {
if (!this.allDocs.has(ref)) {
this.inventory.addNode(ref);
await this.cacheDocument(ref);
}
this.inventory.addDependency(specPath, ref);
}
return document;
}
}
exports.SwaggerInventory = SwaggerInventory;
//# sourceMappingURL=swaggerInventory.js.map