Middleware lets you intercept every command, query, or event that flows through a bus — before and after the handler executes — without modifying the handler itself. HexCore uses the Chain of Responsibility pattern: each middleware in the pipeline receives the message and aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Indroic/HexCore/llms.txt
Use this file to discover all available pages before exploring further.
next_handler callable that invokes the rest of the chain. MiddlewarePipeline assembles the chain and executes it; AbstractMiddleware is the contract every middleware must implement.
NextHandler type alias
await it and propagate its return value so downstream middlewares and the final handler can complete normally.
AbstractMiddleware
handle to add cross-cutting behaviour (logging, timing, authentication, transaction management, etc.).
Abstract method
handle
await) next_handler(message) to continue the chain. You can execute logic before the call (pre-processing), after the call (post-processing), or wrap both in a try/except for error handling.
The
Command, Query, or DomainEvent being processed. The exact type depends on which bus the middleware is attached to.A callable that invokes the next middleware in the chain, or the final handler if this is the last middleware. You must await it and return its result unless you deliberately want to short-circuit the chain.
The result returned by calling
next_handler(message). Pass this through unchanged unless your middleware intentionally transforms the result.IMiddleware alias
IMiddleware continues to work unchanged.
MiddlewarePipeline
execute() wraps the middlewares around the final handler at runtime, with the first middleware added being the outermost wrapper (it runs first before any others).
Constructor
An optional initial list of middleware instances. Equivalent to calling
add() for each element after construction.add
self for fluent chaining.
The middleware instance to append.
Returns
self to allow: pipeline.add(LoggingMiddleware()).add(TimingMiddleware()).add_many
An iterable of middleware instances to append.
Returns
self for fluent chaining.execute
message.
The command, query, or event to process.
The async callable that represents the actual handler (e.g.,
handler.handle). Called last after all middlewares have run.The value returned by
final_handler, propagated back through the chain.Usage examples
The order middlewares are added is the order they wrap the chain.
MW1.add(MW2) means MW1 runs first (outermost), then MW2, then the handler. This mirrors the behaviour of popular frameworks like Django or Express middleware stacks.