1- import ConcurrencyExtras
21import Foundation
32import HTTPTypes
43
@@ -9,11 +8,12 @@ import HTTPTypes
98let version = Helpers . version
109
1110/// An actor representing a client for invoking functions.
12- public final class FunctionsClient : Sendable {
11+ public actor FunctionsClient {
1312 /// Fetch handler used to make requests.
14- public typealias FetchHandler = @Sendable ( _ request: URLRequest ) async throws -> (
15- Data , URLResponse
16- )
13+ public typealias FetchHandler =
14+ @Sendable ( _ request: URLRequest ) async throws -> (
15+ Data , URLResponse
16+ )
1717
1818 /// Request idle timeout: 150s (If an Edge Function doesn't send a response before the timeout, 504 Gateway Timeout will be returned)
1919 ///
@@ -26,19 +26,12 @@ public final class FunctionsClient: Sendable {
2626 /// The Region to invoke the functions in.
2727 let region : FunctionRegion ?
2828
29- struct MutableState {
30- /// Headers to be included in the requests.
31- var headers = HTTPFields ( )
32- }
29+ /// Headers to be included in the requests.
30+ var headers = HTTPFields ( )
3331
3432 private let http : any HTTPClientType
35- private let mutableState = LockIsolated ( MutableState ( ) )
3633 private let sessionConfiguration : URLSessionConfiguration
3734
38- var headers : HTTPFields {
39- mutableState. headers
40- }
41-
4235 /// Initializes a new instance of `FunctionsClient`.
4336 ///
4437 /// - Parameters:
@@ -47,8 +40,7 @@ public final class FunctionsClient: Sendable {
4740 /// - region: The Region to invoke the functions in.
4841 /// - logger: SupabaseLogger instance to use.
4942 /// - fetch: The fetch handler used to make requests. (Default: URLSession.shared.data(for:))
50- @_disfavoredOverload
51- public convenience init (
43+ public init (
5244 url: URL ,
5345 headers: [ String : String ] = [ : ] ,
5446 region: FunctionRegion ? = nil ,
@@ -65,7 +57,7 @@ public final class FunctionsClient: Sendable {
6557 )
6658 }
6759
68- convenience init (
60+ init (
6961 url: URL ,
7062 headers: [ String : String ] = [ : ] ,
7163 region: FunctionRegion ? = nil ,
@@ -101,24 +93,20 @@ public final class FunctionsClient: Sendable {
10193 self . http = http
10294 self . sessionConfiguration = sessionConfiguration
10395
104- mutableState. withValue {
105- $0. headers = HTTPFields ( headers)
106- if $0. headers [ . xClientInfo] == nil {
107- $0. headers [ . xClientInfo] = " functions-swift/ \( version) "
108- }
96+ self . headers = HTTPFields ( headers)
97+ if self . headers [ . xClientInfo] == nil {
98+ self . headers [ . xClientInfo] = " functions-swift/ \( version) "
10999 }
110100 }
111101
112102 /// Updates the authorization header.
113103 ///
114104 /// - Parameter token: The new JWT token sent in the authorization header.
115105 public func setAuth( token: String ? ) {
116- mutableState. withValue {
117- if let token {
118- $0. headers [ . authorization] = " Bearer \( token) "
119- } else {
120- $0. headers [ . authorization] = nil
121- }
106+ if let token {
107+ headers [ . authorization] = " Bearer \( token) "
108+ } else {
109+ headers [ . authorization] = nil
122110 }
123111 }
124112
@@ -136,7 +124,8 @@ public final class FunctionsClient: Sendable {
136124 decode: ( Data , HTTPURLResponse ) throws -> Response
137125 ) async throws -> Response {
138126 let response = try await rawInvoke (
139- functionName: functionName, invokeOptions: options
127+ functionName: functionName,
128+ invokeOptions: options
140129 )
141130 return try decode ( response. data, response. underlyingResponse)
142131 }
@@ -208,7 +197,10 @@ public final class FunctionsClient: Sendable {
208197 let delegate = StreamResponseDelegate ( continuation: continuation)
209198
210199 let session = URLSession (
211- configuration: sessionConfiguration, delegate: delegate, delegateQueue: nil )
200+ configuration: sessionConfiguration,
201+ delegate: delegate,
202+ delegateQueue: nil
203+ )
212204
213205 let urlRequest = buildRequest ( functionName: functionName, options: invokeOptions) . urlRequest
214206
@@ -233,7 +225,7 @@ public final class FunctionsClient: Sendable {
233225 url: url. appendingPathComponent ( functionName) ,
234226 method: FunctionInvokeOptions . httpMethod ( options. method) ?? . post,
235227 query: query,
236- headers: mutableState . headers. merging ( with: options. headers) ,
228+ headers: headers. merging ( with: options. headers) ,
237229 body: options. body,
238230 timeoutInterval: FunctionsClient . requestIdleTimeout
239231 )
@@ -264,7 +256,9 @@ final class StreamResponseDelegate: NSObject, URLSessionDataDelegate, Sendable {
264256 }
265257
266258 func urlSession(
267- _: URLSession , dataTask _: URLSessionDataTask , didReceive response: URLResponse ,
259+ _: URLSession ,
260+ dataTask _: URLSessionDataTask ,
261+ didReceive response: URLResponse ,
268262 completionHandler: @escaping ( URLSession . ResponseDisposition ) -> Void
269263 ) {
270264 defer {
0 commit comments