Skip to content

Commit 50fc325

Browse files
authored
fix: expose SupabaseClient headers (#447)
1 parent 44d88c9 commit 50fc325

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

Sources/Supabase/SupabaseClient.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class SupabaseClient: Sendable {
3636
$0.rest = PostgrestClient(
3737
url: databaseURL,
3838
schema: options.db.schema,
39-
headers: defaultHeaders.dictionary,
39+
headers: headers,
4040
logger: options.global.logger,
4141
fetch: fetchWithAuth,
4242
encoder: options.db.encoder,
@@ -55,7 +55,7 @@ public final class SupabaseClient: Sendable {
5555
$0.storage = SupabaseStorageClient(
5656
configuration: StorageClientConfiguration(
5757
url: storageURL,
58-
headers: defaultHeaders.dictionary,
58+
headers: headers,
5959
session: StorageHTTPSession(fetch: fetchWithAuth, upload: uploadWithAuth),
6060
logger: options.global.logger
6161
)
@@ -77,7 +77,7 @@ public final class SupabaseClient: Sendable {
7777
if $0.functions == nil {
7878
$0.functions = FunctionsClient(
7979
url: functionsURL,
80-
headers: defaultHeaders.dictionary,
80+
headers: headers,
8181
region: options.functions.region,
8282
logger: options.global.logger,
8383
fetch: fetchWithAuth
@@ -88,7 +88,13 @@ public final class SupabaseClient: Sendable {
8888
}
8989
}
9090

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

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

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

150156
auth = AuthClient(
151157
url: supabaseURL.appendingPathComponent("/auth/v1"),
152-
headers: defaultHeaders.dictionary,
158+
headers: _headers.dictionary,
153159
flowType: options.auth.flowType,
154160
redirectToURL: options.auth.redirectToURL,
155161
storageKey: options.auth.storageKey ?? defaultStorageKey,
@@ -167,13 +173,13 @@ public final class SupabaseClient: Sendable {
167173
_realtime = UncheckedSendable(
168174
RealtimeClient(
169175
supabaseURL.appendingPathComponent("/realtime/v1").absoluteString,
170-
headers: defaultHeaders.dictionary,
171-
params: defaultHeaders.dictionary
176+
headers: _headers.dictionary,
177+
params: _headers.dictionary
172178
)
173179
)
174180

175181
var realtimeOptions = options.realtime
176-
realtimeOptions.headers.merge(with: defaultHeaders)
182+
realtimeOptions.headers.merge(with: _headers)
177183

178184
if realtimeOptions.logger == nil {
179185
realtimeOptions.logger = options.global.logger

Tests/SupabaseTests/SupabaseClientTests.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,23 @@ final class SupabaseClientTests: XCTestCase {
6161
)
6262

6363
XCTAssertEqual(
64-
client.defaultHeaders,
64+
client.headers,
6565
[
6666
"X-Client-Info": "supabase-swift/\(Supabase.version)",
6767
"Apikey": "ANON_KEY",
6868
"header_field": "header_value",
6969
"Authorization": "Bearer ANON_KEY",
7070
]
7171
)
72+
XCTAssertNoDifference(client._headers.dictionary, client.headers)
7273

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

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

7879
let realtimeOptions = client.realtimeV2.options
79-
let expectedRealtimeHeader = client.defaultHeaders.merged(with: ["custom_realtime_header_key": "custom_realtime_header_value"])
80+
let expectedRealtimeHeader = client._headers.merged(with: ["custom_realtime_header_key": "custom_realtime_header_value"])
8081
XCTAssertNoDifference(realtimeOptions.headers, expectedRealtimeHeader)
8182
XCTAssertIdentical(realtimeOptions.logger as? Logger, logger)
8283

0 commit comments

Comments
 (0)