Merge pull request #796 from strongloop/feature/cleanup-middleware-config-opts

Cleanup middleware config opts
This commit is contained in:
Miroslav Bajtoš 2014-11-18 19:10:07 +01:00
commit 7581ccf260
2 changed files with 33 additions and 14 deletions

View File

@ -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.<string>} 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();

View File

@ -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) {