Skip to content

Commit

Permalink
feat: adds lazy support for inertia (#61)
Browse files Browse the repository at this point in the history
* feat: adds `lazy` support for inertia
 
* feat: portal stuff
  • Loading branch information
cofin authored Dec 23, 2024
1 parent b91ba17 commit 1c99543
Show file tree
Hide file tree
Showing 11 changed files with 2,332 additions and 649 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ default_language_version:
python: "3"
repos:
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v3.6.0
rev: v4.0.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
Expand All @@ -26,7 +26,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.8.2"
rev: "v0.8.4"
hooks:
# Run the linter.
- id: ruff
Expand Down
Empty file.
178 changes: 89 additions & 89 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ packages = ["src/py/litestar_vite"]
sources = ["src/py"]

[dependency-groups]
dev = ["nodeenv", "litestar[standard]", "bump-my-version", { include-group = "linting" }, { include-group = "test" }, { include-group = "docs" } ]
dev = [ {include-group = "build"}, { include-group = "linting" }, { include-group = "test" }, { include-group = "docs" } ]
build = ["bump-my-version"]
docs = [
"auto-pytabs[sphinx]>=0.4.0",
"sphinx-autobuild>=2021.3.14",
Expand All @@ -71,6 +72,7 @@ docs = [
]
linting = ["pre-commit>=3.4.0", "mypy>=1.5.1", "ruff>=0.0.287", "types-docutils", "slotscheck", "basedpyright", "pyright"]
test = [
"litestar[standard]",
"pytest>=7.4.1",
"pytest-cov",
"coverage",
Expand Down Expand Up @@ -148,7 +150,7 @@ replace = ' "litestar-vite-plugin": "^{new_version}",'
search = ' "litestar-vite-plugin": "^{current_version}",'

[tool.pytest.ini_options]
addopts = "--dist loadfile -n auto"
addopts = ["-q", "-ra"]
filterwarnings = ["ignore::DeprecationWarning:pkg_resources.*"]
testpaths = ["src/py/tests"]

Expand Down Expand Up @@ -338,6 +340,12 @@ show_column_numbers = true
warn_no_return = false
warn_unused_ignores = true

[[tool.mypy.overrides]]
ignore_missing_imports = true
module = [
"awaitlet",
]

[[tool.mypy.overrides]]
disable_error_code = "attr-defined"
disallow_untyped_decorators = false
Expand Down
44 changes: 32 additions & 12 deletions src/py/litestar_vite/inertia/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,8 @@

from typing import TYPE_CHECKING

from litestar.exceptions import ImproperlyConfiguredException
from litestar.middleware import DefineMiddleware
from litestar.middleware.session import SessionMiddleware
from anyio.from_thread import BlockingPortalProvider
from litestar.plugins import InitPluginProtocol
from litestar.security.session_auth.middleware import MiddlewareWrapper
from litestar.utils.predicates import is_class_and_subclass

from litestar_vite.inertia.exception_handler import exception_to_http_response
from litestar_vite.inertia.middleware import InertiaMiddleware
from litestar_vite.inertia.request import InertiaRequest
from litestar_vite.inertia.response import InertiaResponse
from litestar_vite.inertia.routes import generate_js_routes

if TYPE_CHECKING:
from litestar import Litestar
Expand All @@ -24,14 +14,16 @@

def set_js_routes(app: Litestar) -> None:
"""Generate the route structure of the application on startup."""
from litestar_vite.inertia.routes import generate_js_routes

js_routes = generate_js_routes(app)
app.state.js_routes = js_routes


class InertiaPlugin(InitPluginProtocol):
"""Inertia plugin."""

__slots__ = ("config",)
__slots__ = ("config", "portal")

def __init__(self, config: InertiaConfig) -> None:
"""Initialize ``Inertia``.
Expand All @@ -41,12 +33,29 @@ def __init__(self, config: InertiaConfig) -> None:
"""
self.config = config

self.portal = BlockingPortalProvider()

def get_portal(self) -> BlockingPortalProvider:
return self.portal

def on_app_init(self, app_config: AppConfig) -> AppConfig:
"""Configure application for use with Vite.
Args:
app_config: The :class:`AppConfig <litestar.config.app.AppConfig>` instance.
"""

from litestar.exceptions import ImproperlyConfiguredException
from litestar.middleware import DefineMiddleware
from litestar.middleware.session import SessionMiddleware
from litestar.security.session_auth.middleware import MiddlewareWrapper
from litestar.utils.predicates import is_class_and_subclass

from litestar_vite.inertia.exception_handler import exception_to_http_response
from litestar_vite.inertia.middleware import InertiaMiddleware
from litestar_vite.inertia.request import InertiaRequest
from litestar_vite.inertia.response import DeferredProp, InertiaBack, InertiaResponse, StaticProp

for mw in app_config.middleware:
if isinstance(mw, DefineMiddleware) and is_class_and_subclass(
mw.middleware,
Expand All @@ -61,4 +70,15 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
app_config.response_class = InertiaResponse
app_config.middleware.append(InertiaMiddleware)
app_config.on_startup.append(set_js_routes)
app_config.signature_types.extend([InertiaRequest, InertiaResponse, InertiaBack, StaticProp, DeferredProp])
app_config.type_encoders = {
StaticProp: lambda val: val.render(),
DeferredProp: lambda val: val.render(),
**(app_config.type_encoders or {}),
}
app_config.type_decoders = [
(lambda x: x is StaticProp, lambda t, v: t(v)),
(lambda x: x is DeferredProp, lambda t, v: t(v)),
*(app_config.type_decoders or []),
]
return app_config
Loading

0 comments on commit 1c99543

Please sign in to comment.