server-app: middleware API improvements
- Rename `config.config` to `config.params` - Modify methods to return `this` (fluent API)
This commit is contained in:
parent
b0fc88b333
commit
7647339675
|
@ -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.<string>} 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;
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue