Skip to content

Commit 5bba6e3

Browse files
committed
fix Test
1 parent 1b370ff commit 5bba6e3

File tree

87 files changed

+546
-405
lines changed

Some content is hidden

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

87 files changed

+546
-405
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/Functions/FunctionsClient.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,12 @@ 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
)
241+
242+
if options.body != nil && request.headerFields[.contentType] == nil {
243+
request.headerFields[.contentType] = "application/json"
244+
}
241245

242246
if let region = options.region ?? region {
243247
request.headerFields[.xRegion] = region

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-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ package actor HTTPClient: HTTPClientType {
3232
_ request: HTTPRequest,
3333
_ bodyData: Data?
3434
) async throws -> (Data, HTTPResponse) {
35-
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = {
36-
return try await self.fetch($0, $1)
35+
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = { request, bodyData in
36+
var request = request
37+
if bodyData != nil && request.headerFields[.contentType] == nil {
38+
request.headerFields[.contentType] = "application/json"
39+
}
40+
return try await self.fetch(request, bodyData)
3741
}
3842

3943
for interceptor in interceptors.reversed() {

Sources/Helpers/HTTP/HTTPFields.swift

+11-7
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
}
17-
18-
package func merging(with other: Self) -> Self {
18+
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,113 @@
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(for: urlRequest, from: bodyData, delegate: delegate)
72+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
73+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
74+
}
75+
return (data, response)
76+
}
77+
}
78+
79+
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
80+
extension URLSession {
81+
/// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally.
82+
///
83+
/// - Parameter request: The `HTTPRequest` for which to load data.
84+
/// - Returns: Data and response.
85+
public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) {
86+
guard let urlRequest = URLRequest(httpRequest: request) else {
87+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
88+
}
89+
let (data, urlResponse) = try await self.data(for: urlRequest)
90+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
91+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
92+
}
93+
return (data, response)
94+
}
95+
96+
/// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally.
97+
///
98+
/// - Parameter request: The `HTTPRequest` for which to upload data.
99+
/// - Parameter bodyData: Data to upload.
100+
/// - Returns: Data and response.
101+
public func upload(for request: HTTPRequest, from bodyData: Data) async throws -> (Data, HTTPResponse) {
102+
guard let urlRequest = URLRequest(httpRequest: request) else {
103+
throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest
104+
}
105+
let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData)
106+
guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else {
107+
throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse
108+
}
109+
return (data, response)
110+
}
111+
}
112+
113+
#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

-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
201201
var request = URLRequest(url: url)
202202

203203
for (key, value) in headers {
204-
guard let value = value as? String else { continue }
205204
request.addValue(value, forHTTPHeaderField: key)
206205
}
207206

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

Sources/Storage/StorageFileApi.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
8080
options: FileOptions?
8181
) async throws -> FileUploadResponse {
8282
let options = options ?? defaultFileOptions
83-
var headers = options.headers.map { HTTPFields($0) } ?? HTTPFields()
83+
var headers = options.headers ?? HTTPFields()
8484

8585
if method == .post {
8686
headers[.xUpsert] = "\(options.upsert)"
@@ -647,7 +647,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
647647
options: FileOptions?
648648
) async throws -> SignedURLUploadResponse {
649649
let options = options ?? defaultFileOptions
650-
var headers = options.headers.map { HTTPFields($0) } ?? HTTPFields()
650+
var headers = options.headers ?? HTTPFields()
651651

652652
headers[.xUpsert] = "\(options.upsert)"
653653
headers[.duplex] = options.duplex

Sources/Supabase/SupabaseClient.swift

+10-13
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public final class SupabaseClient: Sendable {
4747
$0.rest = PostgrestClient(
4848
url: databaseURL,
4949
schema: options.db.schema,
50-
headers: _headers,
50+
headers: headers,
5151
logger: options.global.logger,
5252
fetch: { request, bodyData in
5353
if let bodyData {
@@ -72,7 +72,7 @@ public final class SupabaseClient: Sendable {
7272
$0.storage = SupabaseStorageClient(
7373
configuration: StorageClientConfiguration(
7474
url: storageURL,
75-
headers: _headers,
75+
headers: headers,
7676
session: StorageHTTPSession { request, bodyData in
7777
if let bodyData {
7878
return try await self.uploadWithAuth(for: request, from: bodyData)
@@ -100,7 +100,7 @@ public final class SupabaseClient: Sendable {
100100
if $0.functions == nil {
101101
$0.functions = FunctionsClient(
102102
url: functionsURL,
103-
headers: _headers,
103+
headers: headers,
104104
region: options.functions.region,
105105
logger: options.global.logger,
106106
fetch: { request, bodyData in
@@ -116,14 +116,11 @@ public final class SupabaseClient: Sendable {
116116
return $0.functions!
117117
}
118118
}
119-
120-
let _headers: HTTPFields
119+
121120
/// Headers provided to the inner clients on initialization.
122121
///
123122
/// - Note: This collection is non-mutable, if you want to provide different headers, pass it in ``SupabaseClientOptions/GlobalOptions/headers``.
124-
public var headers: [String: String] {
125-
_headers.dictionary
126-
}
123+
public let headers: HTTPFields
127124

128125
struct MutableState {
129126
var listenForAuthEventsTask: Task<Void, Never>?
@@ -177,14 +174,14 @@ public final class SupabaseClient: Sendable {
177174
.authorization: "Bearer \(supabaseKey)",
178175
.apiKey: supabaseKey,
179176
]
180-
_headers = headers.merging(with: options.global.headers)
177+
self.headers = options.global.headers.merging(headers) { $1 }
181178

182179
// default storage key uses the supabase project ref as a namespace
183180
let defaultStorageKey = "sb-\(supabaseURL.host!.split(separator: ".")[0])-auth-token"
184181

185182
_auth = AuthClient(
186183
url: supabaseURL.appendingPathComponent("/auth/v1"),
187-
headers: _headers,
184+
headers: self.headers,
188185
flowType: options.auth.flowType,
189186
redirectToURL: options.auth.redirectToURL,
190187
storageKey: options.auth.storageKey ?? defaultStorageKey,
@@ -206,13 +203,13 @@ public final class SupabaseClient: Sendable {
206203
_realtime = UncheckedSendable(
207204
RealtimeClient(
208205
supabaseURL.appendingPathComponent("/realtime/v1").absoluteString,
209-
headers: _headers,
210-
params: _headers.dictionary
206+
headers: headers,
207+
params: headers.dictionary
211208
)
212209
)
213210

214211
var realtimeOptions = options.realtime
215-
realtimeOptions.headers.merge(with: _headers)
212+
realtimeOptions.headers.merge(self.headers) { $1 }
216213
if realtimeOptions.logger == nil {
217214
realtimeOptions.logger = options.global.logger
218215
}

0 commit comments

Comments
 (0)