nxnodex
Search docsv0.1.0GitHub

Middleware

Wrap every node with one reusable chain

Middleware is the right place for logging, auth checks, metrics, tracing context, and shared policy.

@app.middleware
def logger(state, next_node):
    print(f"running {state._current_node}")
    return next_node(state)

Order

Middleware runs in registration order

The first registered middleware wraps the second, and the final callable is the node itself.

Errors

Middleware errors are attributed

If middleware raises, Nodex wraps it in `NodexMiddlewareError` with the middleware function name.

Example

Auth check middleware

from nodex import NodexAuthError

@app.middleware
def auth_check(state, next_node):
    if not state.get("api_key"):
        raise NodexAuthError("Missing api_key")
    return next_node(state)