Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernisation #1

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename init parameters
danielctull committed Mar 22, 2024
commit 6333f8a6f83f90099622e5551e108d4d4bbb902c
67 changes: 47 additions & 20 deletions Sources/Resourceful/Resource.swift
Original file line number Diff line number Diff line change
@@ -17,28 +17,35 @@ public struct Resource<Value> {
/// Creates a resource located with the request and transformed from data
/// using the given transform.
///
/// Any failures from the makeRequest or transform functions will be
/// surfaced when performing the network request using the fetch or
/// publisher methods on URLSession.
///
/// - Parameters:
/// - request: A request for this resource.
/// - transform: Used to transform the response into the desired value.
public init(request: URLRequest,
transform: @escaping (Response) throws -> Value) {
self.init(makeRequest: { request }, transform: transform)
/// - request: Creates a request for this resource.
/// - success: Used to transform the response into the desired value.
public init(
request: @escaping () throws -> URLRequest,
success: @escaping (Response) throws -> Value
) {
_request = request
_success = success
}
}

extension Resource {

/// Creates a resource located with the request and transformed from data
/// using the given transform.
///
/// Any failures from the makeRequest or transform functions will be
/// surfaced when performing the network request using the fetch or
/// publisher methods on URLSession.
///
/// - Parameters:
/// - makeRequest: Used to create a request for this resource.
/// - transform: Used to transform the response into the desired value.
public init(makeRequest: @escaping () throws -> URLRequest,
transform: @escaping (Response) throws -> Value) {
_request = makeRequest
_success = transform
/// - request: A request for this resource.
/// - success: Used to transform the response into the desired value.
public init(
request: URLRequest,
success: @escaping (Response) throws -> Value
) {
self.init(request: { request }, success: success)
}
}

@@ -67,23 +74,24 @@ extension Resource {
_ transform: @escaping (Value) throws -> NewValue
) -> Resource<NewValue> {

return Resource<NewValue>(makeRequest: _request) { response in
Resource<NewValue>(request: _request) { response in
try transform(value(for: response))
}
}

public func mapRequest(
_ modify: @escaping (URLRequest) throws -> URLRequest
) -> Resource {
return Resource(
makeRequest: { try modify(self._request()) },
transform: value(for:))
Resource(
request: { try modify(self._request()) },
success: _success
)
}

public func modifyRequest(
_ modify: @escaping (inout URLRequest) throws -> ()
) -> Resource {
return mapRequest { request in
mapRequest { request in
var request = request
try modify(&request)
return request
@@ -102,4 +110,23 @@ extension Resource {
/// Makes the request for the resource.
@available(*, deprecated, message: "Use request instead.")
public var makeRequest: () throws -> URLRequest { _request }

/// Creates a resource located with the request and transformed from data
/// using the given transform.
///
/// Any failures from the makeRequest or transform functions will be
/// surfaced when performing the network request using the fetch or
/// publisher methods on URLSession.
///
/// - Parameters:
/// - makeRequest: Used to create a request for this resource.
/// - transform: Used to transform the response into the desired value.
@available(*, deprecated, message: "Use init(request:success:) instead.")
public init(
makeRequest: @escaping () throws -> URLRequest,
transform: @escaping (Response) throws -> Value
) {
_request = makeRequest
_success = transform
}
}
6 changes: 4 additions & 2 deletions Tests/ResourcefulTests/Helper.swift
Original file line number Diff line number Diff line change
@@ -58,9 +58,11 @@ extension XCTestCase {
/// - Parameter url: The location of the data.
var failingRequestResource: Resource<String> {
struct TestError: Error {}
return Resource<String>(makeRequest: { throw TestError() }, transform: {
return Resource<String> {
throw TestError()
} success: {
String(data: $0.data, encoding: .utf8) ?? ""
})
}
}
}