Skip to content

Commit

Permalink
Merge pull request #15 from mcollina/option-to-override
Browse files Browse the repository at this point in the history
Pass options object to override function
  • Loading branch information
mcollina authored Jun 22, 2017
2 parents 92c3410 + bce8fe8 commit d2f94a9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ boot(app, {
-------------------------------------------------------
<a name="override"></a>

### app.override(server, plugin)
### app.override(server, plugin, options)

Allows to override the instance of the server for each loading plugin.
It allows the creation of an inheritance chain for the server instances.
The first parameter is the server instance and the second is the plugin function.
The first parameter is the server instance and the second is the plugin function while the third is the options object that you give to use.

```js
const boot = require('avvio')
Expand All @@ -291,7 +291,7 @@ const app = boot(server)

console.log(app !== server, 'override must be set on the Avvio instance')

app.override = function (s, fn) {
app.override = function (s, fn, opts) {
// create a new instance with the
// server as the prototype
const res = Object.create(s)
Expand Down
4 changes: 2 additions & 2 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Boot.prototype._init = function () {
}

// allows to override the instance of a server, given a plugin
Boot.prototype.override = function (server, func) {
Boot.prototype.override = function (server, func, opts) {
return server
}

Expand Down Expand Up @@ -191,7 +191,7 @@ function Plugin (parent, func, opts, callback) {

Plugin.prototype.exec = function (server, cb) {
const func = this.func
this.server = this.parent.override(server, func)
this.server = this.parent.override(server, func, this.opts)
func(this.server, this.opts, cb)
}

Expand Down
24 changes: 24 additions & 0 deletions test/override.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,27 @@ test('skip override - fastify test case', (t) => {
cb()
}
})

test('override can receive options object', (t) => {
t.plan(4)

const server = { my: 'server' }
const options = { hello: 'world' }
const app = boot(server)

app.override = function (s, fn, opts) {
t.equal(s, server)
t.deepEqual(opts, options)

const res = Object.create(s)
res.b = 42

return res
}

app.use(function first (s, opts, cb) {
t.notEqual(s, server)
t.ok(server.isPrototypeOf(s))
cb()
}, options)
})

0 comments on commit d2f94a9

Please sign in to comment.