@muryllo/easy-decorators
TypeScript icon, indicating that this package has built-in type declarations

1.1.9 • Public • Published

Easy Decorators

A simple library to create your own Typescript decorators and your mini-frameworks.

Getting started into Easy Decorators!

To use this library and create your decorators, you need to import the library into your code and use a decorator template to build your own decorator. In Easy Decorators there are 4 types of decorator templates:

  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

Installation

You must run the following terminal command.

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

Dependencies (1)

Dev Dependencies (8)

Package Sidebar

Install

npm i @muryllo/easy-decorators

Weekly Downloads

1

Version

1.1.9

License

MIT

Unpacked Size

42.2 kB

Total Files

20

Last publish

Collaborators

  • muryllo.pimenta