Skip to content

Commit

Permalink
Rename init parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Mar 22, 2024
1 parent 5d34dd7 commit 6333f8a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
67 changes: 47 additions & 20 deletions Sources/Resourceful/Resource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Up @@ -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) ?? ""
})
}
}
}

Expand Down

0 comments on commit 6333f8a

Please sign in to comment.