Skip to content

Commit

Permalink
Add pathAppending
Browse files Browse the repository at this point in the history
  • Loading branch information
kaishin committed Sep 6, 2024
1 parent 6ec3167 commit 92f2f51
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func getUser(id: Int) -> RequestMiddleware {
}
```

Note that the order inside the builder does not matter and middleware are applied
in the order they are defined. If the same middleware is defined multiple times,
the last one will be used. If you're defining custom middleware, try to make them idempotent so as to
avoid unexpected behavior.
Note that the order inside the builder only matters for middleware that are not idempotent, such as the built-in `pathAppending`.

Once you have an endpoint defined, you can use it to create a request for Foundation.

Expand Down Expand Up @@ -112,6 +109,25 @@ let createGame = games/"create"
let featuredGames = games/"featured"
```

You can also compose paths using the built-in `pathAppending` middleware.

```swift
import HTTPRequestBuilder

@RequestBuilder
var userEndpoints: RequestMiddleware = {
path("/users")
}

@RequestBuilder
func editUser(id: Int) -> RequestMiddleware {
userEndpoints
pathAppending("/edit")
pathAppending(id)
Method.put
}
```

## Authentication Middleware

This library comes with a couple middleware functions to help with authentication.
Expand Down
26 changes: 26 additions & 0 deletions Sources/HTTPRequestBuilder/RequestMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@ public func path(
}
}

/// Append to the path of the request.
/// - Parameter component: The path component to append.
/// - Returns: A request middleware.
public func pathAppending(
_ component: CustomStringConvertible
) -> RequestMiddleware {
{ request in
var newRequest = request
newRequest.path = request.path / component
return newRequest
}
}

/// Append to the path of the request.
/// - Parameter component: The path component to append.
/// - Returns: A request middleware.
public func pathAppending<T: RawRepresentable>(
_ component: T
) -> RequestMiddleware where T.RawValue == String {
{ request in
var newRequest = request
newRequest.path = request.path / component
return newRequest
}
}

/// Add headers to the request.
/// - Parameters:
/// - key: The header key.
Expand Down
20 changes: 20 additions & 0 deletions Tests/HTTPRequestBuilderTests/HTTPRequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,24 @@ final class HTTPRequestBuilderTests: XCTestCase {
XCTAssertEqual(urlRequest.url, URL(string: "https://api.example.com/users/12"))
}

func testPathAppending() throws {
enum Action: String {
case edit
case view
}

@RequestBuilder
var example: RequestMiddleware {
path("/users")
pathAppending("12")
pathAppending(Action.edit)
pathAppending(3)
}

let request = try example(
Request()
)

XCTAssertEqual(request.path.fragments, ["users", "12", "edit", "3"])
}
}

0 comments on commit 92f2f51

Please sign in to comment.