dioxus-fullstack 0.7.5

Library for fetching resources from servers in Dioxus apps.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
//! 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

use crate::{
    CantEncode, ClientRequest, ClientResponse, EncodeIsVerified, FromResponse, HttpError,
    IntoRequest, ServerFnError,
};
use axum::response::IntoResponse;
use axum_core::extract::{FromRequest, Request};
use bytes::Bytes;
use dioxus_fullstack_core::RequestError;
use http::StatusCode;
use send_wrapper::SendWrapper;
use serde::Serialize;
use serde::{de::DeserializeOwned, Deserialize};
use std::fmt::Display;
use std::{marker::PhantomData, prelude::rust_2024::Future};

#[doc(hidden)]
pub struct ServerFnEncoder<In, Out>(PhantomData<fn() -> (In, Out)>);
impl<In, Out> ServerFnEncoder<In, Out> {
    #[doc(hidden)]
    pub fn new() -> Self {
        ServerFnEncoder(PhantomData)
    }
}

#[doc(hidden)]
pub struct ServerFnDecoder<Out>(PhantomData<fn() -> Out>);
impl<Out> ServerFnDecoder<Out> {
    #[doc(hidden)]
    pub fn new() -> Self {
        ServerFnDecoder(PhantomData)
    }
}

/// 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.
///
/// Eventually we want to support JsonRPC which requires a different format.
///
/// We use the `___status` field to avoid conflicts with user-defined fields. Hopefully no one uses this field name!
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum RestEndpointPayload<T, E> {
    #[serde(rename = "success")]
    Success(T),

    #[serde(rename = "error")]
    Error(ErrorPayload<E>),
}

/// The error payload structure for REST API errors.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ErrorPayload<E> {
    pub(crate) message: String,

    pub(crate) code: u16,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub(crate) data: Option<E>,
}

/// Convert a `RequestError` into a `ServerFnError`.
///
/// This is a separate function to avoid bringing in `reqwest` into fullstack-core.
pub fn reqwest_response_to_serverfn_err(err: reqwest::Error) -> ServerFnError {
    ServerFnError::Request(reqwest_error_to_request_error(err))
}

pub fn reqwest_error_to_request_error(err: reqwest::Error) -> RequestError {
    let message = err.to_string();
    if err.is_timeout() {
        RequestError::Timeout(message)
    } else if err.is_request() {
        RequestError::Request(message)
    } else if err.is_body() {
        RequestError::Body(message)
    } else if err.is_decode() {
        RequestError::Decode(message)
    } else if err.is_redirect() {
        RequestError::Redirect(message)
    } else if let Some(status) = err.status() {
        RequestError::Status(message, status.as_u16())
    } else {
        #[cfg(not(target_arch = "wasm32"))]
        {
            if err.is_connect() {
                RequestError::Connect(message)
            } else {
                RequestError::Request(message)
            }
        }

        #[cfg(target_arch = "wasm32")]
        {
            RequestError::Request(message)
        }
    }
}

pub use req_to::*;
pub mod req_to {
    use super::*;

    pub trait EncodeRequest<In, Out, R> {
        type VerifyEncode;
        fn fetch_client(
            &self,
            ctx: ClientRequest,
            data: In,
            map: fn(In) -> Out,
        ) -> impl Future<Output = Result<R, RequestError>> + 'static;
        fn verify_can_serialize(&self) -> Self::VerifyEncode;
    }

    /// Using the deserialize path
    impl<T, O> EncodeRequest<T, O, ClientResponse> for &&&&&&&&&&ServerFnEncoder<T, O>
    where
        T: DeserializeOwned + Serialize + 'static,
    {
        type VerifyEncode = EncodeIsVerified;
        fn fetch_client(
            &self,
            ctx: ClientRequest,
            data: T,
            _map: fn(T) -> O,
        ) -> impl Future<Output = Result<ClientResponse, RequestError>> + 'static {
            async move { ctx.send_json(&data).await }
        }

        fn verify_can_serialize(&self) -> Self::VerifyEncode {
            EncodeIsVerified
        }
    }

    /// When we use the FromRequest path, we don't need to deserialize the input type on the client,
    impl<T, O, R> EncodeRequest<T, O, R> for &&&&&&&&&ServerFnEncoder<T, O>
    where
        T: 'static,
        O: IntoRequest<R>,
    {
        type VerifyEncode = EncodeIsVerified;
        fn fetch_client(
            &self,
            ctx: ClientRequest,
            data: T,
            map: fn(T) -> O,
        ) -> impl Future<Output = Result<R, RequestError>> + 'static {
            O::into_request(map(data), ctx)
        }

        fn verify_can_serialize(&self) -> Self::VerifyEncode {
            EncodeIsVerified
        }
    }

    /// The fall-through case that emits a `CantEncode` type which fails to compile when checked by the macro
    impl<T, O> EncodeRequest<T, O, ClientResponse> for &ServerFnEncoder<T, O>
    where
        T: 'static,
    {
        type VerifyEncode = CantEncode;
        #[allow(clippy::manual_async_fn)]
        fn fetch_client(
            &self,
            _ctx: ClientRequest,
            _data: T,
            _map: fn(T) -> O,
        ) -> impl Future<Output = Result<ClientResponse, RequestError>> + 'static {
            async move { unimplemented!() }
        }

        fn verify_can_serialize(&self) -> Self::VerifyEncode {
            CantEncode
        }
    }
}

pub use decode_ok::*;
mod decode_ok {

    use crate::{CantDecode, DecodeIsVerified};

    use super::*;

    /// Convert the reqwest response into the desired type, in place.
    /// The point here is to prefer FromResponse types *first* and then DeserializeOwned types second.
    ///
    /// This is because FromResponse types are more specialized and can handle things like websockets and files.
    /// DeserializeOwned types are more general and can handle things like JSON responses.
    pub trait RequestDecodeResult<T, R> {
        type VerifyDecode;
        fn decode_client_response(
            &self,
            res: Result<R, RequestError>,
        ) -> impl Future<Output = Result<Result<T, ServerFnError>, RequestError>> + Send;
        fn verify_can_deserialize(&self) -> Self::VerifyDecode;
    }

    impl<T: FromResponse<R>, E, R> RequestDecodeResult<T, R> for &&&ServerFnDecoder<Result<T, E>> {
        type VerifyDecode = DecodeIsVerified;
        fn decode_client_response(
            &self,
            res: Result<R, RequestError>,
        ) -> impl Future<Output = Result<Result<T, ServerFnError>, RequestError>> + Send {
            SendWrapper::new(async move {
                match res {
                    Err(err) => Err(err),
                    Ok(res) => Ok(T::from_response(res).await),
                }
            })
        }
        fn verify_can_deserialize(&self) -> Self::VerifyDecode {
            DecodeIsVerified
        }
    }

    impl<T: DeserializeOwned, E> RequestDecodeResult<T, ClientResponse>
        for &&ServerFnDecoder<Result<T, E>>
    {
        type VerifyDecode = DecodeIsVerified;
        fn decode_client_response(
            &self,
            res: Result<ClientResponse, RequestError>,
        ) -> impl Future<Output = Result<Result<T, ServerFnError>, RequestError>> + Send {
            SendWrapper::new(async move {
                match res {
                    Err(err) => Err(err),
                    Ok(res) => {
                        let status = res.status();

                        let bytes = res.bytes().await.unwrap();
                        let as_bytes = if bytes.is_empty() {
                            b"null".as_slice()
                        } else {
                            &bytes
                        };

                        let res = if status.is_success() {
                            serde_json::from_slice::<T>(as_bytes)
                                .map(RestEndpointPayload::Success)
                                .map_err(|e| ServerFnError::Deserialization(e.to_string()))
                        } else {
                            match serde_json::from_slice::<ErrorPayload<serde_json::Value>>(
                                as_bytes,
                            ) {
                                Ok(res) => Ok(RestEndpointPayload::Error(ErrorPayload {
                                    message: res.message,
                                    code: res.code,
                                    data: res.data,
                                })),
                                Err(err) => {
                                    if let Ok(text) = String::from_utf8(as_bytes.to_vec()) {
                                        Ok(RestEndpointPayload::Error(ErrorPayload {
                                            message: format!("HTTP {}: {}", status.as_u16(), text),
                                            code: status.as_u16(),
                                            data: None,
                                        }))
                                    } else {
                                        Err(ServerFnError::Deserialization(err.to_string()))
                                    }
                                }
                            }
                        };

                        match res {
                            Ok(RestEndpointPayload::Success(t)) => Ok(Ok(t)),
                            Ok(RestEndpointPayload::Error(err)) => {
                                Ok(Err(ServerFnError::ServerError {
                                    message: err.message,
                                    details: err.data,
                                    code: err.code,
                                }))
                            }
                            Err(e) => Ok(Err(e)),
                        }
                    }
                }
            })
        }
        fn verify_can_deserialize(&self) -> Self::VerifyDecode {
            DecodeIsVerified
        }
    }

    impl<T, R, E> RequestDecodeResult<T, R> for &ServerFnDecoder<Result<T, E>> {
        type VerifyDecode = CantDecode;

        fn decode_client_response(
            &self,
            _res: Result<R, RequestError>,
        ) -> impl Future<Output = Result<Result<T, ServerFnError>, RequestError>> + Send {
            async move { unimplemented!() }
        }

        fn verify_can_deserialize(&self) -> Self::VerifyDecode {
            CantDecode
        }
    }

    pub trait RequestDecodeErr<T, E> {
        fn decode_client_err(
            &self,
            res: Result<Result<T, ServerFnError>, RequestError>,
        ) -> impl Future<Output = Result<T, E>> + Send;
    }

    impl<T, E> RequestDecodeErr<T, E> for &&&ServerFnDecoder<Result<T, E>>
    where
        E: From<ServerFnError> + DeserializeOwned + Serialize,
    {
        fn decode_client_err(
            &self,
            res: Result<Result<T, ServerFnError>, RequestError>,
        ) -> impl Future<Output = Result<T, E>> + Send {
            SendWrapper::new(async move {
                match res {
                    Ok(Ok(res)) => Ok(res),
                    Ok(Err(e)) => match e {
                        ServerFnError::ServerError {
                            details,
                            message,
                            code,
                        } => {
                            // If there are "details", then we try to deserialize them into the error type.
                            // If there aren't, we just create a generic ServerFnError::ServerError with the message.
                            match details {
                                Some(details) => match serde_json::from_value::<E>(details) {
                                    Ok(res) => Err(res),
                                    Err(err) => Err(E::from(ServerFnError::Deserialization(
                                        err.to_string(),
                                    ))),
                                },
                                None => Err(E::from(ServerFnError::ServerError {
                                    message,
                                    details: None,
                                    code,
                                })),
                            }
                        }
                        err => Err(err.into()),
                    },
                    // todo: implement proper through-error conversion, instead of just ServerFnError::Request
                    // we should expand these cases.
                    Err(err) => Err(ServerFnError::from(err).into()),
                }
            })
        }
    }

    /// Here we convert to ServerFnError and then into the anyhow::Error, letting the user downcast
    /// from the ServerFnError if they want to.
    ///
    /// This loses any actual type information, but is the most flexible for users.
    impl<T> RequestDecodeErr<T, anyhow::Error> for &&ServerFnDecoder<Result<T, anyhow::Error>> {
        fn decode_client_err(
            &self,
            res: Result<Result<T, ServerFnError>, RequestError>,
        ) -> impl Future<Output = Result<T, anyhow::Error>> + Send {
            SendWrapper::new(async move {
                match res {
                    Ok(Ok(res)) => Ok(res),
                    Ok(Err(e)) => Err(anyhow::Error::from(e)),
                    Err(err) => Err(anyhow::Error::from(err)),
                }
            })
        }
    }

    /// This converts to statuscode, which can be useful but loses a lot of information.
    impl<T> RequestDecodeErr<T, StatusCode> for &ServerFnDecoder<Result<T, StatusCode>> {
        fn decode_client_err(
            &self,
            res: Result<Result<T, ServerFnError>, RequestError>,
        ) -> impl Future<Output = Result<T, StatusCode>> + Send {
            SendWrapper::new(async move {
                match res {
                    Ok(Ok(res)) => Ok(res),

                    // We do a best-effort conversion from ServerFnError to StatusCode.
                    Ok(Err(e)) => match e {
                        ServerFnError::Request(error) => {
                            Err(StatusCode::from_u16(error.status_code().unwrap_or(500))
                                .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR))
                        }

                        ServerFnError::ServerError {
                            message: _message,
                            details: _details,
                            code,
                        } => {
                            Err(StatusCode::from_u16(code)
                                .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR))
                        }

                        ServerFnError::Registration(_) | ServerFnError::MiddlewareError(_) => {
                            Err(StatusCode::INTERNAL_SERVER_ERROR)
                        }

                        ServerFnError::Deserialization(_)
                        | ServerFnError::Serialization(_)
                        | ServerFnError::Args(_)
                        | ServerFnError::MissingArg(_)
                        | ServerFnError::StreamError(_) => Err(StatusCode::UNPROCESSABLE_ENTITY),

                        ServerFnError::UnsupportedRequestMethod(_) => {
                            Err(StatusCode::METHOD_NOT_ALLOWED)
                        }

                        ServerFnError::Response(_) => Err(StatusCode::INTERNAL_SERVER_ERROR),
                    },

                    // The reqwest error case, we try to convert the reqwest error into a status code.
                    Err(reqwest_err) => {
                        let code = reqwest_err
                            .status()
                            .unwrap_or(StatusCode::SERVICE_UNAVAILABLE);
                        Err(code)
                    }
                }
            })
        }
    }

    impl<T> RequestDecodeErr<T, HttpError> for &ServerFnDecoder<Result<T, HttpError>> {
        fn decode_client_err(
            &self,
            res: Result<Result<T, ServerFnError>, RequestError>,
        ) -> impl Future<Output = Result<T, HttpError>> + Send {
            SendWrapper::new(async move {
                match res {
                    Ok(Ok(res)) => Ok(res),
                    Ok(Err(res)) => match res {
                        ServerFnError::ServerError { message, code, .. } => Err(HttpError {
                            status: StatusCode::from_u16(code)
                                .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
                            message: Some(message),
                        }),
                        _ => HttpError::internal_server_error("Internal Server Error"),
                    },
                    Err(err) => Err(HttpError::new(
                        err.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
                        err.to_string(),
                    )),
                }
            })
        }
    }
}

pub use req_from::*;
pub mod req_from {
    use super::*;
    use axum::{extract::FromRequestParts, response::Response};
    use dioxus_fullstack_core::FullstackContext;

    pub trait ExtractRequest<In, Out, H, M = ()> {
        fn extract_axum(
            &self,
            state: FullstackContext,
            request: Request,
            map: fn(In) -> Out,
        ) -> impl Future<Output = Result<(Out, H), Response>> + 'static;
    }

    /// When you're extracting entirely on the server, we need to reject client-consuning request bodies
    /// This sits above priority in the combined headers on server / body on client case.
    impl<In, M, H> ExtractRequest<In, (), H, M> for &&&&&&&&&&&ServerFnEncoder<In, ()>
    where
        H: FromRequest<FullstackContext, M> + 'static,
    {
        fn extract_axum(
            &self,
            state: FullstackContext,
            request: Request,
            _map: fn(In) -> (),
        ) -> impl Future<Output = Result<((), H), Response>> + 'static {
            async move {
                H::from_request(request, &state)
                    .await
                    .map_err(|e| e.into_response())
                    .map(|out| ((), out))
            }
        }
    }

    // One-arg case
    impl<In, Out, H> ExtractRequest<In, Out, H> for &&&&&&&&&&ServerFnEncoder<In, Out>
    where
        In: DeserializeOwned + 'static,
        Out: 'static,
        H: FromRequestParts<FullstackContext>,
    {
        fn extract_axum(
            &self,
            _state: FullstackContext,
            request: Request,
            map: fn(In) -> Out,
        ) -> impl Future<Output = Result<(Out, H), Response>> + 'static {
            async move {
                let (mut parts, body) = request.into_parts();
                let headers = H::from_request_parts(&mut parts, &_state)
                    .await
                    .map_err(|e| e.into_response())?;

                let request = Request::from_parts(parts, body);
                let bytes = Bytes::from_request(request, &()).await.unwrap();
                let as_str = String::from_utf8_lossy(&bytes);

                let bytes = if as_str.is_empty() {
                    "{}".as_bytes()
                } else {
                    &bytes
                };

                let out = serde_json::from_slice::<In>(bytes)
                    .map(map)
                    .map_err(|e| ServerFnError::from(e).into_response())?;

                Ok((out, headers))
            }
        }
    }

    /// We skip the BodySerialize wrapper and just go for the output type directly.
    impl<In, Out, M, H> ExtractRequest<In, Out, H, M> for &&&&&&&&&ServerFnEncoder<In, Out>
    where
        Out: FromRequest<FullstackContext, M> + 'static,
        H: FromRequestParts<FullstackContext>,
    {
        fn extract_axum(
            &self,
            state: FullstackContext,
            request: Request,
            _map: fn(In) -> Out,
        ) -> impl Future<Output = Result<(Out, H), Response>> + 'static {
            async move {
                let (mut parts, body) = request.into_parts();
                let headers = H::from_request_parts(&mut parts, &state)
                    .await
                    .map_err(|e| e.into_response())?;

                let request = Request::from_parts(parts, body);

                let res = Out::from_request(request, &state)
                    .await
                    .map_err(|e| e.into_response());

                res.map(|out| (out, headers))
            }
        }
    }
}

pub use resp::*;
mod resp {
    use crate::HttpError;

    use super::*;
    use axum::response::Response;
    use dioxus_core::CapturedError;
    use http::HeaderValue;

    /// A trait for converting the result of the Server Function into an Axum response.
    ///
    /// This is to work around the issue where we want to return both Deserialize types and FromResponse types.
    /// Stuff like websockets
    ///
    /// We currently have an `Input` type even though it's not useful since we might want to support regular axum endpoints later.
    /// For now, it's just Result<T, E> where T is either DeserializeOwned or FromResponse
    pub trait MakeAxumResponse<T, E, R> {
        fn make_axum_response(self, result: Result<T, E>) -> Result<Response, E>;
    }

    // Higher priority impl for special types like websocket/file responses that generate their own responses
    // The FromResponse impl helps narrow types to those usable on the client
    impl<T, E, R> MakeAxumResponse<T, E, R> for &&&&ServerFnDecoder<Result<T, E>>
    where
        T: FromResponse<R> + IntoResponse,
    {
        fn make_axum_response(self, result: Result<T, E>) -> Result<Response, E> {
            result.map(|v| v.into_response())
        }
    }

    // Lower priority impl for regular serializable types
    // We try to match the encoding from the incoming request, otherwise default to JSON
    impl<T, E> MakeAxumResponse<T, E, ()> for &&&ServerFnDecoder<Result<T, E>>
    where
        T: DeserializeOwned + Serialize,
    {
        fn make_axum_response(self, result: Result<T, E>) -> Result<Response, E> {
            match result {
                Ok(res) => {
                    let body = serde_json::to_string(&res).unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = StatusCode::OK;
                    Ok(resp)
                }
                Err(err) => Err(err),
            }
        }
    }

    #[allow(clippy::result_large_err)]
    pub trait MakeAxumError<E> {
        fn make_axum_error(self, result: Result<Response, E>) -> Response;
    }

    /// Get the status code from the error type if possible.
    pub trait AsStatusCode {
        fn as_status_code(&self) -> StatusCode;
    }

    impl AsStatusCode for ServerFnError {
        fn as_status_code(&self) -> StatusCode {
            match self {
                Self::ServerError { code, .. } => {
                    StatusCode::from_u16(*code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
                }
                _ => StatusCode::INTERNAL_SERVER_ERROR,
            }
        }
    }

    impl<T, E> MakeAxumError<E> for &&&ServerFnDecoder<Result<T, E>>
    where
        E: AsStatusCode + From<ServerFnError> + Serialize + DeserializeOwned + Display,
    {
        fn make_axum_error(self, result: Result<Response, E>) -> Response {
            match result {
                Ok(res) => res,
                Err(err) => {
                    let status_code = err.as_status_code();
                    let err = ErrorPayload {
                        code: status_code.as_u16(),
                        message: err.to_string(),
                        data: Some(err),
                    };
                    let body = serde_json::to_string(&err).unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = status_code;
                    resp
                }
            }
        }
    }

