@apistudio/apim-cli
Version:
CLI for API Management Products
61 lines (60 loc) • 2.4 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 addAssetRefValuesForRouteKind = (refObjects, kind, result) => {
if (!isNullOrUndefined(refObjects)) {
refObjects.forEach((endpoint) => {
if (!isNullOrUndefined(endpoint.$ref)) {
result.push({
kind,
ref: endpoint.$ref,
isNewlyAdded: true,
});
}
});
}
};
const getRefsFromRouteAsset = (asset) => {
const routeAsset = asset;
const spec = routeAsset.spec;
const result = [];
try {
checkForNullOrUndefined(routeAsset, "Asset is null or undefined");
// Default endpoint check
if (spec["default-endpoint"]) {
const defaultEndpoint = spec["default-endpoint"];
if (defaultEndpoint.$ref) {
addAssetRefValuesForRouteKind([{ $ref: defaultEndpoint.$ref }], KindEnums.HTTPEndpoint, result);
}
}
// Loadbalance endpoints check
if (Array.isArray(spec["loadbalance-endpoints"])) {
const loadbalanceEndpoints = spec["loadbalance-endpoints"].map((endpoint) => ({
$ref: endpoint.$ref || "",
}));
addAssetRefValuesForRouteKind(loadbalanceEndpoints, KindEnums.HTTPEndpoint, result);
}
// Conditional endpoints check
if (Array.isArray(spec["conditional-endpoints"])) {
spec["conditional-endpoints"].forEach((conditional) => {
if (typeof conditional.endpoint === "object" &&
conditional.endpoint.$ref) {
addAssetRefValuesForRouteKind([{ $ref: conditional.endpoint.$ref }], KindEnums.HTTPEndpoint, result);
}
});
}
// Mock endpoint check
if (spec["mock-endpoint"]) {
const mockEndpoint = spec["mock-endpoint"];
if (mockEndpoint.$ref) {
addAssetRefValuesForRouteKind([{ $ref: mockEndpoint.$ref }], KindEnums.MockEndpoint, result);
}
}
return result;
}
catch (error) {
showWarning(error.message);
return [];
}
};
export { getRefsFromRouteAsset };