Middlewares are functions that perform tasks before and after an operation (that call an API endpoint).
They can be used to modify parameters given to the operation, modify data received by the API, stop an operation call, cache data on the fly, batch requests, etc.
Example middlewares can be found at there; you should read them first as they provide simple examples of what you can achieve with this project.
/**
* @param {String} middlewareAPI.contollerId ie. 'AnalysesController'
* @param {String} middlewareAPI.operationId ie. 'getAnalysisInfo'
* @return {Middleware}
*/
export default function someMiddleware({contollerId, operationId}) {
/**
* @metaparam {Func} next Function to call with modified arguments for the next middleware
* @param {Object} params
* @param {Function} callback
* @param {Object} options
* @return {Boolean} Return false to stop middlewares chain and prevent operation to be called
*/
return next => function(params, callback, options) {
// Perform any kind of modifications on arguments
// Or even stop operation call
};
}
All of the following use cases can be composed to match your needs. Keep in mind that they are just examples.
export default function someMiddleware() {
return next => function(params, callback, options) {
const newParams = {
...params,
foo: 'bar',
};
next(newParams, callback, options);
};
}
export default function someMiddleware() {
return next => function(params, callback, options) {
next(
params,
function(error, result) {
if (!error) {
result = _.indexBy(result, 'id');
}
callback(error, result);
},
options
);
};
}
export default function someMiddleware() {
return next => function(params, callback, options) {
callback(new ForbiddenError());
return false;
//Don't forget to call the callback to give the result to the caller.
};
}
export default function someMiddleware() {
return next => function(params, callback, options) {
setTimeout(() => {
next(...arguments);
}, 3000);
};
}