• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

supabase / supabase-swift / 17321717294

29 Aug 2025 10:44AM UTC coverage: 78.634% (+1.2%) from 77.386%
17321717294

Pull #781

github

web-flow
Merge 80b20054e into e4d8c3718
Pull Request #781: RFC: Migrate HTTP networking from URLSession to Alamofire

1027 of 1123 new or added lines in 27 files covered. (91.45%)

27 existing lines in 8 files now uncovered.

5156 of 6557 relevant lines covered (78.63%)

29.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/Sources/Functions/Types.swift
1
import Alamofire
2
import Foundation
3

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

11
  case unknown(any Error)
12

13
  /// A localized description of the error.
14
  public var errorDescription: String? {
2✔
15
    switch self {
2✔
16
    case .relayError:
2✔
17
      "Relay Error invoking the Edge Function"
1✔
18
    case let .httpError(code, _):
2✔
19
      "Edge Function returned a non-2xx status code: \(code)"
1✔
20
    case let .unknown(error):
2✔
NEW
21
      "Unkown error: \(error.localizedDescription)"
×
22
    }
2✔
23
  }
2✔
24
}
25

26
func mapToFunctionsError(_ error: any Error) -> FunctionsError {
6✔
27
  if let error = error as? FunctionsError {
6✔
NEW
28
    return error
×
29
  }
6✔
30

6✔
31
  if let error = error.asAFError,
6✔
32
    let underlyingError = error.underlyingError as? FunctionsError
6✔
33
  {
6✔
34
    return underlyingError
4✔
35
  }
4✔
36

2✔
37
  return FunctionsError.unknown(error)
2✔
38
}
6✔
39

40
/// Options for invoking a function.
41
public struct FunctionInvokeOptions: Sendable {
42
  /// Method to use in the function invocation.
43
  let method: Method?
44
  /// Headers to be included in the function invocation.
45
  let headers: HTTPHeaders
46
  /// Body data to be sent with the function invocation.
47
  let body: Data?
48
  /// The Region to invoke the function in.
49
  let region: String?
50
  /// The query to be included in the function invocation.
51
  let query: [URLQueryItem]
52

53
  /// Initializes the `FunctionInvokeOptions` structure.
54
  ///
55
  /// - Parameters:
56
  ///   - method: Method to use in the function invocation.
57
  ///   - query: The query to be included in the function invocation.
58
  ///   - headers: Headers to be included in the function invocation. (Default: empty dictionary)
59
  ///   - region: The Region to invoke the function in.
60
  ///   - body: The body data to be sent with the function invocation. (Default: nil)
61
  @_disfavoredOverload
62
  public init(
63
    method: Method? = nil,
64
    query: [URLQueryItem] = [],
65
    headers: [String: String] = [:],
66
    region: String? = nil,
67
    body: some Encodable
68
  ) {
5✔
69
    var defaultHeaders = HTTPHeaders()
5✔
70

5✔
71
    switch body {
5✔
72
    case let string as String:
5✔
73
      defaultHeaders["Content-Type"] = "text/plain"
1✔
74
      self.body = string.data(using: .utf8)
1✔
75
    case let data as Data:
5✔
76
      defaultHeaders["Content-Type"] = "application/octet-stream"
2✔
77
      self.body = data
2✔
78
    default:
5✔
79
      // default, assume this is JSON
2✔
80
      defaultHeaders["Content-Type"] = "application/json"
2✔
81
      self.body = try? JSONEncoder().encode(body)
2✔
82
    }
5✔
83

5✔
84
    headers.forEach {
5✔
85
      defaultHeaders[$0.key] = $0.value
2✔
86
    }
2✔
87

5✔
88
    self.method = method
5✔
89
    self.headers = defaultHeaders
5✔
90
    self.region = region
5✔
91
    self.query = query
5✔
92
  }
5✔
93

94
  /// Initializes the `FunctionInvokeOptions` structure.
95
  ///
96
  /// - Parameters:
97
  ///   - method: Method to use in the function invocation.
98
  ///   - query: The query to be included in the function invocation.
99
  ///   - headers: Headers to be included in the function invocation. (Default: empty dictionary)
100
  ///   - region: The Region to invoke the function in.
101
  @_disfavoredOverload
102
  public init(
103
    method: Method? = nil,
104
    query: [URLQueryItem] = [],
105
    headers: [String: String] = [:],
106
    region: String? = nil
107
  ) {
14✔
108
    self.method = method
14✔
109
    self.headers = HTTPHeaders(headers)
14✔
110
    self.region = region
14✔
111
    self.query = query
14✔
112
    body = nil
14✔
113
  }
14✔
114

115
  public enum Method: String, Sendable {
116
    case get = "GET"
117
    case post = "POST"
118
    case put = "PUT"
119
    case patch = "PATCH"
120
    case delete = "DELETE"
121
  }
122

123
  static func httpMethod(_ method: Method?) -> HTTPMethod? {
20✔
124
    switch method {
20✔
125
    case .get:
20✔
126
      .get
1✔
127
    case .post:
20✔
128
      .post
1✔
129
    case .put:
20✔
130
      .put
1✔
131
    case .patch:
20✔
132
      .patch
1✔
133
    case .delete:
20✔
134
      .delete
2✔
135
    case nil:
20✔
136
      nil
14✔
137
    }
20✔
138
  }
20✔
139
}
140

141
public enum FunctionRegion: String, Sendable {
142
  case apNortheast1 = "ap-northeast-1"
143
  case apNortheast2 = "ap-northeast-2"
144
  case apSouth1 = "ap-south-1"
145
  case apSoutheast1 = "ap-southeast-1"
146
  case apSoutheast2 = "ap-southeast-2"
147
  case caCentral1 = "ca-central-1"
148
  case euCentral1 = "eu-central-1"
149
  case euWest1 = "eu-west-1"
150
  case euWest2 = "eu-west-2"
151
  case euWest3 = "eu-west-3"
152
  case saEast1 = "sa-east-1"
153
  case usEast1 = "us-east-1"
154
  case usWest1 = "us-west-1"
155
  case usWest2 = "us-west-2"
156
}
157

158
extension FunctionInvokeOptions {
159
  /// Initializes the `FunctionInvokeOptions` structure.
160
  ///
161
  /// - Parameters:
162
  ///   - method: Method to use in the function invocation.
163
  ///   - headers: Headers to be included in the function invocation. (Default: empty dictionary)
164
  ///   - region: The Region to invoke the function in.
165
  ///   - body: The body data to be sent with the function invocation. (Default: nil)
166
  public init(
167
    method: Method? = nil,
168
    headers: [String: String] = [:],
169
    region: FunctionRegion? = nil,
170
    body: some Encodable
171
  ) {
5✔
172
    self.init(
5✔
173
      method: method,
5✔
174
      headers: headers,
5✔
175
      region: region?.rawValue,
5✔
176
      body: body
5✔
177
    )
5✔
178
  }
5✔
179

180
  /// Initializes the `FunctionInvokeOptions` structure.
181
  ///
182
  /// - Parameters:
183
  ///   - method: Method to use in the function invocation.
184
  ///   - headers: Headers to be included in the function invocation. (Default: empty dictionary)
185
  ///   - region: The Region to invoke the function in.
186
  public init(
187
    method: Method? = nil,
188
    headers: [String: String] = [:],
189
    region: FunctionRegion? = nil
190
  ) {
13✔
191
    self.init(method: method, headers: headers, region: region?.rawValue)
13✔
192
  }
13✔
193
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc