feat: add ability to customize JSONDecoder

This commit is contained in:
Kyle Fang
2023-07-30 09:12:31 +08:00
parent c8799a673c
commit d5e0a731b1

View File

@@ -2,9 +2,13 @@ import Foundation
public protocol ServiceExecutor {
func execute(_ request: Request) async throws -> (Data, HTTPURLResponse)
var jsonDecoder: JSONDecoder? { get }
}
extension ServiceExecutor {
public var jsonDecoder: JSONDecoder? {
nil
}
public func callAsFunction<Output: Decodable>(_ request: Request) async throws -> Output {
let (output, _): (Output, _) = try await self(request)
return output
@@ -12,7 +16,7 @@ extension ServiceExecutor {
public func callAsFunction<Output: Decodable>(_ request: Request) async throws -> (Output, HTTPURLResponse) {
let (data, response): (Data, HTTPURLResponse) = try await self.execute(request)
let output = try JSONDecoder().decode(Output.self, from: data)
let output = try (jsonDecoder ?? JSONDecoder()).decode(Output.self, from: data)
return (output, response)
}