swagger-typescript-generator
Version:
Generate typescript code from swagger.json. Before start this project I try use [swagger-typescript-codegen](https://github.com/mtennoe/swagger-typescript-codegen) based on [mustache templates](https://github.com/mtennoe/swagger-js-codegen/tree/master/lib
113 lines • 5.07 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerMethod = void 0;
var swagger_base_1 = require("./swagger-base");
var swagger_method_parameter_1 = require("./swagger-method-parameter");
var model_type_1 = require("./model-type");
var SwaggerMethod = /** @class */ (function (_super) {
__extends(SwaggerMethod, _super);
function SwaggerMethod(parent, httpMethod, path, source) {
var _a, _b, _c;
var _this = _super.call(this) || this;
_this.httpMethod = "";
_this.name = "";
_this.tags = "";
_this.url = "";
_this.parameters = [];
_this.parent = parent;
_this.source = source;
_this.url = path.url;
_this.httpMethod = httpMethod;
_this.tags = ((_a = _this.source.tags) === null || _a === void 0 ? void 0 : _a.lenght) ? _this.source.tags[0] : "";
_this.name = _this.utils.getMethodName(_this, _this.source.operationId);
if (source.parameters) {
_this.parameters = source.parameters.map(function (obj) {
return new swagger_method_parameter_1.SwaggerMethodParameter(_this, obj);
});
}
if (source.requestBody && ((_b = source.requestBody) === null || _b === void 0 ? void 0 : _b.content)) {
var parameter = new swagger_method_parameter_1.SwaggerMethodParameter(_this, (_c = source.requestBody) === null || _c === void 0 ? void 0 : _c.content["application/json"]);
parameter.in = swagger_method_parameter_1.EParameterIn.body;
parameter.name = "body";
_this.parameters.push(parameter);
}
_this.responseIsVoid = true;
_this.responseModelType = new model_type_1.ModelType(_this.config);
if (source.responses &&
source.responses["200"] &&
source.responses["200"].content) {
var schema = source.responses["200"].schema ||
source.responses["200"].content["application/json"];
if (schema) {
_this.responseIsVoid = false;
_this.responseModelType.isArray = _this.utils.isArray(schema);
_this.responseModelType.arrayItemType =
_this.utils.getArrayItemType(schema);
_this.responseModelType.type = _this.utils.getMethodResponseType(_this, schema);
}
}
if (_this.responseModelType.type) {
_this.responseModelType.isJsType = _this.utils.isJsType(_this.responseModelType.type);
}
_this.responseModelType.isEnum = _this.utils.isEnum(source)
? true
: undefined;
if (_this.responseModelType.isEnum) {
_this.responseModelType.enumValues = _this.utils.getEnumValues(source);
}
_this.isFileUpload = _this.parameters.some(function (s) { return s.modelType.type === "File"; });
if (source.summary) {
_this.description = source.summary;
}
return _this;
}
SwaggerMethod.prototype.init = function () {
var _this = this;
this.parameters.forEach(function (p) { return p.init(); });
if (!this.responseIsVoid) {
if (!this.responseModelType.isJsType && !this.responseModelType.isEnum) {
var modelRef = this.doc.models.find(function (modelItem) { return modelItem.name === _this.responseModelType.type; });
if (modelRef) {
this.responseModelType.modelRef = modelRef;
}
else {
console.error("Model not found response method", this.name);
}
}
if (this.responseModelType.isEnum) {
var enumRef = this.doc.enums.find(function (enumItem) {
return enumItem.keys.includes(_this.name);
});
if (enumRef) {
this.responseModelType.enumRef = enumRef;
}
else {
console.error("Model for enum not found (response method)" + this.name);
}
}
}
};
SwaggerMethod.prototype.getUrlInfo = function () {
return {
httpMethod: this.httpMethod,
name: this.name,
url: this.url,
};
};
return SwaggerMethod;
}(swagger_base_1.SwaggerBase));
exports.SwaggerMethod = SwaggerMethod;
//# sourceMappingURL=swagger-method.js.map