mirror of
https://github.com/adoyle0/leptos-use.git
synced 2025-01-22 16:49: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
141
Cargo.toml
141
Cargo.toml
|
@ -1,24 +1,25 @@
|
|||
[package]
|
||||
name = "leptos-use"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
authors = ["Marc-Stefan Cassola"]
|
||||
categories = ["gui", "web-programming"]
|
||||
description = "Collection of essential Leptos utilities inspired by SolidJS USE / VueUse"
|
||||
edition = "2021"
|
||||
exclude = ["examples/", "tests/"]
|
||||
homepage = "https://leptos-use.rs"
|
||||
keywords = ["leptos", "utilities"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
name = "leptos-use"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/Synphonyte/leptos-use"
|
||||
homepage = "https://leptos-use.rs"
|
||||
version = "0.9.0"
|
||||
|
||||
[dependencies]
|
||||
base64 = { version = "0.21", optional = true }
|
||||
cfg-if = "1"
|
||||
cookie = { version = "0.18", features = ["percent-encode"] }
|
||||
default-struct-builder = "0.5"
|
||||
futures-util = "0.3"
|
||||
gloo-timers = { version = "0.3.0", features = ["futures"] }
|
||||
gloo-utils = { version = "0.2.0"}
|
||||
gloo-utils = { version = "0.2.0" }
|
||||
js-sys = "0.3"
|
||||
lazy_static = "1"
|
||||
leptos = "0.5"
|
||||
|
@ -32,78 +33,82 @@ wasm-bindgen = "0.2.88"
|
|||
wasm-bindgen-futures = "0.4"
|
||||
|
||||
[dependencies.web-sys]
|
||||
version = "0.3.65"
|
||||
features = [
|
||||
"AddEventListenerOptions",
|
||||
"BinaryType",
|
||||
"Coordinates",
|
||||
"CloseEvent",
|
||||
"CssStyleDeclaration",
|
||||
"CustomEvent",
|
||||
"CustomEventInit",
|
||||
"DisplayMediaStreamConstraints",
|
||||
"DomRect",
|
||||
"DomRectReadOnly",
|
||||
"DataTransfer",
|
||||
"DragEvent",
|
||||
"Element",
|
||||
"EventListener",
|
||||
"EventListenerOptions",
|
||||
"EventTarget",
|
||||
"File",
|
||||
"FileList",
|
||||
"Geolocation",
|
||||
"HtmlElement",
|
||||
"HtmlLinkElement",
|
||||
"HtmlStyleElement",
|
||||
"IntersectionObserver",
|
||||
"IntersectionObserverInit",
|
||||
"IntersectionObserverEntry",
|
||||
"MediaDevices",
|
||||
"MediaQueryList",
|
||||
"MediaStream",
|
||||
"MediaStreamTrack",
|
||||
"MouseEvent",
|
||||
"MutationObserver",
|
||||
"MutationObserverInit",
|
||||
"MutationRecord",
|
||||
"Navigator",
|
||||
"NodeList",
|
||||
"Notification",
|
||||
"NotificationDirection",
|
||||
"NotificationOptions",
|
||||
"NotificationPermission",
|
||||
"PointerEvent",
|
||||
"Position",
|
||||
"PositionError",
|
||||
"PositionOptions",
|
||||
"ResizeObserver",
|
||||
"ResizeObserverBoxOptions",
|
||||
"ResizeObserverEntry",
|
||||
"ResizeObserverOptions",
|
||||
"ResizeObserverSize",
|
||||
"ScrollBehavior",
|
||||
"ScrollToOptions",
|
||||
"ServiceWorker",
|
||||
"ServiceWorkerContainer",
|
||||
"ServiceWorkerRegistration",
|
||||
"ServiceWorkerState",
|
||||
"Storage",
|
||||
"StorageEvent",
|
||||
"Touch",
|
||||
"TouchEvent",
|
||||
"TouchList",
|
||||
"VisibilityState",
|
||||
"WebSocket",
|
||||
"Window",
|
||||
"AddEventListenerOptions",
|
||||
"BinaryType",
|
||||
"Coordinates",
|
||||
"CloseEvent",
|
||||
"CssStyleDeclaration",
|
||||
"CustomEvent",
|
||||
"CustomEventInit",
|
||||
"DisplayMediaStreamConstraints",
|
||||
"DomRect",
|
||||
"DomRectReadOnly",
|
||||
"DataTransfer",
|
||||
"DragEvent",
|
||||
"Element",
|
||||
"EventListener",
|
||||
"EventListenerOptions",
|
||||
"EventTarget",
|
||||
"File",
|
||||
"FileList",
|
||||
"Geolocation",
|
||||
"HtmlDocument",
|
||||
"HtmlElement",
|
||||
"HtmlLinkElement",
|
||||
"HtmlStyleElement",
|
||||
"IntersectionObserver",
|
||||
"IntersectionObserverInit",
|
||||
"IntersectionObserverEntry",
|
||||
"MediaDevices",
|
||||
"MediaQueryList",
|
||||
"MediaStream",
|
||||
"MediaStreamTrack",
|
||||
"MouseEvent",
|
||||
"MutationObserver",
|
||||
"MutationObserverInit",
|
||||
"MutationRecord",
|
||||
"Navigator",
|
||||
"NodeList",
|
||||
"Notification",
|
||||
"NotificationDirection",
|
||||
"NotificationOptions",
|
||||
"NotificationPermission",
|
||||
"PointerEvent",
|
||||
"Position",
|
||||
"PositionError",
|
||||
"PositionOptions",
|
||||
"ResizeObserver",
|
||||
"ResizeObserverBoxOptions",
|
||||
"ResizeObserverEntry",
|
||||
"ResizeObserverOptions",
|
||||
"ResizeObserverSize",
|
||||
"ScrollBehavior",
|
||||
"ScrollToOptions",
|
||||
"ServiceWorker",
|
||||
"ServiceWorkerContainer",
|
||||
"ServiceWorkerRegistration",
|
||||
"ServiceWorkerState",
|
||||
"Storage",
|
||||
"StorageEvent",
|
||||
"Touch",
|
||||
"TouchEvent",
|
||||
"TouchList",
|
||||
"VisibilityState",
|
||||
"WebSocket",
|
||||
"Window",
|
||||
]
|
||||
version = "0.3"
|
||||
|
||||
[features]
|
||||
actix = ["actix-web/cookies"]
|
||||
axum = ["axum-extra/cookie", "leptos_axum"]
|
||||
docs = []
|
||||
math = ["num"]
|
||||
prost = ["base64", "dep:prost"]
|
||||
serde = ["dep:serde", "serde_json"]
|
||||
ssr = []
|
||||
storage = ["serde", "serde_json", "web-sys/StorageEvent"]
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
|
|
@ -20,6 +20,7 @@ mod signal_throttled;
|
|||
mod use_active_element;
|
||||
mod use_breakpoints;
|
||||
mod use_color_mode;
|
||||
mod use_cookie;
|
||||
mod use_css_var;
|
||||
mod use_cycle_list;
|
||||
mod use_debounce_fn;
|
||||
|
@ -78,6 +79,7 @@ pub use signal_throttled::*;
|
|||
pub use use_active_element::*;
|
||||
pub use use_breakpoints::*;
|
||||
pub use use_color_mode::*;
|
||||
pub use use_cookie::*;
|
||||
pub use use_css_var::*;
|
||||
pub use use_cycle_list::*;
|
||||
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