diff --git a/Sources/URLRouting/Client/Client.swift b/Sources/URLRouting/Client/Client.swift index 6645a3ceba..62b5ffa707 100644 --- a/Sources/URLRouting/Client/Client.swift +++ b/Sources/URLRouting/Client/Client.swift @@ -6,13 +6,13 @@ import XCTestDynamicOverlay import FoundationNetworking #endif -/// A type that can make requests to a server, download the response, and decode the response -/// into a model. +/// A type that can make requests to a server, download the response, and decode the response into a +/// model. /// /// You do not typically construct this type directly from its initializer, and instead use the -/// ``live(router:session:)`` static method for creating an API client from a parser-printer, or -/// use the ``failing`` static variable for creating an API client that throws an error when a -/// request is made and then use ``override(_:with:)-1ot4o`` to override certain routes with mocked +/// ``live(router:session:)`` static method for creating an API client from a parser-printer, or use +/// the ``failing`` static variable for creating an API client that throws an error when a request +/// is made and then use ``override(_:with:)-1ot4o`` to override certain routes with mocked /// responses. public struct URLRoutingClient { var request: (Route) async throws -> (Data, URLResponse) @@ -26,6 +26,15 @@ public struct URLRoutingClient { self.decoder = decoder } + /// Makes a request to a route. + /// + /// - Parameter route: The route to request. + /// - Returns: The data and response. + @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) + public func data(for route: Route) async throws -> (value: Data, response: URLResponse) { + try await self.request(route) + } + /// Makes a request to a route. /// /// - Parameters: @@ -34,12 +43,12 @@ public struct URLRoutingClient { /// - decoder: A JSON decoder. /// - Returns: The decoded value. @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) - public func request( - _ route: Route, + public func decodedResponse( + for route: Route, as type: Value.Type = Value.self, decoder: JSONDecoder? = nil ) async throws -> (value: Value, response: URLResponse) { - let (data, response) = try await self.request(route) + let (data, response) = try await self.data(for: route) do { return (try (decoder ?? self.decoder).decode(type, from: data), response) } catch { diff --git a/Sources/URLRouting/Internal/Deprecations.swift b/Sources/URLRouting/Internal/Deprecations.swift new file mode 100644 index 0000000000..2904605646 --- /dev/null +++ b/Sources/URLRouting/Internal/Deprecations.swift @@ -0,0 +1,19 @@ +import Foundation + +#if canImport(FoundationNetworking) + import FoundationNetworking +#endif + +// NB: Deprecated after 0.1.0: + +extension URLRoutingClient { + @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) + @available(*, deprecated, renamed: "responseData(for:as:decoder:)") + public func request( + _ route: Route, + as type: Value.Type = Value.self, + decoder: JSONDecoder? = nil + ) async throws -> (value: Value, response: URLResponse) { + try await self.decodedResponse(for: route, as: type, decoder: decoder) + } +} diff --git a/Sources/URLRouting/Scheme.swift b/Sources/URLRouting/Scheme.swift index 84992001e3..500fb6a992 100644 --- a/Sources/URLRouting/Scheme.swift +++ b/Sources/URLRouting/Scheme.swift @@ -8,6 +8,10 @@ /// ... /// } /// ``` +/// +/// > Note: Do not use the `Scheme` parser for the purpose of preferring to print a particular +/// > scheme from your router. Instead, consider using ``BaseURLPrinter`` via the `baseURL` and +/// > `baseRequestData` methods on routers. public struct Scheme: ParserPrinter { @usableFromInline let name: String @@ -18,11 +22,6 @@ public struct Scheme: ParserPrinter { /// A parser of the `https` scheme. public static let https = Self("https") - /// A parser of custom schemes. - public static func custom(_ scheme: String) -> Self { - Self(scheme) - } - /// Initializes a scheme parser with a scheme name. /// /// - Parameter name: A method name. diff --git a/Tests/URLRoutingTests/URLRoutingClientTests.swift b/Tests/URLRoutingTests/URLRoutingClientTests.swift index b2e4c9c445..0de862cf20 100644 --- a/Tests/URLRoutingTests/URLRoutingClientTests.swift +++ b/Tests/URLRoutingTests/URLRoutingClientTests.swift @@ -19,7 +19,7 @@ class URLRoutingClientTests: XCTestCase { let sut = URLRoutingClient(request: { _ in ("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse()) }) - let response = try await sut.request(.test, as: Response.self) + let response = try await sut.decodedResponse(for: .test, as: Response.self) XCTAssertEqual(response.value, .init(decodableValue: "result")) } @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) @@ -36,7 +36,7 @@ class URLRoutingClientTests: XCTestCase { request: { _ in ("{\"decodable_value\":\"result\"}".data(using: .utf8)!, URLResponse()) }, decoder: customDecoder) - let response = try await sut.request(.test, as: Response.self) + let response = try await sut.decodedResponse(for: .test, as: Response.self) XCTAssertEqual(response.value, .init(decodableValue: "result")) } @available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *) @@ -53,7 +53,7 @@ class URLRoutingClientTests: XCTestCase { request: { _ in ("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse()) }, decoder: customDecoder) - let response = try await sut.request(.test, as: Response.self, decoder: .init()) + let response = try await sut.decodedResponse(for: .test, as: Response.self, decoder: .init()) XCTAssertEqual(response.value, .init(decodableValue: "result")) } #endif diff --git a/Tests/URLRoutingTests/URLRoutingTests.swift b/Tests/URLRoutingTests/URLRoutingTests.swift index b7e5efbef9..1fc5893fe6 100644 --- a/Tests/URLRoutingTests/URLRoutingTests.swift +++ b/Tests/URLRoutingTests/URLRoutingTests.swift @@ -60,7 +60,7 @@ class URLRoutingTests: XCTestCase { let name = try p.parse(&request) XCTAssertEqual("Hello", name) - XCTAssertEqual(["X-Haha": ["Blob"]], request.headers) + XCTAssertEqual(["x-haha": ["Blob"]], request.headers) } func testQuery() throws {