1.1.9 • Published 3 years ago

@muryllo/easy-decorators v1.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Getting started into Easy Decorators!

  1. Class Decorators;
  2. Property Decorators;
  3. Method Decorators;
  4. 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

  1. Create a fork (https://github.com/MurylloEx/easy-decorators/fork)
  2. Create a feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Send a push of your commit (git push origin feature/fooBar)
  5. Open a new Pull Request
1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago