Skip to content

Commit 9bdbb17

Browse files
Merge pull request #1 from grsouza/master
Add PostgrestClient to Supabase
2 parents f2d14c7 + 27ae8aa commit 9bdbb17

File tree

6 files changed

+60
-46
lines changed

6 files changed

+60
-46
lines changed

Package.resolved

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

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@ let package = Package(
99
.library(
1010
name: "Supabase",
1111
targets: ["Supabase"]
12-
),
12+
)
1313
],
1414
dependencies: [
1515
.package(name: "GoTrue", url: "https://github.com/supabase/gotrue-swift.git", .branch("main")),
1616
.package(name: "SupabaseStorage", url: "https://github.com/supabase/storage-swift.git", .branch("main")),
1717
.package(name: "Realtime", url: "https://github.com/supabase/realtime-swift.git", .branch("main")),
18+
.package(name: "PostgREST", url: "https://github.com/supabase/postgrest-swift", .branch("master")),
1819
],
1920
targets: [
2021
.target(
2122
name: "Supabase",
22-
dependencies: ["GoTrue", "SupabaseStorage", "Realtime"]
23+
dependencies: ["GoTrue", "SupabaseStorage", "Realtime", "PostgREST"]
2324
),
2425
.testTarget(
2526
name: "SupabaseTests",

Sources/Supabase/Supabase.swift

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import GoTrue
2+
import PostgREST
23
import Realtime
34
import SupabaseStorage
45

@@ -10,27 +11,44 @@ public class SupabaseClient {
1011
var realtimeUrl: String
1112
var authUrl: String
1213
var storageUrl: String
13-
14+
1415
public var auth: GoTrueClient
16+
1517
public var storage: SupabaseStorageClient {
1618
var headers: [String: String] = [:]
1719
headers["apikey"] = supabaseKey
1820
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
1921
return SupabaseStorageClient(url: storageUrl, headers: headers)
2022
}
21-
23+
24+
public var database: PostgrestClient {
25+
var headers: [String: String] = [:]
26+
headers["apikey"] = supabaseKey
27+
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
28+
return PostgrestClient(url: restUrl, headers: headers, schema: schema)
29+
}
30+
2231
private var realtime: RealtimeClient
23-
24-
public init(supabaseUrl: String, supabaseKey: String, schema: String = "public", autoRefreshToken: Bool = true) {
32+
33+
public init(
34+
supabaseUrl: String,
35+
supabaseKey: String,
36+
schema: String = "public",
37+
autoRefreshToken: Bool = true
38+
) {
2539
self.supabaseUrl = supabaseUrl
2640
self.supabaseKey = supabaseKey
2741
self.schema = schema
2842
restUrl = "\(supabaseUrl)/rest/v1"
2943
realtimeUrl = "\(supabaseUrl)/realtime/v1"
3044
authUrl = "\(supabaseUrl)/auth/v1"
3145
storageUrl = "\(supabaseUrl)/storage/v1"
32-
33-
auth = GoTrueClient(url: authUrl, headers: ["apikey": supabaseKey], autoRefreshToken: autoRefreshToken)
46+
47+
auth = GoTrueClient(
48+
url: authUrl,
49+
headers: ["apikey": supabaseKey],
50+
autoRefreshToken: autoRefreshToken
51+
)
3452
realtime = RealtimeClient(endPoint: realtimeUrl, params: ["apikey": supabaseKey])
3553
}
3654
}

Tests/LinuxMain.swift

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
@testable import Supabase
21
import SupabaseStorage
32
import XCTest
43

5-
final class SupabaseTests: XCTestCase {
6-
var supabase = SupabaseClient(supabaseUrl: SupabaseTests.supabaseUrl(), supabaseKey: SupabaseTests.supabaseKey())
4+
@testable import Supabase
75

6+
final class SupabaseTests: XCTestCase {
7+
var supabase = SupabaseClient(
8+
supabaseUrl: SupabaseTests.supabaseUrl(), supabaseKey: SupabaseTests.supabaseKey())
9+
810
static func supabaseUrl() -> String {
911
if let token = ProcessInfo.processInfo.environment["supabaseUrl"] {
1012
return token
1113
} else {
1214
fatalError()
1315
}
1416
}
15-
17+
1618
static func supabaseKey() -> String {
1719
if let url = ProcessInfo.processInfo.environment["supabaseKey"] {
1820
return url
1921
} else {
2022
fatalError()
2123
}
2224
}
23-
25+
2426
func testSignIN() {
2527
let e = expectation(description: "testSignIN")
26-
28+
2729
supabase.auth.signIn(email: "[email protected]", password: "secret") { result in
2830
switch result {
2931
case let .success(session):
@@ -35,17 +37,17 @@ final class SupabaseTests: XCTestCase {
3537
}
3638
e.fulfill()
3739
}
38-
40+
3941
waitForExpectations(timeout: 30) { error in
4042
if let error = error {
4143
XCTFail("testSignIN failed: \(error.localizedDescription)")
4244
}
4345
}
4446
}
45-
47+
4648
func testListBuckets() {
4749
let e = expectation(description: "listBuckets")
48-
50+
4951
supabase.storage.listBuckets { result in
5052
switch result {
5153
case let .success(buckets):
@@ -56,21 +58,25 @@ final class SupabaseTests: XCTestCase {
5658
}
5759
e.fulfill()
5860
}
59-
61+
6062
waitForExpectations(timeout: 30) { error in
6163
if let error = error {
6264
XCTFail("listBuckets failed: \(error.localizedDescription)")
6365
}
6466
}
6567
}
66-
68+
6769
func testUploadFile() {
6870
let e = expectation(description: "testUploadFile")
69-
let data = try! Data(contentsOf: URL(string: "https://raw.githubusercontent.com/satishbabariya/storage-swift/main/README.md")!)
70-
71+
let data = try! Data(
72+
contentsOf: URL(
73+
string: "https://raw.githubusercontent.com/satishbabariya/storage-swift/main/README.md")!)
74+
7175
let file = File(name: "README.md", data: data, fileName: "README.md", contentType: "text/html")
72-
73-
supabase.storage.from(id: "Demo").upload(path: "\(UUID().uuidString).md", file: file, fileOptions: FileOptions(cacheControl: "3600")) { result in
76+
77+
supabase.storage.from(id: "Demo").upload(
78+
path: "\(UUID().uuidString).md", file: file, fileOptions: FileOptions(cacheControl: "3600")
79+
) { result in
7480
switch result {
7581
case let .success(res):
7682
print(res)
@@ -81,15 +87,11 @@ final class SupabaseTests: XCTestCase {
8187
}
8288
e.fulfill()
8389
}
84-
90+
8591
waitForExpectations(timeout: 30) { error in
8692
if let error = error {
8793
XCTFail("testUploadFile failed: \(error.localizedDescription)")
8894
}
8995
}
9096
}
91-
92-
static var allTests = [
93-
("testListBuckets", testListBuckets),
94-
]
9597
}

Tests/SupabaseTests/XCTestManifests.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)