1.1.9 • Published 3 years ago
@muryllo/easy-decorators v1.1.9
Getting started into Easy Decorators!
- Class Decorators;
- Property Decorators;
- Method Decorators;
- Parameter Decorators
Each decorator adds metadata that is stored in the instance of the class that can be accessed from the getEasyMetadata function.
Installation
npm install @muryllo/easy-decorators --save
How to use?
Take a look at the example below and see the practical application of the 4 types of decorators. Console log output shows the metadata attached to the instance of the decorated class.
import {
EasyClassDecorator
} from '@muryllo/easy-decorators';
const MyDecorator = () => EasyClassDecorator('my:key', { /* My metadata */ });
@MyDecorator()
class Sample {
}
The example below shows how to use the 4 types of decorators.
import {
EasyClassDecorator,
EasyMethodDecorator,
EasyParameterDecorator,
EasyPropertyDecorator,
getEasyMetadata
} from '@muryllo/easy-decorators';
//Creating the decorators using a base decorator.
const WebSockController = (addr: string) => EasyClassDecorator('ws:address', addr);
const WebSockClient = () => EasyPropertyDecorator('ws:client', {});
const WebSockEvent = (event: string) => EasyMethodDecorator('ws:event', event);
const WebSockData = () => EasyParameterDecorator('ws:parameter', {});
@WebSockController('ws://localhost:3000')
class WebSock {
@WebSockClient()
public client: any;
@WebSockEvent('ping')
onMessage(@WebSockData() data: any){
this.client.send('pong', {
pong: data
});
}
}
const sock = new WebSock();
let metadata = getEasyMetadata(sock);
console.log(metadata);
The code above prints the following result:
{
'ws:client': [ { key: 'client', value: {}, type: 'property' } ],
'ws:parameter': [ { key: 'onMessage', value: {}, index: 0, type: 'method' } ],
'ws:event': [ { key: 'onMessage', value: 'ping', type: 'method' } ],
'ws:address': [ { key: null, value: 'ws://localhost:3000', type: 'class' } ]
}
Metadata
Muryllo Pimenta de Oliveira – muryllo.pimenta@upe.br
Distributed under MIT license. See LICENSE
for more informations.
Contributing
- Create a fork (https://github.com/MurylloEx/easy-decorators/fork)
- Create a feature branch (
git checkout -b feature/fooBar
) - Commit your changes (
git commit -am 'Add some fooBar'
) - Send a push of your commit (
git push origin feature/fooBar
) - Open a new Pull Request