@autorest/openapi-to-typespec
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
103 lines • 4.59 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathIncludes = exports.isSingleton = exports.isScopedPath = exports.isScopedSegment = exports.getScopePath = exports.getResourceKeySegment = exports.getResourceKey = exports.getResourceType = void 0;
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;
}, "");
}
exports.getResourceType = getResourceType;
function getResourceKey(path) {
const segments = path.split("/");
return segments[segments.length - 1].replace(/^\{(\w+)\}$/, "$1");
}
exports.getResourceKey = getResourceKey;
function getResourceKeySegment(path) {
const segments = path.split("/");
return segments[segments.length - 2];
}
exports.getResourceKeySegment = getResourceKeySegment;
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;
}
exports.getScopePath = getScopePath;
function isScopedSegment(path) {
const pattern = /^\/?\{\w+\}\/?$/;
return path.match(pattern) !== null;
}
exports.isScopedSegment = isScopedSegment;
function isScopedPath(path) {
return isScopedSegment(getScopePath(path));
}
exports.isScopedPath = isScopedPath;
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);
}
exports.isSingleton = isSingleton;
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;
}
exports.pathIncludes = pathIncludes;
// {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
;