Skip to content

Commit 906d08f

Browse files
committed
plugin options precedence
1 parent c303acb commit 906d08f

File tree

6 files changed

+59
-13
lines changed

6 files changed

+59
-13
lines changed

lib/errors.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,16 @@ module.exports = {
107107
"The definition function for the plugin <%=fullname%> has failed: <%=message%>. This error is considered fatal as all plugins have to initialize correctly. You should test the plugin by itself to verify that it is working correctly. Also ensure that the configuration options passed to the plugin are correct. These are shown below under in the DETAILS section. There could also be a bug in the plugin. If you think that is the case, please create a github issue on the plugin's repository<%=repo%>, and include this error report.",
108108

109109
no_transport_client: "The transport client defined by <%=config%> does not exist for message: <%=msg%>",
110-
110+
111+
invalid_plugin_option: "Plugin <%=name%>: option value is not valid: <%=err_msg%> in options <%=options%>",
112+
113+
111114
// Legacy error message codes
112115

113116
act_invalid_args:
114117
'Action <%=pattern%> has invalid arguments; <%=message%>; ' +
115-
'arguments were: <%=msg%>.'
118+
'arguments were: <%=msg%>.',
119+
116120
}
117121

118122
module.exports.deprecation = {

lib/plugins.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var internals = {
1515
'The plugin <%=name%> uses an unsupported legacy ' +
1616
'callback to indicate plugin definition is complete: <%=init_func_sig%> ' +
1717
'... }. The correct format is: function(options) { ... }. For more details, ' +
18-
'please see http://senecajs.org/tutorials/how-to-write-a-plugin.html'
18+
'please see http://senecajs.org/tutorials/how-to-write-a-plugin.html',
1919
}
2020
})
2121
}
@@ -198,6 +198,8 @@ module.exports.make_delegate = make_delegate
198198
function resolve_options(fullname, plugindef, seneca) {
199199
var so = seneca.options()
200200

201+
var defaults = plugindef.defaults || {}
202+
201203
var fullname_options = _.extend(
202204
{},
203205
so[fullname],
@@ -226,7 +228,16 @@ function resolve_options(fullname, plugindef, seneca) {
226228
plugindef.options || {}
227229
)
228230

229-
return outopts
231+
try {
232+
return seneca.util.Optioner(defaults, {allow_unknown: true})
233+
.check(outopts)
234+
} catch (e) {
235+
throw Common.error('invalid_plugin_option', {
236+
name: fullname,
237+
err_msg: e.message,
238+
options: outopts
239+
})
240+
}
230241
}
231242

232243
function make_delegate(instance, plugin) {

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"rolling-stats": "0.1",
9595
"semver": "5.5",
9696
"seneca-transport": "2.3",
97-
"use-plugin": "2.3",
97+
"use-plugin": "2.4.1",
9898
"wreck": "12.5"
9999
},
100100
"devDependencies": {

seneca.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var Util = require('util')
1010
var _ = require('lodash')
1111
var GateExecutor = require('gate-executor')
1212
var Jsonic = require('jsonic')
13-
var Makeuse = require('use-plugin')
13+
var UsePlugin = require('use-plugin')
1414
var Nid = require('nid')
1515
var Norma = require('norma')
1616
var Patrun = require('patrun')
@@ -222,7 +222,8 @@ var seneca_util = {
222222
Jsonic: Jsonic,
223223
Nid: Nid,
224224
Patrun: Patrun,
225-
Joi: Makeuse.Joi,
225+
Joi: UsePlugin.Joi,
226+
Optioner: UsePlugin.Optioner,
226227

227228
clean: Common.clean,
228229
pattern: Common.pattern,
@@ -510,11 +511,12 @@ function make_seneca(initial_options) {
510511

511512
// private$.plugins = {}
512513
private$.plugin_order = { byname: [], byref: [] }
513-
private$.use = Makeuse({
514+
private$.use = UsePlugin({
514515
prefix: 'seneca-',
515516
module: opts.$.internal.module || module,
516517
msgprefix: false,
517-
builtin: ''
518+
builtin: '',
519+
merge_defaults: false
518520
})
519521

520522
private$.actrouter = opts.$.internal.actrouter

test/plugin.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ describe('plugin', function() {
113113
it('bad-default-options', function(fin) {
114114
Seneca({ log: 'silent', debug: { undead: true } })
115115
.test(function(err) {
116-
expect(err.code).equals('plugin_invalid_option')
116+
expect(err.code).equals('invalid_plugin_option')
117117
fin()
118118
})
119119
.use(
@@ -776,4 +776,33 @@ describe('plugin', function() {
776776
fin()
777777
})
778778
})
779+
780+
781+
it('plugins-options-precedence', function(fin) {
782+
var si = Seneca({
783+
log: 'silent',
784+
legacy: { transport: false },
785+
plugin: {
786+
foo: {a:2,c:2},
787+
bar: {a:2,c:1},
788+
}
789+
})
790+
791+
function bar(opts) {
792+
expect(opts).equal({a:3,b:1,c:1,d:1})
793+
}
794+
bar.defaults = {a:1,b:1}
795+
796+
si
797+
.use({name:'foo', defaults:{a:1,b:1}, init: function(opts) {
798+
expect(opts).equal({a:2,b:2,c:3,d:1})
799+
}}, {b:2,c:3,d:1})
800+
.use(bar, {a:3,d:1})
801+
802+
.ready(function() {
803+
console.log(this.options().plugin)
804+
fin()
805+
})
806+
})
807+
779808
})

0 commit comments

Comments
 (0)