1
0

middleware.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Base Reflex middleware."""
  2. from __future__ import annotations
  3. from abc import ABC, abstractmethod
  4. from typing import TYPE_CHECKING
  5. from reflex.event import Event
  6. from reflex.state import BaseState, StateUpdate
  7. if TYPE_CHECKING:
  8. from reflex.app import App
  9. class Middleware(ABC):
  10. """Middleware to preprocess and postprocess requests."""
  11. @abstractmethod
  12. async def preprocess(
  13. self, app: App, state: BaseState, event: Event
  14. ) -> StateUpdate | None:
  15. """Preprocess the event.
  16. Args:
  17. app: The app.
  18. state: The client state.
  19. event: The event to preprocess.
  20. Returns:
  21. An optional state update to return.
  22. """
  23. return None
  24. async def postprocess(
  25. self, app: App, state: BaseState, event: Event, update: StateUpdate
  26. ) -> StateUpdate:
  27. """Postprocess the event.
  28. Args:
  29. app: The app.
  30. state: The client state.
  31. event: The event to postprocess.
  32. update: The current state update.
  33. Returns:
  34. An optional state to return.
  35. """
  36. return update