Skip to content

Commit d1aa968

Browse files
authored
feat: adds HTMXConfig for plugin customization (#6)
* feat: adds `HTMXConfig` for plugin customization * fix: addss type annotation
1 parent 077c26a commit d1aa968

5 files changed

Lines changed: 55 additions & 6 deletions

File tree

litestar_htmx/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from litestar_htmx.plugin import HTMXPlugin
3+
from litestar_htmx.plugin import HTMXConfig, HTMXPlugin
44
from litestar_htmx.request import HTMXDetails, HTMXHeaders, HTMXRequest
55
from litestar_htmx.response import (
66
ClientRedirect,
@@ -25,6 +25,7 @@
2525

2626
__all__ = (
2727
"HTMXPlugin",
28+
"HTMXConfig",
2829
"HTMXDetails",
2930
"HTMXHeaders",
3031
"HTMXRequest",

litestar_htmx/plugin.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from dataclasses import dataclass
56
from typing import TYPE_CHECKING
67

78
from litestar.plugins import InitPluginProtocol
@@ -24,11 +25,28 @@
2425
from litestar.config.app import AppConfig
2526

2627

28+
@dataclass
29+
class HTMXConfig:
30+
"""Configuration for HTMX Plugin."""
31+
32+
set_request_class_globally: bool = True
33+
"""Sets the `app_config.request_class` to the `HTMXRequest` class at the application level. Defaults to True"""
34+
35+
2736
class HTMXPlugin(InitPluginProtocol):
2837
"""HTMX Plugin."""
2938

30-
def __init__(self) -> None:
31-
"""Initialize the plugin."""
39+
def __init__(self, config: HTMXConfig | None = None) -> None:
40+
"""Initialize the plugin.
41+
42+
Args:
43+
config: Configuration for flash messages, including the template engine instance.
44+
"""
45+
self._config = config or HTMXConfig()
46+
47+
@property
48+
def config(self) -> HTMXConfig:
49+
return self._config
3250

3351
def on_app_init(self, app_config: AppConfig) -> AppConfig:
3452
"""Register the HTMX configuration.
@@ -39,7 +57,7 @@ def on_app_init(self, app_config: AppConfig) -> AppConfig:
3957
Returns:
4058
The application configuration with the message callable registered.
4159
"""
42-
if app_config.request_class is None:
60+
if self.config.set_request_class_globally and app_config.request_class is None:
4361
app_config.request_class = HTMXRequest
4462
app_config.signature_types = [
4563
HTMXRequest,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ maintainers = [
1313
name = "litestar-htmx"
1414
readme = "README.md"
1515
requires-python = ">=3.8, <4.0"
16-
version = "0.2.4"
16+
version = "0.3.0"
1717

1818
[build-system]
1919
build-backend = "hatchling.build"

tests/test_htmx_plugin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
from litestar.config.app import AppConfig
3+
4+
from litestar_htmx import HTMXConfig, HTMXPlugin, HTMXRequest
5+
6+
pytestmark = pytest.mark.anyio
7+
8+
9+
@pytest.mark.parametrize(
10+
"set_request_class_globally",
11+
(True, False),
12+
)
13+
def test_request_class(set_request_class_globally: bool) -> None:
14+
config = HTMXConfig(set_request_class_globally=set_request_class_globally)
15+
plugin = HTMXPlugin(config=config)
16+
app_config = plugin.on_app_init(AppConfig())
17+
if set_request_class_globally:
18+
assert app_config.request_class == HTMXRequest
19+
else:
20+
assert app_config.request_class is None
21+
22+
23+
class CustomHTMXRequest(HTMXRequest):
24+
"""Extra functionality."""
25+
26+
27+
def test_request_class_no_override() -> None:
28+
plugin = HTMXPlugin()
29+
app_config = plugin.on_app_init(AppConfig(request_class=CustomHTMXRequest))
30+
assert app_config.request_class == CustomHTMXRequest

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)