middleware.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 State, StateUpdate
  8. if TYPE_CHECKING:
  9. from pynecone.app import App
  10. class Middleware(Base, ABC):
  11. """Middleware to preprocess and postprocess requests."""
  12. async def preprocess(
  13. self, app: App, state: State, event: Event
  14. ) -> Optional[StateUpdate]:
  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: State, 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