@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
159 lines • 7.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setParentOfResourceCollectionOperation = setParentOfResourceCollectionOperation;
exports.getResourceCollectionOperations = getResourceCollectionOperations;
exports.setParentOfOtherOperation = setParentOfOtherOperation;
exports.getOtherOperations = getOtherOperations;
exports.setParentOfExtensionOperation = setParentOfExtensionOperation;
exports.getExtensionOperation = getExtensionOperation;
exports.getParents = getParents;
const strings_1 = require("../utils/strings");
const constants_1 = require("./constants");
const operation_set_1 = require("./operation-set");
const resource_equivalent_1 = require("./resource-equivalent");
const utils_1 = require("./utils");
const extensionMethodCache = new WeakMap();
const resourceCollectionMethodCache = new WeakMap();
const otherOperationCache = new WeakMap();
function setParentOfResourceCollectionOperation(operation, requestPath, operationSets) {
// first we need to ensure this operation at least returns a collection of something
const itemType = (0, resource_equivalent_1.getPagingItemType)(operation);
if (itemType === undefined)
return false;
// then check if its path is a prefix of which resource's operationSet
const requestScopeType = getScopeResourceType(requestPath);
const candidates = [];
for (const operationSet of operationSets) {
const resourceRequestPath = operationSet.RequestPath;
const resourceScopeType = getScopeResourceType(resourceRequestPath);
if (!(0, utils_1.pathIncludes)(resourceScopeType, requestScopeType) && !resourceScopeType.includes("*"))
continue;
const providerIndexOfResource = resourceRequestPath.lastIndexOf(constants_1.ProvidersSegment);
const providerIndexOfRequest = requestPath.lastIndexOf(constants_1.ProvidersSegment);
const trimmedResourceRequestPath = resourceRequestPath.substring(providerIndexOfResource);
const trimmedRequestPath = requestPath.substring(providerIndexOfRequest);
if (!(0, utils_1.pathIncludes)(trimmedResourceRequestPath, trimmedRequestPath))
continue;
if ((0, operation_set_1.getResourceDataSchema)(operationSet) !== itemType)
continue;
candidates.push(operationSet);
}
// if there are multiple resources that share the same prefix of request path, we choose the shortest one
if (candidates.length === 0)
return false;
const bestOne = candidates.sort((a, b) => b.RequestPath.split("/").length - a.RequestPath.split("/").length)[0];
if (resourceCollectionMethodCache.has(bestOne)) {
resourceCollectionMethodCache.get(bestOne).push(operation);
}
else {
resourceCollectionMethodCache.set(bestOne, [operation]);
}
return true;
}
function getResourceCollectionOperations(set) {
var _a;
return (_a = resourceCollectionMethodCache.get(set)) !== null && _a !== void 0 ? _a : [];
}
function setParentOfOtherOperation(operation, requestPath, operationSets) {
const candidates = operationSets.filter((o) => { var _a; return (0, utils_1.pathIncludes)(requestPath, (_a = o.SingletonRequestPath) !== null && _a !== void 0 ? _a : o.RequestPath); });
if (candidates.length === 0)
return false;
const bestOne = candidates.sort((a, b) => b.RequestPath.split("/").length - a.RequestPath.split("/").length)[0];
if (otherOperationCache.has(bestOne)) {
otherOperationCache.get(bestOne).push(operation);
}
else {
otherOperationCache.set(bestOne, [operation]);
}
return true;
}
function getOtherOperations(set) {
var _a;
return (_a = otherOperationCache.get(set)) !== null && _a !== void 0 ? _a : [];
}
function setParentOfExtensionOperation(operation, requestPath, operationSets) {
const itemType = (0, resource_equivalent_1.getPagingItemType)(operation);
if (itemType === undefined)
return false;
const providerIndexOfRequest = requestPath.lastIndexOf(constants_1.ProvidersSegment);
const trimmedRequestPath = requestPath.substring(providerIndexOfRequest);
let extensionType = "";
switch ((0, utils_1.getScopePath)(requestPath)) {
case constants_1.ResourceGroupPath:
extensionType = "Resource";
break;
case constants_1.SubscriptionPath:
extensionType = "Subscription";
break;
case constants_1.ManagementGroupPath:
extensionType = "ManagementGroup";
break;
case constants_1.TenantPath:
extensionType = "Tenant";
break;
}
const candidates = [];
for (const operationSet of operationSets) {
const resourceRequestPath = operationSet.RequestPath;
const providerIndexOfResource = resourceRequestPath.lastIndexOf(constants_1.ProvidersSegment);
const trimmedResourceRequestPath = resourceRequestPath.substring(providerIndexOfResource);
if (!(0, utils_1.pathIncludes)(trimmedResourceRequestPath, trimmedRequestPath))
continue;
if ((0, operation_set_1.getResourceDataSchema)(operationSet) !== itemType)
continue;
candidates.push(operationSet);
}
if (candidates.length === 0)
return false;
const bestOne = candidates.sort((a, b) => a.RequestPath.split("/").length - b.RequestPath.split("/").length)[0];
if (extensionMethodCache.has(bestOne)) {
extensionMethodCache.get(bestOne).push([operation, extensionType]);
}
else {
extensionMethodCache.set(bestOne, [[operation, extensionType]]);
}
return true;
}
function getExtensionOperation(set) {
var _a;
return (_a = extensionMethodCache.get(set)) !== null && _a !== void 0 ? _a : [];
}
function getParents(requestPath, operationSets) {
let segments = requestPath.split("/");
if (segments.length < 2)
return ["ArmResource"];
segments = segments.slice(0, -2);
if (segments.length < 2)
return ["ArmResource"];
if (segments[segments.length - 2] === "providers")
segments = segments.slice(0, -2);
const parentPath = segments.join("/");
if (parentPath === constants_1.ManagementGroupPath)
return ["ManagementGroupResource"];
if (parentPath === constants_1.ResourceGroupPath)
return ["ResourceGroupResource"];
if (parentPath === constants_1.SubscriptionPath)
return ["SubscriptionResource"];
if (parentPath === constants_1.TenantPath)
return ["TenantResource"];
const operationSet = operationSets.find((set) => { var _a; return ((_a = set.SingletonRequestPath) !== null && _a !== void 0 ? _a : set.RequestPath) === parentPath; });
if (operationSet === undefined) {
return getParents(parentPath, operationSets);
}
return [(0, strings_1.lastWordToSingular)((0, operation_set_1.getResourceDataSchema)(operationSet))];
}
function getScopeResourceType(path) {
const scopePath = (0, utils_1.getScopePath)(path);
if (scopePath === constants_1.SubscriptionPath)
return "Microsoft.Resources/subscriptions";
if (scopePath === constants_1.ResourceGroupPath)
return "Microsoft.Resources/resourceGroups";
if (scopePath === constants_1.ManagementGroupPath)
return "Microsoft.Management/managementGroups";
if (scopePath === constants_1.TenantPath)
return "Microsoft.Resources/tenants";
if ((0, utils_1.isScopedSegment)(scopePath))
return "*";
return (0, utils_1.getResourceType)(scopePath);
}
//# sourceMappingURL=find-parent.js.map