Skip to content

Commit f097959

Browse files
committed
support to register a hemera plugin with custom options
1 parent 0d33c2c commit f097959

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ async function register(server, options) {
2828
)
2929

3030
if (options.plugins) {
31-
options.plugins.forEach(plugin => hemera.use(plugin))
31+
options.plugins.forEach(plugin => {
32+
if (plugin.options) {
33+
hemera.use(plugin.register, plugin.options)
34+
} else {
35+
hemera.use(plugin)
36+
}
37+
})
3238
}
3339

3440
const hemeraInterface = request => {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"eslint": "^4.19.0",
3131
"eslint-config-hemera": "^0.0.2",
3232
"hapi": "^17.2.3",
33+
"hemera-plugin": "^1.1.1",
3334
"hemera-testsuite": "^4.0.0",
3435
"istanbul": "^0.4.5",
3536
"joi": "^13.1.2",

test/hemera-plugin.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Hapi = require('hapi')
44
const HemeraTestsuite = require('hemera-testsuite')
55
const hemeraInternalSymbols = require('nats-hemera/lib/symbols')
66
const Code = require('code')
7+
const Hp = require('hemera-plugin')
78
const HapiHemera = require('../lib')
89

910
const expect = Code.expect
@@ -26,11 +27,10 @@ describe('Hemera plugin registration', function() {
2627
it('Should be able to register a plugin', async () => {
2728
const server = new Hapi.Server()
2829

29-
const myPlugin = async function myPlugin(hemera, options) {}
30-
31-
myPlugin[Symbol.for('dependencies')] = []
32-
myPlugin[Symbol.for('name')] = 'myPlugin'
33-
myPlugin[Symbol.for('options')] = { a: 1 }
30+
const myPlugin = Hp(async function myPlugin(hemera, options) {}, {
31+
name: 'myPlugin',
32+
options: { a: 1 }
33+
})
3434

3535
await server.register({
3636
plugin: HapiHemera,
@@ -48,4 +48,35 @@ describe('Hemera plugin registration', function() {
4848

4949
await server.stop()
5050
})
51+
52+
it('Should be able to register a plugin with options', async () => {
53+
const server = new Hapi.Server()
54+
55+
const myPlugin = Hp(
56+
async function myPlugin(hemera, options) {
57+
expect(options).to.be.equals({ a: 1, b: 2 })
58+
},
59+
{
60+
name: 'myPlugin',
61+
options: { a: 1 }
62+
}
63+
)
64+
65+
await server.register({
66+
plugin: HapiHemera,
67+
options: {
68+
plugins: [{ register: myPlugin, options: { b: 2 } }],
69+
nats: {
70+
url: noAuthUrl
71+
}
72+
}
73+
})
74+
75+
expect(server.hemera).to.exist()
76+
expect(server.hemera[hemeraInternalSymbols.registeredPlugins]).to.be.equals(
77+
['myPlugin']
78+
)
79+
80+
await server.stop()
81+
})
5182
})

0 commit comments

Comments
 (0)