@frui.ts/generator
Advanced tools
Comparing version 0.16.1 to 0.16.2
@@ -70,5 +70,5 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var configPath, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
var defaultConfig, configPath, customConfig; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
@@ -81,15 +81,15 @@ this.project = new ts_morph_1.Project({ | ||
}); | ||
if (!this.params.config) return [3, 2]; | ||
return [4, this.getDefaultConfig()]; | ||
case 1: | ||
defaultConfig = _a.sent(); | ||
if (!this.params.config) return [3, 3]; | ||
configPath = path_1.default.join(process.cwd(), this.params.config); | ||
_a = this; | ||
return [4, Promise.resolve().then(function () { return __importStar(require(configPath)); })]; | ||
case 1: | ||
_a.config = _c.sent(); | ||
case 2: | ||
customConfig = _a.sent(); | ||
this.config = Object.assign({}, defaultConfig, customConfig); | ||
return [3, 4]; | ||
case 2: | ||
_b = this; | ||
return [4, this.getDefaultConfig()]; | ||
case 3: | ||
_b.config = _c.sent(); | ||
_c.label = 4; | ||
this.config = defaultConfig; | ||
_a.label = 4; | ||
case 4: return [2]; | ||
@@ -96,0 +96,0 @@ } |
@@ -5,10 +5,8 @@ { | ||
"entities": { | ||
"entities": { | ||
"EnumValue": false, | ||
"User": { | ||
"exclude": ["code"] | ||
}, | ||
"Partner": { | ||
"include": ["name"] | ||
} | ||
"EnumValue": false, | ||
"User": { | ||
"exclude": ["code"] | ||
}, | ||
"Partner": { | ||
"include": ["name"] | ||
} | ||
@@ -20,3 +18,6 @@ }, | ||
}, | ||
"enums": "enum" | ||
"enums": "enum", | ||
"validations": { | ||
"readOnly": false | ||
} | ||
} |
@@ -17,3 +17,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var aliasEntity_1 = __importDefault(require("../models/aliasEntity")); | ||
var objectEntity_1 = __importDefault(require("../models/objectEntity")); | ||
@@ -56,6 +55,2 @@ var restriction_1 = __importDefault(require("../models/restriction")); | ||
} | ||
var isPropertyArray = property.type.type instanceof aliasEntity_1.default && property.type.type.isArray; | ||
if (property.type.getTypeName() === "number" && !isPropertyArray) { | ||
property.addRestriction(restriction_1.default.number, true); | ||
} | ||
} | ||
@@ -62,0 +57,0 @@ } |
@@ -76,5 +76,6 @@ "use strict"; | ||
var fileGenerator_1 = __importDefault(require("./fileGenerator")); | ||
var modelProcessor_1 = __importDefault(require("./modelProcessor")); | ||
var nameFormatter_1 = __importDefault(require("./formatters/nameFormatter")); | ||
var observableFormatter_1 = __importDefault(require("./formatters/observableFormatter")); | ||
var validationsFormatter_1 = __importDefault(require("./formatters/validationsFormatter")); | ||
var modelProcessor_1 = __importDefault(require("./modelProcessor")); | ||
var OpenApiGenerator = (function (_super) { | ||
@@ -87,3 +88,3 @@ __extends(OpenApiGenerator, _super); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var modelProcessor, types, nameFormatter, observableFormatter, generator; | ||
var modelProcessor, types, nameFormatter, observableFormatter, validationsFormatter, generator; | ||
return __generator(this, function (_a) { | ||
@@ -102,5 +103,7 @@ switch (_a.label) { | ||
observableFormatter = new observableFormatter_1.default(this.config.observable); | ||
validationsFormatter = new validationsFormatter_1.default(); | ||
types.forEach(function (x) { | ||
nameFormatter.formatNames(x); | ||
observableFormatter.format(x); | ||
validationsFormatter.format(x); | ||
}); | ||
@@ -107,0 +110,0 @@ generator = new fileGenerator_1.default(this.project, this.params, this.config); |
declare enum Restriction { | ||
required = 0, | ||
number = 1, | ||
maxLength = 2, | ||
minLength = 3, | ||
pattern = 4, | ||
nullable = 5, | ||
readOnly = 6, | ||
format = 7 | ||
date = 2, | ||
maxLength = 3, | ||
minLength = 4, | ||
pattern = 5, | ||
nullable = 6, | ||
readOnly = 7, | ||
format = 8 | ||
} | ||
export default Restriction; |
@@ -7,10 +7,11 @@ "use strict"; | ||
Restriction[Restriction["number"] = 1] = "number"; | ||
Restriction[Restriction["maxLength"] = 2] = "maxLength"; | ||
Restriction[Restriction["minLength"] = 3] = "minLength"; | ||
Restriction[Restriction["pattern"] = 4] = "pattern"; | ||
Restriction[Restriction["nullable"] = 5] = "nullable"; | ||
Restriction[Restriction["readOnly"] = 6] = "readOnly"; | ||
Restriction[Restriction["format"] = 7] = "format"; | ||
Restriction[Restriction["date"] = 2] = "date"; | ||
Restriction[Restriction["maxLength"] = 3] = "maxLength"; | ||
Restriction[Restriction["minLength"] = 4] = "minLength"; | ||
Restriction[Restriction["pattern"] = 5] = "pattern"; | ||
Restriction[Restriction["nullable"] = 6] = "nullable"; | ||
Restriction[Restriction["readOnly"] = 7] = "readOnly"; | ||
Restriction[Restriction["format"] = 8] = "format"; | ||
})(Restriction || (Restriction = {})); | ||
exports.default = Restriction; | ||
//# sourceMappingURL=restriction.js.map |
@@ -14,2 +14,6 @@ import { BaseParams } from "../generatorBase"; | ||
}; | ||
export declare type ValidationConfig = string | boolean | { | ||
name?: string; | ||
filter?: string; | ||
}; | ||
export interface IConfig { | ||
@@ -20,3 +24,4 @@ api: string; | ||
dates?: "native" | "date-fns"; | ||
validations?: Record<string, ValidationConfig>; | ||
} | ||
export {}; |
@@ -21,2 +21,5 @@ import { CodeBlockWriter, Directory, SourceFile } from "ts-morph"; | ||
writePropertyTypeConversion(writer: CodeBlockWriter, property: EntityProperty): void; | ||
private writeValidationEntity; | ||
private writeValidationProperty; | ||
private getRestrictionDefinition; | ||
} |
@@ -81,3 +81,3 @@ "use strict"; | ||
if (this.params.generateValidation) { | ||
writeValidationEntity(writer, definition, baseClass); | ||
this.writeValidationEntity(writer, definition, baseClass); | ||
} | ||
@@ -92,3 +92,3 @@ }; | ||
var nullable = (_b = property.restrictions) === null || _b === void 0 ? void 0 : _b.get(restriction_1.default.nullable); | ||
var required = nullable === false || (((_c = property.restrictions) === null || _c === void 0 ? void 0 : _c.has(restriction_1.default.required)) && nullable !== true); | ||
var required = nullable !== true && ((_c = property.restrictions) === null || _c === void 0 ? void 0 : _c.has(restriction_1.default.required)); | ||
writer | ||
@@ -215,61 +215,61 @@ .conditionalWrite(readOnly, "readonly ") | ||
}; | ||
return ObjectEntityWriter; | ||
}()); | ||
exports.default = ObjectEntityWriter; | ||
function writeValidationEntity(writer, entity, baseClass) { | ||
if (hasValidation(entity)) { | ||
if (baseClass && hasValidation(baseClass)) { | ||
writer.blankLineIfLastNot().writeLine("static ValidationRules = Object.assign("); | ||
writer.indent(function () { | ||
writer.inlineBlock(function () { return entity.properties.forEach(function (p) { return writeValidationProperty(writer, p); }); }); | ||
writer.write(",").newLine(); | ||
writer.writeLine(baseClass.name + ".ValidationRules"); | ||
}); | ||
writer.writeLine(");"); | ||
ObjectEntityWriter.prototype.writeValidationEntity = function (writer, entity, baseClass) { | ||
var _this = this; | ||
if (hasValidation(entity)) { | ||
if (baseClass && hasValidation(baseClass)) { | ||
writer.blankLineIfLastNot().writeLine("static ValidationRules = Object.assign("); | ||
writer.indent(function () { | ||
writer.inlineBlock(function () { return entity.properties.forEach(function (p) { return _this.writeValidationProperty(writer, p); }); }); | ||
writer.write(",").newLine(); | ||
writer.writeLine(baseClass.name + ".ValidationRules"); | ||
}); | ||
writer.writeLine(");"); | ||
} | ||
else { | ||
writer.blankLineIfLastNot().write("static ValidationRules = {"); | ||
writer.indent(function () { return entity.properties.forEach(function (p) { return _this.writeValidationProperty(writer, p); }); }); | ||
writer.write("};").newLine(); | ||
} | ||
} | ||
else { | ||
writer.blankLineIfLastNot().write("static ValidationRules = {"); | ||
writer.indent(function () { return entity.properties.forEach(function (p) { return writeValidationProperty(writer, p); }); }); | ||
writer.write("};").newLine(); | ||
else if (baseClass && hasValidation(baseClass)) { | ||
writer.blankLineIfLastNot().writeLine("static ValidationRules = " + baseClass.name + ".ValidationRules;"); | ||
} | ||
} | ||
else if (baseClass && hasValidation(baseClass)) { | ||
writer.blankLineIfLastNot().writeLine("static ValidationRules = " + baseClass.name + ".ValidationRules;"); | ||
} | ||
} | ||
function hasValidation(entity) { | ||
return entity.properties.some(function (p) { var _a; return (_a = p.restrictions) === null || _a === void 0 ? void 0 : _a.size; }); | ||
} | ||
function writeValidationProperty(writer, property) { | ||
var _a; | ||
if ((_a = property.restrictions) === null || _a === void 0 ? void 0 : _a.size) { | ||
var definitions_1 = []; | ||
property.restrictions.forEach(function (params, key) { | ||
var definition = getRestrictionDefinition(key, params); | ||
if (definition && !definitions_1.includes(definition)) { | ||
definitions_1.push(definition); | ||
}; | ||
ObjectEntityWriter.prototype.writeValidationProperty = function (writer, property) { | ||
var _this = this; | ||
var _a; | ||
if ((_a = property.restrictions) === null || _a === void 0 ? void 0 : _a.size) { | ||
var definitions_1 = []; | ||
property.restrictions.forEach(function (params, key) { | ||
var definition = _this.getRestrictionDefinition(key, params); | ||
if (definition && !definitions_1.includes(definition)) { | ||
definitions_1.push(definition); | ||
} | ||
}); | ||
if (definitions_1.length) { | ||
writer.write(property.name).write(": { "); | ||
definitions_1.forEach(function (x, i) { return writer.conditionalWrite(i > 0, ", ").write(x); }); | ||
writer.write(" },").newLine(); | ||
} | ||
}); | ||
if (definitions_1.length) { | ||
writer.write(property.name).write(": { "); | ||
definitions_1.forEach(function (x, i) { return writer.conditionalWrite(i > 0, ", ").write(x); }); | ||
writer.write(" },").newLine(); | ||
} | ||
} | ||
} | ||
function getRestrictionDefinition(restriction, params) { | ||
switch (restriction) { | ||
case restriction_1.default.nullable: { | ||
if (params === false) { | ||
return "required: true"; | ||
} | ||
break; | ||
}; | ||
ObjectEntityWriter.prototype.getRestrictionDefinition = function (restriction, params) { | ||
var _a, _b; | ||
var restrictionName = restriction_1.default[restriction]; | ||
var restrictionConfiguration = (_a = this.config.validations) === null || _a === void 0 ? void 0 : _a[restrictionName]; | ||
if (restrictionConfiguration === false) { | ||
return undefined; | ||
} | ||
case restriction_1.default.format: | ||
case restriction_1.default.readOnly: { | ||
var validationParams = JSON.stringify(params); | ||
if (!shouldWriteValidation(restrictionConfiguration, validationParams)) { | ||
return undefined; | ||
} | ||
default: | ||
return restriction_1.default[restriction] + ": " + JSON.stringify(params); | ||
} | ||
var validationName = (_b = getValidationName(restrictionConfiguration)) !== null && _b !== void 0 ? _b : restrictionName; | ||
return validationName + ": " + validationParams; | ||
}; | ||
return ObjectEntityWriter; | ||
}()); | ||
exports.default = ObjectEntityWriter; | ||
function hasValidation(entity) { | ||
return entity.properties.some(function (p) { var _a; return (_a = p.restrictions) === null || _a === void 0 ? void 0 : _a.size; }); | ||
} | ||
@@ -284,2 +284,19 @@ function needsImport(reference) { | ||
} | ||
function getValidationName(config) { | ||
if (typeof config === "string") { | ||
return config; | ||
} | ||
if (typeof config === "object") { | ||
return config.name; | ||
} | ||
return undefined; | ||
} | ||
function shouldWriteValidation(config, value) { | ||
if (typeof config === "object" && config.filter) { | ||
return !!value.match(config.filter); | ||
} | ||
else { | ||
return true; | ||
} | ||
} | ||
//# sourceMappingURL=objectEntityWriter.js.map |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "0.16.1", | ||
"version": "0.16.2", | ||
"description": "Frui.ts code generator", | ||
@@ -56,3 +56,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "2d0dac85798eaefa37064a10fabca474c871c5d5" | ||
"gitHead": "4e9a698e964a13779157b1fe4a2dbb52cd7a64af" | ||
} |
@@ -199,2 +199,3 @@ # Frui.ts code generator | ||
dates?: "native" | "date-fns"; | ||
validations?: Record<string, ValidationConfig>; | ||
} | ||
@@ -214,2 +215,9 @@ | ||
}; | ||
export type ObservableConfig = | ||
| boolean | ||
| { | ||
entities: Record<string, boolean | HasExclude>; | ||
properties?: HasExclude; | ||
}; | ||
``` | ||
@@ -224,10 +232,8 @@ | ||
"entities": { | ||
"entities": { | ||
"EnumValue": false, | ||
"User": { | ||
"exclude": ["code"] | ||
}, | ||
"Partner": { | ||
"include": ["name"] | ||
} | ||
"EnumValue": false, | ||
"User": { | ||
"exclude": ["code"] | ||
}, | ||
"Partner": { | ||
"include": ["name"] | ||
} | ||
@@ -245,2 +251,31 @@ }, | ||
```json | ||
{ | ||
"api": "openapi/swagger.yml", | ||
"observable": { | ||
"entities": { | ||
"EnumValue": false, | ||
"User": { | ||
"exclude": ["code"] | ||
}, | ||
"Partner": { | ||
"include": ["name"] | ||
} | ||
}, | ||
"properties": { | ||
"exclude": ["id", "created"] | ||
} | ||
}, | ||
"enums": "enum", | ||
"dates": "date-fns", | ||
"validations": { | ||
"number": "isNumber", | ||
"readOnly": false, | ||
"nullable": { | ||
"filter": "false" | ||
} | ||
} | ||
} | ||
``` | ||
Generated file | ||
@@ -256,7 +291,9 @@ | ||
@observable | ||
@Type(() => Date) | ||
@Transform(value => (value ? new Date(value) : undefined), { toClassOnly: true }) | ||
@Transform(value => (value ? formatISO(value, { representation: "date" }) : undefined), { toPlainOnly: true }) | ||
createdAt!: Date; | ||
@observable | ||
@Type(() => Date) | ||
@Transform(value => (value ? new Date(value) : undefined), { toClassOnly: true }) | ||
@Transform(value => (value ? formatISO(value, { representation: "date" }) : undefined), { toPlainOnly: true }) | ||
updatedAt!: Date; | ||
@@ -263,0 +300,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
283649
132
3071
308