Module magic

Module magic 

Source
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 custom thiserror impl)
  • 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: FromResponse
  • T 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§

pub use req_to::*;
pub use req_from::*;

Modules§

req_from
req_to

Structs§

ErrorPayload
The error payload structure for REST API errors.

Enums§

RestEndpointPayload
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§

AsStatusCode
Get the status code from the error type if possible.
MakeAxumError
MakeAxumResponse
A trait for converting the result of the Server Function into an Axum response.
RequestDecodeErr
RequestDecodeResult
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 RequestError into a ServerFnError.