Skip to content

Commit f77341f

Browse files
authored
Add support for customizing client initialization (#72)
* Add support for customizing client initialization * Remove `storageKey` as it isn't supported by GoTrue yet
1 parent e4b5e81 commit f77341f

File tree

5 files changed

+92
-23
lines changed

5 files changed

+92
-23
lines changed

Examples/Examples/TodoListView.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ struct TodoListView: View {
8080
.task {
8181
do {
8282
error = nil
83-
todos = IdentifiedArrayOf(
84-
uniqueElements: try await supabase.database.from("todos")
83+
todos = try await IdentifiedArrayOf(
84+
uniqueElements: supabase.database.from("todos")
8585
.select()
8686
.execute()
8787
.value as [Todo]

Sources/Supabase/Deprecations.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
extension SupabaseClient {
4+
@available(
5+
*,
6+
deprecated,
7+
message: "Deprecated initializer, use init(supabaseURL:supabaseKey:options) instead."
8+
)
9+
public convenience init(
10+
supabaseURL: URL,
11+
supabaseKey: String,
12+
schema: String = "public",
13+
headers: [String: String] = [:],
14+
httpClient: HTTPClient = HTTPClient()
15+
) {
16+
self.init(
17+
supabaseURL: supabaseURL,
18+
supabaseKey: supabaseKey,
19+
options: SupabaseClientOptions(
20+
db: SupabaseClientOptions.DatabaseOptions(schema: schema),
21+
global: SupabaseClientOptions.GlobalOptions(
22+
headers: headers,
23+
httpClient: httpClient
24+
)
25+
)
26+
)
27+
}
28+
}

Sources/Supabase/SupabaseClient.swift

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,26 @@ public class SupabaseClient {
5656

5757
private var defaultHeaders: [String: String]
5858

59-
/// Init `Supabase` with the provided parameters.
60-
/// - Parameters:
61-
/// - supabaseURL: Unique Supabase project url
62-
/// - supabaseKey: Supabase anonymous API Key
63-
/// - schema: Database schema name, defaults to `public`
64-
/// - headers: Optional headers for initializing the client.
59+
/// Create a new client.
6560
public init(
6661
supabaseURL: URL,
6762
supabaseKey: String,
68-
schema: String = "public",
69-
headers: [String: String] = [:],
70-
httpClient: HTTPClient = HTTPClient()
63+
options: SupabaseClientOptions = .init()
7164
) {
7265
self.supabaseURL = supabaseURL
7366
self.supabaseKey = supabaseKey
74-
self.schema = schema
75-
self.httpClient = httpClient
67+
schema = options.db.schema
68+
httpClient = options.global.httpClient
7669

7770
defaultHeaders = [
7871
"X-Client-Info": "supabase-swift/\(version)",
7972
"apikey": supabaseKey,
80-
].merging(headers) { _, new in new }
73+
].merging(options.global.headers) { _, new in new }
8174

8275
auth = GoTrueClient(
8376
url: supabaseURL.appendingPathComponent("/auth/v1"),
84-
headers: defaultHeaders
77+
headers: defaultHeaders,
78+
localStorage: options.auth.storage
8579
)
8680

8781
let isPlatform =

Sources/Supabase/Types.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Foundation
2+
import GoTrue
3+
4+
public struct SupabaseClientOptions {
5+
public let db: DatabaseOptions
6+
public let auth: AuthOptions
7+
public let global: GlobalOptions
8+
9+
public struct DatabaseOptions {
10+
/// The Postgres schema which your tables belong to. Must be on the list of exposed schemas in
11+
/// Supabase. Defaults to `public`.
12+
public let schema: String
13+
14+
public init(schema: String = "public") {
15+
self.schema = schema
16+
}
17+
}
18+
19+
public struct AuthOptions {
20+
/// A storage provider. Used to store the logged-in session.
21+
public let storage: GoTrueLocalStorage?
22+
23+
public init(storage: GoTrueLocalStorage? = nil) {
24+
self.storage = storage
25+
}
26+
}
27+
28+
public struct GlobalOptions {
29+
public let headers: [String: String]
30+
public let httpClient: SupabaseClient.HTTPClient
31+
32+
public init(headers: [String: String] = [:], httpClient: SupabaseClient.HTTPClient = .init()) {
33+
self.headers = headers
34+
self.httpClient = httpClient
35+
}
36+
}
37+
38+
public init(
39+
db: DatabaseOptions = .init(),
40+
auth: AuthOptions = .init(),
41+
global: GlobalOptions = .init()
42+
) {
43+
self.db = db
44+
self.auth = auth
45+
self.global = global
46+
}
47+
}

supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved

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

0 commit comments

Comments
 (0)