Allow to change the order of middleware
daffl opened this issue · comments
David Luecke commented
This is a follow-up for feathersjs/feathers#1590 to discuss options for allowing to change the order of middleware. Registering new middleware is currently possible using registerMiddleware
:
const { hooks, registerMiddleware, getMiddleware } = require('@feathersjs/hooks');
// Hooks can be used with a function like this:
const sayHello = hooks(async name => {
return `Hello ${name}!`;
}, [
logRuntime,
validateName
]);
registerMiddleware(sayHello, [
otherHook
]);
However, it currently appends otherHook
to the existing chain. One improvement possible would be to add a setMiddleware
that allows to replace all middleware in combination with getMiddleware
like this:
const { hooks, setMiddleware, getMiddleware } = require('@feathersjs/hooks');
// Hooks can be used with a function like this:
const sayHello = hooks(async name => {
return `Hello ${name}!`;
}, [
logRuntime,
validateName
]);
setMiddleware(sayHello, [
otherHook,
...getMiddleware(sayHello)
]);
Now otherHook
would run first and your middleware chains are still explicitly defined.
David Luecke commented
Additionally it is also possible to customize where middleware is coming from using the collect
option.
Kévin Berthommier commented
setMiddleware(sayHello, registeredHooks => [
otherHook,
...registeredHooks
]);
like setState
from React ?