mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
Merge pull request #99 from javierEd/main
Add spin feature for `use_cookie`
This commit is contained in:
commit
9507c6a6d6
2 changed files with 59 additions and 18 deletions
|
@ -29,6 +29,7 @@ lazy_static = "1"
|
|||
leptos = "0.6"
|
||||
leptos_axum = { version = "0.6", optional = true }
|
||||
leptos_actix = { version = "0.6", optional = true }
|
||||
leptos-spin = { version = "0.1", optional = true }
|
||||
num = { version = "0.4", optional = true }
|
||||
paste = "1"
|
||||
prost = { version = "0.12", optional = true }
|
||||
|
@ -142,6 +143,7 @@ docs = []
|
|||
math = ["num"]
|
||||
prost = ["base64", "dep:prost"]
|
||||
serde = ["dep:serde", "serde_json"]
|
||||
spin = ["dep:leptos-spin", "dep:http1"]
|
||||
ssr = []
|
||||
msgpack = ["dep:rmp-serde", "dep:serde"]
|
||||
|
||||
|
|
|
@ -485,9 +485,15 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
#[cfg(all(feature = "actix", feature = "axum"))]
|
||||
compile_error!("You cannot enable only one of features \"actix\" and \"axum\" at the same time");
|
||||
|
||||
#[cfg(all(feature = "actix", feature = "spin"))]
|
||||
compile_error!("You cannot enable only one of features \"actix\" and \"spin\" at the same time");
|
||||
|
||||
#[cfg(all(feature = "axum", feature = "spin"))]
|
||||
compile_error!("You cannot enable only one of features \"axum\" and \"spin\" at the same time");
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
const COOKIE: http0_2::HeaderName = http0_2::header::COOKIE;
|
||||
#[cfg(feature = "axum")]
|
||||
#[cfg(any(feature = "axum", feature = "spin"))]
|
||||
const COOKIE: http1::HeaderName = http1::header::COOKIE;
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
|
@ -495,7 +501,7 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
#[cfg(feature = "axum")]
|
||||
type HeaderValue = http1::HeaderValue;
|
||||
|
||||
#[cfg(any(feature = "axum", feature = "actix"))]
|
||||
#[cfg(any(feature = "axum", feature = "actix", feature = "spin"))]
|
||||
let headers;
|
||||
#[cfg(feature = "actix")]
|
||||
{
|
||||
|
@ -506,14 +512,24 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
{
|
||||
headers = use_context::<http1::request::Parts>().map(|parts| parts.headers);
|
||||
}
|
||||
|
||||
#[cfg(all(not(feature = "axum"), not(feature = "actix")))]
|
||||
#[cfg(feature = "spin")]
|
||||
{
|
||||
leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_cookies_header_getter`");
|
||||
headers = use_context::<leptos_spin::RequestParts>()
|
||||
.map(|parts| parts.headers().clone());
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
not(feature = "axum"),
|
||||
not(feature = "actix"),
|
||||
not(feature = "spin")
|
||||
))]
|
||||
{
|
||||
leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_cookies_header_getter`");
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "axum", feature = "actix"))]
|
||||
{
|
||||
headers.map(|headers| {
|
||||
headers
|
||||
.get(COOKIE)
|
||||
|
@ -524,6 +540,16 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
.to_owned()
|
||||
})
|
||||
}
|
||||
#[cfg(feature = "spin")]
|
||||
{
|
||||
headers.and_then(|headers| {
|
||||
headers
|
||||
.iter()
|
||||
.find(|(key, _)| **key == COOKIE)
|
||||
.and_then(|(_, value)| String::from_utf8(value.to_vec()).ok())
|
||||
})
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "ssr"))]
|
||||
None
|
||||
}),
|
||||
|
@ -534,21 +560,27 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
use leptos_actix::ResponseOptions;
|
||||
#[cfg(feature = "axum")]
|
||||
use leptos_axum::ResponseOptions;
|
||||
#[cfg(feature = "spin")]
|
||||
use leptos_spin::ResponseOptions;
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
const SET_COOKIE: http0_2::HeaderName = http0_2::header::SET_COOKIE;
|
||||
#[cfg(feature = "axum")]
|
||||
#[cfg(any(feature = "axum", feature = "spin"))]
|
||||
const SET_COOKIE: http1::HeaderName = http1::header::SET_COOKIE;
|
||||
|
||||
#[cfg(feature = "actix")]
|
||||
type HeaderValue = http0_2::HeaderValue;
|
||||
#[cfg(feature = "axum")]
|
||||
#[cfg(any(feature = "axum", feature = "spin"))]
|
||||
type HeaderValue = http1::HeaderValue;
|
||||
|
||||
#[cfg(all(not(feature = "axum"), not(feature = "actix")))]
|
||||
#[cfg(all(
|
||||
not(feature = "axum"),
|
||||
not(feature = "actix"),
|
||||
not(feature = "spin")
|
||||
))]
|
||||
{
|
||||
let _ = cookie;
|
||||
leptos::logging::warn!("If you're using use_cookie without the feature `axum` or `actix` enabled, you should provide the option `ssr_set_cookie`");
|
||||
leptos::logging::warn!("If you're using use_cookie without the feature `axum`, `actix` or `spin` enabled, you should provide the option `ssr_set_cookie`");
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "axum", feature = "actix"))]
|
||||
|
@ -561,6 +593,13 @@ impl<T, Err> Default for UseCookieOptions<T, Err> {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[cfg(feature = "spin")]
|
||||
{
|
||||
if let Some(response_options) = use_context::<ResponseOptions>() {
|
||||
let header_value = cookie.encoded().to_string().as_bytes().to_vec();
|
||||
response_options.insert_header(SET_COOKIE.as_str(), header_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let _ = cookie;
|
||||
|
|
Loading…
Add table
Reference in a new issue