Skip to content

Commit ee57ffe

Browse files
peterfriesepaulb777
authored andcommitted
Allow caller to use type inferrence when calling HTTPSCallable functions
Signed-off-by: Peter Friese <[email protected]>
1 parent 694760b commit ee57ffe

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

FirebaseFunctionsSwift/Sources/Codable/Callable+Codable.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ public extension Functions {
3636
-> Callable<Request, Response> {
3737
return Callable(callable: httpsCallable(name), encoder: encoder, decoder: decoder)
3838
}
39+
40+
/// Creates a reference to the Callable HTTPS trigger with the given name. The types of the `Encodable`
41+
/// request and tyhe `Decodable` response will be inferred by the compiler.
42+
///
43+
/// At the call site, use the following syntax:
44+
///
45+
/// let greeter: Callable<DataTestRequest, DataTestResponse> = functions.httpsCallable("greeter")
46+
/// try greeter(data) { result in
47+
/// print(result.greeting)
48+
/// }
49+
///
50+
/// - Parameters:
51+
/// - name: The name of the Callable HTTPS trigger
52+
func httpsCallable<Request, Response>(_ name: String,
53+
encoder: StructureEncoder = StructureEncoder(),
54+
decoder: StructureDecoder = StructureDecoder())
55+
-> Callable<Request, Response> where Request: Encodable, Response: Decodable {
56+
return Callable(callable: httpsCallable(name), encoder: encoder, decoder: decoder)
57+
}
3958
}
4059

4160
/**

FirebaseFunctionsSwift/Tests/IntegrationTests.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,4 +587,58 @@ class IntegrationTests: XCTestCase {
587587
XCTAssertEqual(response, expected)
588588
}
589589
#endif
590+
591+
func testInferredTypes() throws {
592+
let expectation = expectation(description: #function)
593+
let data = DataTestRequest(
594+
bool: true,
595+
int: 2,
596+
long: 9_876_543_210,
597+
string: "four",
598+
array: [5, 6],
599+
null: nil
600+
)
601+
let function: Callable<DataTestRequest, DataTestResponse> = functions.httpsCallable("dataTest")
602+
603+
try function(data) { result in
604+
do {
605+
let response = try result.get()
606+
let expected = DataTestResponse(
607+
message: "stub response",
608+
long: 420,
609+
code: 42
610+
)
611+
XCTAssertEqual(response, expected)
612+
expectation.fulfill()
613+
} catch {
614+
XCTAssert(false, "Failed to unwrap the function result: \(error)")
615+
}
616+
}
617+
waitForExpectations(timeout: 5)
618+
}
619+
620+
#if compiler(>=5.5) && canImport(_Concurrency)
621+
@available(iOS 15, tvOS 15, macOS 12, watchOS 8, *)
622+
func testInferredTyesAsync() async throws {
623+
let data = DataTestRequest(
624+
bool: true,
625+
int: 2,
626+
long: 9_876_543_210,
627+
string: "four",
628+
array: [5, 6],
629+
null: nil
630+
)
631+
632+
let function: Callable<DataTestRequest, DataTestResponse> = functions
633+
.httpsCallable("dataTest")
634+
635+
let response = try await function(data)
636+
let expected = DataTestResponse(
637+
message: "stub response",
638+
long: 420,
639+
code: 42
640+
)
641+
XCTAssertEqual(response, expected)
642+
}
643+
#endif
590644
}

0 commit comments

Comments
 (0)