From 7bc8ed4d8c62dd562877eaea06bd29f66a561add Mon Sep 17 00:00:00 2001 From: Renato Almeida Date: Mon, 10 Jul 2023 14:11:17 +0100 Subject: [PATCH] feat: add mapToContent function to Internal Response --- Sources/YData/Client/InternalResponse+Vapor.swift | 4 ++-- Sources/YData/Client/InternalResponse.swift | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/YData/Client/InternalResponse+Vapor.swift b/Sources/YData/Client/InternalResponse+Vapor.swift index 03149e4..4b806f0 100644 --- a/Sources/YData/Client/InternalResponse+Vapor.swift +++ b/Sources/YData/Client/InternalResponse+Vapor.swift @@ -18,8 +18,8 @@ public extension EventLoopFuture where Value: InternalResponse { func mapContent(_ callback: @escaping (ContentContainer) throws -> (NewValue)) -> EventLoopFuture where NewValue: Content { flatMapThrowing { try $0.map(callback) } } - func mapToContent() -> EventLoopFuture where R: Decodable { - flatMapThrowing { response -> R in try response.content.decode(R.self) } + func mapToContent(_ type: D.Type = D.self) -> EventLoopFuture { + flatMapThrowing { response -> D in try response.content.decode(type) } } func flatMapContentThrowing(_ callback: @escaping (ContentContainer) throws -> (NewValue)) diff --git a/Sources/YData/Client/InternalResponse.swift b/Sources/YData/Client/InternalResponse.swift index 39648ca..ef87c8d 100644 --- a/Sources/YData/Client/InternalResponse.swift +++ b/Sources/YData/Client/InternalResponse.swift @@ -28,6 +28,11 @@ public extension InternalResponse { return Self.init(headers: headers, status: status, body: nil) } + + @inlinable + func mapToContent(_ type: D.Type = D.self) throws -> D { + try content.decode(type) + } } public extension Internal {