supabase_client_rs/
config.rs

1//! Configuration types for the Supabase client.
2
3use std::time::Duration;
4
5/// Configuration options for the Supabase client.
6#[derive(Debug, Clone)]
7pub struct SupabaseConfig {
8    /// The Supabase project URL (e.g., `https://xyzcompany.supabase.co`)
9    pub url: String,
10
11    /// The Supabase API key (anon key or service role key)
12    pub api_key: String,
13
14    /// Optional JWT for authenticated requests
15    pub jwt: Option<String>,
16
17    /// Custom schema (default: "public")
18    pub schema: String,
19
20    /// Request timeout
21    pub timeout: Duration,
22
23    /// Custom headers to include in all requests
24    pub headers: Vec<(String, String)>,
25
26    /// Auto-refresh token settings
27    pub auto_refresh_token: bool,
28
29    /// Persist session
30    pub persist_session: bool,
31}
32
33impl SupabaseConfig {
34    /// Create a new configuration with the given URL and API key.
35    pub fn new(url: impl Into<String>, api_key: impl Into<String>) -> Self {
36        Self {
37            url: url.into(),
38            api_key: api_key.into(),
39            jwt: None,
40            schema: "public".to_string(),
41            timeout: Duration::from_secs(30),
42            headers: Vec::new(),
43            auto_refresh_token: true,
44            persist_session: true,
45        }
46    }
47
48    /// Set a custom schema.
49    pub fn schema(mut self, schema: impl Into<String>) -> Self {
50        self.schema = schema.into();
51        self
52    }
53
54    /// Set the request timeout.
55    pub fn timeout(mut self, timeout: Duration) -> Self {
56        self.timeout = timeout;
57        self
58    }
59
60    /// Set a JWT for authenticated requests.
61    pub fn jwt(mut self, jwt: impl Into<String>) -> Self {
62        self.jwt = Some(jwt.into());
63        self
64    }
65
66    /// Add a custom header.
67    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
68        self.headers.push((key.into(), value.into()));
69        self
70    }
71
72    /// Disable auto-refresh token.
73    pub fn auto_refresh_token(mut self, enabled: bool) -> Self {
74        self.auto_refresh_token = enabled;
75        self
76    }
77
78    /// Disable session persistence.
79    pub fn persist_session(mut self, enabled: bool) -> Self {
80        self.persist_session = enabled;
81        self
82    }
83
84    /// Get the REST API URL.
85    pub fn rest_url(&self) -> String {
86        format!("{}/rest/v1", self.url.trim_end_matches('/'))
87    }
88
89    /// Get the Auth API URL.
90    pub fn auth_url(&self) -> String {
91        format!("{}/auth/v1", self.url.trim_end_matches('/'))
92    }
93
94    /// Get the Storage API URL.
95    pub fn storage_url(&self) -> String {
96        format!("{}/storage/v1", self.url.trim_end_matches('/'))
97    }
98
99    /// Get the Realtime URL.
100    pub fn realtime_url(&self) -> String {
101        let base = self.url.trim_end_matches('/');
102        let ws_url = base
103            .replace("https://", "wss://")
104            .replace("http://", "ws://");
105        format!("{}/realtime/v1", ws_url)
106    }
107
108    /// Get the Functions URL.
109    pub fn functions_url(&self) -> String {
110        format!("{}/functions/v1", self.url.trim_end_matches('/'))
111    }
112}