From d7755eb5cce3f0b59f6344229995a41637cc9333 Mon Sep 17 00:00:00 2001 From: Greg Johnston Date: Sat, 13 Jan 2024 15:17:23 -0500 Subject: [PATCH] support setting server URL on either platform --- server_fn/src/client.rs | 16 +++++++++++++++- server_fn/src/request/browser.rs | 27 ++++++++++++++++++++++----- server_fn/src/request/reqwest.rs | 17 +---------------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/server_fn/src/client.rs b/server_fn/src/client.rs index fdfc85358f..84538bb333 100644 --- a/server_fn/src/client.rs +++ b/server_fn/src/client.rs @@ -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. diff --git a/server_fn/src/request/browser.rs b/server_fn/src/request/browser.rs index c1ce0f89f9..b2af59d7fc 100644 --- a/server_fn/src/request/browser.rs +++ b/server_fn/src/request/browser.rs @@ -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 ClientReq for BrowserRequest { content_type: &str, query: &str, ) -> Result> { - 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 ClientReq for BrowserRequest { content_type: &str, body: String, ) -> Result> { + 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 ClientReq for BrowserRequest { content_type: &str, body: Bytes, ) -> Result> { + 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 ClientReq for BrowserRequest { accepts: &str, body: Self::FormData, ) -> Result> { + 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()))?, diff --git a/server_fn/src/request/reqwest.rs b/server_fn/src/request/reqwest.rs index 5e63956554..bfff453f60 100644 --- a/server_fn/src/request/reqwest.rs +++ b/server_fn/src/request/reqwest.rs @@ -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 = 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 ClientReq for Request { type FormData = Form;