You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- If you find yourself on `main`, create a feature branch before making any changes.
10
+
5
11
## Build and Test Commands
6
12
7
13
```bash
@@ -20,26 +26,40 @@ swift test --filter "MiddlewareTests/middlewareCallOrder"
20
26
21
27
## Architecture
22
28
23
-
Simplicity is a type-safe HTTP client library for Swift, inspired by swift-openapi-generator's client design.
29
+
Simplicity is a type-safe HTTP client library for Swift, inspired by swift-openapi-generator's client design. It uses Apple's `swift-http-types` (`HTTPTypes`, `HTTPTypesFoundation`) for standard HTTP primitives.
24
30
25
31
### Core Components
26
32
27
-
-**HTTPClient** (`Sources/Simplicity/HTTPClient.swift`): The main entry point. Executes requests through a middleware chain using `URLSession`. The `send(request:)` method builds the middleware chain in reverse order, so middlewares execute in the order they were added.
33
+
-**Client protocol** (`Sources/Simplicity/Protocol/Client.swift`): The main entry point. Defines `send(_:)` and `upload(_:)` methods that execute requests through a middleware chain. Uses Apple's `HTTPRequest.Method`, `HTTPResponse.Status`, and `HTTPFields` throughout.
34
+
35
+
-**Request protocol** (`Sources/Simplicity/Protocol/Request.swift`): Defines type-safe requests with associated `RequestBody`, `SuccessResponseBody`, and `FailureResponseBody` types. Uses Apple's `HTTPRequest.Method` and `HTTPFields` for properties. Provides default JSON encoding/decoding implementations. Use `Never?` for `RequestBody` on requests without a body.
36
+
37
+
-**Response struct** (`Sources/Simplicity/HTTP/Response.swift`): Wraps Apple's `HTTPResponse` and adds typed decoding via `decodeSuccessBody()` / `decodeFailureBody()`. Convenience accessors: `.status`, `.headerFields`.
38
+
39
+
-**Middleware protocol** (`Sources/Simplicity/Protocol/Middleware.swift`): Intercepts requests and responses via an `intercept` method that receives a `next` closure. Uses `MiddlewareRequest` and `MiddlewareResponse` structs that embed Apple's `HTTPRequest` and `HTTPResponse`.
40
+
41
+
-**Transport protocol** (`Sources/Simplicity/Protocol/Transport.swift`): Abstracts the network layer at the `HTTPRequest`/`HTTPResponse` level. `URLSessionTransport` is the default implementation wrapping `URLSession`; tests inject `MockTransport`.
42
+
43
+
-**URLSessionClient** (`Sources/Simplicity/Implementation/URLSessionClient.swift`): Concrete `Client` implementation. Delegates network calls to a `Transport` (defaults to `URLSessionTransport`). Accepts a `URLSession` convenience init for backward compatibility.
28
44
29
-
-**HTTPRequest protocol** (`Sources/Simplicity/Protocol/HTTPRequest.swift`): Defines type-safe requests with associated `RequestBody` and `ResponseBody` types. Provides default JSON encoding/decoding implementations. Use `Never?` for `RequestBody` on requests without a body.
45
+
### HTTP Types
30
46
31
-
-**Middleware protocol** (`Sources/Simplicity/Protocol/Middleware.swift`): Intercepts requests and responses via an `intercept` method that receives a `next` closure. Middlewares can modify requests before calling `next` and inspect/modify responses after.
47
+
The library uses Apple's `swift-http-types` throughout:
48
+
-`HTTPRequest.Method` (extensible struct) instead of custom enum
49
+
-`HTTPResponse.Status` (supports any status code, `.kind` categorization) instead of custom enum
50
+
-`HTTPFields` (case-insensitive, multi-value, type-safe names) instead of `[String: String]`
32
51
33
52
### Request Body Handling
34
53
35
-
The `HTTPRequest` protocol has special handling for bodyless requests:
36
-
- Use `Never?` as `RequestBody` and set `httpBody` to `nil` for GET/DELETE requests
37
-
- The protocol has private extensions that skip body encoding when `RequestBody` is `Never` or `Never?`
54
+
The `Request` protocol has special handling for bodyless requests:
55
+
- Use `Never?` as `RequestBody` and set `body` to `nil` for GET/DELETE requests
56
+
- The protocol has extensions that skip body encoding when `RequestBody` is `Never` or `Never?`
38
57
39
58
### Testing Approach
40
59
41
-
Tests use Swift Testing framework (`@Suite`, `@Test`, `#expect`). Network tests mock `URLSession` via `MockURLProtocol` configured on an ephemeral session configuration.
60
+
Tests use Swift Testing framework (`@Suite`, `@Test`, `#expect`). Network tests inject a `MockTransport` conforming to the `Transport` protocol — each test gets its own isolated handler closure with zero shared state.
42
61
- When running tests, be sure to run tests for all supported platforms
62
+
- Prefer `@Test(arguments:)` for parameterized tests over writing separate test functions for each input variation. One parameterized test with an array of inputs is cleaner than many near-identical test cases.
43
63
44
64
## Documentation Lookup (Context7)
45
65
@@ -48,4 +68,4 @@ When working on this project, **always use Context7** to look up documentation b
0 commit comments