Skip to content

Commit d740b00

Browse files
committed
Remove Get dependency
1 parent c680cdf commit d740b00

File tree

5 files changed

+114
-38
lines changed

5 files changed

+114
-38
lines changed

Package.resolved

Lines changed: 0 additions & 14 deletions
This file was deleted.

Package.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ let package = Package(
1212
.tvOS(.v13),
1313
],
1414
products: [
15-
.library(name: "Functions", targets: ["Functions"]),
16-
],
17-
dependencies: [
18-
.package(url: "https://github.com/kean/Get", from: "2.1.5"),
15+
.library(name: "Functions", targets: ["Functions"])
1916
],
17+
dependencies: [],
2018
targets: [
2119
.target(
2220
name: "Functions",
23-
dependencies: ["Get"]
21+
dependencies: []
2422
),
2523
.testTarget(
2624
name: "FunctionsTests",

Sources/Functions/FunctionsClient.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
@preconcurrency import Foundation
2-
@preconcurrency import Get
1+
import Foundation
32

43
public final class FunctionsClient {
4+
public typealias FetchHandler = (URLRequest) async throws -> (Data, URLResponse)
5+
56
let url: URL
67
var headers: [String: String]
78

8-
let client: APIClient
9+
private let fetch: FetchHandler
910

1011
public init(
1112
url: URL,
1213
headers: [String: String] = [:],
13-
apiClientDelegate: APIClientDelegate? = nil
14+
fetch: @escaping FetchHandler = URLSession.shared.data(for:)
1415
) {
1516
self.url = url
1617
self.headers = headers
1718
self.headers["X-Client-Info"] = "functions-swift/\(version)"
18-
client = APIClient(baseURL: url) {
19-
$0.delegate = apiClientDelegate
20-
}
19+
self.fetch = fetch
2120
}
2221

2322
/// Updates the authorization header.
@@ -74,28 +73,28 @@ public final class FunctionsClient {
7473
functionName: String,
7574
invokeOptions: FunctionInvokeOptions
7675
) async throws -> (Data, HTTPURLResponse) {
77-
let request = Request(
78-
path: functionName,
79-
method: .post,
80-
body: invokeOptions.body,
81-
headers: invokeOptions.headers.merging(headers) { first, _ in first }
82-
)
76+
let url = self.url.appendingPathComponent(functionName)
77+
78+
var urlRequest = URLRequest(url: url)
79+
urlRequest.allHTTPHeaderFields = invokeOptions.headers.merging(headers) { first, _ in first }
80+
urlRequest.httpMethod = "POST"
81+
urlRequest.httpBody = invokeOptions.body
8382

84-
let response = try await client.data(for: request)
83+
let (data, response) = try await fetch(urlRequest)
8584

86-
guard let httpResponse = response.response as? HTTPURLResponse else {
85+
guard let httpResponse = response as? HTTPURLResponse else {
8786
throw URLError(.badServerResponse)
8887
}
8988

90-
guard 200 ..< 300 ~= httpResponse.statusCode else {
91-
throw FunctionsError.httpError(code: httpResponse.statusCode, data: response.data)
89+
guard 200..<300 ~= httpResponse.statusCode else {
90+
throw FunctionsError.httpError(code: httpResponse.statusCode, data: data)
9291
}
9392

9493
let isRelayError = httpResponse.value(forHTTPHeaderField: "x-relay-error") == "true"
9594
if isRelayError {
9695
throw FunctionsError.relayError
9796
}
9897

99-
return (response.data, httpResponse)
98+
return (data, httpResponse)
10099
}
101100
}

Tests/FunctionsTests/FunctionInvokeOptionsTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
@testable import Functions
21
import XCTest
32

3+
@testable import Functions
4+
45
final class FunctionInvokeOptionsTests: XCTestCase {
56
func testStringBody() {
67
let options = FunctionInvokeOptions(body: "string value")
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import XCTest
2+
3+
@testable import Functions
4+
5+
final class FunctionsClientTests: XCTestCase {
6+
let url = URL(string: "http://localhost:5432/functions/v1")!
7+
let apiKey = "supabase.anon.key"
8+
9+
func testInvoke() async throws {
10+
var _request: URLRequest?
11+
let sut = FunctionsClient(url: url, headers: ["apikey": apiKey]) {
12+
_request = $0
13+
return (Data(), HTTPURLResponse.mock(url: self.url))
14+
}
15+
16+
let body = ["name": "Supabase"]
17+
18+
try await sut.invoke(
19+
functionName: "hello_world",
20+
invokeOptions: .init(headers: ["X-Custom-Key": "value"], body: body)
21+
)
22+
23+
let request = try XCTUnwrap(_request)
24+
25+
XCTAssertEqual(request.url, URL(string: "http://localhost:5432/functions/v1/hello_world"))
26+
XCTAssertEqual(request.httpMethod, "POST")
27+
XCTAssertEqual(request.value(forHTTPHeaderField: "apikey"), apiKey)
28+
XCTAssertEqual(request.value(forHTTPHeaderField: "X-Custom-Key"), "value")
29+
XCTAssertEqual(
30+
request.value(forHTTPHeaderField: "X-Client-Info"), "functions-swift/\(Functions.version)")
31+
}
32+
33+
func testInvoke_shouldThrow_URLError_badServerResponse() async {
34+
let sut = FunctionsClient(url: url) { _ in
35+
(Data(), URLResponse())
36+
}
37+
38+
do {
39+
try await sut.invoke(functionName: "hello_world")
40+
} catch let urlError as URLError {
41+
XCTAssertEqual(urlError.code, .badServerResponse)
42+
} catch {
43+
XCTFail("Unexpected error thrown \(error)")
44+
}
45+
}
46+
47+
func testInvoke_shouldThrow_FunctionsError_httpError() async {
48+
let sut = FunctionsClient(url: url) { _ in
49+
(
50+
"error".data(using: .utf8)!,
51+
HTTPURLResponse.mock(url: self.url, statusCode: 300)
52+
)
53+
}
54+
55+
do {
56+
try await sut.invoke(functionName: "hello_world")
57+
XCTFail("Invoke should fail.")
58+
} catch let FunctionsError.httpError(code, data) {
59+
XCTAssertEqual(code, 300)
60+
XCTAssertEqual(data, "error".data(using: .utf8))
61+
} catch {
62+
XCTFail("Unexpected error thrown \(error)")
63+
}
64+
}
65+
66+
func testInvoke_shouldThrow_FunctionsError_relayError() async {
67+
let sut = FunctionsClient(url: url) { _ in
68+
(
69+
Data(),
70+
HTTPURLResponse.mock(url: self.url, headerFields: ["x-relay-error": "true"])
71+
)
72+
}
73+
74+
do {
75+
try await sut.invoke(functionName: "hello_world")
76+
XCTFail("Invoke should fail.")
77+
} catch FunctionsError.relayError {
78+
} catch {
79+
XCTFail("Unexpected error thrown \(error)")
80+
}
81+
}
82+
}
83+
84+
extension HTTPURLResponse {
85+
static func mock(
86+
url: URL,
87+
statusCode: Int = 200,
88+
headerFields: [String: String]? = nil
89+
) -> HTTPURLResponse {
90+
HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: nil, headerFields: headerFields)!
91+
}
92+
}

0 commit comments

Comments
 (0)