Expand description
ServerFn request magical 🧙 decoders and encoders.
The Dioxus Server Function implementation brings a lot of magic to the types of endpoints we can handle. Our ultimate goal is to handle all endpoints, even axum endpoints, with the macro.
Unfortunately, some axum traits like FromRequest overlap with some of the default magic we want
to provide, like allowing DeserializedOwned groups.
Our ultimate goal - to accept all axum handlers - is feasible but not fully implemented.
Broadly, we support the following categories of handlers arguments:
- Handlers with a single argument that implements
FromRequest+IntoRequest - Handlers with multiple arguments that implement all
DeserializeOwned(and thus can be deserialized from a JSON body)
The handler error return types we support are:
Result<T, E> where E: From<ServerFnError> + Serialize + DeserializeOwned(basically any customthiserrorimpl)Result<T, anyhow::Error>where we transport the error as a string and/or through ServerFnError
The handler return types we support are:
T where T: FromResponseT where T: DeserializeOwned
Note that FromResponse and IntoRequest are custom traits defined in this crate. The intention is to provide “inverse” traits of the axum traits, allowing types to flow seamlessly between client and server.
These are unfortunately in conflict with the serialization traits. Types like Bytes implement both
IntoResponse and Serialize, so what should you use?
This module implements auto-deref specialization to allow tiering of the above cases.
This is sadly quite “magical”, but it works. Because the FromResponse traits are defined in this crate, they are sealed against types that implement Deserialize/Serialize, meaning you cannot implement FromResponse for a type that implements Serialize.
This module is broken up into several parts, attempting to match how the server macro generates code:
- ReqwestEncoder: encodes a set of arguments into a reqwest request
Re-exports§
Modules§
Structs§
- Error
Payload - The error payload structure for REST API errors.
Enums§
- Rest
Endpoint Payload - A response structure for a regular REST API, with a success and error case where the status is encoded in the body and all fields are serializable. This lets you call fetch().await.json() and get a strongly typed result.
Traits§
- AsStatus
Code - Get the status code from the error type if possible.
- Make
Axum Error - Make
Axum Response - A trait for converting the result of the Server Function into an Axum response.
- Request
Decode Err - Request
Decode Result - Convert the reqwest response into the desired type, in place. The point here is to prefer FromResponse types first and then DeserializeOwned types second.
Functions§
- reqwest_
error_ to_ request_ error - reqwest_
response_ to_ serverfn_ err - Convert a
RequestErrorinto aServerFnError.