@apistudio/apim-cli
Version:
CLI for API Management Products
75 lines (74 loc) • 3.44 kB
JavaScript
import { KindEnums } from "@apic/api-model/common/StudioEnums.js";
import { checkForNullOrUndefined, } from "../../common/data-helper.js";
import { showWarning } from "./../../common/message-helper.js";
const getRefsFromMockEndpointAsset = (asset) => {
const mockEndpointAsset = asset;
const spec = mockEndpointAsset.spec;
const result = [];
try {
checkForNullOrUndefined(mockEndpointAsset, "MockEndpoint asset is null or undefined");
if (spec.paths) {
const uniqueRefs = new Set();
for (const path in spec.paths) {
const pathObject = spec.paths[path];
const httpMethods = [
"get",
"post",
"put",
"patch",
"delete",
"head",
"options",
"trace",
];
// Iterate over the valid HTTP methods
for (const method of httpMethods) {
const methodObject = pathObject[method];
if (methodObject) {
// Check defaultResponse
if (methodObject.defaultResponse) {
for (const statusCode in methodObject.defaultResponse) {
const responseObj = methodObject.defaultResponse[statusCode];
if (responseObj.response?.$ref) {
const refValue = responseObj.response.$ref;
// Only add if this $ref hasn't been seen before
if (!uniqueRefs.has(refValue)) {
uniqueRefs.add(refValue);
result.push({
kind: KindEnums.MockResponse,
isNewlyAdded: true,
ref: responseObj.response.$ref,
});
}
}
}
}
// Check conditionalResponse
if (methodObject.conditionalResponse) {
methodObject.conditionalResponse.forEach((condition, index) => {
if (condition.response?.$ref) {
const refValue = condition.response.$ref;
// Only add if this $ref hasn't been seen before
if (!uniqueRefs.has(refValue)) {
uniqueRefs.add(refValue);
result.push({
kind: KindEnums.MockResponse,
isNewlyAdded: true,
ref: condition.response.$ref,
});
}
}
});
}
}
}
}
}
return result;
}
catch (error) {
showWarning(error.message);
return [];
}
};
export { getRefsFromMockEndpointAsset };