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