@allgemein/schema-api
Version:
Library for schema api
57 lines • 2.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Property = Property;
require("reflect-metadata");
const lodash_1 = require("lodash");
const base_1 = require("@allgemein/base");
const Constants_1 = require("../lib/Constants");
const MetadataRegistry_1 = require("../lib/registry/MetadataRegistry");
function Property(typeOrOptions = null) {
return function (source, propertyName) {
let options = {
type: undefined
};
if ((0, lodash_1.isString)(typeOrOptions)) {
options.type = typeOrOptions;
}
else if ((0, lodash_1.isFunction)(typeOrOptions)) {
const name = base_1.ClassUtils.getClassName(typeOrOptions);
if ([Function.name, '', 'type'].includes(name)) {
options.type = typeOrOptions();
}
else {
options.type = typeOrOptions;
}
}
else if (typeOrOptions && (0, lodash_1.isObjectLike)(typeOrOptions)) {
(0, lodash_1.assign)(options, typeOrOptions);
if (options.type && (0, lodash_1.isFunction)(options.type)) {
const name = base_1.ClassUtils.getClassName(options.type);
if ([Function.name, '', 'type'].includes(name)) {
options.type = options.type();
}
}
}
options.target = source.constructor;
options.propertyName = propertyName;
const reflectMetadataType = Reflect && Reflect.getMetadata ? Reflect.getMetadata(Constants_1.REFLECT_DESIGN_TYPE, source, propertyName) : undefined;
if (reflectMetadataType) {
const className = base_1.ClassUtils.getClassName(reflectMetadataType);
if (Constants_1.JS_PRIMATIVE_TYPES.includes(className.toLowerCase())) {
options.type = options.type ? options.type : className.toLowerCase();
}
else if (className === Array.name) {
options.type = options.type ? options.type : Constants_1.T_OBJECT;
options.cardinality = 0;
}
else {
options.type = options.type ? options.type : reflectMetadataType;
}
}
if (!options.type) {
options.type = Constants_1.T_STRING;
}
MetadataRegistry_1.MetadataRegistry.$().add(Constants_1.METATYPE_PROPERTY, options);
};
}
//# sourceMappingURL=Property.js.map