You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

23 lines
757 B

from collections.abc import Callable
from functools import wraps
from typing import Any
def route_middleware(*middlewares: Callable[..., Any]) -> Callable[..., Any]:
def decorator(route_func: Callable[..., Any]) -> Callable[..., Any]:
@wraps(route_func)
async def wrapper(*args: Any, **kwargs: Any) -> Any:
req = kwargs.get("req")
if req is None:
raise ValueError("Route must have 'request: Request' parameter")
for middleware in middlewares:
result = middleware(req)
if callable(getattr(result, "__await__", None)):
await result
return await route_func(*args, **kwargs)
return wrapper
return decorator