From 480d74174981a208b47b11d7450a3f8d2c448ded Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Fri, 8 Mar 2024 15:22:12 -0500 Subject: [PATCH] chore: update to gloo-net 0.5 (closes #2411) (#2416) --- router/Cargo.toml | 2 +- router/src/components/form.rs | 47 ++++++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/router/Cargo.toml b/router/Cargo.toml index f1974bd4c9..3d3c9a5d7f 100644 --- a/router/Cargo.toml +++ b/router/Cargo.toml @@ -15,7 +15,7 @@ leptos_integration_utils = { workspace = true, optional = true } leptos_meta = { workspace = true, optional = true } cached = { version = "0.45.0", optional = true } cfg-if = "1" -gloo-net = { version = "0.2", features = ["http"] } +gloo-net = { version = "0.5", features = ["http"] } lazy_static = "1" linear-map = { version = "1", features = ["serde_impl"] } once_cell = "1.18" diff --git a/router/src/components/form.rs b/router/src/components/form.rs index 98e90408a9..96f75ba842 100644 --- a/router/src/components/form.rs +++ b/router/src/components/form.rs @@ -80,6 +80,32 @@ pub fn Form( where A: ToHref + 'static, { + async fn post_form_data( + action: &str, + form_data: FormData, + ) -> Result { + gloo_net::http::Request::post(action) + .header("Accept", "application/json") + .redirect(RequestRedirect::Follow) + .body(form_data)? + .send() + .await + } + + async fn post_params( + action: &str, + enctype: &str, + params: web_sys::UrlSearchParams, + ) -> Result { + gloo_net::http::Request::post(action) + .header("Accept", "application/json") + .header("Content-Type", enctype) + .redirect(RequestRedirect::Follow) + .body(params)? + .send() + .await + } + fn inner( has_router: bool, method: Option<&'static str>, @@ -138,12 +164,7 @@ where let on_response = on_response.clone(); let on_error = on_error.clone(); spawn_local(async move { - let res = gloo_net::http::Request::post(&action) - .header("Accept", "application/json") - .redirect(RequestRedirect::Follow) - .body(form_data) - .send() - .await; + let res = post_form_data(&action, form_data).await; match res { Err(e) => { error!("
error while POSTing: {e:#?}"); @@ -155,6 +176,7 @@ where } } Ok(resp) => { + let resp = web_sys::Response::from(resp); if let Some(version) = action_version { version.update(|n| *n += 1); } @@ -162,7 +184,7 @@ where error.try_set(None); } if let Some(on_response) = on_response.clone() { - on_response(resp.as_raw()); + on_response(&resp); } // Check all the logical 3xx responses that might // get returned from a server function @@ -216,13 +238,7 @@ where let on_response = on_response.clone(); let on_error = on_error.clone(); spawn_local(async move { - let res = gloo_net::http::Request::post(&action) - .header("Accept", "application/json") - .header("Content-Type", &enctype) - .redirect(RequestRedirect::Follow) - .body(params) - .send() - .await; + let res = post_params(&action, &enctype, params).await; match res { Err(e) => { error!(" error while POSTing: {e:#?}"); @@ -234,6 +250,7 @@ where } } Ok(resp) => { + let resp = web_sys::Response::from(resp); if let Some(version) = action_version { version.update(|n| *n += 1); } @@ -241,7 +258,7 @@ where error.try_set(None); } if let Some(on_response) = on_response.clone() { - on_response(resp.as_raw()); + on_response(&resp); } // Check all the logical 3xx responses that might // get returned from a server function