supabase/
types.rs

1//! Common types and data structures for Supabase operations
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use uuid::Uuid;
7
8/// Configuration for Supabase client
9#[derive(Debug, Clone)]
10pub struct SupabaseConfig {
11    /// Project URL (e.g., https://your-project.supabase.co)
12    pub url: String,
13    /// API key (anon key for client-side operations)
14    pub key: String,
15    /// HTTP client configuration
16    pub http_config: HttpConfig,
17    /// Auth configuration
18    pub auth_config: AuthConfig,
19    /// Database configuration
20    pub database_config: DatabaseConfig,
21    /// Storage configuration
22    pub storage_config: StorageConfig,
23}
24
25/// HTTP client configuration
26#[derive(Debug, Clone)]
27pub struct HttpConfig {
28    /// Request timeout in seconds
29    pub timeout: u64,
30    /// Connection timeout in seconds
31    pub connect_timeout: u64,
32    /// Maximum number of redirects to follow
33    pub max_redirects: usize,
34    /// Custom headers to include in all requests
35    pub default_headers: HashMap<String, String>,
36}
37
38impl Default for HttpConfig {
39    fn default() -> Self {
40        Self {
41            timeout: 60,
42            connect_timeout: 10,
43            max_redirects: 10,
44            default_headers: HashMap::new(),
45        }
46    }
47}
48
49/// Authentication configuration
50#[derive(Debug, Clone)]
51pub struct AuthConfig {
52    /// Auto-refresh tokens before expiry
53    pub auto_refresh_token: bool,
54    /// Token refresh threshold in seconds before expiry
55    pub refresh_threshold: u64,
56    /// Persist session in storage
57    pub persist_session: bool,
58    /// Custom storage implementation
59    pub storage_key: String,
60}
61
62impl Default for AuthConfig {
63    fn default() -> Self {
64        Self {
65            auto_refresh_token: true,
66            refresh_threshold: 300, // 5 minutes
67            persist_session: true,
68            storage_key: "supabase.auth.token".to_string(),
69        }
70    }
71}
72
73/// Database configuration
74#[derive(Debug, Clone)]
75pub struct DatabaseConfig {
76    /// Default schema to use
77    pub schema: String,
78    /// Maximum number of retry attempts
79    pub max_retries: u32,
80    /// Retry delay in milliseconds
81    pub retry_delay: u64,
82}
83
84impl Default for DatabaseConfig {
85    fn default() -> Self {
86        Self {
87            schema: "public".to_string(),
88            max_retries: 3,
89            retry_delay: 1000,
90        }
91    }
92}
93
94/// Storage configuration
95#[derive(Debug, Clone)]
96pub struct StorageConfig {
97    /// Default bucket for operations
98    pub default_bucket: Option<String>,
99    /// File upload timeout in seconds
100    pub upload_timeout: u64,
101    /// Maximum file size in bytes
102    pub max_file_size: u64,
103}
104
105impl Default for StorageConfig {
106    fn default() -> Self {
107        Self {
108            default_bucket: None,
109            upload_timeout: 300,             // 5 minutes
110            max_file_size: 50 * 1024 * 1024, // 50MB
111        }
112    }
113}
114
115/// Generic response wrapper
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct SupabaseResponse<T> {
118    pub data: Option<T>,
119    pub error: Option<SupabaseError>,
120}
121
122/// Supabase API error response
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct SupabaseError {
125    pub message: String,
126    pub details: Option<String>,
127    pub hint: Option<String>,
128    pub code: Option<String>,
129}
130
131/// Pagination information
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct Pagination {
134    /// Current page number (0-based)
135    pub page: u32,
136    /// Number of items per page
137    pub per_page: u32,
138    /// Total number of items
139    pub total: Option<u64>,
140    /// Whether there are more pages
141    pub has_more: Option<bool>,
142}
143
144/// Query filter operations
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub enum FilterOperator {
147    #[serde(rename = "eq")]
148    Equal,
149    #[serde(rename = "neq")]
150    NotEqual,
151    #[serde(rename = "gt")]
152    GreaterThan,
153    #[serde(rename = "gte")]
154    GreaterThanOrEqual,
155    #[serde(rename = "lt")]
156    LessThan,
157    #[serde(rename = "lte")]
158    LessThanOrEqual,
159    #[serde(rename = "like")]
160    Like,
161    #[serde(rename = "ilike")]
162    ILike,
163    #[serde(rename = "is")]
164    Is,
165    #[serde(rename = "in")]
166    In,
167    #[serde(rename = "cs")]
168    Contains,
169    #[serde(rename = "cd")]
170    ContainedBy,
171    #[serde(rename = "sl")]
172    StrictlyLeft,
173    #[serde(rename = "sr")]
174    StrictlyRight,
175    #[serde(rename = "nxr")]
176    NotExtendToRight,
177    #[serde(rename = "nxl")]
178    NotExtendToLeft,
179    #[serde(rename = "adj")]
180    Adjacent,
181}
182
183/// Order direction for sorting
184#[derive(Debug, Clone, Serialize, Deserialize)]
185pub enum OrderDirection {
186    #[serde(rename = "asc")]
187    Ascending,
188    #[serde(rename = "desc")]
189    Descending,
190}
191
192/// HTTP method types
193#[derive(Debug, Clone, Copy)]
194pub enum HttpMethod {
195    Get,
196    Post,
197    Put,
198    Patch,
199    Delete,
200    Head,
201    Options,
202}
203
204impl HttpMethod {
205    pub fn as_str(&self) -> &'static str {
206        match self {
207            HttpMethod::Get => "GET",
208            HttpMethod::Post => "POST",
209            HttpMethod::Put => "PUT",
210            HttpMethod::Patch => "PATCH",
211            HttpMethod::Delete => "DELETE",
212            HttpMethod::Head => "HEAD",
213            HttpMethod::Options => "OPTIONS",
214        }
215    }
216}
217
218/// Generic timestamp type
219pub type Timestamp = DateTime<Utc>;
220
221/// Generic ID type
222pub type Id = Uuid;
223
224/// JSON value type for dynamic data
225pub type JsonValue = serde_json::Value;
226
227/// Headers type for HTTP requests
228pub type Headers = HashMap<String, String>;
229
230/// Query parameters type for HTTP requests
231pub type QueryParams = HashMap<String, String>;