@apistudio/apim-cli
Version:
CLI for API Management Products
70 lines (58 loc) • 1.74 kB
text/typescript
import {
RefObj,
BaseAsset,
PlanSpec,
PlanAsset,
} from "../../../model/assets-model.js";
import { KindEnums } from "@apic/api-model/common/StudioEnums.js";
import { AssetCacheModel } from "../../../model/asset-cache-model.js";
import {
checkForNullOrUndefined,
isNullOrUndefined,
} from "../../common/data-helper.js";
import { showWarning } from "../../common/message-helper.js";
const addAssetRefValuesForPlanKind = (
refObjects: RefObj[],
kind: KindEnums,
result: AssetCacheModel[]
) => {
if (!isNullOrUndefined(refObjects)) {
refObjects.forEach((endpoint) => {
if (!isNullOrUndefined(endpoint.$ref)) {
result.push({
kind,
ref: endpoint.$ref,
isNewlyAdded: true,
});
}
});
}
};
const getRefsFromPlanAsset = (asset: BaseAsset): AssetCacheModel[] => {
const spec = asset.spec as any;
const result: AssetCacheModel[] = [];
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 is { $ref: string } => !!item?.$ref
);
if (validRefs.length > 0) {
addAssetRefValuesForPlanKind(validRefs, KindEnums.Quota, result);
}
}
});
}
return result;
} catch (error: unknown) {
showWarning((error as Error).message);
return [];
}
};
export { getRefsFromPlanAsset }