11import Foundation
2+ import Functions
23import GoTrue
34import PostgREST
45import Realtime
@@ -13,6 +14,7 @@ public class SupabaseClient {
1314 private let realtimeURL : URL
1415 private let authURL : URL
1516 private let storageURL : URL
17+ private let functionsURL : URL
1618
1719 /// Supabase Auth allows you to create and manage user sessions for access to data that is secured
1820 /// by access policies.
@@ -40,6 +42,17 @@ public class SupabaseClient {
4042 /// Realtime client for Supabase
4143 public var realtime : RealtimeClient
4244
45+ /// Supabase Functions allows you to deploy and invoke edge functions.
46+ public var functions : FunctionsClient {
47+ var headers : [ String : String ] = defaultHeaders
48+ headers [ " Authorization " ] = " Bearer \( auth. session? . accessToken ?? supabaseKey) "
49+ return FunctionsClient (
50+ url: functionsURL,
51+ headers: headers,
52+ http: self
53+ )
54+ }
55+
4356 private var defaultHeaders : [ String : String ]
4457
4558 /// Init `Supabase` with the provided parameters.
@@ -63,6 +76,7 @@ public class SupabaseClient {
6376 realtimeURL = supabaseURL. appendingPathComponent ( " /realtime/v1 " )
6477 authURL = supabaseURL. appendingPathComponent ( " /auth/v1 " )
6578 storageURL = supabaseURL. appendingPathComponent ( " /storage/v1 " )
79+ functionsURL = supabaseURL. appendingPathComponent ( " /functions/v1 " )
6680
6781 defaultHeaders = [
6882 " X-Client-Info " : " supabase-swift/ \( version) " ,
@@ -77,12 +91,18 @@ public class SupabaseClient {
7791 }
7892
7993 public struct HTTPClient {
80- public let storage : StorageHTTPClient
81- public let postgrest : PostgrestHTTPClient
82-
83- public init ( storage: StorageHTTPClient ? = nil , postgrest: PostgrestHTTPClient ? = nil ) {
94+ let storage : StorageHTTPClient
95+ let postgrest : PostgrestHTTPClient
96+ let functions : FunctionsHTTPClient
97+
98+ public init (
99+ storage: StorageHTTPClient ? = nil ,
100+ postgrest: PostgrestHTTPClient ? = nil ,
101+ functions: FunctionsHTTPClient ? = nil
102+ ) {
84103 self . storage = storage ?? DefaultStorageHTTPClient ( )
85104 self . postgrest = postgrest ?? DefaultPostgrestHTTPClient ( )
105+ self . functions = functions ?? DefaultFunctionsHTTPClient ( )
86106 }
87107 }
88108
@@ -122,3 +142,13 @@ extension SupabaseClient: StorageHTTPClient {
122142 return try await httpClient. storage. upload ( request, from: data)
123143 }
124144}
145+
146+ extension SupabaseClient : FunctionsHTTPClient {
147+ public func execute(
148+ _ request: URLRequest ,
149+ client: FunctionsClient
150+ ) async throws -> ( Data , HTTPURLResponse ) {
151+ let request = try await adapt ( request: request)
152+ return try await httpClient. functions. execute ( request, client: client)
153+ }
154+ }
0 commit comments