Intermediate · 12 min
Model typed endpoints
Move repeated server operations behind reusable endpoint types.
You’ll buildA small Endpoint type that owns its request and response contract.
1
Step 1
Declare the contract
The associated response keeps endpoint use sites strongly typed.
struct TodoEndpoint: Endpoint {
typealias Response = Todo
let id: Int
}2
Step 2
Create the request
The endpoint owns URL construction and request defaults.
func makeRequest() throws -> APIRequest {
APIRequest(
method: .get,
url: URL(string: "https://jsonplaceholder.typicode.com/todos/\(id)")!
)
}3
Step 3
Execute by intent
Feature code now names the operation instead of assembling HTTP details.
let todo = try await client.execute(
endpoint: TodoEndpoint(id: 1),
authScope: "public"
)
print(todo.title)Tutorial complete