@apistudio/apim-cli
Version:
CLI for API Management Products
73 lines (65 loc) • 2.02 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 planAsset = asset as unknown as PlanAsset;
const spec: PlanSpec = planAsset.spec;
const result: AssetCacheModel[] = [];
try {
checkForNullOrUndefined(planAsset, "Asset is null or undefined");
// withRateLimit check
if (spec.qos) {
if (Array.isArray(spec.qos["withRateLimit"])) {
const requestLimitRefs: RefObj[] = spec.qos["withRateLimit"].map(
(reqLimit) => ({
$ref: reqLimit.$ref || "",
})
);
addAssetRefValuesForPlanKind(requestLimitRefs, KindEnums.RequestLimit, result);
}
// withQuota check
if (spec.qos["withQuota"]) {
const withQuota = spec.qos["withQuota"];
if (withQuota.$ref) {
addAssetRefValuesForPlanKind(
[{ $ref: withQuota.$ref }],
KindEnums.RequestLimit,
result
);
}
}
}
return result;
} catch (error: unknown) {
showWarning((error as Error).message);
return [];
}
};
export{ getRefsFromPlanAsset}