Skip to content

Commit

Permalink
fix: expose SupabaseClient headers (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Jul 6, 2024
1 parent 44d88c9 commit 50fc325
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
24 changes: 15 additions & 9 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public final class SupabaseClient: Sendable {
$0.rest = PostgrestClient(
url: databaseURL,
schema: options.db.schema,
headers: defaultHeaders.dictionary,
headers: headers,
logger: options.global.logger,
fetch: fetchWithAuth,
encoder: options.db.encoder,
Expand All @@ -55,7 +55,7 @@ public final class SupabaseClient: Sendable {
$0.storage = SupabaseStorageClient(
configuration: StorageClientConfiguration(
url: storageURL,
headers: defaultHeaders.dictionary,
headers: headers,
session: StorageHTTPSession(fetch: fetchWithAuth, upload: uploadWithAuth),
logger: options.global.logger
)
Expand All @@ -77,7 +77,7 @@ public final class SupabaseClient: Sendable {
if $0.functions == nil {
$0.functions = FunctionsClient(
url: functionsURL,
headers: defaultHeaders.dictionary,
headers: headers,
region: options.functions.region,
logger: options.global.logger,
fetch: fetchWithAuth
Expand All @@ -88,7 +88,13 @@ public final class SupabaseClient: Sendable {
}
}

let defaultHeaders: HTTPHeaders
let _headers: HTTPHeaders
/// Headers provided to the inner clients on initialization.
///
/// - Note: This collection is non-mutable, if you want to provide different headers, pass it in ``SupabaseClientOptions/GlobalOptions/headers``.
public var headers: [String: String] {
_headers.dictionary
}

struct MutableState {
var listenForAuthEventsTask: Task<Void, Never>?
Expand Down Expand Up @@ -137,7 +143,7 @@ public final class SupabaseClient: Sendable {
databaseURL = supabaseURL.appendingPathComponent("/rest/v1")
functionsURL = supabaseURL.appendingPathComponent("/functions/v1")

defaultHeaders = HTTPHeaders([
_headers = HTTPHeaders([
"X-Client-Info": "supabase-swift/\(version)",
"Authorization": "Bearer \(supabaseKey)",
"Apikey": supabaseKey,
Expand All @@ -149,7 +155,7 @@ public final class SupabaseClient: Sendable {

auth = AuthClient(
url: supabaseURL.appendingPathComponent("/auth/v1"),
headers: defaultHeaders.dictionary,
headers: _headers.dictionary,
flowType: options.auth.flowType,
redirectToURL: options.auth.redirectToURL,
storageKey: options.auth.storageKey ?? defaultStorageKey,
Expand All @@ -167,13 +173,13 @@ public final class SupabaseClient: Sendable {
_realtime = UncheckedSendable(
RealtimeClient(
supabaseURL.appendingPathComponent("/realtime/v1").absoluteString,
headers: defaultHeaders.dictionary,
params: defaultHeaders.dictionary
headers: _headers.dictionary,
params: _headers.dictionary
)
)

var realtimeOptions = options.realtime
realtimeOptions.headers.merge(with: defaultHeaders)
realtimeOptions.headers.merge(with: _headers)

if realtimeOptions.logger == nil {
realtimeOptions.logger = options.global.logger
Expand Down
5 changes: 3 additions & 2 deletions Tests/SupabaseTests/SupabaseClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,23 @@ final class SupabaseClientTests: XCTestCase {
)

XCTAssertEqual(
client.defaultHeaders,
client.headers,
[
"X-Client-Info": "supabase-swift/\(Supabase.version)",
"Apikey": "ANON_KEY",
"header_field": "header_value",
"Authorization": "Bearer ANON_KEY",
]
)
XCTAssertNoDifference(client._headers.dictionary, client.headers)

XCTAssertEqual(client.functions.region, "ap-northeast-1")

let realtimeURL = client.realtimeV2.url
XCTAssertEqual(realtimeURL.absoluteString, "https://project-ref.supabase.co/realtime/v1")

let realtimeOptions = client.realtimeV2.options
let expectedRealtimeHeader = client.defaultHeaders.merged(with: ["custom_realtime_header_key": "custom_realtime_header_value"])
let expectedRealtimeHeader = client._headers.merged(with: ["custom_realtime_header_key": "custom_realtime_header_value"])
XCTAssertNoDifference(realtimeOptions.headers, expectedRealtimeHeader)
XCTAssertIdentical(realtimeOptions.logger as? Logger, logger)

Expand Down

0 comments on commit 50fc325

Please sign in to comment.