Skip to content

Commit 141d780

Browse files
committed
Refactor Cargo.toml and response handling to streamline dependencies and improve type handling
1 parent 4415aba commit 141d780

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

models/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ headers = { workspace = true, features = [] }
2525
hex = { workspace = true, features = ["default"] }
2626
http = { workspace = true, features = ["default"] }
2727
ipnetwork = { workspace = true, features = ["default"] }
28-
leptos = { workspace = true, default-features = false }
28+
leptos = { workspace = true, features = [] }
2929
macros = { workspace = true, features = [] }
3030
preprocess = { workspace = true, features = [] }
3131
regex = { workspace = true, features = ["default"] }
@@ -35,7 +35,6 @@ serde = { workspace = true, features = ["default", "derive"] }
3535
serde_json = { workspace = true, features = ["default"] }
3636
serde_test = { workspace = true, features = [] }
3737
serde_urlencoded = { workspace = true, features = [] }
38-
server_fn = { workspace = true, features = ["reqwest"] }
3938
strum = { workspace = true, features = ["default", "derive"] }
4039
thiserror = { workspace = true, features = [] }
4140
tokio = { workspace = true, features = [] }
@@ -48,6 +47,7 @@ url = { workspace = true, features = ["default"] }
4847
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
4948
axum-typed-websockets = { workspace = true, features = ["default"] }
5049
getrandom_03 = { workspace = true, features = [] }
50+
server_fn = { workspace = true, features = ["reqwest"] }
5151
sqlx = { workspace = true, features = ["macros", "postgres", "sqlite", "uuid"] }
5252
time = { workspace = true, features = ["default", "serde-human-readable"] }
5353
uuid = { workspace = true, features = ["default", "v1", "v4"] }

models/src/request.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::marker::PhantomData;
22

33
use http::Method;
4-
use leptos::server_fn::codec::{Encoding, GetUrl};
4+
use leptos::server_fn::{
5+
ContentType,
6+
codec::{Encoding, GetUrl},
7+
};
58
use preprocess::Preprocessable;
69
use typed_builder::TypedBuilder;
710

@@ -13,7 +16,7 @@ pub struct ApiEncoding<E>(PhantomData<E>)
1316
where
1417
E: ApiEndpoint;
1518

16-
impl<E> Encoding for ApiEncoding<E>
19+
impl<E> ContentType for ApiEncoding<E>
1720
where
1821
E: ApiEndpoint,
1922
{
@@ -24,6 +27,12 @@ where
2427
// of the response. So we just return the default content type of binary data.
2528
"application/octet-stream"
2629
};
30+
}
31+
32+
impl<E> Encoding for ApiEncoding<E>
33+
where
34+
E: ApiEndpoint,
35+
{
2736
const METHOD: Method = E::FRONTEND_API_METHOD;
2837
}
2938

models/src/response.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ where
5555
E: ApiEndpoint,
5656
<E::RequestBody as Preprocessable>::Processed: Send,
5757
{
58-
async fn into_res(
59-
self,
60-
) -> Result<leptos::server_fn::response::BrowserMockRes, ServerFnError<ErrorType>> {
58+
async fn into_res(self) -> Result<leptos::server_fn::response::BrowserMockRes, ErrorType> {
6159
unreachable!()
6260
}
6361
}
@@ -68,7 +66,7 @@ where
6866
E: ApiEndpoint,
6967
<E::RequestBody as Preprocessable>::Processed: Send,
7068
{
71-
async fn into_res(self) -> Result<http::Response<axum::body::Body>, ServerFnError<ErrorType>> {
69+
async fn into_res(self) -> Result<http::Response<axum::body::Body>, ErrorType> {
7270
let AppResponse {
7371
status_code,
7472
headers,
@@ -85,7 +83,7 @@ where
8583

8684
response
8785
.body(body.into_axum_response().into_body())
88-
.map_err(|err| ServerFnError::Response(err.to_string()))
86+
.map_err(ErrorType::server_error)
8987
}
9088
}
9189

@@ -96,11 +94,11 @@ where
9694
E::RequestBody: Serialize + DeserializeOwned,
9795
E::ResponseBody: Serialize + DeserializeOwned,
9896
{
99-
async fn from_res(res: BrowserResponse) -> Result<Self, ServerFnError<ErrorType>> {
97+
async fn from_res(res: BrowserResponse) -> Result<Self, ErrorType> {
10098
let status_code = <BrowserResponse as ClientRes<ErrorType>>::status(&res);
10199
let status_code = StatusCode::from_u16(status_code)
102100
.map_err(|err| ServerFnError::Response(err.to_string()))?;
103-
let headers = E::ResponseHeaders::from_header_map(res.headers())
101+
let headers = E::ResponseHeaders::from_header_map(&res.generate_headers())
104102
.map_err(|err| ServerFnError::Response(err.to_string()))?;
105103
let body = res.try_into_string().await?;
106104
let body = serde_urlencoded::from_str(&body)

0 commit comments

Comments
 (0)