openapi-ts-generator
Version:
Based on swagger-ts-generator, this is a type script model generator specifically for services with OpenApi spec documentation.
69 lines (68 loc) • 3.81 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
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.FormGroupFactoryGenerator = void 0;
var _ = require("lodash");
var base_generator_1 = require("./base.generator");
var HandleBars = require("handlebars");
var FormGroupFactoryGenerator = /** @class */ (function (_super) {
__extends(FormGroupFactoryGenerator, _super);
function FormGroupFactoryGenerator(options) {
var _this = this;
var _a;
_this = _super.call(this, options, (_a = options.templates) === null || _a === void 0 ? void 0 : _a.formGroupFactory) || this;
_this.GeneratorName = 'FormGroupFactoryGenerator';
return _this;
}
FormGroupFactoryGenerator.prototype.generate = function (templateData) {
var _this = this;
var _a;
this.registerHelpers();
(_a = templateData.entities) === null || _a === void 0 ? void 0 : _a.filter(function (val) { var _a, _b; return ((_a = val.valueProperties) === null || _a === void 0 ? void 0 : _a.length) + ((_b = val.referenceProperties) === null || _b === void 0 ? void 0 : _b.length) > 0; }).forEach(function (entity) {
_super.prototype.generateFile.call(_this, "".concat(_this.generatorOptions.outputPath, "/").concat(_.kebabCase(entity.name), ".form-group-fac.ts"), entity);
});
};
FormGroupFactoryGenerator.prototype.registerHelpers = function () {
this.registerValidatorHelper('minimum', 'min');
this.registerValidatorHelper('maximum', 'max');
this.registerValidatorHelper('minLength');
this.registerValidatorHelper('maxLength');
this.registerValidatorHelper('maxItems', 'maxLength');
this.registerValidatorHelper('minItems', 'minLength');
this.registerValidatorHelper('pattern');
};
FormGroupFactoryGenerator.prototype.registerValidatorHelper = function (validatorName, angularValidatorName) {
var _this = this;
if (angularValidatorName === void 0) { angularValidatorName = validatorName; }
HandleBars.registerHelper("".concat(validatorName, "Validator"), function (x, y) {
return _this.validatorFactory(x, y, validatorName, angularValidatorName);
});
};
FormGroupFactoryGenerator.prototype.validatorFactory = function (propertyCollection, propertyContext, validationName, angularValidatorFunctionName) {
var props = propertyContext.data.root[propertyCollection];
var prop = props[propertyContext.data.index];
var value = prop[validationName];
if (value !== undefined && value !== null) {
var hasMultipleValidators = prop.hasMultipleValidators;
value = typeof value === 'string' ? "'".concat(value, "'") : value;
return "Validators.".concat(angularValidatorFunctionName, "(").concat(value, ")").concat(hasMultipleValidators ? ', ' : '');
}
return '';
};
return FormGroupFactoryGenerator;
}(base_generator_1.BaseGenerator));
exports.FormGroupFactoryGenerator = FormGroupFactoryGenerator;