Skip to content

patera.middleware

See the middleware guide.

Middleware base class

MiddlewareBase

MiddlewareBase(app: AppT, next_app: Callable[[Request], Awaitable[Response]])

Bases: Injectable, ABC, Generic[AppT, ConfT]

Base class for middleware

Accepts the application and the next part of the middleware chain

app property

app: AppT

Returns the application instance

next property

next: Callable[[Request], Awaitable[Response]]

Returns the next part of the middleware chain

configs property

configs: ConfT

Returns the middleware's configuration instance.

Dictionaries are validated with the declared config model and a missing configuration falls back to the model's defaults.

middleware abstractmethod async

middleware(req: Request) -> Response

Middleware method to be implemented by subclasses

middleware

middleware(cls: MiddlewareClsT) -> MiddlewareClsT
middleware(*, order: int | None = None) -> MiddlewareDecorator
middleware(cls: MiddlewareClsT | None = None, *, order: int | None = None) -> MiddlewareClsT | MiddlewareDecorator

Decorator to mark a middleware implementation as middleware to be used by the application.

order sets the middleware's position in the chain; lower orders run first on the way in. It must be greater than 0, because negative orders are reserved for the framework's own middleware (CORS -101, database sessions -100, authentication -99). A middleware registered without an order, here or in register_middleware, raises ValueError. If two classes ask for the same order, the later one moves to the next free number and a warning is logged.