@@ -2,11 +2,30 @@ import ConcurrencyExtras
22import Foundation
33import HTTPTypes
44import IssueReporting
5+ import OpenAPIURLSession
56
67#if canImport(FoundationNetworking)
78 import FoundationNetworking
89#endif
910
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+
1029/// Supabase Client.
1130public final class SupabaseClient : Sendable {
1231 let options : SupabaseClientOptions
@@ -16,6 +35,7 @@ public final class SupabaseClient: Sendable {
1635 let databaseURL : URL
1736 let functionsURL : URL
1837
38+ private let transport : any ClientTransport
1939 private let _auth : AuthClient
2040
2141 /// 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 {
89109 headers: headers,
90110 region: options. functions. region,
91111 logger: options. global. logger,
92- fetch: fetchWithAuth
112+ transport: AuthClientTransport (
113+ transport: transport,
114+ accessToken: { try ? await self . _getAccessToken ( ) }
115+ )
93116 )
94117 }
95118
@@ -149,6 +172,12 @@ public final class SupabaseClient: Sendable {
149172 self . supabaseKey = supabaseKey
150173 self . options = options
151174
175+ self . transport = URLSessionTransport (
176+ configuration: URLSessionTransport . Configuration (
177+ session: options. global. session
178+ )
179+ )
180+
152181 storageURL = supabaseURL. appendingPathComponent ( " /storage/v1 " )
153182 databaseURL = supabaseURL. appendingPathComponent ( " /rest/v1 " )
154183 functionsURL = supabaseURL. appendingPathComponent ( " /functions/v1 " )
0 commit comments