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