@apistudio/apim-cli
Version:
CLI for API Management Products
57 lines (56 loc) • 2.02 kB
JavaScript
import { checkForNullOrUndefined, isNullOrUndefined, } from "../../common/data-helper.js";
import { showError } from "./../../common/message-helper.js";
import { KindEnums } from "@apic/api-model/common/StudioEnums.js";
const addAssetRefValuesForTestKind = (refObjects, result, kind) => {
if (!isNullOrUndefined(refObjects)) {
refObjects.forEach((refObj) => {
if (!isNullOrUndefined(refObj.$ref)) {
result.push({
kind,
ref: refObj.$ref,
isNewlyAdded: true
});
}
});
}
};
const processReferences = (refContainer, result, kind) => {
if (!refContainer)
return;
// Format 1: Single reference with direct $ref property
if (refContainer.$ref && typeof refContainer.$ref === 'string') {
addAssetRefValuesForTestKind([{ $ref: refContainer.$ref }], result, kind);
}
// Format 2: Multiple references with individual $ref properties (array of objects)
else if (Array.isArray(refContainer)) {
refContainer.forEach((item) => {
if (item.$ref) {
addAssetRefValuesForTestKind([{ $ref: item.$ref }], result, kind);
}
});
}
};
const getRefsFromTestAsset = (asset) => {
const testAsset = asset;
const spec = testAsset.spec;
const result = [];
try {
checkForNullOrUndefined(testAsset, "Asset is null or undefined");
// Process environment references
processReferences(spec.environment, result, KindEnums.Environment);
// Process assertion references in requests
if (spec.request) {
spec.request.forEach((request) => {
if (request.assertions) {
processReferences(request.assertions, result, KindEnums.Assertion);
}
});
}
return result;
}
catch (error) {
showError(error.message);
return [];
}
};
export { getRefsFromTestAsset };