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.

71 lines 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonNavigation = void 0; const assert_1 = require("assert"); const jsonPointer = require("json-pointer"); const ApiRefResolver_1 = require("./ApiRefResolver"); class JsonNavigation { constructor(document, ...keys) { (0, assert_1.strict)(document); (0, assert_1.strict)(typeof document === 'object' || Array.isArray(document)); this.document = document; if (keys) { this.keys = [...keys]; } else { this.keys = []; } } path() { return [...this.keys]; } itemAtFragment(fragment) { if (!fragment) { return undefined; } const noHash = fragment.substring(1); const val = jsonPointer(this.document, noHash); const clone = ApiRefResolver_1.ApiRefResolver.deepClone(val); return clone; } static itemAtFragment(document, fragment) { return new JsonNavigation(document).itemAtFragment(fragment); } asFragment() { const fragment = jsonPointer.compile(this.keys); return `#${fragment}`; } static asFragment(keys, withHash = false) { const fragment = jsonPointer.compile(keys); return withHash ? `#${fragment}` : fragment; } static asKeys(fragment) { const keys = jsonPointer.parse(fragment.substring(1)).map((key) => { if (/^\d+$/.exec(key)) { return parseInt(key); } return key; }); return keys; } isAtComponent() { return this.keys.length === 3 && this.keys[0] === 'components'; } itemAtPointer(keys) { return jsonPointer(this.document, keys); } currentKey() { return this.keys[this.keys.length - 1]; } lastItem() { return this.itemAtPointer(this.keys); } with(key) { const newNav = new JsonNavigation(this.document); newNav.keys = [...this.keys]; newNav.keys.push(key); return newNav; } } exports.JsonNavigation = JsonNavigation; //# sourceMappingURL=JsonNavigation.js.map