@protocolnebula/ts-openapi-generator
Version:
Build API and models from Swagger/OpenAPI to use in any project type
111 lines • 3.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiModel = void 0;
const case_1 = require("case");
const models_util_1 = require("../utils/models.util");
class ApiModel {
constructor(url, verb) {
this.tags = [];
this.url = url;
this.verb = verb;
}
get name() {
return (0, case_1.capital)(`${this.groupName} ${this.operationId}`, '', true);
}
get groupName() {
var _a;
return ((_a = this.tags[0]) === null || _a === void 0 ? void 0 : _a.length) ? (0, case_1.capital)(this.tags[0], '', true) : null;
}
get isFormRequest() {
return this.requestContentType === 'multipart/form-data';
}
get isResponsePrimitive() {
var _a;
return !this.response || ((_a = this.response) === null || _a === void 0 ? void 0 : _a.typeIsPrimitive);
}
get isResponseArray() {
var _a;
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.isArray;
}
get isResponseTypeText() {
var _a;
return ((_a = this.responseContentType) === null || _a === void 0 ? void 0 : _a.indexOf('text/')) === 0;
}
get isResponseTypeJson() {
return this.responseContentType === 'application/json';
}
get isResponseTypeXML() {
return this.responseContentType === 'application/xml';
}
get isResponseTypeFile() {
return (!this.isResponseTypeText &&
!this.isResponseTypeJson &&
!this.isResponseTypeXML);
}
get responseArrayLevelsRepeater() {
var _a;
return (_a = this.response) === null || _a === void 0 ? void 0 : _a.arrayLevelsRepeater;
}
get models() {
return [this.queryParams, this.requestBody, this.response].filter((el) => !!el);
}
get fileName() {
return this.groupName;
}
get operationId() {
if (!this._operationId) {
// Avoid multiple generations (to increase performance)
this._operationId = this.generateOperationId();
}
return this._operationId;
}
set operationId(operation) {
this._operationId = (0, case_1.camel)(operation);
}
get hasComments() {
return !!this.description || !!this.example || !!this.deprecated;
}
get queryParamsType() {
var _a;
return (_a = this.queryParams) === null || _a === void 0 ? void 0 : _a.type;
}
get requestBodyType() {
var _a;
return (_a = this.requestBody) === null || _a === void 0 ? void 0 : _a.type;
}
get responseType() {
var _a;
return ((_a = this.response) === null || _a === void 0 ? void 0 : _a.type) || (0, models_util_1.getFixedTypeName)('empty');
}
get verb() {
return this._verb;
}
set verb(verb) {
this._verb = (0, case_1.capital)(verb, '', true);
}
getDependences(modelStore) {
const dependences = new Set();
for (const model of this.models) {
const type = model.typeURI;
if (type.includes('#')) {
const dependence = modelStore.getByUri(type);
if (dependence) {
dependences.add(dependence);
}
else {
console.error('ERROR: Dependence', type, 'not found!');
}
}
}
return [...dependences];
}
toString() {
return this.operationId;
}
generateOperationId() {
const name = this.url.substring(this.url.lastIndexOf('/') + 1);
return `${this.verb.toLowerCase()}${name}`;
}
}
exports.ApiModel = ApiModel;
//# sourceMappingURL=api.model.js.map