mirror of
https://github.com/robertwayne/axum-htmx
synced 2024-11-23 20:02:50 +01:00
Feature
This commit is contained in:
parent
cbfe4782d8
commit
57e3e067b1
4 changed files with 23 additions and 16 deletions
|
@ -15,14 +15,17 @@ default = []
|
|||
unstable = []
|
||||
guards = ["tower", "futures-core", "pin-project-lite"]
|
||||
serde = ["dep:serde", "dep:serde_json"]
|
||||
auto-vary = ["axum", "futures", "tokio"]
|
||||
|
||||
[dependencies]
|
||||
axum-core = "0.4"
|
||||
http = { version = "1.0", default-features = false }
|
||||
async-trait = "0.1"
|
||||
axum = "0.7" # TODO: remove
|
||||
tokio = { version = "1", features = ["sync"] } # TODO: hide behind a feature?
|
||||
futures = "0.3" # TODO
|
||||
|
||||
# Optional dependencies required for the `auto-vary` feature.
|
||||
axum = { version = "0.7", default-features = false, optional = true }
|
||||
tokio = { version = "1", features = ["sync"], optional = true }
|
||||
futures = { version = "0.3", default-features = false, optional = true }
|
||||
|
||||
# Optional dependencies required for the `guards` feature.
|
||||
tower = { version = "0.4", default-features = false, optional = true }
|
||||
|
|
|
@ -17,7 +17,7 @@ use crate::{
|
|||
const MIDDLEWARE_DOUBLE_USE: &str =
|
||||
"Configuration error: `axum_httpx::vary_middleware` is used twice";
|
||||
|
||||
pub trait Notifier {
|
||||
pub(crate) trait Notifier {
|
||||
fn sender(&mut self) -> Option<Sender<()>>;
|
||||
|
||||
fn notify(&mut self) {
|
||||
|
@ -59,7 +59,7 @@ define_notifiers!(
|
|||
HxTriggerNameExtracted
|
||||
);
|
||||
|
||||
pub async fn vary_middleware(mut request: Request, next: Next) -> Response {
|
||||
pub async fn middleware(mut request: Request, next: Next) -> Response {
|
||||
let exts = request.extensions_mut();
|
||||
let rx_header = [
|
||||
(HxRequestExtracted::insert(exts), HX_REQUEST_STR),
|
||||
|
@ -122,7 +122,7 @@ mod tests {
|
|||
"/multiple-extractors",
|
||||
get(|_: HxRequest, _: HxTarget, _: HxTrigger, _: HxTriggerName| async { () }),
|
||||
)
|
||||
.layer(axum::middleware::from_fn(vary_middleware));
|
||||
.layer(axum::middleware::from_fn(middleware));
|
||||
axum_test::TestServer::new(app).unwrap()
|
||||
}
|
||||
|
|
@ -137,10 +137,11 @@ where
|
|||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
#[cfg(feature = "auto-vary")]
|
||||
parts
|
||||
.extensions
|
||||
.get_mut::<crate::vary_middleware::HxRequestExtracted>()
|
||||
.map(crate::vary_middleware::Notifier::notify);
|
||||
.get_mut::<crate::auto_vary::HxRequestExtracted>()
|
||||
.map(crate::auto_vary::Notifier::notify);
|
||||
|
||||
if parts.headers.contains_key(HX_REQUEST) {
|
||||
return Ok(HxRequest(true));
|
||||
|
@ -169,10 +170,11 @@ where
|
|||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
#[cfg(feature = "auto-vary")]
|
||||
parts
|
||||
.extensions
|
||||
.get_mut::<crate::vary_middleware::HxTargetExtracted>()
|
||||
.map(crate::vary_middleware::Notifier::notify);
|
||||
.get_mut::<crate::auto_vary::HxTargetExtracted>()
|
||||
.map(crate::auto_vary::Notifier::notify);
|
||||
|
||||
if let Some(target) = parts.headers.get(HX_TARGET) {
|
||||
if let Ok(target) = target.to_str() {
|
||||
|
@ -203,10 +205,11 @@ where
|
|||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
#[cfg(feature = "auto-vary")]
|
||||
parts
|
||||
.extensions
|
||||
.get_mut::<crate::vary_middleware::HxTriggerNameExtracted>()
|
||||
.map(crate::vary_middleware::Notifier::notify);
|
||||
.get_mut::<crate::auto_vary::HxTriggerNameExtracted>()
|
||||
.map(crate::auto_vary::Notifier::notify);
|
||||
|
||||
if let Some(trigger_name) = parts.headers.get(HX_TRIGGER_NAME) {
|
||||
if let Ok(trigger_name) = trigger_name.to_str() {
|
||||
|
@ -237,10 +240,11 @@ where
|
|||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
|
||||
#[cfg(feature = "auto-vary")]
|
||||
parts
|
||||
.extensions
|
||||
.get_mut::<crate::vary_middleware::HxTriggerExtracted>()
|
||||
.map(crate::vary_middleware::Notifier::notify);
|
||||
.get_mut::<crate::auto_vary::HxTriggerExtracted>()
|
||||
.map(crate::auto_vary::Notifier::notify);
|
||||
|
||||
if let Some(trigger) = parts.headers.get(HX_TRIGGER) {
|
||||
if let Ok(trigger) = trigger.to_str() {
|
||||
|
|
|
@ -23,5 +23,5 @@ pub use headers::*;
|
|||
#[doc(inline)]
|
||||
pub use responders::*;
|
||||
|
||||
pub(crate) mod vary_middleware;
|
||||
pub use vary_middleware::vary_middleware;
|
||||
#[cfg(feature = "auto-vary")]
|
||||
pub mod auto_vary;
|
||||
|
|
Loading…
Reference in a new issue