@apistudio/apim-cli
Version:
CLI for API Management Products
44 lines (43 loc) • 1.59 kB
JavaScript
import { KindEnums } from "@apic/api-model/common/StudioEnums.js";
import { checkForNullOrUndefined, isNullOrUndefined, } from "../../common/data-helper.js";
import { showWarning } from "../../common/message-helper.js";
const addAssetRefValuesForPlanKind = (refObjects, kind, result) => {
if (!isNullOrUndefined(refObjects)) {
refObjects.forEach((endpoint) => {
if (!isNullOrUndefined(endpoint.$ref)) {
result.push({
kind,
ref: endpoint.$ref,
isNewlyAdded: true,
});
}
});
}
};
const getRefsFromPlanAsset = (asset) => {
const spec = asset.spec;
const result = [];
try {
checkForNullOrUndefined(spec, "Asset is null or undefined");
if (spec.qos?.withQuota) {
const withQuotaSection = spec.qos["withQuota"];
// Loop over all keys under withQuota
Object.keys(withQuotaSection).forEach((key) => {
const entry = withQuotaSection[key];
// Ensure it's an array of objects with $ref strings
if (Array.isArray(entry)) {
const validRefs = entry.filter((item) => !!item?.$ref);
if (validRefs.length > 0) {
addAssetRefValuesForPlanKind(validRefs, KindEnums.Quota, result);
}
}
});
}
return result;
}
catch (error) {
showWarning(error.message);
return [];
}
};
export { getRefsFromPlanAsset };