UNPKG

@apiture/api-ref-resolver

Version:

Tool to merge multiple OpenAPI or AsyncAPI documents that use JSON Reference links (`$ref`) to reference API definition elements across source files.

61 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.walkObject = exports.visitRefObjects = exports.isRef = void 0; const JsonNavigation_1 = require("./JsonNavigation"); function isRef(node) { return (node !== null && typeof node === 'object' && node.hasOwnProperty('$ref') && typeof node.$ref === 'string'); } exports.isRef = isRef; function isResolved(node) { return node !== null && typeof node === 'object' && node.hasOwnProperty('x__resolved__'); } async function visitRefObjects(node, refCallback, nav) { const objectVisitor = async (node, nav) => { if (isRef(node)) { if (isResolved(node)) { return node; } return await refCallback(node, nav); } return node; }; return walkObject(node, objectVisitor, nav); } exports.visitRefObjects = visitRefObjects; async function walkObject(node, objectCallback, nav) { return walkObj(node, nav || new JsonNavigation_1.JsonNavigation(node)); async function walkObj(node, location) { const object = objectCallback(node, location); if (object !== null && typeof object === 'object') { const keys = [...Object.keys(node)]; for (const key of keys) { const val = node[key]; if (Array.isArray(val)) { node[key] = await walkArray(val, location.with(key)); } else if (val !== null && typeof val === 'object') { node[key] = await walkObj(val, location.with(key)); } } } return object; } async function walkArray(a, nav) { const array = a; for (let index = 0; index < a.length; index += 1) { const val = array[index]; if (val !== null && typeof val === 'object') { array[index] = (await walkObj(val, nav.with(index))); } else if (Array.isArray(val)) { array[index] = (await walkArray(val, nav.with(index))); } } return a; } } exports.walkObject = walkObject; //# sourceMappingURL=RefVisitor.js.map