UNPKG

@autorest/openapi-to-typespec

Version:

Autorest plugin to scaffold a Typespec definition from an OpenAPI document

66 lines 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getPagingItemType = exports.isTrackedResource = exports.isResource = void 0; const codemodel_1 = require("@autorest/codemodel"); const schemas_1 = require("../utils/schemas"); // Common-type v2 resource doesn't have systemData function isResource(schema) { let idPropertyFound = false; let typePropertyFound = false; let namePropertyFound = false; for (const property of (0, codemodel_1.getAllProperties)(schema)) { if (property.flattenedNames) continue; if (property.serializedName === "id" && ((0, schemas_1.isStringSchema)(property.schema) || (0, schemas_1.isArmIdSchema)(property.schema))) { idPropertyFound = true; } else if (property.serializedName === "type" && (0, schemas_1.isStringSchema)(property.schema)) { typePropertyFound = true; } else if (property.serializedName === "name" && (0, schemas_1.isStringSchema)(property.schema)) { namePropertyFound = true; } } return idPropertyFound && typePropertyFound && namePropertyFound; } exports.isResource = isResource; function isTrackedResource(schema) { if (!isResource(schema)) return false; let isLocationFound = false; let isTagsFound = false; for (const property of (0, codemodel_1.getAllProperties)(schema)) { if (property.flattenedNames) continue; if (property.serializedName === "location" && (0, schemas_1.isStringSchema)(property.schema)) { isLocationFound = true; } else if (property.serializedName === "tags" && (0, schemas_1.isDictionarySchema)(property.schema)) { isTagsFound = true; } } return isLocationFound && isTagsFound; } exports.isTrackedResource = isTrackedResource; function getPagingItemType(operation) { var _a, _b, _c, _d, _e, _f; const response = (_a = operation.responses) === null || _a === void 0 ? void 0 : _a.find((r) => (0, schemas_1.isResponseSchema)(r)); if (response === undefined) return undefined; let itemName = "value"; if ((_c = (_b = operation.extensions) === null || _b === void 0 ? void 0 : _b["x-ms-pageable"]) === null || _c === void 0 ? void 0 : _c.itemName) { itemName = (_e = (_d = operation.extensions) === null || _d === void 0 ? void 0 : _d["x-ms-pageable"]) === null || _e === void 0 ? void 0 : _e.itemName; } const schemaResponse = response; if ((0, schemas_1.isArraySchema)(schemaResponse.schema)) return schemaResponse.schema.elementType.language.default.name; if ((0, codemodel_1.isObjectSchema)(schemaResponse.schema)) { const responseSchema = (_f = schemaResponse.schema.properties) === null || _f === void 0 ? void 0 : _f.find((p) => p.serializedName === itemName && (0, schemas_1.isArraySchema)(p.schema)); if (!responseSchema) return undefined; return responseSchema.schema.elementType.language.default.name; } return undefined; } exports.getPagingItemType = getPagingItemType; //# sourceMappingURL=resource-equivalent.js.map