UNPKG

@dipscope/type-manager

Version:

Transform JSON strings or plain objects into JS class instances.

62 lines 3.15 kB
import { nameOf } from './functions/name-of'; import { TypeManager } from './type-manager'; export function Property(x, y, z) { const 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')) { const decoratorContext = context; const kind = decoratorContext.kind; const propertyName = decoratorContext.name; if (kind === 'method' || kind === 'class') { throw new Error(`${String(propertyName)}: property decorator cannot be applied to a method or a class.`); } if (propertyName === undefined) { throw new Error(`${String(propertyName)}: property decorator cannot be applied to undefined values.`); } if (typeof propertyName === 'symbol') { throw new Error(`${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')) { const legacyTarget = target; const propertyName = context; if (typeof legacyTarget === 'function' && legacyTarget.name !== '') { throw new Error(`${nameOf(legacyTarget)}.${String(propertyName)}: property decorator cannot be applied to a static member.`); } if (propertyName === undefined) { throw new Error(`${nameOf(legacyTarget)}.${String(propertyName)}: property decorator cannot be applied to undefined values.`); } if (typeof propertyName === 'symbol') { throw new Error(`${nameOf(legacyTarget.constructor)}.${String(propertyName)}: property decorator cannot be applied to a symbol.`); } if (typeof legacyTarget[propertyName] === 'function') { throw new Error(`${nameOf(legacyTarget.constructor)}.${String(propertyName)}: property decorator cannot be applied to a method.`); } const 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 [${target}] and context [${context}].`); }; } //# sourceMappingURL=property.js.map