hemerajs / hemera

🔬 Writing reliable & fault-tolerant microservices in Node.js https://hemerajs.github.io/hemera/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I create plugin with Typescript?

vforv opened this issue · comments

Here is my try:

const hp = require('hemera-plugin');

export class MongoExtend {
    public register: any = {
    };

    constructor() {

        this.register.attributes = {
            name: 'mongo-store-ext',
            description: 'Extend default mongo-store service',
            version: '1.0.0'
        };

        this.register.plugin = hp((options: any, next: any) => {
            console.log(this);
            next()
        });

        this.register.options = {}
    }


}

const mongoex = new MongoExtend;

hemera.use(mongoex.register);

But it doesn't work

It registers ok, but I don't get hemera instance inside plugin... Anyone know what is a problem?

As documented https://hemerajs.github.io/hemera/1_plugins.html A hemera plugin is a simple function you don't need a class.

exports.plugin = function myPlugin (options, next) {
  const hemera = this
  const topic = "math"

  hemera.add({
    topic,
    cmd: "add"
  }, function (req, cb) {
    cb(null, req.a + req.b)
  })

  next()
}

exports.options = {
  payloadValidator: 'hemera-joi'
}

exports.attributes = {
  pkg: require('./package.json')
}

That's not Typescript style... But I figured out...

This part (options: any, next: any) => should be like this: function(options: any, next: any) {

There isn't a typescript style. Typescript is a superset of Javascript. You can just use it.