decorator-metadata
v1.0.2
Published
Use ES7 decorators to add and retrieve arbitrary metadata about classes, methods and properties.
Downloads
36
Maintainers
Readme
Usage
Define your metadata functions like this:
import Metadata from 'decorator-metadata';
function myClassDecoration(metadataToSave) {
return Metadata.DecorateClassWithParams(myDecoration, metadataToSave);
}
function myClassDecorationBool() {
return Metadata.DecorateClass(myDecoration, ...arguments);
}
function myDecoration(metadataToSave) {
return Metadata.DecorateWithParams(myDecoration, metadataToSave);
}
function myDecorationBool() {
return Metadata.Decorate(myDecoration, ...arguments);
}
Now you can decorate as follows:
@myClassDecoration('this string will be stored along with the class')
@myClassDecorationBool // only the existence of this decorator will be saved
class ClassWithMetadata {
@myDecoration('this string will be stored along with the method')
someMethod() {
...
}
@myDecorationBool
otherMethod() {
...
}
}
Retrieve the metadata:
let classesDecoratedWithMyClassDecoration = Metadata.GetAllClassesDecoratedWith(myClassDecoration);
let classesDecoratedWithMyClassDecorationBool = Metadata.GetAllClassesDecoratedWith(myClassDecorationBool);
Get specific instances of decoration and passed in parameters:
for (let decoratedClass of classesDecoratedWithMyClassDecoration) {
let classMetadata = Metadata.Get(decoratedClass);
let decorationInstances = classMetadata.getDecorations(myClassDecoration);
let decorationInstance = decorationInstances[0];
let argumentsPassed = classMetadata.getDecorationArguments(decoratedClass, decorationInstance);
console.log(argumentsPassed);
// prints:
// [0] => 'this string will be stored along with the class'
}