@@ -2,11 +2,30 @@ import ConcurrencyExtras
2
2
import Foundation
3
3
import HTTPTypes
4
4
import IssueReporting
5
+ import OpenAPIURLSession
5
6
6
7
#if canImport(FoundationNetworking)
7
8
import FoundationNetworking
8
9
#endif
9
10
11
+ struct AuthClientTransport : ClientTransport {
12
+ let transport : any ClientTransport
13
+ let accessToken : @Sendable ( ) async -> String ?
14
+
15
+ func send(
16
+ _ request: HTTPTypes . HTTPRequest ,
17
+ body: HTTPBody ? ,
18
+ baseURL: URL ,
19
+ operationID: String
20
+ ) async throws -> ( HTTPTypes . HTTPResponse , HTTPBody ? ) {
21
+ var request = request
22
+ if let token = await accessToken ( ) {
23
+ request. headerFields [ . authorization] = " Bearer \( token) "
24
+ }
25
+ return try await transport. send ( request, body: body, baseURL: baseURL, operationID: operationID)
26
+ }
27
+ }
28
+
10
29
/// Supabase Client.
11
30
public final class SupabaseClient : Sendable {
12
31
let options : SupabaseClientOptions
@@ -16,6 +35,7 @@ public final class SupabaseClient: Sendable {
16
35
let databaseURL : URL
17
36
let functionsURL : URL
18
37
38
+ private let transport : any ClientTransport
19
39
private let _auth : AuthClient
20
40
21
41
/// Supabase Auth allows you to create and manage user sessions for access to data that is secured by access policies.
@@ -89,7 +109,10 @@ public final class SupabaseClient: Sendable {
89
109
headers: headers,
90
110
region: options. functions. region,
91
111
logger: options. global. logger,
92
- fetch: fetchWithAuth
112
+ transport: AuthClientTransport (
113
+ transport: transport,
114
+ accessToken: { try ? await self . _getAccessToken ( ) }
115
+ )
93
116
)
94
117
}
95
118
@@ -149,6 +172,12 @@ public final class SupabaseClient: Sendable {
149
172
self . supabaseKey = supabaseKey
150
173
self . options = options
151
174
175
+ self . transport = URLSessionTransport (
176
+ configuration: URLSessionTransport . Configuration (
177
+ session: options. global. session
178
+ )
179
+ )
180
+
152
181
storageURL = supabaseURL. appendingPathComponent ( " /storage/v1 " )
153
182
databaseURL = supabaseURL. appendingPathComponent ( " /rest/v1 " )
154
183
functionsURL = supabaseURL. appendingPathComponent ( " /functions/v1 " )
0 commit comments