How can I create plugin with Typescript?
vforv opened this issue · comments
Vladimir Djukic commented
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?
Dustin Deus commented
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')
}
Vladimir Djukic commented
That's not Typescript style... But I figured out...
This part (options: any, next: any) =>
should be like this: function(options: any, next: any) {
Dustin Deus commented
There isn't a typescript style. Typescript is a superset of Javascript. You can just use it.