@dipscope/type-manager
Version:
Transform JSON strings or plain objects into JS class instances.
62 lines • 3.26 kB
JavaScript
import { nameOf } from './functions/name-of';
import { TypeManager } from './type-manager';
export function Property(x, y, z) {
var propertyOptions = {};
if (typeof z === 'object') {
Object.assign(propertyOptions, z);
}
if (typeof y === 'object' && !Array.isArray(y)) {
Object.assign(propertyOptions, y);
}
if (typeof x === 'object' && !Array.isArray(x)) {
Object.assign(propertyOptions, x);
}
if (Array.isArray(y)) {
propertyOptions.genericArguments = y;
}
if (Array.isArray(x)) {
propertyOptions.genericArguments = x;
}
if (typeof x === 'string' || typeof x === 'function') {
propertyOptions.typeArgument = x;
}
return function (target, context) {
if (context !== null && typeof context === 'object' && context.hasOwnProperty('kind')) {
var decoratorContext = context;
var kind = decoratorContext.kind;
var propertyName = decoratorContext.name;
if (kind === 'method' || kind === 'class') {
throw new Error("".concat(String(propertyName), ": property decorator cannot be applied to a method or a class."));
}
if (propertyName === undefined) {
throw new Error("".concat(String(propertyName), ": property decorator cannot be applied to undefined values."));
}
if (typeof propertyName === 'symbol') {
throw new Error("".concat(String(propertyName), ": property decorator cannot be applied to a symbol."));
}
TypeManager.typeScope.addPropertyOptions(propertyName, propertyOptions);
return;
}
if (target !== null && typeof target === 'object' && (typeof context === 'string' || typeof context === 'symbol')) {
var legacyTarget = target;
var propertyName = context;
if (typeof legacyTarget === 'function' && legacyTarget.name !== '') {
throw new Error("".concat(nameOf(legacyTarget), ".").concat(String(propertyName), ": property decorator cannot be applied to a static member."));
}
if (propertyName === undefined) {
throw new Error("".concat(nameOf(legacyTarget), ".").concat(String(propertyName), ": property decorator cannot be applied to undefined values."));
}
if (typeof propertyName === 'symbol') {
throw new Error("".concat(nameOf(legacyTarget.constructor), ".").concat(String(propertyName), ": property decorator cannot be applied to a symbol."));
}
if (typeof legacyTarget[propertyName] === 'function') {
throw new Error("".concat(nameOf(legacyTarget.constructor), ".").concat(String(propertyName), ": property decorator cannot be applied to a method."));
}
var typeFn = legacyTarget.constructor;
TypeManager.configureTypeMetadata(typeFn).configurePropertyMetadata(propertyName, propertyOptions);
return;
}
throw new Error("Property decorator was not able to detect correct resolver for the following target [".concat(target, "] and context [").concat(context, "]."));
};
}
//# sourceMappingURL=property.js.map