supabase_client_rs/
traits.rs

1//! Traits for extensibility and future community crates.
2//!
3//! These traits define the interfaces that auth, storage, and functions
4//! providers must implement. This allows the community to create their
5//! own implementations while maintaining compatibility with the main client.
6
7use crate::error::Result;
8use serde::{Serialize, de::DeserializeOwned};
9
10// Re-export async_trait for implementors
11pub use async_trait::async_trait;
12
13/// Authentication provider trait.
14///
15/// Implement this trait to provide authentication functionality.
16/// The community can create crates like `supabase-auth-rs` that implement this.
17#[async_trait]
18pub trait AuthProvider: Send + Sync {
19    /// The user type returned by auth operations.
20    type User: DeserializeOwned + Send;
21
22    /// The session type.
23    type Session: DeserializeOwned + Send;
24
25    /// Sign up with email and password.
26    async fn sign_up_with_email(&self, email: &str, password: &str) -> Result<Self::Session>;
27
28    /// Sign in with email and password.
29    async fn sign_in_with_email(&self, email: &str, password: &str) -> Result<Self::Session>;
30
31    /// Sign out the current user.
32    async fn sign_out(&self) -> Result<()>;
33
34    /// Get the current session.
35    async fn get_session(&self) -> Result<Option<Self::Session>>;
36
37    /// Get the current user.
38    async fn get_user(&self) -> Result<Option<Self::User>>;
39
40    /// Refresh the session token.
41    async fn refresh_session(&self) -> Result<Self::Session>;
42}
43
44/// Storage provider trait.
45///
46/// Implement this trait to provide file storage functionality.
47#[async_trait]
48pub trait StorageProvider: Send + Sync {
49    /// Upload a file to a bucket.
50    async fn upload(
51        &self,
52        bucket: &str,
53        path: &str,
54        data: Vec<u8>,
55        content_type: Option<&str>,
56    ) -> Result<String>;
57
58    /// Download a file from a bucket.
59    async fn download(&self, bucket: &str, path: &str) -> Result<Vec<u8>>;
60
61    /// Delete a file from a bucket.
62    async fn remove(&self, bucket: &str, paths: &[&str]) -> Result<()>;
63
64    /// List files in a bucket path.
65    async fn list(&self, bucket: &str, path: Option<&str>) -> Result<Vec<StorageObject>>;
66
67    /// Get a public URL for a file.
68    fn get_public_url(&self, bucket: &str, path: &str) -> String;
69
70    /// Create a signed URL for temporary access.
71    async fn create_signed_url(&self, bucket: &str, path: &str, expires_in: u64) -> Result<String>;
72}
73
74/// A storage object (file or folder).
75#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
76pub struct StorageObject {
77    /// The name of the file or folder
78    pub name: String,
79    /// Optional unique identifier
80    pub id: Option<String>,
81    /// Timestamp of last update
82    pub updated_at: Option<String>,
83    /// Timestamp of creation
84    pub created_at: Option<String>,
85    /// Timestamp of last access
86    pub last_accessed_at: Option<String>,
87    /// Optional metadata as JSON
88    pub metadata: Option<serde_json::Value>,
89}
90
91/// Edge Functions provider trait.
92#[async_trait]
93pub trait FunctionsProvider: Send + Sync {
94    /// Invoke an edge function.
95    async fn invoke<T, R>(&self, function_name: &str, body: Option<T>) -> Result<R>
96    where
97        T: Serialize + Send + Sync,
98        R: DeserializeOwned;
99
100    /// Invoke an edge function and return raw bytes.
101    async fn invoke_raw<T>(&self, function_name: &str, body: Option<T>) -> Result<Vec<u8>>
102    where
103        T: Serialize + Send + Sync;
104}