import "reflect-metadata" ; import * as t from "io-ts" ; import * as E from "fp-ts/lib/Either" ; export interface RefType < T > extends Function { new ( ... args ) : T ; } export function decode < I , T extends t . Type < I > > ( t , json ) { const validResult = t . decode ( json ) ; if ( E . isLeft ( validResult ) ) { throw 1 ; } return < I > validResult . right ; } function FhirGuard4MethodDecorator ( target , propertyKey , descriptor ) { const returnType = new target [ propertyKey ] ( ) ; const originalFunction = descriptor . value ; descriptor . value = function ( ... args ) { const result = originalFunction . apply ( this , args ) ; return decode ( returnType , result ) ; } ; } function FhirGuard4PropertyDecorator < T > ( target , key ) { var _val = this [ key ] ; var getter = function ( ) { return _val ; } ; var setter = function ( newVal ) { console . log ( `Set: ${ key } => ${ newVal }` ) ; _val = newVal ; } ; } export function FHIRGuard < T > ( ... args ) { switch ( args . length ) { case 2 : return FhirGuard4PropertyDecorator . apply ( this , args ) ; case 3 : return FhirGuard4MethodDecorator . apply ( this , args ) ; default : throw new Error ( "Decorators are not valid here!" ) ; } }