mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-10 14:54:16 +00:00
support setting server URL on either platform
This commit is contained in:
parent
5e08253521
commit
88fee243a8
3 changed files with 38 additions and 22 deletions
|
@ -1,5 +1,19 @@
|
|||
use crate::{error::ServerFnError, request::ClientReq, response::ClientRes};
|
||||
use std::future::Future;
|
||||
use std::{future::Future, sync::OnceLock};
|
||||
|
||||
static ROOT_URL: OnceLock<&'static str> = OnceLock::new();
|
||||
|
||||
/// Set the root server URL that all server function paths are relative to for the client.
|
||||
///
|
||||
/// If this is not set, it defaults to the origin.
|
||||
pub fn set_server_url(url: &'static str) {
|
||||
ROOT_URL.set(url).unwrap();
|
||||
}
|
||||
|
||||
/// Returns the root server URL for all server functions.
|
||||
pub fn get_server_url() -> &'static str {
|
||||
ROOT_URL.get().copied().unwrap_or("")
|
||||
}
|
||||
|
||||
/// A client defines a pair of request/response types and the logic to send
|
||||
/// and receive them.
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use super::ClientReq;
|
||||
use crate::error::ServerFnError;
|
||||
use crate::{client::get_server_url, error::ServerFnError};
|
||||
use bytes::Bytes;
|
||||
pub use gloo_net::http::Request;
|
||||
use js_sys::Uint8Array;
|
||||
|
@ -35,7 +35,12 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
|
|||
content_type: &str,
|
||||
query: &str,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let mut url = path.to_owned();
|
||||
let server_url = get_server_url();
|
||||
let mut url = String::with_capacity(
|
||||
server_url.len() + path.len() + 1 + query.len(),
|
||||
);
|
||||
url.push_str(server_url);
|
||||
url.push_str(path);
|
||||
url.push('?');
|
||||
url.push_str(query);
|
||||
Ok(Self(SendWrapper::new(
|
||||
|
@ -53,8 +58,12 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
|
|||
content_type: &str,
|
||||
body: String,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let server_url = get_server_url();
|
||||
let mut url = String::with_capacity(server_url.len() + path.len());
|
||||
url.push_str(server_url);
|
||||
url.push_str(path);
|
||||
Ok(Self(SendWrapper::new(
|
||||
Request::post(path)
|
||||
Request::post(&url)
|
||||
.header("Content-Type", content_type)
|
||||
.header("Accept", accepts)
|
||||
.body(body)
|
||||
|
@ -68,10 +77,14 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
|
|||
content_type: &str,
|
||||
body: Bytes,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let server_url = get_server_url();
|
||||
let mut url = String::with_capacity(server_url.len() + path.len());
|
||||
url.push_str(server_url);
|
||||
url.push_str(path);
|
||||
let body: &[u8] = &body;
|
||||
let body = Uint8Array::from(body).buffer();
|
||||
Ok(Self(SendWrapper::new(
|
||||
Request::post(path)
|
||||
Request::post(&url)
|
||||
.header("Content-Type", content_type)
|
||||
.header("Accept", accepts)
|
||||
.body(body)
|
||||
|
@ -84,8 +97,12 @@ impl<CustErr> ClientReq<CustErr> for BrowserRequest {
|
|||
accepts: &str,
|
||||
body: Self::FormData,
|
||||
) -> Result<Self, ServerFnError<CustErr>> {
|
||||
let server_url = get_server_url();
|
||||
let mut url = String::with_capacity(server_url.len() + path.len());
|
||||
url.push_str(server_url);
|
||||
url.push_str(path);
|
||||
Ok(Self(SendWrapper::new(
|
||||
Request::post(path)
|
||||
Request::post(&url)
|
||||
.header("Accept", accepts)
|
||||
.body(body.0.take())
|
||||
.map_err(|e| ServerFnError::Request(e.to_string()))?,
|
||||
|
|
|
@ -1,26 +1,11 @@
|
|||
use super::ClientReq;
|
||||
use crate::error::ServerFnError;
|
||||
use crate::{client::get_server_url, 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();
|
||||
|
||||
/// Set the root server url that all server function paths are relative to for the client.
|
||||
///
|
||||
/// If this is not set, it defaults to the origin.
|
||||
pub fn set_server_url(url: &'static str) {
|
||||
ROOT_URL.set(url).unwrap();
|
||||
}
|
||||
|
||||
fn get_server_url() -> &'static str {
|
||||
ROOT_URL
|
||||
.get()
|
||||
.expect("Call `set_root_url` before calling a server function.")
|
||||
}
|
||||
|
||||
impl<CustErr> ClientReq<CustErr> for Request {
|
||||
type FormData = Form;
|
||||
|
|
Loading…
Reference in a new issue