UNPKG

@autorest/openapi-to-typespec

Version:

Autorest plugin to scaffold a Typespec definition from an OpenAPI document

100 lines 4.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isResource = isResource; exports.isTrackedResource = isTrackedResource; exports.getPagingItemType = getPagingItemType; 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; } function isTrackedResource(schema) { if (!isResource(schema)) return [false, false]; let isLocationFound = false; let isOptionalLocation = 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; isOptionalLocation = property.required !== true; } else if (property.serializedName === "tags" && (0, schemas_1.isDictionarySchema)(property.schema)) { isTagsFound = true; } } return [isLocationFound && isTagsFound, isLocationFound && isTagsFound && isOptionalLocation]; } function getPagingItemType(operation, markPaging = false) { 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; markPaging && markPagination(schemaResponse.schema, operation); return responseSchema.schema.elementType.language.default.name; } return undefined; } function markPagination(schema, operation) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; schema.language.default.paging = { ...schema.language.default.paging, isPageable: true, }; if (!(0, codemodel_1.isObjectSchema)(schema)) return; let itemName = "value"; if ((_b = (_a = operation.extensions) === null || _a === void 0 ? void 0 : _a["x-ms-pageable"]) === null || _b === void 0 ? void 0 : _b.itemName) { itemName = (_d = (_c = operation.extensions) === null || _c === void 0 ? void 0 : _c["x-ms-pageable"]) === null || _d === void 0 ? void 0 : _d.itemName; } let nextLinkName = null; if ((_f = (_e = operation.extensions) === null || _e === void 0 ? void 0 : _e["x-ms-pageable"]) === null || _f === void 0 ? void 0 : _f.nextLinkName) { nextLinkName = (_h = (_g = operation.extensions) === null || _g === void 0 ? void 0 : _g["x-ms-pageable"]) === null || _h === void 0 ? void 0 : _h.nextLinkName; } for (const property of (_j = schema.properties) !== null && _j !== void 0 ? _j : []) { if (property.serializedName === nextLinkName) { property.language.default.paging = { ...property.language.default.paging, isNextLink: true, }; } if (property.serializedName === itemName) { property.language.default.paging = { ...property.language.default.paging, isValue: true, }; } } } //# sourceMappingURL=resource-equivalent.js.map