Skip to content

Commit d2df9ed

Browse files
added storage client
1 parent ca91d74 commit d2df9ed

File tree

5 files changed

+95
-16
lines changed

5 files changed

+95
-16
lines changed

Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@ let package = Package(
1313
products: [
1414
.library(
1515
name: "Supabase",
16-
targets: ["Supabase"]),
16+
targets: ["Supabase"]
17+
),
1718
],
1819
dependencies: [
1920
.package(name: "gotrue", url: "https://github.com/satishbabariya/gotrue-swift.git", .branch("main")),
21+
.package(name: "SupabaseStorage", url: "https://github.com/satishbabariya/storage-swift.git", .revision("fc718b30956303cc098caf7e4ad27035b89022d5")),
22+
2023
],
2124
targets: [
2225
.target(
2326
name: "Supabase",
24-
dependencies: ["gotrue"]),
27+
dependencies: ["gotrue", "SupabaseStorage"]
28+
),
2529
.testTarget(
2630
name: "SupabaseTests",
27-
dependencies: ["Supabase"]),
31+
dependencies: ["Supabase"]
32+
),
2833
]
2934
)

Sources/Supabase/Supabase.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import gotrue
2+
import SupabaseStorage
23

34
public class SupabaseClient {
45
var supabaseUrl: String
@@ -10,6 +11,12 @@ public class SupabaseClient {
1011
var storageUrl: String
1112

1213
public var auth: GoTrueClient
14+
public var storage: SupabaseStorageClient {
15+
var headers: [String: String] = [:]
16+
headers["apikey"] = supabaseKey
17+
headers["Authorization"] = "Bearer \(auth.session?.accessToken ?? supabaseKey)"
18+
return SupabaseStorageClient(url: storageUrl, headers: headers)
19+
}
1320

1421
public init(supabaseUrl: String, supabaseKey: String, schema: String = "public", autoRefreshToken: Bool = true) {
1522
self.supabaseUrl = supabaseUrl
Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,73 @@
1-
import XCTest
21
@testable import Supabase
2+
import SupabaseStorage
3+
import XCTest
34

45
final class SupabaseTests: XCTestCase {
5-
func testExample() {
6-
// This is an example of a functional test case.
7-
// Use XCTAssert and related functions to verify your tests produce the correct
8-
// results.
9-
// XCTAssertEqual(Supabase().text, "Hello, World!")
6+
var supabase = SupabaseClient(supabaseUrl: SupabaseTests.supabaseUrl(), supabaseKey: SupabaseTests.supabaseKey())
7+
8+
static func supabaseUrl() -> String {
9+
if let token = ProcessInfo.processInfo.environment["supabaseUrl"] {
10+
return token
11+
} else {
12+
fatalError()
13+
}
14+
}
15+
16+
static func supabaseKey() -> String {
17+
if let url = ProcessInfo.processInfo.environment["supabaseKey"] {
18+
return url
19+
} else {
20+
fatalError()
21+
}
22+
}
23+
24+
func testListBuckets() {
25+
let e = expectation(description: "listBuckets")
26+
27+
supabase.storage.listBuckets { result in
28+
switch result {
29+
case let .success(buckets):
30+
XCTAssertEqual(buckets.count >= 0, true)
31+
case let .failure(error):
32+
print(error.localizedDescription)
33+
XCTFail("listBuckets failed: \(error.localizedDescription)")
34+
}
35+
e.fulfill()
36+
}
37+
38+
waitForExpectations(timeout: 30) { error in
39+
if let error = error {
40+
XCTFail("listBuckets failed: \(error.localizedDescription)")
41+
}
42+
}
43+
}
44+
45+
func testUploadFile() {
46+
let e = expectation(description: "testUploadFile")
47+
let data = try! Data(contentsOf: URL(string: "https://raw.githubusercontent.com/satishbabariya/storage-swift/main/README.md")!)
48+
49+
let file = File(name: "README.md", data: data, fileName: "README.md", contentType: "text/html")
50+
51+
supabase.storage.from(id: "Demo").upload(path: "\(UUID().uuidString).md", file: file, fileOptions: FileOptions(cacheControl: "3600")) { result in
52+
switch result {
53+
case let .success(res):
54+
print(res)
55+
XCTAssertEqual(true, true)
56+
case let .failure(error):
57+
print(error.localizedDescription)
58+
XCTFail("testUploadFile failed: \(error.localizedDescription)")
59+
}
60+
e.fulfill()
61+
}
62+
63+
waitForExpectations(timeout: 30) { error in
64+
if let error = error {
65+
XCTFail("testUploadFile failed: \(error.localizedDescription)")
66+
}
67+
}
1068
}
1169

1270
static var allTests = [
13-
("testExample", testExample),
71+
("testListBuckets", testListBuckets),
1472
]
1573
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import XCTest
22

33
#if !canImport(ObjectiveC)
4-
public func allTests() -> [XCTestCaseEntry] {
5-
return [
6-
testCase(SupabaseTests.allTests),
7-
]
8-
}
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(SupabaseTests.allTests),
7+
]
8+
}
99
#endif

0 commit comments

Comments
 (0)