Skip to content

Commit 4b3abed

Browse files
committed
feat(fetch): Request::fetch
1 parent 785da7d commit 4b3abed

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

examples/fetch/src/post.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
2828

2929
let token = "YWxhZGRpbjpvcGVuc2VzYW1l";
3030
// Created outside async block because of lifetime reasons
31-
// (we can't use reference to `model.from` in async
32-
// function).
31+
// (we can't use reference to `model.form` in async function).
3332
let request = Request::new("/")
3433
.method(Method::Post)
3534
.header(Header::custom("Accept-Language", "en"))

examples/server_integration/client/src/example_a.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
5656
}
5757

5858
async fn send_message(new_message: String) -> fetch::Result<shared::SendMessageResponseBody> {
59-
let request = Request::new(get_request_url())
59+
Request::new(get_request_url())
6060
.method(Method::Post)
61-
.json(&shared::SendMessageRequestBody { text: new_message })?;
62-
63-
fetch(request).await?.check_status()?.json().await
61+
.json(&shared::SendMessageRequestBody { text: new_message })?
62+
.fetch()
63+
.await?
64+
.check_status()?
65+
.json()
66+
.await
6467
}
6568

6669
// ------ ------

examples/server_integration/client/src/example_e.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
115115
}
116116

117117
async fn send_request(form: FormData) -> fetch::Result<String> {
118-
let request = Request::new(get_request_url())
118+
Request::new(get_request_url())
119119
.method(fetch::Method::Post)
120-
.body(form.into());
121-
122-
fetch(request).await?.text().await
120+
.body(form.into())
121+
.fetch()
122+
.await?
123+
.text()
124+
.await
123125
}
124126

125127
#[allow(clippy::option_map_unit_fn)]

src/browser/fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! Seed Fetch API is very similar to the browser [native one][fetch-mdn].
44
//!
55
//! There is one entry point: [`fetch`][fetch] function.
6-
//! It can accept both, string urls as well as [`Request`][request].
6+
//! It can accept both string urls as well as [`Request`][request].
77
//!
88
//! To get a [`Response`][response] you need to `.await` fetch:
99
//! ```rust

src/browser/fetch/request.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The Request of the Fetch API.
22
3-
use super::{FetchError, Header, Headers, Method, Result};
3+
use super::{fetch, FetchError, Header, Headers, Method, Response, Result};
44
use crate::browser::Url;
55
use gloo_timers::callback::Timeout;
66
use serde::Serialize;
@@ -172,6 +172,31 @@ impl<'a> Request<'a> {
172172
let controller = self.controller.clone();
173173
(self, controller)
174174
}
175+
176+
/// Fetch request. It's a chainable alternative to `fetch(request)`.
177+
///
178+
/// # Example
179+
///
180+
/// ```rust,no_run
181+
/// orders.perform_cmd({
182+
/// let message = model.new_message.clone();
183+
/// async { Msg::Fetched(send_message(message).await) }
184+
/// });
185+
/// ...
186+
/// async fn send_message(new_message: String) -> fetch::Result<shared::SendMessageResponseBody> {
187+
/// Request::new(get_request_url())
188+
/// .method(Method::Post)
189+
/// .json(&shared::SendMessageRequestBody { text: new_message })?
190+
/// .fetch()
191+
/// .await?
192+
/// .check_status()?
193+
/// .json()
194+
/// .await
195+
/// }
196+
/// ```
197+
pub async fn fetch(self) -> Result<Response> {
198+
fetch(self).await
199+
}
175200
}
176201

177202
impl<'a, T: Into<Cow<'a, str>>> From<T> for Request<'a> {

0 commit comments

Comments
 (0)