From d5e0a731b1da6c02810d801a50757e85b68f0348 Mon Sep 17 00:00:00 2001 From: Kyle Fang Date: Sun, 30 Jul 2023 09:12:31 +0800 Subject: [PATCH] feat: add ability to customize JSONDecoder --- Sources/SwiftRequest/ServiceExecutor.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftRequest/ServiceExecutor.swift b/Sources/SwiftRequest/ServiceExecutor.swift index a614480..d2dba37 100644 --- a/Sources/SwiftRequest/ServiceExecutor.swift +++ b/Sources/SwiftRequest/ServiceExecutor.swift @@ -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(_ request: Request) async throws -> Output { let (output, _): (Output, _) = try await self(request) return output @@ -12,7 +16,7 @@ extension ServiceExecutor { public func callAsFunction(_ 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) }