Skip to content

Commit 7ef1362

Browse files
committed
fix test
1 parent 02dfc3a commit 7ef1362

File tree

51 files changed

+84
-70
lines changed

Some content is hidden

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

51 files changed

+84
-70
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 5 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 11 additions & 7 deletions
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

Lines changed: 4 additions & 0 deletions
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
"""

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)