Skip to content

Commit e4d905f

Browse files
authored
Merge pull request #97 from 417-72KI/only-decodable
Make type for `load/post` just `Decodable`
2 parents 3eddc76 + e6af77c commit e4d905f

2 files changed

Lines changed: 27 additions & 25 deletions

File tree

Sources/RequestKit/JSONPostRouter.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import FoundationNetworking
55

66
public protocol JSONPostRouter: Router {
77
func postJSON<T>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
8-
func post<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
9-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
10-
func post<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
8+
func post<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
9+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
10+
func post<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void)
11+
-> URLSessionDataTaskProtocol?
1112

1213
#if compiler(>=5.5.2) && canImport(_Concurrency)
1314
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
1415
func postJSON<T>(_ session: RequestKitURLSession, expectedResultType: T.Type) async throws -> T?
1516

1617
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
17-
func post<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
18+
func post<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
1819
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
19-
func post<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T
20+
func post<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type) async throws -> T
2021
#endif
2122
}
2223

@@ -91,8 +92,8 @@ public extension JSONPostRouter {
9192
}
9293
#endif
9394

94-
func post<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
95-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
95+
func post<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
96+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
9697
{
9798
let decoder = JSONDecoder()
9899
if let dateDecodingStrategy = dateDecodingStrategy {
@@ -101,8 +102,8 @@ public extension JSONPostRouter {
101102
return post(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion)
102103
}
103104

104-
func post<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
105-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
105+
func post<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
106+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
106107
{
107108
guard let request = request() else {
108109
return nil
@@ -148,7 +149,7 @@ public extension JSONPostRouter {
148149

149150
#if compiler(>=5.5.2) && canImport(_Concurrency)
150151
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
151-
func post<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
152+
func post<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
152153
let decoder = JSONDecoder()
153154
if let dateDecodingStrategy = dateDecodingStrategy {
154155
decoder.dateDecodingStrategy = dateDecodingStrategy
@@ -157,7 +158,7 @@ public extension JSONPostRouter {
157158
}
158159

159160
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
160-
func post<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
161+
func post<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
161162
guard let request = request() else {
162163
throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil)
163164
}

Sources/RequestKit/Router.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,18 @@ public protocol Router {
5858

5959
func urlQuery(_ parameters: [String: Any]) -> [URLQueryItem]?
6060
func request(_ urlComponents: URLComponents, parameters: [String: Any]) -> URLRequest?
61-
func loadJSON<T: Codable>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
62-
func load<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
63-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
64-
func load<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
61+
func loadJSON<T: Decodable>(_ session: RequestKitURLSession, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
62+
func load<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
63+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
64+
func load<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType: T.Type, completion: @escaping (_ json: T?, _ error: Error?) -> Void)
65+
-> URLSessionDataTaskProtocol?
6566
func request() -> URLRequest?
6667

6768
#if compiler(>=5.5.2) && canImport(_Concurrency)
6869
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
69-
func load<T: Codable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T
70+
func load<T: Decodable>(_ session: RequestKitURLSession, decoder: JSONDecoder, expectedResultType _: T.Type) async throws -> T
7071
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
71-
func load<T: Codable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
72+
func load<T: Decodable>(_ session: RequestKitURLSession, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T
7273
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
7374
func load(_ session: RequestKitURLSession) async throws
7475
#endif
@@ -150,14 +151,14 @@ public extension Router {
150151
}
151152

152153
@available(*, deprecated, message: "Plase use `load` method instead")
153-
func loadJSON<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type,
154-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
154+
func loadJSON<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, expectedResultType: T.Type,
155+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
155156
{
156157
return load(session, expectedResultType: expectedResultType, completion: completion)
157158
}
158159

159-
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
160-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
160+
func load<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type,
161+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
161162
{
162163
let decoder = JSONDecoder()
163164
if let dateDecodingStrategy = dateDecodingStrategy {
@@ -166,8 +167,8 @@ public extension Router {
166167
return load(session, decoder: decoder, expectedResultType: expectedResultType, completion: completion)
167168
}
168169

169-
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
170-
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
170+
func load<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type,
171+
completion: @escaping (_ json: T?, _ error: Error?) -> Void) -> URLSessionDataTaskProtocol?
171172
{
172173
guard let request = request() else {
173174
return nil
@@ -231,7 +232,7 @@ public extension Router {
231232
#if compiler(>=5.5.2) && canImport(_Concurrency)
232233
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
233234
public extension Router {
234-
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
235+
func load<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, decoder: JSONDecoder = JSONDecoder(), expectedResultType _: T.Type) async throws -> T {
235236
guard let request = request() else {
236237
throw NSError(domain: configuration.errorDomain, code: -876, userInfo: nil)
237238
}
@@ -251,7 +252,7 @@ public extension Router {
251252
return try decoder.decode(T.self, from: responseTuple.0)
252253
}
253254

254-
func load<T: Codable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
255+
func load<T: Decodable>(_ session: RequestKitURLSession = URLSession.shared, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy?, expectedResultType: T.Type) async throws -> T {
255256
let decoder = JSONDecoder()
256257
if let dateDecodingStrategy = dateDecodingStrategy {
257258
decoder.dateDecodingStrategy = dateDecodingStrategy

0 commit comments

Comments
 (0)