Skip to content

Commit 477cced

Browse files
committed
Documentation
1 parent 479a311 commit 477cced

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

Diff for: Sources/Functions/FunctionsClient.swift

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import Foundation
22

3+
/// An actor representing a client for invoking functions.
34
public actor FunctionsClient {
5+
/// Typealias for the fetch handler used to make requests.
46
public typealias FetchHandler = @Sendable (_ request: URLRequest) async throws -> (
57
Data, URLResponse
68
)
79

10+
/// The base URL for the functions.
811
let url: URL
12+
/// Headers to be included in the requests.
913
var headers: [String: String]
14+
/// The fetch handler used to make requests.
1015
let fetch: FetchHandler
1116

17+
/// Initializes a new instance of `FunctionsClient`.
18+
///
19+
/// - Parameters:
20+
/// - url: The base URL for the functions.
21+
/// - headers: Headers to be included in the requests. (Default: empty dictionary)
22+
/// - fetch: The fetch handler used to make requests. (Default: URLSession.shared.data(for:))
1223
public init(
1324
url: URL,
1425
headers: [String: String] = [:],
@@ -21,63 +32,70 @@ public actor FunctionsClient {
2132
}
2233

2334
/// Updates the authorization header.
24-
/// - Parameter token: the new JWT token sent in the authorization header
35+
///
36+
/// - Parameter token: The new JWT token sent in the authorization header.
2537
public func setAuth(token: String) {
2638
headers["Authorization"] = "Bearer \(token)"
2739
}
2840

29-
/// Invokes a function.
41+
/// Invokes a function and decodes the response.
42+
///
3043
/// - Parameters:
31-
/// - functionName: the name of the function to invoke.
44+
/// - functionName: The name of the function to invoke.
45+
/// - invokeOptions: Options for invoking the function. (Default: empty `FunctionInvokeOptions`)
46+
/// - decode: A closure to decode the response data and HTTPURLResponse into a `Response` object.
47+
/// - Returns: The decoded `Response` object.
3248
public func invoke<Response>(
3349
functionName: String,
3450
invokeOptions: FunctionInvokeOptions = .init(),
3551
decode: (Data, HTTPURLResponse) throws -> Response
3652
) async throws -> Response {
3753
let (data, response) = try await rawInvoke(
38-
functionName: functionName,
39-
invokeOptions: invokeOptions
40-
)
54+
functionName: functionName, invokeOptions: invokeOptions)
4155
return try decode(data, response)
4256
}
4357

44-
/// Invokes a function.
58+
/// Invokes a function and decodes the response as a specific type.
59+
///
4560
/// - Parameters:
46-
/// - functionName: the name of the function to invoke.
61+
/// - functionName: The name of the function to invoke.
62+
/// - invokeOptions: Options for invoking the function. (Default: empty `FunctionInvokeOptions`)
63+
/// - decoder: The JSON decoder to use for decoding the response. (Default: `JSONDecoder()`)
64+
/// - Returns: The decoded object of type `T`.
4765
public func invoke<T: Decodable>(
4866
functionName: String,
4967
invokeOptions: FunctionInvokeOptions = .init(),
5068
decoder: JSONDecoder = JSONDecoder()
5169
) async throws -> T {
52-
try await invoke(
53-
functionName: functionName,
54-
invokeOptions: invokeOptions,
55-
decode: { data, _ in try decoder.decode(T.self, from: data) }
56-
)
70+
try await invoke(functionName: functionName, invokeOptions: invokeOptions) { data, _ in
71+
try decoder.decode(T.self, from: data)
72+
}
5773
}
5874

59-
/// Invokes a function.
75+
/// Invokes a function without expecting a response.
76+
///
6077
/// - Parameters:
61-
/// - functionName: the name of the function to invoke.
78+
/// - functionName: The name of the function to invoke.
79+
/// - invokeOptions: Options for invoking the function. (Default: empty `FunctionInvokeOptions`)
6280
public func invoke(
6381
functionName: String,
6482
invokeOptions: FunctionInvokeOptions = .init()
6583
) async throws {
66-
try await invoke(
67-
functionName: functionName,
68-
invokeOptions: invokeOptions,
69-
decode: { _, _ in () }
70-
)
84+
try await invoke(functionName: functionName, invokeOptions: invokeOptions) { _, _ in () }
7185
}
7286

7387
private func rawInvoke(
7488
functionName: String,
7589
invokeOptions: FunctionInvokeOptions
7690
) async throws -> (Data, HTTPURLResponse) {
7791
let url = self.url.appendingPathComponent(functionName)
78-
7992
var urlRequest = URLRequest(url: url)
80-
urlRequest.allHTTPHeaderFields = invokeOptions.headers.merging(headers) { first, _ in first }
93+
94+
let headers = invokeOptions.headers.merging(self.headers) { invokeOption, _ in invokeOption }
95+
headers.forEach { key, value in
96+
urlRequest.setValue(value, forHTTPHeaderField: key)
97+
}
98+
8199
urlRequest.httpMethod = "POST"
82100
urlRequest.httpBody = invokeOptions.body
83101

Diff for: Sources/Functions/Types.swift

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
11
import Foundation
22

3+
/// An error type representing various errors that can occur while invoking functions.
34
public enum FunctionsError: Error, LocalizedError {
5+
/// Error indicating a relay error while invoking the Edge Function.
46
case relayError
7+
/// Error indicating a non-2xx status code returned by the Edge Function.
58
case httpError(code: Int, data: Data)
69

10+
/// A localized description of the error.
711
public var errorDescription: String? {
812
switch self {
9-
case .relayError: return "Relay Error invoking the Edge Function"
10-
case let .httpError(code, _): return "Edge Function returned a non-2xx status code: \(code)"
13+
case .relayError:
14+
return "Relay Error invoking the Edge Function"
15+
case let .httpError(code, _):
16+
return "Edge Function returned a non-2xx status code: \(code)"
1117
}
1218
}
1319
}
1420

21+
/// Options for invoking a function.
1522
public struct FunctionInvokeOptions {
23+
/// Headers to be included in the function invocation.
1624
let headers: [String: String]
25+
/// Body data to be sent with the function invocation.
1726
let body: Data?
1827

28+
/// Initializes the `FunctionInvokeOptions` structure.
29+
///
30+
/// - Parameters:
31+
/// - headers: Headers to be included in the function invocation. (Default: empty dictionary)
32+
/// - body: The body data to be sent with the function invocation. (Default: nil)
1933
public init(headers: [String: String] = [:], body: some Encodable) {
2034
var headers = headers
2135

@@ -35,6 +49,9 @@ public struct FunctionInvokeOptions {
3549
self.headers = headers
3650
}
3751

52+
/// Initializes the `FunctionInvokeOptions` structure.
53+
///
54+
/// - Parameter headers: Headers to be included in the function invocation. (Default: empty dictionary)
3855
public init(headers: [String: String] = [:]) {
3956
self.headers = headers
4057
body = nil

0 commit comments

Comments
 (0)