UNPKG

@autorest/openapi-to-typespec

Version:

Autorest plugin to scaffold a Typespec definition from an OpenAPI document

153 lines 7.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.filterArmEnums = exports.filterArmModels = exports.isResourceSchema = exports.tagSchemaAsResource = exports.getResourceExistOperation = exports.getSingletonResouceListOperation = exports.getResourceOperations = void 0; const codemodel_1 = require("@autorest/codemodel"); const autorest_session_1 = require("../autorest-session"); const type_mapping_1 = require("./type-mapping"); let metadataCache; function getResourceOperations(resource) { const operations = {}; const codeModel = (0, autorest_session_1.getSession)().model; const allOperations = [ resource.GetOperation, resource.CreateOperation, resource.ExistOperation, resource.UpdateOperation, resource.DeleteOperation, ].filter((o) => o !== undefined) .concat(resource.ListOperations) .concat(resource.OperationsFromResourceGroupExtension) .concat(resource.OperationsFromSubscriptionExtension) .concat(resource.OperationsFromManagementGroupExtension) .concat(resource.OperationsFromTenantExtension) .concat(resource.OtherOperations); for (const operationGroup of codeModel.operationGroups) { for (const operation of operationGroup.operations) { for (const operationMetadata of allOperations) { if (operation.operationId === operationMetadata.OperationID) { operations[operation.operationId] = operation; operation.isResourceOperation = true; } } } } return operations; } exports.getResourceOperations = getResourceOperations; function getSingletonResouceListOperation(resource) { var _a, _b, _c, _d; const codeModel = (0, autorest_session_1.getSession)().model; if (resource.IsSingletonResource) { let predictSingletonResourcePath; if (resource.IsSingletonResource) { predictSingletonResourcePath = resource.GetOperation.Path.split("/").slice(0, -1).join("/"); } for (const operationGroup of codeModel.operationGroups) { for (const operation of operationGroup.operations) { // for singleton resource, c# will drop the list operation but we need to get it back if (((_a = operation.requests) === null || _a === void 0 ? void 0 : _a.length) && ((_c = (_b = operation.requests[0].protocol) === null || _b === void 0 ? void 0 : _b.http) === null || _c === void 0 ? void 0 : _c.path) === predictSingletonResourcePath && ((_d = operation.requests[0].protocol.http) === null || _d === void 0 ? void 0 : _d.method) === "get") { return operation; } } } } } exports.getSingletonResouceListOperation = getSingletonResouceListOperation; function getResourceExistOperation(resource) { var _a, _b, _c, _d; const codeModel = (0, autorest_session_1.getSession)().model; for (const operationGroup of codeModel.operationGroups) { for (const operation of operationGroup.operations) { if (((_a = operation.requests) === null || _a === void 0 ? void 0 : _a.length) && ((_c = (_b = operation.requests[0].protocol) === null || _b === void 0 ? void 0 : _b.http) === null || _c === void 0 ? void 0 : _c.path) === resource.GetOperation.Path && ((_d = operation.requests[0].protocol.http) === null || _d === void 0 ? void 0 : _d.method) === "head") { return operation; } } } } exports.getResourceExistOperation = getResourceExistOperation; function tagSchemaAsResource(schema, metadata) { var _a; const resourcesMetadata = metadata.Resources; for (const resourceName in resourcesMetadata) { if (resourcesMetadata[resourceName][0].SwaggerModelName === schema.language.default.name) { schema.resourceMetadata = resourcesMetadata[resourceName]; const propertiesModel = (_a = schema.properties) === null || _a === void 0 ? void 0 : _a.find((p) => p.serializedName === "properties"); if (propertiesModel && (0, codemodel_1.isObjectSchema)(propertiesModel.schema)) { propertiesModel.schema.isPropertiesModel = true; } return; } } } exports.tagSchemaAsResource = tagSchemaAsResource; function isResourceSchema(schema) { return Boolean(schema.resourceMetadata); } exports.isResourceSchema = isResourceSchema; const _ArmCoreTypes = [ "ResourceProvisioningState", "OperationListResult", "Origin", "OperationDisplay", "OperationStatusResult", "ErrorDetail", "ErrorAdditionalInfo", "SystemData", "Operation", "ErrorResponse", ]; const _ArmCoreCustomTypes = [ "TrackedResource", "ProxyResource", "ExtensionResource", "ManagedIdentityProperties", "UserAssignedIdentity", "ManagedSystemAssignedIdentity", "ManagedSystemIdentityProperties", "EntityTag", "ResourcePlan", "ResourceSku", ]; function filterArmModels(codeModel, objects) { var _a, _b, _c, _d, _e, _f, _g, _h; const filtered = [..._ArmCoreTypes]; if ((0, autorest_session_1.getArmCommonTypeVersion)()) { filtered.push(..._ArmCoreCustomTypes); } for (const operationGroup of codeModel.operationGroups) { for (const operation of operationGroup.operations) { if ((_c = (_b = (_a = operation.requests) === null || _a === void 0 ? void 0 : _a[0].protocol) === null || _b === void 0 ? void 0 : _b.http) === null || _c === void 0 ? void 0 : _c.path.match(/^\/providers\/[^/]+\/operations$/)) { const okResponse = (_e = (_d = operation.responses) === null || _d === void 0 ? void 0 : _d.filter((o) => { var _a; return (_a = o.protocol.http) === null || _a === void 0 ? void 0 : _a.statusCodes.includes("200"); })) === null || _e === void 0 ? void 0 : _e[0]; const objectName = (_f = okResponse === null || okResponse === void 0 ? void 0 : okResponse.schema) === null || _f === void 0 ? void 0 : _f.language.default.name; if (objectName) { filtered.push(objectName); } } } } filtered.push(...((_h = (_g = codeModel.schemas.objects) === null || _g === void 0 ? void 0 : _g.filter((o) => isResourceSchema(o)).map((o) => o.language.default.name)) !== null && _h !== void 0 ? _h : [])); filtered.push(...(0, type_mapping_1.getSkipList)()); return objects.filter((o) => !filtered.includes(o.name)); } exports.filterArmModels = filterArmModels; const _ArmCoreEnums = [ "CreatedByType", "Origin", "ActionType", "CheckNameAvailabilityRequest", "CheckNameAvailabilityReason", ]; const _ArmCoreCustomEnums = ["ManagedIdentityType", "ManagedSystemIdentityType", "SkuTier"]; function filterArmEnums(enums) { const filtered = [..._ArmCoreEnums]; if ((0, autorest_session_1.getArmCommonTypeVersion)()) { filtered.push(..._ArmCoreCustomEnums); } return enums.filter((e) => !filtered.includes(e.name)); } exports.filterArmEnums = filterArmEnums; //# sourceMappingURL=resource-discovery.js.map