Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
zunda-pixel committed Nov 30, 2024
1 parent 02dfc3a commit 7ef1362
Show file tree
Hide file tree
Showing 51 changed files with 84 additions and 70 deletions.
8 changes: 5 additions & 3 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,12 @@ public final class AuthClient: Sendable {

if let jwt {
request.headerFields[.authorization] = "Bearer \(jwt)"
let (data, _) = try await api.execute(for: request, from: nil)
return try configuration.decoder.decode(User.self, from: data)
} else {
let (data, _) = try await api.authorizedExecute(for: request, from: nil)
return try configuration.decoder.decode(User.self, from: data)
}

let (data, _) = try await api.authorizedExecute(for: request, from: nil)
return try configuration.decoder.decode(User.self, from: data)
}

/// Updates user data, if there is a logged in user.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClientConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension AuthClient {
},
autoRefreshToken: Bool = AuthClient.Configuration.defaultAutoRefreshToken
) {
let headers = headers.merging(with: Configuration.defaultHeaders)
let headers = Configuration.defaultHeaders.merging(headers) { $1 }

self.url = url ?? defaultAuthURL
self.headers = headers
Expand Down
7 changes: 2 additions & 5 deletions Sources/Auth/Internal/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ struct APIClient: Sendable {
func execute(
for request: HTTPRequest,
from bodyData: Data?
) async throws -> (
Data,
HTTPResponse
) {
) async throws -> (Data, HTTPResponse) {
var request = request
request.headerFields = HTTPFields(configuration.headers).merging(with: request.headerFields)
request.headerFields = request.headerFields.merging(configuration.headers) { $1 }

if request.headerFields[.apiVersionHeaderName] == nil {
request.headerFields[.apiVersionHeaderName] = apiVersions[._20240101]!.name.rawValue
Expand Down
6 changes: 5 additions & 1 deletion Sources/Functions/FunctionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,12 @@ public final class FunctionsClient: Sendable {
url: url
.appendingPathComponent(functionName)
.appendingQueryItems(options.query),
headerFields: mutableState.headers.merging(with: options.headers)
headerFields: mutableState.headers.merging(options.headers) { $1 }
)

if options.body != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}

if let region = options.region ?? region {
request.headerFields[.xRegion] = region
Expand Down
2 changes: 1 addition & 1 deletion Sources/Functions/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public struct FunctionInvokeOptions: Sendable {
}

self.method = method
self.headers = defaultHeaders.merging(with: headers)
self.headers = defaultHeaders.merging(headers) { $1 }
self.region = region
self.query = query
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/Helpers/HTTP/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ package actor HTTPClient: HTTPClientType {
_ request: HTTPRequest,
_ bodyData: Data?
) async throws -> (Data, HTTPResponse) {
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = {
return try await self.fetch($0, $1)
var next: @Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse) = { request, bodyData in
var request = request
if bodyData != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}
return try await self.fetch(request, bodyData)
}

for interceptor in interceptors.reversed() {
Expand Down
18 changes: 11 additions & 7 deletions Sources/Helpers/HTTP/HTTPFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ extension HTTPFields {
return .init(keyValues, uniquingKeysWith: { $1 })
}

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

package func merging(with other: Self) -> Self {

package func merging(
_ other: Self,
uniquingKeysWith combine: (String, String) throws -> String
) rethrows -> HTTPFields {
var copy = self

for field in other {
copy[field.name] = field.value
copy[field.name] = try combine(self[field.name] ?? "", field.value)
}

return copy
Expand Down
4 changes: 4 additions & 0 deletions Sources/Helpers/HTTP/LoggerInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ package struct LoggerInterceptor: HTTPClientInterceptor {
)

do {
var request = request
if bodyData != nil && request.headerFields[.contentType] == nil {
request.headerFields[.contentType] = "application/json"
}
let (data, response) = try await next(request, bodyData)
logger.verbose(
"""
Expand Down
6 changes: 3 additions & 3 deletions Sources/PostgREST/PostgrestBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class PostgrestBuilder: @unchecked Sendable {
options: FetchOptions,
decode: (Data) throws -> T
) async throws -> PostgrestResponse<T> {
let request = mutableState.withValue {
let (request, bodyData) = mutableState.withValue {
$0.fetchOptions = options

if $0.fetchOptions.head {
Expand Down Expand Up @@ -130,10 +130,10 @@ public class PostgrestBuilder: @unchecked Sendable {
}
}

return $0.request
return ($0.request, $0.bodyData)
}

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

guard 200..<300 ~= response.status.code else {
if let error = try? configuration.decoder.decode(PostgrestError.self, from: data) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PostgREST/PostgrestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final class PostgrestClient: Sendable {
public init(configuration: Configuration) {
_configuration = LockIsolated(configuration)
_configuration.withValue {
$0.headers.merge(with: Configuration.defaultHeaders)
$0.headers.merge(Configuration.defaultHeaders) { l, _ in l }
}
}

Expand Down
1 change: 0 additions & 1 deletion Sources/Realtime/PhoenixTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
var request = URLRequest(url: url)

for (key, value) in headers {
guard let value = value as? String else { continue }
request.addValue(value, forHTTPHeaderField: key)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Storage/StorageApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class StorageApi: @unchecked Sendable {
from bodyData: Data?
) async throws -> (Data, HTTPResponse) {
var request = request
request.headerFields = configuration.headers.merging(with: request.headerFields)
request.headerFields = configuration.headers.merging(request.headerFields) { $1 }

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

Expand Down
4 changes: 2 additions & 2 deletions Sources/Storage/StorageFileApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
options: FileOptions?
) async throws -> FileUploadResponse {
let options = options ?? defaultFileOptions
var headers = options.headers.map { HTTPFields($0) } ?? HTTPFields()
var headers = options.headers ?? HTTPFields()

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

headers[.xUpsert] = "\(options.upsert)"
headers[.duplex] = options.duplex
Expand Down
4 changes: 2 additions & 2 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public final class SupabaseClient: Sendable {
.authorization: "Bearer \(supabaseKey)",
.apiKey: supabaseKey,
]
self.headers = headers.merging(with: options.global.headers)
self.headers = options.global.headers.merging(headers) { $1 }

// default storage key uses the supabase project ref as a namespace
let defaultStorageKey = "sb-\(supabaseURL.host!.split(separator: ".")[0])-auth-token"
Expand Down Expand Up @@ -209,7 +209,7 @@ public final class SupabaseClient: Sendable {
)

var realtimeOptions = options.realtime
realtimeOptions.headers.merge(with: self.headers)
realtimeOptions.headers.merge(self.headers) { $1 }
if realtimeOptions.logger == nil {
realtimeOptions.logger = options.global.logger
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request DELETE \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"should_soft_delete\":false}" \
"http://localhost:54321/auth/v1/admin/users/E621E1F8-C36C-495A-93FC-0C247A3E6E5F"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
curl \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/user/identities/authorize?extra_key=extra_value&provider=github&redirect_to=https://supabase.com&scopes=user:email&skip_http_redirect=true"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/factors/123/challenge"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"channel\":\"whatsapp\"}" \
"http://localhost:54321/auth/v1/factors/123/challenge"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"factor_type\":\"totp\",\"friendly_name\":\"test\",\"issuer\":\"supabase.com\"}" \
"http://localhost:54321/auth/v1/factors"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"factor_type\":\"phone\",\"friendly_name\":\"test\",\"phone\":\"+1 202-918-2132\"}" \
"http://localhost:54321/auth/v1/factors"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"factor_type\":\"totp\",\"friendly_name\":\"test\",\"issuer\":\"supabase.com\"}" \
"http://localhost:54321/auth/v1/factors"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
curl \
--request DELETE \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/factors/123"
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"challenge_id\":\"123\",\"code\":\"123456\",\"factor_id\":\"123\"}" \
"http://localhost:54321/auth/v1/factors/123/verify"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
curl \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/reauthenticate"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"refresh_token\":\"refresh-token\"}" \
"http://localhost:54321/auth/v1/token?grant_type=refresh_token"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"email\":\"[email protected]\",\"gotrue_meta_security\":{\"captcha_token\":\"captcha-token\"},\"type\":\"email_change\"}" \
"http://localhost:54321/auth/v1/resend?redirect_to=https://supabase.com"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"gotrue_meta_security\":{\"captcha_token\":\"captcha-token\"},\"phone\":\"+1 202-918-2132\",\"type\":\"phone_change\"}" \
"http://localhost:54321/auth/v1/resend"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"email\":\"[email protected]\",\"gotrue_meta_security\":{\"captcha_token\":\"captcha-token\"}}" \
"http://localhost:54321/auth/v1/recover?redirect_to=https://supabase.com"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
curl \
--header "Apikey: dummy.api.key" \
--header "Authorization: bearer accesstoken" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/user"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"refresh_token\":\"dummy-refresh-token\"}" \
"http://localhost:54321/auth/v1/token?grant_type=refresh_token"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
curl \
--header "Apikey: dummy.api.key" \
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjo0ODUyMTYzNTkzLCJzdWIiOiJmMzNkM2VjOS1hMmVlLTQ3YzQtODBlMS01YmQ5MTlmM2Q4YjgiLCJlbWFpbCI6ImhpQGJpbmFyeXNjcmFwaW5nLmNvIiwicGhvbmUiOiIiLCJhcHBfbWV0YWRhdGEiOnsicHJvdmlkZXIiOiJlbWFpbCIsInByb3ZpZGVycyI6WyJlbWFpbCJdfSwidXNlcl9tZXRhZGF0YSI6e30sInJvbGUiOiJhdXRoZW50aWNhdGVkIn0.UiEhoahP9GNrBKw_OHBWyqYudtoIlZGkrjs7Qa8hU7I" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
"http://localhost:54321/auth/v1/user"
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
curl \
--request POST \
--header "Apikey: dummy.api.key" \
--header "Content-Type: application/json" \
--header "X-Client-Info: gotrue-swift/x.y.z" \
--header "X-Supabase-Api-Version: 2024-01-01" \
--header "apiKey: dummy.api.key" \
--data "{\"data\":{\"custom_key\":\"custom_value\"},\"gotrue_meta_security\":{\"captcha_token\":\"captcha-token\"}}" \
"http://localhost:54321/auth/v1/signup"
Loading

0 comments on commit 7ef1362

Please sign in to comment.