@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
122 lines • 5.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getResourceType = getResourceType;
exports.getResourceKey = getResourceKey;
exports.getResourceKeySegment = getResourceKeySegment;
exports.getScopePath = getScopePath;
exports.isScopedSegment = isScopedSegment;
exports.isScopedPath = isScopedPath;
exports.getExtensionResourceType = getExtensionResourceType;
exports.isSingleton = isSingleton;
exports.pathIncludes = pathIncludes;
const schemas_1 = require("../utils/schemas");
const constants_1 = require("./constants");
function getResourceType(path) {
const index = path.lastIndexOf(constants_1.ProvidersSegment);
if (index < 0 || path.substring(index + constants_1.ProvidersSegment.length).includes("/") === false) {
const pathToLower = path.toLowerCase();
if (pathToLower.startsWith(constants_1.ResourceGroupScopePrefix.toLowerCase()))
return "Microsoft.Resources/resourceGroups";
if (pathToLower.startsWith(constants_1.SubscriptionScopePrefix.toLowerCase()))
return "Microsoft.Resources/subscriptions";
if (pathToLower.startsWith(constants_1.TenantScopePrefix.toLowerCase()))
return "Microsoft.Resources/tenants";
throw `Path ${path} doesn't have resource type`;
}
return path
.substring(index + constants_1.ProvidersSegment.length)
.split("/")
.reduce((result, current, index) => {
if (index === 0 || index % 2 === 1)
return result === "" ? current : `${result}/${current}`;
else
return result;
}, "");
}
function getResourceKey(path) {
const segments = path.split("/");
return segments[segments.length - 1].replace(/^\{(\w+)\}$/, "$1");
}
function getResourceKeySegment(path) {
const segments = path.split("/");
return segments[segments.length - 2];
}
function getScopePath(path) {
const pathToLower = path.toLowerCase();
const index = pathToLower.lastIndexOf(constants_1.ProvidersSegment);
if (pathToLower.startsWith(constants_1.ManagementGroupScopePrefix.toLowerCase()))
return constants_1.ManagementGroupPath;
if (index >= 0)
return path.slice(0, index);
if (pathToLower.startsWith(constants_1.ResourceGroupScopePrefix.toLowerCase()))
return constants_1.ResourceGroupPath;
if (pathToLower.startsWith(constants_1.SubscriptionScopePrefix.toLowerCase()))
return constants_1.SubscriptionPath;
if (pathToLower.startsWith(constants_1.TenantScopePrefix.toLowerCase()))
return constants_1.TenantPath;
return path;
}
function isScopedSegment(path) {
const pattern = /^\/?\{\w+\}\/?$/;
return path.match(pattern) !== null;
}
function isScopedPath(path) {
return isScopedSegment(getScopePath(path));
}
function getExtensionResourceType(path) {
if (isScopedPath(path))
return "Scope";
const index = path.lastIndexOf(constants_1.ProvidersSegment);
if (index < 0)
return "NA";
const pathBeforeProviders = path.substring(0, index).toLowerCase();
if (path.toLowerCase().startsWith(constants_1.ManagementGroupScopePrefix.toLowerCase()))
return "ManagementGroup";
if (pathBeforeProviders.includes(constants_1.ProvidersSegment))
return "Extension";
if (path.toLowerCase().startsWith(constants_1.ResourceGroupScopePrefix.toLowerCase()))
return "ResourceGroup";
if (path.toLowerCase().startsWith(constants_1.SubscriptionScopePrefix.toLowerCase()))
return "Subscription";
if (path.toLowerCase().startsWith(constants_1.ProvidersSegment))
return "Tenant";
return "NA";
}
function isSingleton(set) {
var _a;
const lastSegment = getLastSegment(set.RequestPath);
if (!isVariable(lastSegment))
return true;
const resourceKey = lastSegment.replace(/^\{(\w+)\}$/, "$1");
const resourceKeyParameter = (_a = set.Operations[0].parameters) === null || _a === void 0 ? void 0 : _a.find((p) => p.language.default.name === resourceKey || p.language.default.serializedName === resourceKey);
if (resourceKeyParameter === undefined)
throw `Cannot find parameter ${resourceKey} in operation ${set.Operations[0].operationId}`;
return (0, schemas_1.isConstantSchema)(resourceKeyParameter === null || resourceKeyParameter === void 0 ? void 0 : resourceKeyParameter.schema);
}
function pathIncludes(path1, path2) {
const lowerPath1 = path1.toLowerCase();
const lowerPath2 = path2.toLowerCase();
const segments1 = lowerPath1.split("/");
const segments2 = lowerPath2.split("/");
if (segments2.length > segments1.length)
return false;
// If the segment is a variable, then different variable names are still equivalent
for (let index = 0; index < segments2.length; ++index) {
if (isVariable(segments1[index])) {
if (!isVariable(segments2[index]))
return false;
}
else if (segments1[index] !== segments2[index])
return false;
}
return true;
}
// {variableName}
function isVariable(segment) {
return segment.match(/^\{\w+\}$/) !== null;
}
function getLastSegment(path) {
const segments = path.split("/");
return segments[segments.length - 1];
}
//# sourceMappingURL=utils.js.map