Skip to main content

supabase_client_functions/
lib.rs

1//! Supabase Edge Functions HTTP client.
2//!
3//! This crate provides an HTTP client for invoking Supabase Edge Functions
4//! deployed at `/functions/v1/{function_name}`.
5//!
6//! # Usage
7//!
8//! ```ignore
9//! use supabase_client_sdk::prelude::*;
10//! use serde_json::json;
11//!
12//! let client = SupabaseClient::new(config).await?;
13//! let functions = client.functions()?;
14//!
15//! let response = functions.invoke("hello", InvokeOptions::new()
16//!     .body(json!({"name": "World"}))
17//! ).await?;
18//! let data: serde_json::Value = response.json()?;
19//! ```
20
21pub mod client;
22pub mod error;
23pub mod types;
24
25// Re-exports for convenient access
26pub use client::FunctionsClient;
27pub use error::{FunctionsApiErrorResponse, FunctionsError};
28pub use types::*;
29
30use supabase_client_core::SupabaseClient;
31
32/// Extension trait to create a [`FunctionsClient`] from a [`SupabaseClient`].
33///
34/// # Example
35/// ```ignore
36/// use supabase_client_sdk::prelude::*;
37/// use supabase_client_functions::SupabaseClientFunctionsExt;
38///
39/// let client = SupabaseClient::new(config).await?;
40/// let functions = client.functions()?;
41/// let response = functions.invoke("hello", InvokeOptions::new()).await?;
42/// ```
43pub trait SupabaseClientFunctionsExt {
44    /// Create a [`FunctionsClient`] from the client's configuration.
45    ///
46    /// Requires `supabase_url` and `supabase_key` to be set in the config.
47    fn functions(&self) -> Result<FunctionsClient, FunctionsError>;
48}
49
50impl SupabaseClientFunctionsExt for SupabaseClient {
51    fn functions(&self) -> Result<FunctionsClient, FunctionsError> {
52        FunctionsClient::new(self.supabase_url(), self.api_key())
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use supabase_client_core::config::SupabaseConfig;
60
61    #[test]
62    fn test_functions_extension_trait() {
63        let config = SupabaseConfig::new("http://localhost:54321", "test-key");
64        let client = SupabaseClient::new(config).unwrap();
65        let functions = client.functions();
66        assert!(functions.is_ok());
67        let functions = functions.unwrap();
68        assert_eq!(functions.base_url().path(), "/functions/v1");
69        assert_eq!(functions.api_key(), "test-key");
70    }
71}