use std::time::Duration;
#[derive(Debug, Clone)]
pub struct SupabaseConfig {
pub url: String,
pub api_key: String,
pub jwt: Option<String>,
pub schema: String,
pub timeout: Duration,
pub headers: Vec<(String, String)>,
pub auto_refresh_token: bool,
pub persist_session: bool,
}
impl SupabaseConfig {
pub fn new(url: impl Into<String>, api_key: impl Into<String>) -> Self {
Self {
url: url.into(),
api_key: api_key.into(),
jwt: None,
schema: "public".to_string(),
timeout: Duration::from_secs(30),
headers: Vec::new(),
auto_refresh_token: true,
persist_session: true,
}
}
pub fn schema(mut self, schema: impl Into<String>) -> Self {
self.schema = schema.into();
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn jwt(mut self, jwt: impl Into<String>) -> Self {
self.jwt = Some(jwt.into());
self
}
pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((key.into(), value.into()));
self
}
pub fn auto_refresh_token(mut self, enabled: bool) -> Self {
self.auto_refresh_token = enabled;
self
}
pub fn persist_session(mut self, enabled: bool) -> Self {
self.persist_session = enabled;
self
}
pub fn rest_url(&self) -> String {
format!("{}/rest/v1", self.url.trim_end_matches('/'))
}
pub fn auth_url(&self) -> String {
format!("{}/auth/v1", self.url.trim_end_matches('/'))
}
pub fn storage_url(&self) -> String {
format!("{}/storage/v1", self.url.trim_end_matches('/'))
}
pub fn realtime_url(&self) -> String {
let base = self.url.trim_end_matches('/');
let ws_url = base
.replace("https://", "wss://")
.replace("http://", "ws://");
format!("{}/realtime/v1", ws_url)
}
pub fn functions_url(&self) -> String {
format!("{}/functions/v1", self.url.trim_end_matches('/'))
}
}