@azure-tools/openapi
Version:
OpenAPI common code for Azure Tools.
87 lines • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isExtensionKey = isExtensionKey;
exports.includeXDashKeys = includeXDashKeys;
exports.omitXDashKeys = omitXDashKeys;
exports.includeXDashProperties = includeXDashProperties;
exports.omitXDashProperties = omitXDashProperties;
exports.isReference = isReference;
exports.dereference = dereference;
/**
* Returns true if the key starts with `x-`
*/
function isExtensionKey(key) {
return key.startsWith("x-");
}
/**
* Only return properties starting with x-
* @param dictionary
*/
function includeXDashKeys(dictionary) {
return Object.keys(dictionary).filter(isExtensionKey);
}
/**
* Only return properties NOT starting with x-
* @param dictionary
*/
function omitXDashKeys(dictionary) {
return Object.keys(dictionary).filter((v) => !v.startsWith("x-"));
}
function includeXDashProperties(obj) {
if (obj === undefined) {
return undefined;
}
const result = {};
for (const key of includeXDashKeys(obj)) {
result[key] = obj[key];
}
return result;
}
function omitXDashProperties(obj) {
if (obj === undefined) {
return undefined;
}
const result = {};
for (const key of omitXDashKeys(obj)) {
result[key] = obj[key];
}
return result;
}
/**
* Identifies if a given refable is a reference or an instance
* @param item Check if item is a reference.
*/
function isReference(item) {
return item && item.$ref ? true : false;
}
/**
* Gets an object instance for the item, regardless if it's a reference or not.
* @param document Entire document.
* @param item Reference item.
* @param stack Stack for circular dependencies.
*/
function dereference(document, item, stack = []) {
let name;
if (isReference(item)) {
let node = document;
const path = item.$ref;
if (stack.indexOf(path) > -1) {
throw new Error(`Circular $ref in Model -- ${path} :: ${JSON.stringify(stack)}`);
}
stack.push(path);
const parts = path.replace("#/", "").split("/");
for (name of parts) {
if (!node[name]) {
throw new Error(`Invalid Reference ${name} -- ${path}`);
}
node = node[name];
}
if (isReference(node)) {
// it's a ref to a ref.
return dereference(document, node, stack);
}
return { instance: node, name: name || "", fromRef: true };
}
return { instance: item, name: "", fromRef: false };
}
//# sourceMappingURL=utils.js.map