@apistudio/apim-cli
Version:
CLI for API Management Products
138 lines (137 loc) • 4.66 kB
JavaScript
import { checkForNullOrUndefined } from "../helpers/common/data-helper.js";
import { showWarning } from "../helpers/common/message-helper.js";
import { findPolicyKind } from "../helpers/apim/asset-kinds/policy-helper.js";
const getRefsFromApiAsset = (asset) => {
const apiAsset = asset;
const dependentAssets = [];
const dependentPolicySequenceAssets = checkPolicySequence(apiAsset);
dependentAssets.push(...dependentPolicySequenceAssets);
// Also check for properties references
const dependentPropertiesAssets = checkProperties(apiAsset);
dependentAssets.push(...dependentPropertiesAssets);
// Also check for uriSchemes references
const dependentUriSchems = checkuriSchemes(apiAsset);
dependentAssets.push(...dependentUriSchems);
// Also check for CORS references
const dependentCORS = checkCORS(apiAsset);
dependentAssets.push(...dependentCORS);
// Also check for scopes references
const dependentScopes = checkScopes(apiAsset);
dependentAssets.push(...dependentScopes);
return dependentAssets;
};
const getAPIDefPath = (asset) => {
const apiAsset = asset;
const spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
const apiSpec = checkForNullOrUndefined(spec["api-spec"], `Attribute 'api-spec' is not defined
for kind 'API' and name '${apiAsset.metadata?.name}'`);
return apiSpec.$path;
};
const checkPolicySequence = (apiAsset) => {
let spec = null;
try {
spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
}
catch (error) {
showWarning(error.message);
return [];
}
const policySeq = spec["policy-sequence"];
let policyKind = findPolicyKind(apiAsset);
return policySeq.map((pSeq) => {
return {
kind: policyKind,
ref: pSeq.$ref,
isNewlyAdded: true,
};
});
};
const checkProperties = (apiAsset) => {
let spec = null;
try {
spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
}
catch (error) {
showWarning(error.message);
return [];
}
// Use type assertion to access properties that might not be in the type definition
const properties = spec.properties;
if (!properties) {
return [];
}
const policySeq = properties;
return policySeq.map((pSeq) => {
return {
kind: 'properties',
ref: pSeq.$ref,
isNewlyAdded: true,
};
});
};
const checkuriSchemes = (apiAsset) => {
let spec = null;
try {
spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
}
catch (error) {
showWarning(error.message);
return [];
}
// Use type assertion to access properties that might not be in the type definition
const uriSchemes = spec.uriSchemes;
if (!uriSchemes) {
return [];
}
const policySeq = uriSchemes;
return [{
kind: 'URISchemes',
ref: policySeq.$ref,
isNewlyAdded: true,
}];
};
const checkCORS = (apiAsset) => {
let spec = null;
try {
spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
}
catch (error) {
showWarning(error.message);
return [];
}
// Use type assertion to access properties that might not be in the type definition
const cors = spec.cors;
if (!cors) {
return [];
}
const policySeq = cors;
return [{
kind: 'CORS',
ref: policySeq.$ref,
isNewlyAdded: true,
}];
};
const checkScopes = (apiAsset) => {
let spec = null;
try {
spec = checkForNullOrUndefined(apiAsset.spec, `Spec is not defined for the asset with kind 'API' and name '${apiAsset.metadata?.name}'`);
}
catch (error) {
showWarning(error.message);
return [];
}
// Use type assertion to access properties that might not be in the type definition
const scopes = spec.scopes;
if (!scopes) {
return [];
}
const policySeq = scopes;
return policySeq.map((pSeq) => {
return {
kind: 'scopes',
ref: pSeq.$ref,
isNewlyAdded: true,
};
});
};
export { getRefsFromApiAsset, getAPIDefPath };