diff --git a/Sources/Auth/AuthClient.swift b/Sources/Auth/AuthClient.swift index 54678363..ea192f2c 100644 --- a/Sources/Auth/AuthClient.swift +++ b/Sources/Auth/AuthClient.swift @@ -1122,7 +1122,7 @@ public final class AuthClient: Sendable { if let jwt { request.headerFields[.authorization] = "Bearer \(jwt)" - let (data, _) = try await api.execute(for: request, from: nil) + 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) diff --git a/Sources/Auth/Internal/SessionStorage.swift b/Sources/Auth/Internal/SessionStorage.swift index 29be29b7..f23dd083 100644 --- a/Sources/Auth/Internal/SessionStorage.swift +++ b/Sources/Auth/Internal/SessionStorage.swift @@ -125,7 +125,8 @@ extension StorageMigration { let storedSession = try? AuthClient.Configuration.jsonDecoder.decode( StoredSession.self, from: data - ) { + ) + { let session = try AuthClient.Configuration.jsonEncoder.encode(storedSession.session) try storage.store(key: key, value: session) } diff --git a/Sources/Functions/FunctionsClient.swift b/Sources/Functions/FunctionsClient.swift index 782b8904..b35f6272 100644 --- a/Sources/Functions/FunctionsClient.swift +++ b/Sources/Functions/FunctionsClient.swift @@ -238,10 +238,10 @@ public final class FunctionsClient: Sendable { .appendingQueryItems(options.query), headerFields: mutableState.headers.merging(options.headers) { $1 } ) - - if options.body != nil && request.headerFields[.contentType] == nil { - request.headerFields[.contentType] = "application/json" - } + + if options.body != nil && request.headerFields[.contentType] == nil { + request.headerFields[.contentType] = "application/json" + } if let region = options.region ?? region { request.headerFields[.xRegion] = region diff --git a/Sources/Helpers/HTTP/HTTPClient.swift b/Sources/Helpers/HTTP/HTTPClient.swift index 8b277dc5..03b69b41 100644 --- a/Sources/Helpers/HTTP/HTTPClient.swift +++ b/Sources/Helpers/HTTP/HTTPClient.swift @@ -32,11 +32,12 @@ package actor HTTPClient: HTTPClientType { _ request: HTTPRequest, _ bodyData: Data? ) async throws -> (Data, HTTPResponse) { - 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" - } + 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) } diff --git a/Sources/Helpers/HTTP/HTTPFields.swift b/Sources/Helpers/HTTP/HTTPFields.swift index e06ca019..b900632f 100644 --- a/Sources/Helpers/HTTP/HTTPFields.swift +++ b/Sources/Helpers/HTTP/HTTPFields.swift @@ -15,7 +15,7 @@ extension HTTPFields { ) rethrows { self = try self.merging(other, uniquingKeysWith: combine) } - + package func merging( _ other: Self, uniquingKeysWith combine: (String, String) throws -> String diff --git a/Sources/Helpers/HTTP/URLSession+HTTPRequest.swift b/Sources/Helpers/HTTP/URLSession+HTTPRequest.swift index daf775f9..bdf51860 100644 --- a/Sources/Helpers/HTTP/URLSession+HTTPRequest.swift +++ b/Sources/Helpers/HTTP/URLSession+HTTPRequest.swift @@ -2,56 +2,56 @@ import Foundation import HTTPTypes #if canImport(FoundationNetworking) -import FoundationNetworking + import FoundationNetworking #endif #if !os(WASI) -extension URLSessionTask { + extension URLSessionTask { /// The original HTTP request this task was created with. public var originalHTTPRequest: HTTPRequest? { - self.originalRequest?.httpRequest + self.originalRequest?.httpRequest } /// The current HTTP request -- may differ from the `originalHTTPRequest` due to HTTP redirection. public var currentHTTPRequest: HTTPRequest? { - self.currentRequest?.httpRequest + self.currentRequest?.httpRequest } /// The HTTP response received from the server. public var httpResponse: HTTPResponse? { - (self.response as? HTTPURLResponse)?.httpResponse + (self.response as? HTTPURLResponse)?.httpResponse } -} + } -private enum HTTPTypeConversionError: Error { + private enum HTTPTypeConversionError: Error { case failedToConvertHTTPRequestToURLRequest case failedToConvertURLResponseToHTTPResponse -} + } #endif #if canImport(FoundationNetworking) && compiler(<6) -@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) -extension URLSession { + @available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) + extension URLSession { /// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally. /// /// - Parameter request: The `HTTPRequest` for which to load data. /// - Parameter delegate: Task-specific delegate. /// - Returns: Data and response. public func data( - for request: HTTPRequest, - delegate: (any URLSessionTaskDelegate)? = nil + for request: HTTPRequest, + delegate: (any URLSessionTaskDelegate)? = nil ) async throws -> (Data, HTTPResponse) { - guard let urlRequest = URLRequest(httpRequest: request) else { - throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest - } - let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate) - guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { - throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse - } - return (data, response) + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await self.data(for: urlRequest, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) } /// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally. @@ -61,36 +61,37 @@ extension URLSession { /// - Parameter delegate: Task-specific delegate. /// - Returns: Data and response. public func upload( - for request: HTTPRequest, - from bodyData: Data, - delegate: (any URLSessionTaskDelegate)? = nil + for request: HTTPRequest, + from bodyData: Data, + delegate: (any URLSessionTaskDelegate)? = nil ) async throws -> (Data, HTTPResponse) { - guard let urlRequest = URLRequest(httpRequest: request) else { - throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest - } - let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData, delegate: delegate) - guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { - throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse - } - return (data, response) + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await self.upload( + for: urlRequest, from: bodyData, delegate: delegate) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) } -} + } -@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) -extension URLSession { + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) + extension URLSession { /// Convenience method to load data using an `HTTPRequest`; creates and resumes a `URLSessionDataTask` internally. /// /// - Parameter request: The `HTTPRequest` for which to load data. /// - Returns: Data and response. public func data(for request: HTTPRequest) async throws -> (Data, HTTPResponse) { - guard let urlRequest = URLRequest(httpRequest: request) else { - throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest - } - let (data, urlResponse) = try await self.data(for: urlRequest) - guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { - throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse - } - return (data, response) + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await self.data(for: urlRequest) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) } /// Convenience method to upload data using an `HTTPRequest`, creates and resumes a `URLSessionUploadTask` internally. @@ -98,16 +99,18 @@ extension URLSession { /// - Parameter request: The `HTTPRequest` for which to upload data. /// - Parameter bodyData: Data to upload. /// - Returns: Data and response. - public func upload(for request: HTTPRequest, from bodyData: Data) async throws -> (Data, HTTPResponse) { - guard let urlRequest = URLRequest(httpRequest: request) else { - throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest - } - let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData) - guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { - throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse - } - return (data, response) + public func upload(for request: HTTPRequest, from bodyData: Data) async throws -> ( + Data, HTTPResponse + ) { + guard let urlRequest = URLRequest(httpRequest: request) else { + throw HTTPTypeConversionError.failedToConvertHTTPRequestToURLRequest + } + let (data, urlResponse) = try await self.upload(for: urlRequest, from: bodyData) + guard let response = (urlResponse as? HTTPURLResponse)?.httpResponse else { + throw HTTPTypeConversionError.failedToConvertURLResponseToHTTPResponse + } + return (data, response) } -} + } #endif diff --git a/Sources/Realtime/PhoenixTransport.swift b/Sources/Realtime/PhoenixTransport.swift index c5e17ba8..bdc5ee25 100644 --- a/Sources/Realtime/PhoenixTransport.swift +++ b/Sources/Realtime/PhoenixTransport.swift @@ -204,9 +204,9 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD url: url, headerFields: headers ) - + let urlRequest = URLRequest(httpRequest: request)! - + task = session?.webSocketTask(with: urlRequest) // Start the task diff --git a/Sources/Realtime/V2/RealtimeChannelV2.swift b/Sources/Realtime/V2/RealtimeChannelV2.swift index e0b98fd3..e9c74017 100644 --- a/Sources/Realtime/V2/RealtimeChannelV2.swift +++ b/Sources/Realtime/V2/RealtimeChannelV2.swift @@ -37,10 +37,11 @@ struct Socket: Sendable { var addChannel: @Sendable (_ channel: RealtimeChannelV2) -> Void var removeChannel: @Sendable (_ channel: RealtimeChannelV2) async -> Void var push: @Sendable (_ message: RealtimeMessageV2) async -> Void - var httpSend: @Sendable ( - _ request: HTTPRequest, - _ bodyData: Data? - ) async throws -> (Data, HTTPResponse) + var httpSend: + @Sendable ( + _ request: HTTPRequest, + _ bodyData: Data? + ) async throws -> (Data, HTTPResponse) } extension Socket { diff --git a/Sources/Realtime/V2/Types.swift b/Sources/Realtime/V2/Types.swift index e1172bc9..116ce8c2 100644 --- a/Sources/Realtime/V2/Types.swift +++ b/Sources/Realtime/V2/Types.swift @@ -21,10 +21,13 @@ public struct RealtimeClientOptions: Sendable { var timeoutInterval: TimeInterval var disconnectOnSessionLoss: Bool var connectOnSubscribe: Bool - var fetch: (@Sendable ( - _ request: HTTPRequest, - _ bodyData: Data? - ) async throws -> (Data, HTTPResponse))? + var fetch: + ( + @Sendable ( + _ request: HTTPRequest, + _ bodyData: Data? + ) async throws -> (Data, HTTPResponse) + )? package var logger: (any SupabaseLogger)? public static let defaultHeartbeatInterval: TimeInterval = 15 @@ -40,10 +43,12 @@ public struct RealtimeClientOptions: Sendable { timeoutInterval: TimeInterval = Self.defaultTimeoutInterval, disconnectOnSessionLoss: Bool = Self.defaultDisconnectOnSessionLoss, connectOnSubscribe: Bool = Self.defaultConnectOnSubscribe, - fetch: (@Sendable ( - _ request: HTTPRequest, - _ bodyData: Data? - ) async throws -> (Data, HTTPResponse))? = nil, + fetch: ( + @Sendable ( + _ request: HTTPRequest, + _ bodyData: Data? + ) async throws -> (Data, HTTPResponse) + )? = nil, logger: (any SupabaseLogger)? = nil ) { self.headers = headers diff --git a/Sources/Storage/StorageHTTPClient.swift b/Sources/Storage/StorageHTTPClient.swift index 17a3221c..b16fd55b 100644 --- a/Sources/Storage/StorageHTTPClient.swift +++ b/Sources/Storage/StorageHTTPClient.swift @@ -7,10 +7,11 @@ import HTTPTypesFoundation #endif public struct StorageHTTPSession: Sendable { - public var fetch: @Sendable ( - _ request: HTTPRequest, - _ bodyData: Data? - ) async throws -> (Data, HTTPResponse) + public var fetch: + @Sendable ( + _ request: HTTPRequest, + _ bodyData: Data? + ) async throws -> (Data, HTTPResponse) public init( fetch: @escaping @Sendable ( diff --git a/Sources/Supabase/SupabaseClient.swift b/Sources/Supabase/SupabaseClient.swift index 196e508e..93abdff5 100644 --- a/Sources/Supabase/SupabaseClient.swift +++ b/Sources/Supabase/SupabaseClient.swift @@ -116,7 +116,7 @@ public final class SupabaseClient: Sendable { return $0.functions! } } - + /// Headers provided to the inner clients on initialization. /// /// - Note: This collection is non-mutable, if you want to provide different headers, pass it in ``SupabaseClientOptions/GlobalOptions/headers``. @@ -181,7 +181,7 @@ public final class SupabaseClient: Sendable { _auth = AuthClient( url: supabaseURL.appendingPathComponent("/auth/v1"), - headers: self.headers, + headers: self.headers, flowType: options.auth.flowType, redirectToURL: options.auth.redirectToURL, storageKey: options.auth.storageKey ?? defaultStorageKey, @@ -209,7 +209,7 @@ public final class SupabaseClient: Sendable { ) var realtimeOptions = options.realtime - realtimeOptions.headers.merge(self.headers) { $1 } + realtimeOptions.headers.merge(self.headers) { $1 } if realtimeOptions.logger == nil { realtimeOptions.logger = options.global.logger } diff --git a/Sources/TestHelpers/HTTPClientMock.swift b/Sources/TestHelpers/HTTPClientMock.swift index aa09ecc9..57a1402b 100644 --- a/Sources/TestHelpers/HTTPClientMock.swift +++ b/Sources/TestHelpers/HTTPClientMock.swift @@ -7,12 +7,12 @@ import ConcurrencyExtras import Foundation +import HTTPTypes import Helpers import XCTestDynamicOverlay -import HTTPTypes package actor HTTPClientMock: HTTPClientType { - + package struct MockNotFound: Error {} private var mocks = [@Sendable (HTTPRequest, Data?) async throws -> (Data, HTTPResponse)?]() @@ -46,7 +46,10 @@ package actor HTTPClientMock: HTTPClientType { when({ _, _ in true }, return: response) } - package func send(_ request: HTTPRequest, _ bodyData: Data?) async throws -> (Data, HTTPResponse) { + package func send( + _ request: HTTPRequest, + _ bodyData: Data? + ) async throws -> (Data, HTTPResponse) { receivedRequests.append((request, bodyData)) for mock in mocks {