mirror of
https://github.com/tuono-labs/tuono
synced 2026-07-25 12:52:47 -07:00
67d4e1bcac
* refactor: useRouter to useInternalRouter * feat: create useRouter hook * feat: optimized payload sent to client * feat: add support for query and pathname value in useRouter * feat: update version to v0.4.6
55 lines
1.2 KiB
Rust
55 lines
1.2 KiB
Rust
use serde::Serialize;
|
|
use std::collections::HashMap;
|
|
|
|
use axum::http::{HeaderMap, Uri};
|
|
|
|
/// Location must match client side interface
|
|
#[derive(Serialize, Debug)]
|
|
pub struct Location {
|
|
href: String,
|
|
pathname: String,
|
|
#[serde(rename(serialize = "searchStr"))]
|
|
search_str: String,
|
|
search: HashMap<String, String>,
|
|
}
|
|
|
|
impl Location {
|
|
pub fn pathname(&self) -> &String {
|
|
&self.pathname
|
|
}
|
|
}
|
|
|
|
impl From<Uri> for Location {
|
|
fn from(uri: Uri) -> Self {
|
|
let query = uri.query().unwrap_or("");
|
|
Location {
|
|
// TODO: build correct href
|
|
href: uri.to_string(),
|
|
pathname: uri.path().to_string(),
|
|
search_str: query.to_string(),
|
|
search: serde_urlencoded::from_str(query).unwrap_or(HashMap::new()),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Request {
|
|
uri: Uri,
|
|
pub headers: HeaderMap,
|
|
pub params: HashMap<String, String>,
|
|
}
|
|
|
|
impl Request {
|
|
pub fn new(uri: Uri, headers: HeaderMap, params: HashMap<String, String>) -> Request {
|
|
Request {
|
|
uri,
|
|
headers,
|
|
params,
|
|
}
|
|
}
|
|
|
|
pub fn location(&self) -> Location {
|
|
Location::from(self.uri.to_owned())
|
|
}
|
|
}
|