@autorest/openapi-to-cadl
Version:
Autorest plugin to scaffold a Typespec definition from an OpenAPI document
50 lines • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateObject = void 0;
const decorators_1 = require("../utils/decorators");
const docs_1 = require("../utils/docs");
function generateObject(cadlObject) {
var _a, _b;
const definitions = [];
const fixme = getFixme(cadlObject);
fixme && definitions.push(fixme);
const doc = (0, docs_1.generateDocs)(cadlObject);
definitions.push(doc);
const decorators = (0, decorators_1.generateDecorators)(cadlObject.decorators);
decorators && definitions.push(decorators);
if ((_a = cadlObject.extendedParents) === null || _a === void 0 ? void 0 : _a.length) {
const firstParent = cadlObject.extendedParents[0];
definitions.push(`model ${cadlObject.name} extends ${firstParent} {`);
}
else if (cadlObject.alias) {
const { alias, params } = cadlObject.alias;
definitions.push(`model ${cadlObject.name} is ${alias}${params ? `<${params.join(",")}>` : ""} {`);
}
else {
definitions.push(`model ${cadlObject.name} {`);
}
for (const parent of (_b = cadlObject.spreadParents) !== null && _b !== void 0 ? _b : []) {
definitions.push(`...${parent};`);
}
for (const property of cadlObject.properties) {
const propertyDoc = (0, docs_1.generateDocs)(property);
propertyDoc && definitions.push(propertyDoc);
const decorators = (0, decorators_1.generateDecorators)(property.decorators);
decorators && definitions.push(decorators);
property.fixMe && property.fixMe.length && definitions.push(property.fixMe.join("\n"));
definitions.push(`"${property.name}"${getOptionalOperator(property)}: ${property.type};`);
}
definitions.push("}");
return definitions.join("\n");
}
exports.generateObject = generateObject;
function getFixme(cadlObject) {
if (!cadlObject.fixMe) {
return undefined;
}
return cadlObject.fixMe.join("\n");
}
function getOptionalOperator(property) {
return property.isOptional ? "?" : "";
}
//# sourceMappingURL=generate-object.js.map