diff --git a/lib/server-app.js b/lib/server-app.js index 84148aa2..ac8e09ea 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 * } * }); @@ -30,9 +30,18 @@ module.exports = function loopbackExpress() { * * @param {function} factory The factory function creating a middleware handler. * Typically a result of `require()` call, e.g. `require('compression')`. - * @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. + * @options {Object} config The configuration. + * @property {String} phase The phase to register the middelware in. + * @property {Boolean} [enabled] Whether the middleware is enabled. + * Default: `true`. + * @property {Array|*} [params] The arguments to pass to the factory + * function. Either an array of arguments, + * or the value of the first argument when the factory expects + * a single argument only. + * + * @returns {object} this (fluent API) + * + * @header app.middlewareFromConfig(factory, config) */ proto.middlewareFromConfig = function(factory, config) { assert(typeof factory === 'function', '"factory" must be a function'); @@ -43,15 +52,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 +92,10 @@ 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) + * + * @header app.defineMiddlewarePhases(nameOrArray) */ proto.defineMiddlewarePhases = function(nameOrArray) { this.lazyrouter(); @@ -90,6 +105,8 @@ proto.defineMiddlewarePhases = function(nameOrArray) { } else { this._requestHandlingPhases.addBefore('routes', nameOrArray); } + + return this; }; /** @@ -99,6 +116,8 @@ proto.defineMiddlewarePhases = function(nameOrArray) { * `function(req, res, next)` or * `function(err, req, res, next)` * @returns {object} this (fluent API) + * + * @header app.middleware(name, handler) */ proto.middleware = function(name, handler) { this.lazyrouter(); diff --git a/test/app.test.js b/test/app.test.js index 951bce38..a5e02add 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -134,28 +134,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) {