Created
September 21, 2016 06:13
-
-
Save CarlosLanderas/1da4463406abe711fec45b3149e1a681 to your computer and use it in GitHub Desktop.
Typescript metadata / decorators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Main from "./main"; | |
import {Program} from "./program"; | |
Main.bootstrap(Program); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "reflect-metadata"; | |
export function classMetadata(metadata: IMetadata){ | |
return function (target: Function) { | |
Reflect.defineMetadata("metadata", metadata, target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function log(target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) { | |
let originalMethod = descriptor.value; | |
descriptor.value = function(...args: any[]) { | |
console.log("The method args are: " + JSON.stringify(args)); | |
let result = originalMethod.apply(this, args); | |
console.log("The return value is: " + result); | |
return result; | |
}; | |
return descriptor; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "reflect-metadata"; | |
export default class Main { | |
static bootstrap(program:any) { | |
var programMetadata = Main.GetMetadata(program); | |
//Print found metadata in class | |
console.log(programMetadata); | |
//Log with be printed when calling getData | |
new program().getData(5); | |
//output | |
//[ { name: 'field1', value: 'value1' }, | |
// { name: 'field2', value: 'value2' } ] | |
// The method args are: [5] | |
//The return value is: Data1,Data2,Data3 | |
} | |
private static GetMetadata(obj:any): Array<IMetadata> { | |
var componentInstance = new obj(); | |
return Reflect.getMetadata("metadata", componentInstance.constructor).Metadata; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {classMetadata} from "./decorators/class-metadata-decorator"; | |
import {HttpService} from "./services/http-service"; | |
import {log} from "./decorators/log-decorator"; | |
@classMetadata({ | |
Metadata: [ | |
{name:"field1", value:"value1"}, | |
{name:"field2", value:"value2"} | |
] | |
}) | |
export class Program { | |
constructor(private httpService:HttpService) { | |
this.httpService = new HttpService(); | |
} | |
@log | |
getData(value:number){ | |
return this.httpService.getData(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment