type

Middleware

typescript
type Middleware = (message: AnyMessage<TDefs>, direction: "incoming" | "outgoing") => AnyMessage<TDefs> | void | Promise<AnyMessage<TDefs> | void>

Middleware function type for message inspection/transformation. Applied sequentially in the order provided.

Messages are sealed before being passed to middleware, so you can modify existing properties but cannot add new ones.

Examples

typescript
const logMiddleware: Middleware<MyMessages> = (msg, direction) => {
  console.log(`${direction}:`, msg.type);
  return msg;
};