UNPKG

@protocolnebula/ts-openapi-generator

Version:

Build API and models from Swagger/OpenAPI to use in any project type

71 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModelModel = void 0; const case_1 = require("case"); const models_util_1 = require("../utils/models.util"); const regexKebabDTO = /[d]-?[t]-?[o]$/gi; class ModelModel { constructor(name) { this._attributes = []; this.name = name; } get fileName() { let fileName = (0, case_1.kebab)(this.name); fileName = fileName.replace(regexKebabDTO, 'dto'); return fileName; } get uri() { return `#/components/schemas/${this.name}`; } get attributes() { return this._attributes; } get hasComments() { return !!this.description || !!this.example || !!this.deprecated; } getDependences(store) { const dependences = new Set(); for (const attribute of this.attributes) { const type = attribute.typeURI; if (type.includes('#')) { const dependence = store.getByUri(type); if (dependence) { dependences.add(dependence); } else { console.error('ERROR: Dependence', type, 'not found!'); } } } return (0, models_util_1.sortDependences)([...dependences]); } addAttributes(attributes) { if ((attributes === null || attributes === void 0 ? void 0 : attributes.length) > 0) { attributes.forEach((attribute) => { this.addAttribute(attribute); }); } } addAttribute(attribute) { attribute.model = this; this._attributes.push(attribute); } toString() { return this.name; } /** * Sort the attributesthe attributes */ sort() { this._attributes = this.attributes.sort((a, b) => { if (a.name > b.name) return 1; if (a.name < b.name) return -1; return 0; }); return this; } } exports.ModelModel = ModelModel; //# sourceMappingURL=model.model.js.map