Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass options object to override function #15

Merged
merged 3 commits into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})