Generic middleware for composing functions