middleware.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Base Pynecone middleware."""
  2. from __future__ import annotations
  3. from abc import ABC
  4. from typing import TYPE_CHECKING, Optional
  5. from pynecone.base import Base
  6. from pynecone.event import Event
  7. from pynecone.state import Delta, State
  8. if TYPE_CHECKING:
  9. from pynecone.app import App
  10. class Middleware(Base, ABC):
  11. """Middleware to preprocess and postprocess requests."""
  12. def preprocess(self, app: App, state: State, event: Event) -> Optional[Delta]:
  13. """Preprocess the event.
  14. Args:
  15. app: The app.
  16. state: The client state.
  17. event: The event to preprocess.
  18. Returns:
  19. An optional state to return.
  20. """
  21. return None
  22. def postprocess(
  23. self, app: App, state: State, event: Event, delta
  24. ) -> Optional[Delta]:
  25. """Postprocess the event.
  26. Args:
  27. app: The app.
  28. state: The client state.
  29. event: The event to postprocess.
  30. delta: The delta to postprocess.
  31. Returns:
  32. An optional state to return.
  33. """
  34. return None