Skip to main content

supabase_client_storage/
lib.rs

1//! Supabase Storage HTTP client.
2//!
3//! This crate provides an HTTP client for the Supabase Storage API.
4//! It communicates with Storage REST endpoints at `/storage/v1/...`.
5//!
6//! # Usage
7//!
8//! ```ignore
9//! use supabase_client_sdk::prelude::*;
10//!
11//! let client = SupabaseClient::new(config).await?;
12//! let storage = client.storage()?;
13//!
14//! // Bucket operations
15//! let buckets = storage.list_buckets().await?;
16//! storage.create_bucket("photos", BucketOptions::new().public(true)).await?;
17//!
18//! // File operations
19//! let file_api = storage.from("photos");
20//! file_api.upload("photo.png", data, FileOptions::new().content_type("image/png")).await?;
21//! let bytes = file_api.download("photo.png").await?;
22//! ```
23
24pub mod bucket_api;
25pub mod client;
26pub mod error;
27pub mod types;
28
29// Re-exports for convenient access
30pub use bucket_api::StorageBucketApi;
31pub use client::StorageClient;
32pub use error::{StorageApiErrorResponse, StorageError};
33pub use types::*;
34
35use supabase_client_core::SupabaseClient;
36
37/// Extension trait to create a [`StorageClient`] from a [`SupabaseClient`].
38///
39/// # Example
40/// ```ignore
41/// use supabase_client_sdk::prelude::*;
42/// use supabase_client_storage::SupabaseClientStorageExt;
43///
44/// let client = SupabaseClient::new(config).await?;
45/// let storage = client.storage()?;
46/// let buckets = storage.list_buckets().await?;
47/// ```
48pub trait SupabaseClientStorageExt {
49    /// Create a [`StorageClient`] from the client's configuration.
50    ///
51    /// Requires `supabase_url` and `supabase_key` to be set in the config.
52    fn storage(&self) -> Result<StorageClient, StorageError>;
53}
54
55impl SupabaseClientStorageExt for SupabaseClient {
56    fn storage(&self) -> Result<StorageClient, StorageError> {
57        StorageClient::new(self.supabase_url(), self.api_key())
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use supabase_client_core::config::SupabaseConfig;
65
66    #[test]
67    fn test_storage_extension_trait() {
68        let config = SupabaseConfig::new("http://localhost:54321", "test-key");
69        let client = SupabaseClient::new(config).unwrap();
70        let storage = client.storage();
71        assert!(storage.is_ok());
72        let storage = storage.unwrap();
73        assert_eq!(storage.base_url().path(), "/storage/v1");
74    }
75}