1
1
import Foundation
2
+ import Functions
2
3
import GoTrue
3
4
import PostgREST
4
5
import Realtime
@@ -13,6 +14,7 @@ public class SupabaseClient {
13
14
private let realtimeURL : URL
14
15
private let authURL : URL
15
16
private let storageURL : URL
17
+ private let functionsURL : URL
16
18
17
19
/// Supabase Auth allows you to create and manage user sessions for access to data that is secured
18
20
/// by access policies.
@@ -40,6 +42,17 @@ public class SupabaseClient {
40
42
/// Realtime client for Supabase
41
43
public var realtime : RealtimeClient
42
44
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
+
43
56
private var defaultHeaders : [ String : String ]
44
57
45
58
/// Init `Supabase` with the provided parameters.
@@ -63,6 +76,7 @@ public class SupabaseClient {
63
76
realtimeURL = supabaseURL. appendingPathComponent ( " /realtime/v1 " )
64
77
authURL = supabaseURL. appendingPathComponent ( " /auth/v1 " )
65
78
storageURL = supabaseURL. appendingPathComponent ( " /storage/v1 " )
79
+ functionsURL = supabaseURL. appendingPathComponent ( " /functions/v1 " )
66
80
67
81
defaultHeaders = [
68
82
" X-Client-Info " : " supabase-swift/ \( version) " ,
@@ -77,12 +91,18 @@ public class SupabaseClient {
77
91
}
78
92
79
93
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
+ ) {
84
103
self . storage = storage ?? DefaultStorageHTTPClient ( )
85
104
self . postgrest = postgrest ?? DefaultPostgrestHTTPClient ( )
105
+ self . functions = functions ?? DefaultFunctionsHTTPClient ( )
86
106
}
87
107
}
88
108
@@ -122,3 +142,13 @@ extension SupabaseClient: StorageHTTPClient {
122
142
return try await httpClient. storage. upload ( request, from: data)
123
143
}
124
144
}
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