diff --git a/lib/server-app.js b/lib/server-app.js index 84148aa2..4a55f108 100644 --- a/lib/server-app.js +++ b/lib/server-app.js @@ -22,7 +22,7 @@ module.exports = function loopbackExpress() { * app.middlewareFromConfig(compression, { * enabled: true, * phase: 'initial', - * config: { + * params: { * threshold: 128 * } * }); @@ -33,6 +33,8 @@ module.exports = function loopbackExpress() { * @param {Array|*} config The configuration. Either an array of arguments * to pass to the factory function, or the value of the first argument * when the factory expects a single argument only. + * + * @returns {object} this (fluent API) */ proto.middlewareFromConfig = function(factory, config) { assert(typeof factory === 'function', '"factory" must be a function'); @@ -43,15 +45,17 @@ proto.middlewareFromConfig = function(factory, config) { if (config.enabled === false) return; - var args = config.config; - if (args === undefined) { - args = []; - } else if (!Array.isArray(args)) { - args = [args]; + var params = config.params; + if (params === undefined) { + params = []; + } else if (!Array.isArray(params)) { + params = [params]; } - var handler = factory.apply(null, args); + var handler = factory.apply(null, params); this.middleware(config.phase, handler); + + return this; }; /** @@ -81,6 +85,8 @@ proto.middlewareFromConfig = function(factory, config) { * * @param {string|Array.} nameOrArray A phase name or a list of phase * names to add. + * + * @returns {object} this (fluent API) */ proto.defineMiddlewarePhases = function(nameOrArray) { this.lazyrouter(); @@ -90,6 +96,8 @@ proto.defineMiddlewarePhases = function(nameOrArray) { } else { this._requestHandlingPhases.addBefore('routes', nameOrArray); } + + return this; }; /** diff --git a/test/app.test.js b/test/app.test.js index 20240595..2b85ccba 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -110,28 +110,28 @@ describe('app', function() { app.middlewareFromConfig(handlerFactory, { enabled: true, phase: 'session', - config: expectedConfig + params: expectedConfig }); // Config as a value (single arg) app.middlewareFromConfig(handlerFactory, { enabled: true, phase: 'session:before', - config: 'before' + params: 'before' }); // Config as a list of args app.middlewareFromConfig(handlerFactory, { enabled: true, phase: 'session:after', - config: ['after', 2] + params: ['after', 2] }); // Disabled by configuration app.middlewareFromConfig(handlerFactory, { enabled: false, phase: 'initial', - config: null + params: null }); executeMiddlewareHandlers(app, function(err) {