Skip to content

Commit dae6a38

Browse files
committed
Add Functions
1 parent b08e6d1 commit dae6a38

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let package = Package(
2626
url: "https://github.com/supabase-community/postgrest-swift",
2727
branch: "master"
2828
),
29+
.package(url: "https://github.com/supabase-community/functions-swift", branch: "main"),
2930
],
3031
targets: [
3132
.target(
@@ -35,6 +36,7 @@ let package = Package(
3536
.product(name: "SupabaseStorage", package: "storage-swift"),
3637
.product(name: "Realtime", package: "realtime-swift"),
3738
.product(name: "PostgREST", package: "postgrest-swift"),
39+
.product(name: "Functions", package: "functions-swift"),
3840
]
3941
),
4042
]

Sources/Supabase/SupabaseClient.swift

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Functions
23
import GoTrue
34
import PostgREST
45
import 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

Comments
 (0)