seed
seed copied to clipboard
[Examples] fetch example to be abstracted into a function, with return type providing `.json()` with type information.
In the fetch() example in documentation:
async fn send_message(new_message: String) -> fetch::Result<shared::SendMessageResponseBody> {
Request::new(get_request_url())
.method(Method::Post)
.json(&shared::SendMessageRequestBody { text: new_message })?
.fetch()
.await?
.check_status()?
.json()
.await
}
Similarly, you can find it in other example code:
// server_integration/client/src/example_a.rs
async fn send_message(new_message: String) -> fetch::Result<shared::SendMessageResponseBody> {
Request::new(get_request_url())
.method(Method::Post)
.json(&shared::SendMessageRequestBody { text: new_message })?
.fetch()
.await?
.check_status()?
.json()
.await
}
To contrast with these two examples, the current fetch example in the code repo has the following lines in the update() function:
// examples/fetch/src/post.rs
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
/* ... */
let request = Request::new("/")
.method(Method::Post)
.header(Header::custom("Accept-Language", "en"))
.header(Header::bearer(token))
.json(&model.form)
.expect("Serialization failed");
/* ... */
}
In the current fetch example, the use of the function signature as above is not demonstrated. It's of my view that the first two examples are more idiomatic, but more importantly, show the use of following up a fetch with a json() in a more learnable manner. I feel fetch example might be improved by following this style.
Once I've gotten a bit more confidence taking this approach, I'll be happy to implement this change.