mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-23 00:59:22 -05:00
Added use-cookie
TODO docs
This commit is contained in:
parent
3a75c73767
commit
73cad742f1
3 changed files with 122 additions and 68 deletions
17
Cargo.toml
17
Cargo.toml
|
@ -1,24 +1,25 @@
|
||||||
[package]
|
[package]
|
||||||
name = "leptos-use"
|
|
||||||
version = "0.9.0"
|
|
||||||
edition = "2021"
|
|
||||||
authors = ["Marc-Stefan Cassola"]
|
authors = ["Marc-Stefan Cassola"]
|
||||||
categories = ["gui", "web-programming"]
|
categories = ["gui", "web-programming"]
|
||||||
description = "Collection of essential Leptos utilities inspired by SolidJS USE / VueUse"
|
description = "Collection of essential Leptos utilities inspired by SolidJS USE / VueUse"
|
||||||
|
edition = "2021"
|
||||||
exclude = ["examples/", "tests/"]
|
exclude = ["examples/", "tests/"]
|
||||||
|
homepage = "https://leptos-use.rs"
|
||||||
keywords = ["leptos", "utilities"]
|
keywords = ["leptos", "utilities"]
|
||||||
license = "MIT OR Apache-2.0"
|
license = "MIT OR Apache-2.0"
|
||||||
|
name = "leptos-use"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
repository = "https://github.com/Synphonyte/leptos-use"
|
repository = "https://github.com/Synphonyte/leptos-use"
|
||||||
homepage = "https://leptos-use.rs"
|
version = "0.9.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = { version = "0.21", optional = true }
|
base64 = { version = "0.21", optional = true }
|
||||||
cfg-if = "1"
|
cfg-if = "1"
|
||||||
|
cookie = { version = "0.18", features = ["percent-encode"] }
|
||||||
default-struct-builder = "0.5"
|
default-struct-builder = "0.5"
|
||||||
futures-util = "0.3"
|
futures-util = "0.3"
|
||||||
gloo-timers = { version = "0.3.0", features = ["futures"] }
|
gloo-timers = { version = "0.3.0", features = ["futures"] }
|
||||||
gloo-utils = { version = "0.2.0"}
|
gloo-utils = { version = "0.2.0" }
|
||||||
js-sys = "0.3"
|
js-sys = "0.3"
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
leptos = "0.5"
|
leptos = "0.5"
|
||||||
|
@ -32,7 +33,6 @@ wasm-bindgen = "0.2.88"
|
||||||
wasm-bindgen-futures = "0.4"
|
wasm-bindgen-futures = "0.4"
|
||||||
|
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
version = "0.3.65"
|
|
||||||
features = [
|
features = [
|
||||||
"AddEventListenerOptions",
|
"AddEventListenerOptions",
|
||||||
"BinaryType",
|
"BinaryType",
|
||||||
|
@ -53,6 +53,7 @@ features = [
|
||||||
"File",
|
"File",
|
||||||
"FileList",
|
"FileList",
|
||||||
"Geolocation",
|
"Geolocation",
|
||||||
|
"HtmlDocument",
|
||||||
"HtmlElement",
|
"HtmlElement",
|
||||||
"HtmlLinkElement",
|
"HtmlLinkElement",
|
||||||
"HtmlStyleElement",
|
"HtmlStyleElement",
|
||||||
|
@ -97,13 +98,17 @@ features = [
|
||||||
"WebSocket",
|
"WebSocket",
|
||||||
"Window",
|
"Window",
|
||||||
]
|
]
|
||||||
|
version = "0.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
actix = ["actix-web/cookies"]
|
||||||
|
axum = ["axum-extra/cookie", "leptos_axum"]
|
||||||
docs = []
|
docs = []
|
||||||
math = ["num"]
|
math = ["num"]
|
||||||
prost = ["base64", "dep:prost"]
|
prost = ["base64", "dep:prost"]
|
||||||
serde = ["dep:serde", "serde_json"]
|
serde = ["dep:serde", "serde_json"]
|
||||||
ssr = []
|
ssr = []
|
||||||
|
storage = ["serde", "serde_json", "web-sys/StorageEvent"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
|
@ -20,6 +20,7 @@ mod signal_throttled;
|
||||||
mod use_active_element;
|
mod use_active_element;
|
||||||
mod use_breakpoints;
|
mod use_breakpoints;
|
||||||
mod use_color_mode;
|
mod use_color_mode;
|
||||||
|
mod use_cookie;
|
||||||
mod use_css_var;
|
mod use_css_var;
|
||||||
mod use_cycle_list;
|
mod use_cycle_list;
|
||||||
mod use_debounce_fn;
|
mod use_debounce_fn;
|
||||||
|
@ -78,6 +79,7 @@ pub use signal_throttled::*;
|
||||||
pub use use_active_element::*;
|
pub use use_active_element::*;
|
||||||
pub use use_breakpoints::*;
|
pub use use_breakpoints::*;
|
||||||
pub use use_color_mode::*;
|
pub use use_color_mode::*;
|
||||||
|
pub use use_cookie::*;
|
||||||
pub use use_css_var::*;
|
pub use use_css_var::*;
|
||||||
pub use use_cycle_list::*;
|
pub use use_cycle_list::*;
|
||||||
pub use use_debounce_fn::*;
|
pub use use_debounce_fn::*;
|
||||||
|
|
47
src/use_cookie.rs
Normal file
47
src/use_cookie.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
use cookie::Cookie;
|
||||||
|
|
||||||
|
pub struct UseCookie {
|
||||||
|
pub cookie: Option<Cookie<'static>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_cookie(cookie_name: &str) -> UseCookie {
|
||||||
|
let cookies;
|
||||||
|
#[cfg(feature = "ssr")]
|
||||||
|
{
|
||||||
|
use http::HeaderValue;
|
||||||
|
use leptos::expect_context;
|
||||||
|
|
||||||
|
let headers;
|
||||||
|
#[cfg(feature = "actix")]
|
||||||
|
{
|
||||||
|
headers = expect_context::<actix_web::HttpRequest>().headers().clone();
|
||||||
|
}
|
||||||
|
#[cfg(feature = "axum")]
|
||||||
|
{
|
||||||
|
headers = expect_context::<leptos_axum::RequestParts>().headers;
|
||||||
|
}
|
||||||
|
cookies = headers
|
||||||
|
.get(http::header::COOKIE)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| HeaderValue::from_static(""))
|
||||||
|
.to_str()
|
||||||
|
.unwrap_or_default()
|
||||||
|
.to_owned();
|
||||||
|
}
|
||||||
|
#[cfg(not(feature = "ssr"))]
|
||||||
|
{
|
||||||
|
use wasm_bindgen::JsCast;
|
||||||
|
|
||||||
|
let js_value: wasm_bindgen::JsValue = leptos::window().document().unwrap().into();
|
||||||
|
let document: web_sys::HtmlDocument = js_value.unchecked_into();
|
||||||
|
cookies = document.cookie().unwrap_or_default();
|
||||||
|
}
|
||||||
|
|
||||||
|
let cookie = Cookie::split_parse_encoded(cookies)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|cookie| cookie.ok())
|
||||||
|
.find(|cookie| cookie.name() == cookie_name)
|
||||||
|
.map(|cookie| cookie.into_owned());
|
||||||
|
|
||||||
|
UseCookie { cookie }
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue