mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 06:44:17 +00:00
cargo fmt
This commit is contained in:
parent
dd368a845c
commit
2dbc5899f3
19 changed files with 237 additions and 112 deletions
|
@ -113,9 +113,7 @@
|
|||
////! CBOR forms encounter the same issue as `PUT`, `DELETE`, or JSON: they do not degrade gracefully if the WASM version of
|
||||
////! your app is not available.
|
||||
|
||||
pub use server_fn::{
|
||||
error::ServerFnErrorErr, ServerFnError,
|
||||
};
|
||||
pub use server_fn::{error::ServerFnErrorErr, ServerFnError};
|
||||
|
||||
mod action;
|
||||
mod multi_action;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use server_fn::{ServerFn, ServerFnError};
|
||||
use leptos_reactive::{
|
||||
create_rw_signal, is_suppressing_resource_load, signal_prelude::*,
|
||||
spawn_local, store_value, untrack, ReadSignal, RwSignal, StoredValue,
|
||||
};
|
||||
use server_fn::{ServerFn, ServerFnError};
|
||||
use std::{future::Future, pin::Pin, rc::Rc};
|
||||
|
||||
/// An action that synchronizes multiple imperative `async` calls to the reactive system,
|
||||
|
|
|
@ -14,7 +14,8 @@ pub trait Client<CustErr> {
|
|||
pub mod browser {
|
||||
use super::Client;
|
||||
use crate::{
|
||||
error::ServerFnError, request::browser::BrowserRequest, response::browser::BrowserResponse,
|
||||
error::ServerFnError, request::browser::BrowserRequest,
|
||||
response::browser::BrowserResponse,
|
||||
};
|
||||
use send_wrapper::SendWrapper;
|
||||
use std::future::Future;
|
||||
|
@ -27,7 +28,8 @@ pub mod browser {
|
|||
|
||||
fn send(
|
||||
req: Self::Request,
|
||||
) -> impl Future<Output = Result<Self::Response, ServerFnError<CustErr>>> + Send {
|
||||
) -> impl Future<Output = Result<Self::Response, ServerFnError<CustErr>>>
|
||||
+ Send {
|
||||
SendWrapper::new(async move {
|
||||
req.0
|
||||
.take()
|
||||
|
@ -56,7 +58,8 @@ pub mod reqwest {
|
|||
|
||||
fn send(
|
||||
req: Self::Request,
|
||||
) -> impl Future<Output = Result<Self::Response, ServerFnError<CustErr>>> + Send {
|
||||
) -> impl Future<Output = Result<Self::Response, ServerFnError<CustErr>>>
|
||||
+ Send {
|
||||
CLIENT
|
||||
.execute(req)
|
||||
.map_err(|e| ServerFnError::Request(e.to_string()))
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use super::{Encoding, FromReq, FromRes, IntoReq, IntoRes};
|
||||
use crate::error::ServerFnError;
|
||||
use crate::request::{ClientReq, Req};
|
||||
use crate::response::{ClientRes, Res};
|
||||
use crate::{
|
||||
error::ServerFnError,
|
||||
request::{ClientReq, Req},
|
||||
response::{ClientRes, Res},
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
/// Pass arguments and receive responses using `cbor` in a `POST` request.
|
||||
pub struct Cbor;
|
||||
|
@ -18,11 +19,20 @@ where
|
|||
Request: ClientReq<CustErr>,
|
||||
T: Serialize + Send,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
ciborium::ser::into_writer(&self, &mut buffer)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Request::try_new_post_bytes(path, accepts, Cbor::CONTENT_TYPE, Bytes::from(buffer))
|
||||
Request::try_new_post_bytes(
|
||||
path,
|
||||
accepts,
|
||||
Cbor::CONTENT_TYPE,
|
||||
Bytes::from(buffer),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,7 +68,8 @@ where
|
|||
{
|
||||
async fn from_res(res: Response) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let data = res.try_into_bytes().await?;
|
||||
ciborium::de::from_reader(data.as_ref()).map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
ciborium::de::from_reader(data.as_ref())
|
||||
.map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
use super::{Encoding, FromReq, FromRes};
|
||||
use crate::error::ServerFnError;
|
||||
use crate::request::{ClientReq, Req};
|
||||
use crate::response::{ClientRes, Res};
|
||||
use crate::{IntoReq, IntoRes};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use crate::{
|
||||
error::ServerFnError,
|
||||
request::{ClientReq, Req},
|
||||
response::{ClientRes, Res},
|
||||
IntoReq, IntoRes,
|
||||
};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
/// Pass arguments and receive responses as JSON in the body of a `POST` request.
|
||||
pub struct Json;
|
||||
|
||||
|
@ -17,7 +18,11 @@ where
|
|||
Request: ClientReq<CustErr>,
|
||||
T: Serialize + Send,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let data = serde_json::to_string(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Request::try_new_post(path, accepts, Json::CONTENT_TYPE, data)
|
||||
|
@ -31,7 +36,8 @@ where
|
|||
{
|
||||
async fn from_req(req: Request) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let string_data = req.try_into_string().await?;
|
||||
serde_json::from_str::<Self>(&string_data).map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
serde_json::from_str::<Self>(&string_data)
|
||||
.map_err(|e| ServerFnError::Args(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,6 +60,7 @@ where
|
|||
{
|
||||
async fn from_res(res: Response) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let data = res.try_into_string().await?;
|
||||
serde_json::from_str(&data).map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
serde_json::from_str(&data)
|
||||
.map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,23 +29,32 @@ pub trait FromReq<CustErr, Request, Encoding>
|
|||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn from_req(req: Request) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send;
|
||||
fn from_req(
|
||||
req: Request,
|
||||
) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send;
|
||||
}
|
||||
|
||||
pub trait IntoReq<CustErr, Request, Encoding> {
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>>;
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>>;
|
||||
}
|
||||
|
||||
pub trait FromRes<CustErr, Response, Encoding>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
fn from_res(res: Response)
|
||||
-> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send;
|
||||
fn from_res(
|
||||
res: Response,
|
||||
) -> impl Future<Output = Result<Self, ServerFnError<CustErr>>> + Send;
|
||||
}
|
||||
|
||||
pub trait IntoRes<CustErr, Response, Encoding> {
|
||||
fn into_res(self) -> impl Future<Output = Result<Response, ServerFnError<CustErr>>> + Send;
|
||||
fn into_res(
|
||||
self,
|
||||
) -> impl Future<Output = Result<Response, ServerFnError<CustErr>>> + Send;
|
||||
}
|
||||
|
||||
pub trait Encoding {
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use super::{Encoding, FromReq};
|
||||
use crate::error::ServerFnError;
|
||||
use crate::request::browser::BrowserFormData;
|
||||
use crate::request::{ClientReq, Req};
|
||||
use crate::IntoReq;
|
||||
use crate::{
|
||||
error::ServerFnError,
|
||||
request::{browser::BrowserFormData, ClientReq, Req},
|
||||
IntoReq,
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use multer::Multipart;
|
||||
use web_sys::FormData;
|
||||
|
@ -46,9 +47,17 @@ where
|
|||
Request: ClientReq<CustErr, FormData = BrowserFormData>,
|
||||
T: Into<MultipartData>,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let multi = self.into();
|
||||
Request::try_new_multipart(path, accepts, multi.into_client_data().unwrap())
|
||||
Request::try_new_multipart(
|
||||
path,
|
||||
accepts,
|
||||
multi.into_client_data().unwrap(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,8 +73,10 @@ where
|
|||
.and_then(|ct| multer::parse_boundary(ct).ok())
|
||||
.expect("couldn't parse boundary");
|
||||
let stream = req.try_into_stream()?;
|
||||
let data =
|
||||
multer::Multipart::new(stream.map(|data| data.map_err(|e| e.to_string())), boundary);
|
||||
let data = multer::Multipart::new(
|
||||
stream.map(|data| data.map_err(|e| e.to_string())),
|
||||
boundary,
|
||||
);
|
||||
Ok(MultipartData::Server(data).into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
use std::pin::Pin;
|
||||
|
||||
use super::{Encoding, FromRes};
|
||||
use crate::error::{NoCustomError, ServerFnError};
|
||||
use crate::response::{ClientRes, Res};
|
||||
use crate::IntoRes;
|
||||
use crate::{
|
||||
error::{NoCustomError, ServerFnError},
|
||||
response::{ClientRes, Res},
|
||||
IntoRes,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use futures::{Stream, StreamExt};
|
||||
use std::pin::Pin;
|
||||
|
||||
pub struct Streaming;
|
||||
|
||||
|
@ -38,7 +39,9 @@ pub struct ByteStream<CustErr = NoCustomError>(
|
|||
);
|
||||
|
||||
impl<CustErr> ByteStream<CustErr> {
|
||||
pub fn into_inner(self) -> impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>> + Send {
|
||||
pub fn into_inner(
|
||||
self,
|
||||
) -> impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>> + Send {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +56,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<CustErr, Response> IntoRes<CustErr, Response, Streaming> for ByteStream<CustErr>
|
||||
impl<CustErr, Response> IntoRes<CustErr, Response, Streaming>
|
||||
for ByteStream<CustErr>
|
||||
where
|
||||
Response: Res<CustErr>,
|
||||
CustErr: 'static,
|
||||
|
@ -84,7 +88,9 @@ pub struct TextStream<CustErr = NoCustomError>(
|
|||
);
|
||||
|
||||
impl<CustErr> TextStream<CustErr> {
|
||||
pub fn into_inner(self) -> impl Stream<Item = Result<String, ServerFnError<CustErr>>> + Send {
|
||||
pub fn into_inner(
|
||||
self,
|
||||
) -> impl Stream<Item = Result<String, ServerFnError<CustErr>>> + Send {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
@ -99,7 +105,8 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<CustErr, Response> IntoRes<CustErr, Response, StreamingText> for TextStream<CustErr>
|
||||
impl<CustErr, Response> IntoRes<CustErr, Response, StreamingText>
|
||||
for TextStream<CustErr>
|
||||
where
|
||||
Response: Res<CustErr>,
|
||||
CustErr: 'static,
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use super::{Encoding, FromReq, IntoReq};
|
||||
use crate::error::ServerFnError;
|
||||
use crate::request::{ClientReq, Req};
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
use crate::{
|
||||
error::ServerFnError,
|
||||
request::{ClientReq, Req},
|
||||
};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
/// Pass arguments as a URL-encoded query string of a `GET` request.
|
||||
pub struct GetUrl;
|
||||
|
@ -19,9 +20,13 @@ where
|
|||
Request: ClientReq<CustErr>,
|
||||
T: Serialize + Send,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let data =
|
||||
serde_qs::to_string(&self).map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let data = serde_qs::to_string(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Request::try_new_get(path, accepts, GetUrl::CONTENT_TYPE, &data)
|
||||
}
|
||||
}
|
||||
|
@ -48,9 +53,13 @@ where
|
|||
Request: ClientReq<CustErr>,
|
||||
T: Serialize + Send,
|
||||
{
|
||||
fn into_req(self, path: &str, accepts: &str) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let qs =
|
||||
serde_qs::to_string(&self).map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
fn into_req(
|
||||
self,
|
||||
path: &str,
|
||||
accepts: &str,
|
||||
) -> Result<Request, ServerFnError<CustErr>> {
|
||||
let qs = serde_qs::to_string(&self)
|
||||
.map_err(|e| ServerFnError::Serialization(e.to_string()))?;
|
||||
Request::try_new_post(path, accepts, PostUrl::CONTENT_TYPE, qs)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,19 +13,23 @@ impl<Req, Res> BoxedService<Req, Res> {
|
|||
}
|
||||
|
||||
pub trait Service<Request, Response> {
|
||||
fn run(&mut self, req: Request) -> Pin<Box<dyn Future<Output = Response> + Send>>;
|
||||
fn run(
|
||||
&mut self,
|
||||
req: Request,
|
||||
) -> Pin<Box<dyn Future<Output = Response> + Send>>;
|
||||
}
|
||||
|
||||
#[cfg(feature = "axum")]
|
||||
mod axum {
|
||||
use super::{BoxedService, Service};
|
||||
use crate::{response::Res, ServerFnError};
|
||||
use axum::body::Body;
|
||||
use http::{Request, Response};
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
|
||||
use super::{BoxedService, Service};
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
};
|
||||
|
||||
impl<S> super::Service<Request<Body>, Response<Body>> for S
|
||||
where
|
||||
|
@ -47,11 +51,18 @@ mod axum {
|
|||
}
|
||||
}
|
||||
|
||||
impl tower::Service<Request<Body>> for BoxedService<Request<Body>, Response<Body>> {
|
||||
impl tower::Service<Request<Body>>
|
||||
for BoxedService<Request<Body>, Response<Body>>
|
||||
{
|
||||
type Response = Response<Body>;
|
||||
type Error = ServerFnError;
|
||||
type Future =
|
||||
Pin<Box<dyn std::future::Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
||||
type Future = Pin<
|
||||
Box<
|
||||
dyn std::future::Future<
|
||||
Output = Result<Self::Response, Self::Error>,
|
||||
> + Send,
|
||||
>,
|
||||
>;
|
||||
|
||||
fn poll_ready(
|
||||
&mut self,
|
||||
|
@ -68,7 +79,10 @@ mod axum {
|
|||
|
||||
impl<L> super::Layer<Request<Body>, Response<Body>> for L
|
||||
where
|
||||
L: tower_layer::Layer<BoxedService<Request<Body>, Response<Body>>> + Sync + Send + 'static,
|
||||
L: tower_layer::Layer<BoxedService<Request<Body>, Response<Body>>>
|
||||
+ Sync
|
||||
+ Send
|
||||
+ 'static,
|
||||
L::Service: Service<Request<Body>, Response<Body>> + Send + 'static,
|
||||
{
|
||||
fn layer(
|
||||
|
@ -87,8 +101,11 @@ mod actix {
|
|||
ServerFnError,
|
||||
};
|
||||
use actix_web::{HttpRequest, HttpResponse};
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::{future::Future, pin::Pin};
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
};
|
||||
|
||||
impl<S> super::Service<HttpRequest, HttpResponse> for S
|
||||
where
|
||||
|
@ -96,7 +113,10 @@ mod actix {
|
|||
S::Future: Send + 'static,
|
||||
S::Error: Into<ServerFnError> + Debug + Display + 'static,
|
||||
{
|
||||
fn run(&mut self, req: HttpRequest) -> Pin<Box<dyn Future<Output = HttpResponse> + Send>> {
|
||||
fn run(
|
||||
&mut self,
|
||||
req: HttpRequest,
|
||||
) -> Pin<Box<dyn Future<Output = HttpResponse> + Send>> {
|
||||
let inner = self.call(req);
|
||||
Box::pin(async move {
|
||||
inner.await.unwrap_or_else(|e| {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
static REDIRECT_HOOK: OnceLock<Box<dyn Fn(&str) + Send + Sync>> = OnceLock::new();
|
||||
static REDIRECT_HOOK: OnceLock<Box<dyn Fn(&str) + Send + Sync>> =
|
||||
OnceLock::new();
|
||||
|
||||
pub fn set_redirect_hook(hook: impl Fn(&str) + Send + Sync + 'static) {
|
||||
REDIRECT_HOOK.set(Box::new(hook));
|
||||
|
|
|
@ -33,11 +33,12 @@ impl<CustErr> Req<CustErr> for Request<Body> {
|
|||
|
||||
fn try_into_stream(
|
||||
self,
|
||||
) -> Result<impl Stream<Item = Result<Bytes, ServerFnError>> + Send, ServerFnError<CustErr>>
|
||||
{
|
||||
Ok(self
|
||||
.into_body()
|
||||
.into_data_stream()
|
||||
.map(|chunk| chunk.map_err(|e| ServerFnError::Deserialization(e.to_string()))))
|
||||
) -> Result<
|
||||
impl Stream<Item = Result<Bytes, ServerFnError>> + Send,
|
||||
ServerFnError<CustErr>,
|
||||
> {
|
||||
Ok(self.into_body().into_data_stream().map(|chunk| {
|
||||
chunk.map_err(|e| ServerFnError::Deserialization(e.to_string()))
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::error::ServerFnError;
|
||||
|
||||
use super::ClientReq;
|
||||
use crate::error::ServerFnError;
|
||||
use bytes::Bytes;
|
||||
pub use gloo_net::http::Request;
|
||||
use js_sys::Uint8Array;
|
||||
|
|
|
@ -59,16 +59,22 @@ where
|
|||
fn to_content_type(&self) -> Option<String>;
|
||||
|
||||
/// Attempts to extract the body of the request into [`Bytes`].
|
||||
fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send;
|
||||
fn try_into_bytes(
|
||||
self,
|
||||
) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send;
|
||||
|
||||
/// Attempts to convert the body of the request into a string.
|
||||
fn try_into_string(self)
|
||||
-> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send;
|
||||
fn try_into_string(
|
||||
self,
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send;
|
||||
|
||||
/// Attempts to convert the body of the request into a string.
|
||||
fn try_into_stream(
|
||||
self,
|
||||
) -> Result<impl Stream<Item = Result<Bytes, ServerFnError>> + Send, ServerFnError<CustErr>>;
|
||||
) -> Result<
|
||||
impl Stream<Item = Result<Bytes, ServerFnError>> + Send,
|
||||
ServerFnError<CustErr>,
|
||||
>;
|
||||
}
|
||||
|
||||
/// A mocked request type that can be used in place of the actual server request,
|
||||
|
@ -84,20 +90,26 @@ impl<CustErr> Req<CustErr> for BrowserMockReq {
|
|||
unreachable!()
|
||||
}
|
||||
|
||||
fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send {
|
||||
fn try_into_bytes(
|
||||
self,
|
||||
) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send
|
||||
{
|
||||
async { unreachable!() }
|
||||
}
|
||||
|
||||
fn try_into_string(
|
||||
self,
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send {
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send
|
||||
{
|
||||
async { unreachable!() }
|
||||
}
|
||||
|
||||
fn try_into_stream(
|
||||
self,
|
||||
) -> Result<impl Stream<Item = Result<Bytes, ServerFnError>> + Send, ServerFnError<CustErr>>
|
||||
{
|
||||
) -> Result<
|
||||
impl Stream<Item = Result<Bytes, ServerFnError>> + Send,
|
||||
ServerFnError<CustErr>,
|
||||
> {
|
||||
Ok(futures::stream::once(async { unreachable!() }))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
use std::sync::OnceLock;
|
||||
|
||||
use crate::error::ServerFnError;
|
||||
|
||||
use super::ClientReq;
|
||||
use crate::error::ServerFnError;
|
||||
use bytes::Bytes;
|
||||
use once_cell::sync::Lazy;
|
||||
use reqwest::header::{ACCEPT, CONTENT_TYPE};
|
||||
pub use reqwest::{multipart::Form, Client, Method, Request, Url};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
pub(crate) static CLIENT: Lazy<Client> = Lazy::new(Client::new);
|
||||
static ROOT_URL: OnceLock<&'static str> = OnceLock::new();
|
||||
|
@ -34,8 +32,8 @@ impl<CustErr> ClientReq<CustErr> for Request {
|
|||
query: &str,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let url = format!("{}{}", get_server_url(), path);
|
||||
let mut url =
|
||||
Url::try_from(url.as_str()).map_err(|e| ServerFnError::Request(e.to_string()))?;
|
||||
let mut url = Url::try_from(url.as_str())
|
||||
.map_err(|e| ServerFnError::Request(e.to_string()))?;
|
||||
url.set_query(Some(query));
|
||||
let req = CLIENT
|
||||
.get(url)
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
use super::Res;
|
||||
use crate::error::ServerFnError;
|
||||
use actix_web::{http::header, http::StatusCode, HttpResponse};
|
||||
use actix_web::{
|
||||
http::{header, StatusCode},
|
||||
HttpResponse,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use futures::Stream;
|
||||
use send_wrapper::SendWrapper;
|
||||
|
@ -18,7 +21,10 @@ impl<CustErr> Res<CustErr> for ActixResponse
|
|||
where
|
||||
CustErr: Display,
|
||||
{
|
||||
fn try_from_string(content_type: &str, data: String) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_string(
|
||||
content_type: &str,
|
||||
data: String,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let mut builder = HttpResponse::build(StatusCode::OK);
|
||||
Ok(ActixResponse(SendWrapper::new(
|
||||
builder
|
||||
|
@ -27,7 +33,10 @@ where
|
|||
)))
|
||||
}
|
||||
|
||||
fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_bytes(
|
||||
content_type: &str,
|
||||
data: Bytes,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let mut builder = HttpResponse::build(StatusCode::OK);
|
||||
Ok(ActixResponse(SendWrapper::new(
|
||||
builder
|
||||
|
@ -38,7 +47,8 @@ where
|
|||
|
||||
fn error_response(err: ServerFnError<CustErr>) -> Self {
|
||||
ActixResponse(SendWrapper::new(
|
||||
HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR).body(err.to_string()),
|
||||
HttpResponse::build(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(err.to_string()),
|
||||
))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::error::ServerFnError;
|
||||
|
||||
use super::ClientRes;
|
||||
use crate::error::ServerFnError;
|
||||
use bytes::Bytes;
|
||||
use futures::{Stream, StreamExt};
|
||||
pub use gloo_net::http::Response;
|
||||
|
@ -14,7 +13,8 @@ pub struct BrowserResponse(pub(crate) SendWrapper<Response>);
|
|||
impl<CustErr> ClientRes<CustErr> for BrowserResponse {
|
||||
fn try_into_string(
|
||||
self,
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send {
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send
|
||||
{
|
||||
// the browser won't send this async work between threads (because it's single-threaded)
|
||||
// so we can safely wrap this
|
||||
SendWrapper::new(async move {
|
||||
|
@ -25,7 +25,10 @@ impl<CustErr> ClientRes<CustErr> for BrowserResponse {
|
|||
})
|
||||
}
|
||||
|
||||
fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send {
|
||||
fn try_into_bytes(
|
||||
self,
|
||||
) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send
|
||||
{
|
||||
// the browser won't send this async work between threads (because it's single-threaded)
|
||||
// so we can safely wrap this
|
||||
SendWrapper::new(async move {
|
||||
|
|
|
@ -10,7 +10,10 @@ impl<CustErr> Res<CustErr> for Response<Body>
|
|||
where
|
||||
CustErr: Send + Sync + Debug + Display + 'static,
|
||||
{
|
||||
fn try_from_string(content_type: &str, data: String) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_string(
|
||||
content_type: &str,
|
||||
data: String,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let builder = http::Response::builder();
|
||||
builder
|
||||
.status(200)
|
||||
|
@ -19,7 +22,10 @@ where
|
|||
.map_err(|e| ServerFnError::Response(e.to_string()))
|
||||
}
|
||||
|
||||
fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_bytes(
|
||||
content_type: &str,
|
||||
data: Bytes,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let builder = http::Response::builder();
|
||||
builder
|
||||
.status(200)
|
||||
|
@ -30,9 +36,12 @@ where
|
|||
|
||||
fn try_from_stream(
|
||||
content_type: &str,
|
||||
data: impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>> + Send + 'static,
|
||||
data: impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>>
|
||||
+ Send
|
||||
+ 'static,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let body = Body::from_stream(data.map(|n| n.map_err(ServerFnErrorErr::from)));
|
||||
let body =
|
||||
Body::from_stream(data.map(|n| n.map_err(ServerFnErrorErr::from)));
|
||||
let builder = http::Response::builder();
|
||||
builder
|
||||
.status(200)
|
||||
|
|
|
@ -18,15 +18,23 @@ where
|
|||
Self: Sized,
|
||||
{
|
||||
/// Attempts to convert a UTF-8 string into an HTTP response.
|
||||
fn try_from_string(content_type: &str, data: String) -> Result<Self, ServerFnError<CustErr>>;
|
||||
fn try_from_string(
|
||||
content_type: &str,
|
||||
data: String,
|
||||
) -> Result<Self, ServerFnError<CustErr>>;
|
||||
|
||||
/// Attempts to convert a binary blob represented as bytes into an HTTP response.
|
||||
fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, ServerFnError<CustErr>>;
|
||||
fn try_from_bytes(
|
||||
content_type: &str,
|
||||
data: Bytes,
|
||||
) -> Result<Self, ServerFnError<CustErr>>;
|
||||
|
||||
/// Attempts to convert a stream of bytes into an HTTP response.
|
||||
fn try_from_stream(
|
||||
content_type: &str,
|
||||
data: impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>> + Send + 'static,
|
||||
data: impl Stream<Item = Result<Bytes, ServerFnError<CustErr>>>
|
||||
+ Send
|
||||
+ 'static,
|
||||
) -> Result<Self, ServerFnError<CustErr>>;
|
||||
|
||||
fn error_response(err: ServerFnError<CustErr>) -> Self;
|
||||
|
@ -35,11 +43,14 @@ where
|
|||
/// Represents the response as received by the client.
|
||||
pub trait ClientRes<CustErr> {
|
||||
/// Attempts to extract a UTF-8 string from an HTTP response.
|
||||
fn try_into_string(self)
|
||||
-> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send;
|
||||
fn try_into_string(
|
||||
self,
|
||||
) -> impl Future<Output = Result<String, ServerFnError<CustErr>>> + Send;
|
||||
|
||||
/// Attempts to extract a binary blob from an HTTP response.
|
||||
fn try_into_bytes(self) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send;
|
||||
fn try_into_bytes(
|
||||
self,
|
||||
) -> impl Future<Output = Result<Bytes, ServerFnError<CustErr>>> + Send;
|
||||
|
||||
/// Attempts to extract a binary stream from an HTTP response.
|
||||
fn try_into_stream(
|
||||
|
@ -64,11 +75,17 @@ pub trait ClientRes<CustErr> {
|
|||
pub struct BrowserMockRes;
|
||||
|
||||
impl<CustErr> Res<CustErr> for BrowserMockRes {
|
||||
fn try_from_string(content_type: &str, data: String) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_string(
|
||||
content_type: &str,
|
||||
data: String,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
fn try_from_bytes(content_type: &str, data: Bytes) -> Result<Self, ServerFnError<CustErr>> {
|
||||
fn try_from_bytes(
|
||||
content_type: &str,
|
||||
data: Bytes,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue