Skip to content

Add support for customizing client initialization #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Examples/Examples/TodoListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ struct TodoListView: View {
.task {
do {
error = nil
todos = IdentifiedArrayOf(
uniqueElements: try await supabase.database.from("todos")
todos = try await IdentifiedArrayOf(
uniqueElements: supabase.database.from("todos")
.select()
.execute()
.value as [Todo]
Expand Down
28 changes: 28 additions & 0 deletions Sources/Supabase/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

extension SupabaseClient {
@available(
*,
deprecated,
message: "Deprecated initializer, use init(supabaseURL:supabaseKey:options) instead."
)
public convenience init(
supabaseURL: URL,
supabaseKey: String,
schema: String = "public",
headers: [String: String] = [:],
httpClient: HTTPClient = HTTPClient()
) {
self.init(
supabaseURL: supabaseURL,
supabaseKey: supabaseKey,
options: SupabaseClientOptions(
db: SupabaseClientOptions.DatabaseOptions(schema: schema),
global: SupabaseClientOptions.GlobalOptions(
headers: headers,
httpClient: httpClient
)
)
)
}
}
20 changes: 7 additions & 13 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,26 @@ public class SupabaseClient {

private var defaultHeaders: [String: String]

/// Init `Supabase` with the provided parameters.
/// - Parameters:
/// - supabaseURL: Unique Supabase project url
/// - supabaseKey: Supabase anonymous API Key
/// - schema: Database schema name, defaults to `public`
/// - headers: Optional headers for initializing the client.
/// Create a new client.
public init(
supabaseURL: URL,
supabaseKey: String,
schema: String = "public",
headers: [String: String] = [:],
httpClient: HTTPClient = HTTPClient()
options: SupabaseClientOptions = .init()
) {
self.supabaseURL = supabaseURL
self.supabaseKey = supabaseKey
self.schema = schema
self.httpClient = httpClient
schema = options.db.schema
httpClient = options.global.httpClient

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

auth = GoTrueClient(
url: supabaseURL.appendingPathComponent("/auth/v1"),
headers: defaultHeaders
headers: defaultHeaders,
localStorage: options.auth.storage
)

let isPlatform =
Expand Down
47 changes: 47 additions & 0 deletions Sources/Supabase/Types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Foundation
import GoTrue

public struct SupabaseClientOptions {
public let db: DatabaseOptions
public let auth: AuthOptions
public let global: GlobalOptions

public struct DatabaseOptions {
/// The Postgres schema which your tables belong to. Must be on the list of exposed schemas in
/// Supabase. Defaults to `public`.
public let schema: String

public init(schema: String = "public") {
self.schema = schema
}
}

public struct AuthOptions {
/// A storage provider. Used to store the logged-in session.
public let storage: GoTrueLocalStorage?

public init(storage: GoTrueLocalStorage? = nil) {
self.storage = storage
}
}

public struct GlobalOptions {
public let headers: [String: String]
public let httpClient: SupabaseClient.HTTPClient

public init(headers: [String: String] = [:], httpClient: SupabaseClient.HTTPClient = .init()) {
self.headers = headers
self.httpClient = httpClient
}
}

public init(
db: DatabaseOptions = .init(),
auth: AuthOptions = .init(),
global: GlobalOptions = .init()
) {
self.db = db
self.auth = auth
self.global = global
}
}
16 changes: 8 additions & 8 deletions supabase-swift.xcworkspace/xcshareddata/swiftpm/Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.