Hello there,
I am building a web app using Rust and WASM, and I would like to profit as much as possible from using the same APIs both on the client and the server. I have been using REST so far, but I was wondering if we can do better. Would it be possible to request some kind of reference from the server, which acts just like a Rust reference, but processes HTTP communication under the hood? Is there anything comparable already available today?
I am imagining something like this:
// Server side
listen(|request| {
let ref_request: HttpReferenceRequest = request.body().try_into().unwrap();
Response::from(
match ref_request.table_name().as_str() {
"users" => {
DATABASE.users().reference(ref_request.entry_id()) as HttpReference<User>
},
_ => Err("Unknown table!"),
}
)
})
// Client side
let user_reference: HttpReference<User> =
fetch(
Request::get("/database")
.body(
HttpReferenceRequest::new(
Id<User>::from(user_id) // get reference for User by id
)
)
)
.expect("Fetch failed!")
.try_into()
.expect("Unable to convert to reference!");
assert_eq!(user_reference.get().unwrap(), User { name: "User from Server", ... });
user_reference.set_name("New Name".to_string()).expect("Failed to update remote entry!");
Does this make sense? I like the idea of a data type for lazy remote access, but I am not sure if it is as simple or useful as I think. I would very much like to hear some thoughts on this.