chain
JSFunction
chain(
Chains multiple middleware functions into a single handler.
Each middleware must call next() to continue the chain.
If any middleware doesn't call next(), the chain stops.
Implementation
JSFunction chain(List<JSFunction> middlewares) =>
((Request req, Response res, JSFunction next) {
var index = 0;
void runNext() {
if (index < middlewares.length) {
final current = middlewares[index++];
current.callAsFunction(
null,
req,
res,
((JSAny? err) {
if (err != null) {
// Error occurred, skip to error handler
next.callAsFunction(null, err);
} else {
runNext();
}
}).toJS,
);
} else {
// All middleware executed, call final next
next.callAsFunction(null);
}
}
runNext();
}).toJS;