Skip to content

Commit 509fe92

Browse files
committed
Only enforce Decodable for API calls
1 parent 88e443a commit 509fe92

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

Sources/ComposableArchitecturePattern/Server+API.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public protocol ServerAPI: Identifiable, Equatable {
4646
func request(_ method: HTTPMethod, in environment: ServerEnvironment?, additionalHeaders: [String: String]?, additionalQueries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?) throws -> URLRequest
4747

4848
/// Whether or not the provided type is supported by the API. Defaults to checking if the type is found in `supportedReturnObjects` or returning `false` if not found.
49-
func supports<T: Codable>(_ object: T.Type) -> Bool
49+
func supports<T: Decodable>(_ object: T.Type) -> Bool
5050
}
5151

5252
extension Sequence where Element == Codable.Type {
@@ -96,7 +96,7 @@ public extension ServerAPI {
9696

9797
var strictEnvironmentEnforcement: Bool { true }
9898

99-
func supports<T: Codable>(_ object: T.Type) -> Bool {
99+
func supports<T: Decodable>(_ object: T.Type) -> Bool {
100100
return self.supportedReturnObjects?.contains(where: { object == $0 }) ?? false
101101
}
102102
}

Sources/ComposableArchitecturePattern/Server.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public protocol Server: Actor {
5353
///
5454
/// - Note: `additionalHeaders` will override a key-value in `additionalHTTPHeaders`.
5555
/// - Note: The server automatically checks against these values to check whether they're supported by the API or not. For instance, if the specified return type is not supported, a `ServerAPIError.badRequest` error is thrown. If the specified API doesn't support this function, a `ServerAPIError.badRequest` error is thrown.
56-
func get<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
56+
func get<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
5757

5858
/// Sends a POST request and returns the specified value type from the given API.
5959
///
6060
/// - Note: `additionalHeaders` will override a key-value in `additionalHTTPHeaders`.
6161
/// - Note: The server automatically checks against these values to check whether they're supported by the API or not. For instance, if the specified return type is not supported, a `ServerAPIError.badRequest` error is thrown. If the specified API doesn't support this function, a `ServerAPIError.badRequest` error is thrown.
62-
func post<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
62+
func post<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
6363

6464
/// Sends a POST request and returns the specified value type from the given API.
6565
///
@@ -71,7 +71,7 @@ public protocol Server: Actor {
7171
///
7272
/// - Note: `additionalHeaders` will override a key-value in `additionalHTTPHeaders`.
7373
/// - Note: The server automatically checks against these values to check whether they're supported by the API or not. For instance, if the specified return type is not supported, a `ServerAPIError.badRequest` error is thrown. If the specified API doesn't support this function, a `ServerAPIError.badRequest` error is thrown.
74-
func put<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
74+
func put<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
7575

7676
/// Sends a PUT request and returns the specified value type from the given API.
7777
///
@@ -83,7 +83,7 @@ public protocol Server: Actor {
8383
///
8484
/// - Note: `additionalHeaders` will override a key-value in `additionalHTTPHeaders`.
8585
/// - Note: The server automatically checks against these values to check whether they're supported by the API or not. For instance, if the specified return type is not supported, a `ServerAPIError.badRequest` error is thrown. If the specified API doesn't support this function, a `ServerAPIError.badRequest` error is thrown.
86-
func delete<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
86+
func delete<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]?, queries: [URLQueryItem]?, httpBodyOverride httpBody: Data?, timeoutInterval: TimeInterval?, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
8787

8888
/// Sends a DELETE request and returns the specified value type from the given API.
8989
///
@@ -94,7 +94,7 @@ public protocol Server: Actor {
9494
/// Send the given request to the server and return the decoded object.
9595
/// - Returns: The given decoded type or an `APIError`.
9696
/// - Throws: A `ServerAPIError` if unable to decode or an error encountered during the request.
97-
func sendRequest<T: Codable>(_ request: URLRequest, requestUID: UUID, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
97+
func sendRequest<T: Decodable>(_ request: URLRequest, requestUID: UUID, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy) async throws -> T
9898

9999
/// Send the given request to the server.
100100
/// - Returns: A boolean indicating the success of the request.
@@ -122,7 +122,7 @@ public extension Server {
122122
var blockAllAPIsNotSupported: Bool { true }
123123

124124
// GETs
125-
func get<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
125+
func get<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
126126
if self.blockAllAPIsNotSupported {
127127
try self._checkAPIsContainAPI(api)
128128

@@ -156,7 +156,7 @@ public extension Server {
156156
}
157157

158158
// POSTs
159-
func post<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
159+
func post<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
160160
if self.blockAllAPIsNotSupported {
161161
try self._checkAPIsContainAPI(api)
162162

@@ -219,7 +219,7 @@ public extension Server {
219219
}
220220

221221
// PUTs
222-
func put<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
222+
func put<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
223223
if self.blockAllAPIsNotSupported {
224224
try self._checkAPIsContainAPI(api)
225225

@@ -284,7 +284,7 @@ public extension Server {
284284
}
285285

286286
// DELETEs
287-
func delete<T: Codable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
287+
func delete<T: Decodable>(_ api: any ServerAPI, additionalHeaders: [String: String]? = nil, queries: [URLQueryItem]? = nil, httpBodyOverride httpBody: Data? = nil, timeoutInterval: TimeInterval? = nil, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
288288
if self.blockAllAPIsNotSupported {
289289
try self._checkAPIsContainAPI(api)
290290

@@ -346,7 +346,7 @@ public extension Server {
346346
return wasSuccessful
347347
}
348348

349-
func sendRequest<T: Codable>(_ request: URLRequest, requestUID: UUID, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
349+
func sendRequest<T: Decodable>(_ request: URLRequest, requestUID: UUID, dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .iso8601, keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys) async throws -> T {
350350
if self.logActivity == .all {
351351
self.logger.info("\(Date()) - (\(requestUID)) Request to \(String(describing: request.url?.description)) [Start]")
352352
}
@@ -410,7 +410,7 @@ public extension Server {
410410
}
411411
}
412412

413-
fileprivate func _checkAPISupportsType<T: Codable>(_ api: any ServerAPI, type: T.Type) throws {
413+
fileprivate func _checkAPISupportsType<T: Decodable>(_ api: any ServerAPI, type: T.Type) throws {
414414
guard api.supports(T.self) else {
415415
throw ServerAPIError.badRequest(description: NSLocalizedString("API doesn't support type: `\(T.self)`.", comment: ""))
416416
}

0 commit comments

Comments
 (0)