    impl<T> MakeAxumError<CapturedError> for &&ServerFnDecoder<Result<T, CapturedError>> {
        fn make_axum_error(self, result: Result<Response, CapturedError>) -> Response {
            match result {
                Ok(res) => res,

                // Optimize the case where we have sole ownership of the error
                Err(errr) if errr._strong_count() == 1 => {
                    let err = errr.into_inner().unwrap();
                    <&&ServerFnDecoder<Result<T, anyhow::Error>> as MakeAxumError<anyhow::Error>>::make_axum_error(
                        &&ServerFnDecoder::new(),
                        Err(err),
                    )
                }

                Err(errr) => {
                    // The `WithHttpError` trait emits ServerFnErrors so we can downcast them here
                    // to create richer responses.
                    let payload = match errr.downcast_ref::<ServerFnError>() {
                        Some(ServerFnError::ServerError {
                            message,
                            code,
                            details,
                        }) => ErrorPayload {
                            message: message.clone(),
                            code: *code,
                            data: details.clone(),
                        },
                        Some(other) => ErrorPayload {
                            message: other.to_string(),
                            code: 500,
                            data: None,
                        },
                        None => match errr.downcast_ref::<HttpError>() {
                            Some(http_err) => ErrorPayload {
                                message: http_err
                                    .message
                                    .clone()
                                    .unwrap_or_else(|| http_err.status.to_string()),
                                code: http_err.status.as_u16(),
                                data: None,
                            },
                            None => ErrorPayload {
                                code: 500,
                                message: errr.to_string(),
                                data: None,
                            },
                        },
                    };

                    let body = serde_json::to_string(&payload).unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
                    resp
                }
            }
        }
    }

    impl<T> MakeAxumError<anyhow::Error> for &&ServerFnDecoder<Result<T, anyhow::Error>> {
        fn make_axum_error(self, result: Result<Response, anyhow::Error>) -> Response {
            match result {
                Ok(res) => res,
                Err(errr) => {
                    // The `WithHttpError` trait emits ServerFnErrors so we can downcast them here
                    // to create richer responses.
                    let payload = match errr.downcast::<ServerFnError>() {
                        Ok(ServerFnError::ServerError {
                            message,
                            code,
                            details,
                        }) => ErrorPayload {
                            message,
                            code,
                            data: details,
                        },
                        Ok(other) => ErrorPayload {
                            message: other.to_string(),
                            code: 500,
                            data: None,
                        },
                        Err(err) => match err.downcast::<HttpError>() {
                            Ok(http_err) => ErrorPayload {
                                message: http_err
                                    .message
                                    .unwrap_or_else(|| http_err.status.to_string()),
                                code: http_err.status.as_u16(),
                                data: None,
                            },
                            Err(err) => ErrorPayload {
                                code: 500,
                                message: err.to_string(),
                                data: None,
                            },
                        },
                    };

                    let body = serde_json::to_string(&payload).unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
                    resp
                }
            }
        }
    }

    impl<T> MakeAxumError<StatusCode> for &&ServerFnDecoder<Result<T, StatusCode>> {
        fn make_axum_error(self, result: Result<Response, StatusCode>) -> Response {
            match result {
                Ok(resp) => resp,
                Err(status) => {
                    let body = serde_json::to_string(&ErrorPayload::<()> {
                        code: status.as_u16(),
                        message: status.to_string(),
                        data: None,
                    })
                    .unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = status;
                    resp
                }
            }
        }
    }

    impl<T> MakeAxumError<HttpError> for &ServerFnDecoder<Result<T, HttpError>> {
        fn make_axum_error(self, result: Result<Response, HttpError>) -> Response {
            match result {
                Ok(resp) => resp,
                Err(http_err) => {
                    let body = serde_json::to_string(&ErrorPayload::<()> {
                        code: http_err.status.as_u16(),
                        message: http_err
                            .message
                            .unwrap_or_else(|| http_err.status.to_string()),
                        data: None,
                    })
                    .unwrap();
                    let mut resp = Response::new(body.into());
                    resp.headers_mut().insert(
                        http::header::CONTENT_TYPE,
                        HeaderValue::from_static("application/json"),
                    );
                    *resp.status_mut() = http_err.status;
                    resp
                }
            }
        }
    }
}