Skip to content

Commit 9322ce2

Browse files
committed
fix Test
1 parent 1b370ff commit 9322ce2

File tree

92 files changed

+586
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+586
-427
lines changed

Sources/Auth/AuthClient.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,12 @@ public final class AuthClient: Sendable {
11221122

11231123
if let jwt {
11241124
request.headerFields[.authorization] = "Bearer \(jwt)"
1125+
let (data, _) = try await api.execute(for: request, from: nil)
1126+
return try configuration.decoder.decode(User.self, from: data)
1127+
} else {
1128+
let (data, _) = try await api.authorizedExecute(for: request, from: nil)
1129+
return try configuration.decoder.decode(User.self, from: data)
11251130
}
1126-
1127-
let (data, _) = try await api.authorizedExecute(for: request, from: nil)
1128-
return try configuration.decoder.decode(User.self, from: data)
11291131
}
11301132

11311133
/// Updates user data, if there is a logged in user.

Sources/Auth/AuthClientConfiguration.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension AuthClient {
8383
},
8484
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
8585
) {
86-
let headers = headers.merging(with: Configuration.defaultHeaders)
86+
let headers = Configuration.defaultHeaders.merging(headers) { $1 }
8787

8888
self.url = url ?? defaultAuthURL
8989
self.headers = headers

Sources/Auth/Internal/APIClient.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ struct APIClient: Sendable {
3535
func execute(
3636
for request: HTTPRequest,
3737
from bodyData: Data?
38-
) async throws -> (
39-
Data,
40-
HTTPResponse
41-
) {
38+
) async throws -> (Data, HTTPResponse) {
4239
var request = request
43-
request.headerFields = HTTPFields(configuration.headers).merging(with: request.headerFields)
40+
request.headerFields = request.headerFields.merging(configuration.headers) { $1 }
4441

4542
if request.headerFields[.apiVersionHeaderName] == nil {
4643
request.headerFields[.apiVersionHeaderName] = apiVersions[._20240101]!.name.rawValue

Sources/Auth/Internal/SessionStorage.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ extension StorageMigration {
125125
let storedSession = try? AuthClient.Configuration.jsonDecoder.decode(
126126
StoredSession.self,
127127
from: data
128-
) {
128+
)
129+
{
129130
let session = try AuthClient.Configuration.jsonEncoder.encode(storedSession.session)
130131
try storage.store(key: key, value: session)
131132
}

Sources/Functions/FunctionsClient.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,13 @@ public final class FunctionsClient: Sendable {
236236
url: url
237237
.appendingPathComponent(functionName)
238238
.appendingQueryItems(options.query),
239-
headerFields: mutableState.headers.merging(with: options.headers)
239+
headerFields: mutableState.headers.merging(options.headers) { $1 }
240240
)
241241

242+
if options.body != nil && request.headerFields[.contentType] == nil {
243+
request.headerFields[.contentType] = "application/json"
244+
}
245+
242246
if let region = options.region ?? region {
243247
request.headerFields[.xRegion] = region
244248
}

Sources/Functions/Types.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public struct FunctionInvokeOptions: Sendable {
6565
}
6666

6767
self.method = method
68-
self.headers = defaultHeaders.merging(with: headers)
68+
self.headers = defaultHeaders.merging(headers) { $1 }
6969
self.region = region
7070
self.query = query
7171
}

Sources/Helpers/HTTP/HTTPClient.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ package actor HTTPClient: HTTPClientType {
3333
_ bodyData: Data?
3434
) async throws -> (Data, HTTPResponse) {
3535
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = {
36-
return try await self.fetch($0, $1)
36+
request, bodyData in
37+
var request = request
38+
if bodyData != nil && request.headerFields[.contentType] == nil {
39+
request.headerFields[.contentType] = "application/json"
40+
}
41+
return try await self.fetch(request, bodyData)
3742
}
3843

3944
for interceptor in interceptors.reversed() {

Sources/Helpers/HTTP/HTTPFields.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@ extension HTTPFields {
99
return .init(keyValues, uniquingKeysWith: { $1 })
1010
}
1111

12-
package mutating func merge(with other: Self) {
13-
for field in other {
14-
self[field.name] = field.value
15-
}
12+
package mutating func merge(
13+
_ other: Self,
14+
uniquingKeysWith combine: (String, String) throws -> String
15+
) rethrows {
16+
self = try self.merging(other, uniquingKeysWith: combine)
1617
}
1718

18-
package func merging(with other: Self) -> Self {
19+
package func merging(
20+
_ other: Self,
21+
uniquingKeysWith combine: (String, String) throws -> String
22+
) rethrows -> HTTPFields {
1923
var copy = self
2024

2125
for field in other {
22-
copy[field.name] = field.value
26+
copy[field.name] = try combine(self[field.name] ?? "", field.value)
2327
}
2428

2529
return copy

Sources/Helpers/HTTP/LoggerInterceptor.swift

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ package struct LoggerInterceptor: HTTPClientInterceptor {
3131
)
3232

3333
do {
34+
var request = request
35+
if bodyData != nil && request.headerFields[.contentType] == nil {
36+
request.headerFields[.contentType] = "application/json"
37+
}
3438
let (data, response) = try await next(request, bodyData)
3539
logger.verbose(
3640
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import Foundation
2+
import HTTPTypes
3+
4+
#if canImport(FoundationNetworking)
5+
import FoundationNetworking
6+
#endif
7+
8+
#if !os(WASI)
9+
10+
extension URLSessionTask {
11+
/// The original HTTP request this task was created with.
12+
public var originalHTTPRequest: HTTPRequest? {
13+
self.originalRequest?.httpRequest
14+
}
15+
16+
/// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection.
17+
public var currentHTTPRequest: HTTPRequest? {
18+
self.currentRequest?.httpRequest
19+
}
20+
21+
/// The HTTP response received from the server.
22+
public var httpResponse: HTTPResponse? {
23+
(self.response as? HTTPURLResponse)?.httpResponse
24+
}
25+
}
26+
27+
private enum HTTPTypeConversionError: Error {
28+
case failedToConvertHTTPRequestToURLRequest
29+
case failedToConvertURLResponseToHTTPResponse
30+
}
31+
32+
#endif
33+
34+
#if canImport(FoundationNetworking) && compiler(<6)
35+
36+
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
37+
extension URLSession {
38+
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
39+
///
40+
/// - Parameter request: The `HTTPRequest` for which to load data.
41+
/// - Parameter delegate: Task-specific delegate.
42+
/// - Returns: Data and response.
43+
public func data(
44+
for request: HTTPRequest,
45+
delegate: (any URLSessionTaskDelegate)? = nil
46+
) async throws -> (Data, HTTPResponse) {
47+
guard let urlRequest = URLRequest(httpRequest: request) else {
48+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
49+
}
50+
let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate)
51+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
52+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
53+
}
54+
return (data, response)
55+
}
56+
57+
/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
58+
///
59+
/// - Parameter request: The `HTTPRequest` for which to upload data.
60+
/// - Parameter bodyData: Data to upload.
61+
/// - Parameter delegate: Task-specific delegate.
62+
/// - Returns: Data and response.
63+
public func upload(
64+
for request: HTTPRequest,
65+
from bodyData: Data,
66+
delegate: (any URLSessionTaskDelegate)? = nil
67+
) async throws -> (Data, HTTPResponse) {
68+
guard let urlRequest = URLRequest(httpRequest: request) else {
69+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
70+
}
71+
let (data, urlResponse) = try await self.upload(
72+
for: urlRequest, from: bodyData, delegate: delegate)
73+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
74+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
75+
}
76+
return (data, response)
77+
}
78+
}
79+
80+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
81+
extension URLSession {
82+
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
83+
///
84+
/// - Parameter request: The `HTTPRequest` for which to load data.
85+
/// - Returns: Data and response.
86+
public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) {
87+
guard let urlRequest = URLRequest(httpRequest: request) else {
88+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
89+
}
90+
let (data, urlResponse) = try await self.data(for: urlRequest)
91+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
92+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
93+
}
94+
return (data, response)
95+
}
96+
97+
/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
98+
///
99+
/// - Parameter request: The `HTTPRequest` for which to upload data.
100+
/// - Parameter bodyData: Data to upload.
101+
/// - Returns: Data and response.
102+
public func upload(for request: HTTPRequest, from bodyData: Data) async throws -> (
103+
Data, HTTPResponse
104+
) {
105+
guard let urlRequest = URLRequest(httpRequest: request) else {
106+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
107+
}
108+
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData)
109+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
110+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
111+
}
112+
return (data, response)
113+
}
114+
}
115+
116+
#endif

Sources/PostgREST/PostgrestBuilder.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class PostgrestBuilder: @unchecked Sendable {
102102
options: FetchOptions,
103103
decode: (Data) throws -> T
104104
) async throws -> PostgrestResponse<T> {
105-
let request = mutableState.withValue {
105+
let (request, bodyData) = mutableState.withValue {
106106
$0.fetchOptions = options
107107

108108
if $0.fetchOptions.head {
@@ -130,10 +130,10 @@ public class PostgrestBuilder: @unchecked Sendable {
130130
}
131131
}
132132

133-
return $0.request
133+
return ($0.request, $0.bodyData)
134134
}
135135

136-
let (data, response) = try await http.send(request, nil)
136+
let (data, response) = try await http.send(request, bodyData)
137137

138138
guard 200..<300 ~= response.status.code else {
139139
if let error = try? configuration.decoder.decode(PostgrestError.self, from: data) {

Sources/PostgREST/PostgrestClient.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public final class PostgrestClient: Sendable {
7272
public init(configuration: Configuration) {
7373
_configuration = LockIsolated(configuration)
7474
_configuration.withValue {
75-
$0.headers.merge(with: Configuration.defaultHeaders)
75+
$0.headers.merge(Configuration.defaultHeaders) { l, _ in l }
7676
}
7777
}
7878

Sources/Realtime/PhoenixTransport.swift

+10-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// THE SOFTWARE.
2020

2121
import Foundation
22+
import HTTPTypes
2223

2324
#if canImport(FoundationNetworking)
2425
import FoundationNetworking
@@ -46,7 +47,7 @@ public protocol PhoenixTransport {
4647
- Parameters:
4748
- headers: Headers to include in the URLRequests when opening the Websocket connection. Can be empty [:]
4849
*/
49-
func connect(with headers: [String: String])
50+
func connect(with headers: HTTPFields)
5051

5152
/**
5253
Disconnect from the server.
@@ -192,20 +193,21 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
192193
public var readyState: PhoenixTransportReadyState = .closed
193194
public var delegate: (any PhoenixTransportDelegate)? = nil
194195

195-
public func connect(with headers: [String: String]) {
196+
public func connect(with headers: HTTPFields) {
196197
// Set the transport state as connecting
197198
readyState = .connecting
198199

199200
// Create the session and websocket task
200201
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
201-
var request = URLRequest(url: url)
202+
let request = HTTPRequest(
203+
method: .get,
204+
url: url,
205+
headerFields: headers
206+
)
202207

203-
for (key, value) in headers {
204-
guard let value = value as? String else { continue }
205-
request.addValue(value, forHTTPHeaderField: key)
206-
}
208+
let urlRequest = URLRequest(httpRequest: request)!
207209

208-
task = session?.webSocketTask(with: request)
210+
task = session?.webSocketTask(with: urlRequest)
209211

210212
// Start the task
211213
task?.resume()

Sources/Realtime/RealtimeClient.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public class RealtimeClient: PhoenixTransportDelegate {
344344
// self.connection?.enabledSSLCipherSuites = enabledSSLCipherSuites
345345
// #endif
346346

347-
connection?.connect(with: headers.dictionary)
347+
connection?.connect(with: headers)
348348
}
349349

350350
/// Disconnects the socket

Sources/Realtime/V2/RealtimeChannelV2.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ struct Socket: Sendable {
3737
var addChannel: @Sendable (_ channel: RealtimeChannelV2) -> Void
3838
var removeChannel: @Sendable (_ channel: RealtimeChannelV2) async -> Void
3939
var push: @Sendable (_ message: RealtimeMessageV2) async -> Void
40-
var httpSend: @Sendable (
41-
_ request: HTTPRequest,
42-
_ bodyData: Data?
43-
) async throws -> (Data, HTTPResponse)
40+
var httpSend:
41+
@Sendable (
42+
_ request: HTTPRequest,
43+
_ bodyData: Data?
44+
) async throws -> (Data, HTTPResponse)
4445
}
4546

4647
extension Socket {

Sources/Realtime/V2/Types.swift

+13-8
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ public struct RealtimeClientOptions: Sendable {
2121
var timeoutInterval: TimeInterval
2222
var disconnectOnSessionLoss: Bool
2323
var connectOnSubscribe: Bool
24-
var fetch: (@Sendable (
25-
_ request: HTTPRequest,
26-
_ bodyData: Data?
27-
) async throws -> (Data, HTTPResponse))?
24+
var fetch:
25+
(
26+
@Sendable (
27+
_ request: HTTPRequest,
28+
_ bodyData: Data?
29+
) async throws -> (Data, HTTPResponse)
30+
)?
2831
package var logger: (any SupabaseLogger)?
2932

3033
public static let defaultHeartbeatInterval: TimeInterval = 15
@@ -40,10 +43,12 @@ public struct RealtimeClientOptions: Sendable {
4043
timeoutInterval: TimeInterval = Self.defaultTimeoutInterval,
4144
disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss,
4245
connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe,
43-
fetch: (@Sendable (
44-
_ request: HTTPRequest,
45-
_ bodyData: Data?
46-
) async throws -> (Data, HTTPResponse))? = nil,
46+
fetch: (
47+
@Sendable (
48+
_ request: HTTPRequest,
49+
_ bodyData: Data?
50+
) async throws -> (Data, HTTPResponse)
51+
)? = nil,
4752
logger: (any SupabaseLogger)? = nil
4853
) {
4954
self.headers = headers

Sources/Storage/StorageApi.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class StorageApi: @unchecked Sendable {
3535
from bodyData: Data?
3636
) async throws -> (Data, HTTPResponse) {
3737
var request = request
38-
request.headerFields = configuration.headers.merging(with: request.headerFields)
38+
request.headerFields = configuration.headers.merging(request.headerFields) { $1 }
3939

4040
let (data, response) = try await http.send(request, bodyData)
4141

0 commit comments

Comments
 (0)