$npx -y skills add rshankras/claude-code-apple-skills --skill networking-layerGenerates a protocol-based networking layer with async/await, error handling, and swappable implementations. Use when user wants to add API client, networking, or HTTP layer.
| 1 | # Networking Layer Generator |
| 2 | |
| 3 | Generate a modern, protocol-based networking layer using Swift's async/await concurrency, with proper error handling and easy testability. |
| 4 | |
| 5 | ## When This Skill Activates |
| 6 | |
| 7 | Use this skill when the user: |
| 8 | - Asks to "add networking" or "create API client" |
| 9 | - Mentions "HTTP layer" or "REST API" |
| 10 | - Wants to "fetch data from API" |
| 11 | - Asks about "URLSession wrapper" or "network requests" |
| 12 | |
| 13 | ## Pre-Generation Checks |
| 14 | |
| 15 | ### 1. Project Context Detection |
| 16 | - [ ] Check Swift version (async/await requires Swift 5.5+) |
| 17 | - [ ] Check deployment target (async/await requires iOS 15+ / macOS 12+) |
| 18 | - [ ] Search for existing networking implementations |
| 19 | - [ ] Identify source file locations |
| 20 | |
| 21 | ### 2. Conflict Detection |
| 22 | Search for existing networking: |
| 23 | ``` |
| 24 | Glob: **/*API*.swift, **/*Network*.swift, **/*Client*.swift |
| 25 | Grep: "URLSession" or "HTTPURLResponse" |
| 26 | ``` |
| 27 | |
| 28 | If found, ask user: |
| 29 | - Replace existing implementation? |
| 30 | - Extend with new endpoints? |
| 31 | |
| 32 | ## Configuration Questions |
| 33 | |
| 34 | Ask user via AskUserQuestion: |
| 35 | |
| 36 | 1. **Authentication type?** |
| 37 | - Bearer token |
| 38 | - API key (header or query param) |
| 39 | - None |
| 40 | - Custom |
| 41 | |
| 42 | 2. **Base URL configuration?** |
| 43 | - Single environment |
| 44 | - Multiple environments (dev/staging/prod) |
| 45 | |
| 46 | 3. **Additional features?** |
| 47 | - Retry logic with exponential backoff |
| 48 | - Request/response logging |
| 49 | - Caching |
| 50 | |
| 51 | ## Generation Process |
| 52 | |
| 53 | ### Step 1: Create Core Files |
| 54 | |
| 55 | Generate these files: |
| 56 | 1. `APIClient.swift` - Protocol and implementation |
| 57 | 2. `APIEndpoint.swift` - Endpoint definition protocol |
| 58 | 3. `NetworkError.swift` - Typed errors |
| 59 | 4. `APIConfiguration.swift` - Base URL and auth config |
| 60 | |
| 61 | ### Step 2: Optional Files |
| 62 | |
| 63 | Based on configuration: |
| 64 | - `RetryPolicy.swift` - If retry logic selected |
| 65 | - `NetworkLogger.swift` - If logging selected |
| 66 | |
| 67 | ### Step 3: Determine File Location |
| 68 | |
| 69 | Check project structure: |
| 70 | - If `Sources/` exists → `Sources/Networking/` |
| 71 | - If `App/` exists → `App/Networking/` |
| 72 | - Otherwise → `Networking/` |
| 73 | |
| 74 | ## Swift 6.2 Concurrency Notes |
| 75 | |
| 76 | Reference: Apple's Swift Concurrency Updates |
| 77 | |
| 78 | ### @concurrent for Background Work |
| 79 | Use `@concurrent` to offload heavy processing: |
| 80 | ```swift |
| 81 | @concurrent |
| 82 | static func parseResponse<T: Decodable>(_ data: Data) async throws -> T { |
| 83 | try JSONDecoder().decode(T.self, from: data) |
| 84 | } |
| 85 | ``` |
| 86 | |
| 87 | ### MainActor for UI Updates |
| 88 | Keep UI-related code on MainActor: |
| 89 | ```swift |
| 90 | @Observable |
| 91 | @MainActor |
| 92 | final class NetworkViewModel { |
| 93 | var items: [Item] = [] |
| 94 | |
| 95 | func fetch() async { |
| 96 | items = try await apiClient.fetch(ItemsEndpoint()) |
| 97 | } |
| 98 | } |
| 99 | ``` |
| 100 | |
| 101 | ## Output Format |
| 102 | |
| 103 | After generation, provide: |
| 104 | |
| 105 | ### Files Created |
| 106 | ``` |
| 107 | Sources/Networking/ |
| 108 | ├── APIClient.swift # Protocol + URLSession implementation |
| 109 | ├── APIEndpoint.swift # Endpoint protocol |
| 110 | ├── NetworkError.swift # Error types |
| 111 | ├── APIConfiguration.swift # Config (base URL, auth) |
| 112 | └── Endpoints/ # Example endpoints |
| 113 | └── ExampleEndpoint.swift |
| 114 | ``` |
| 115 | |
| 116 | ### Integration Steps |
| 117 | |
| 118 | **Define an Endpoint:** |
| 119 | ```swift |
| 120 | struct UsersEndpoint: APIEndpoint { |
| 121 | typealias Response = [User] |
| 122 | |
| 123 | var path: String { "/users" } |
| 124 | var method: HTTPMethod { .get } |
| 125 | } |
| 126 | ``` |
| 127 | |
| 128 | **Make a Request:** |
| 129 | ```swift |
| 130 | let client = URLSessionAPIClient(configuration: .production) |
| 131 | let users = try await client.request(UsersEndpoint()) |
| 132 | ``` |
| 133 | |
| 134 | **With SwiftUI:** |
| 135 | ```swift |
| 136 | struct UsersView: View { |
| 137 | @State private var users: [User] = [] |
| 138 | @Environment(\.apiClient) private var apiClient |
| 139 | |
| 140 | var body: some View { |
| 141 | List(users) { user in |
| 142 | Text(user.name) |
| 143 | } |
| 144 | .task { |
| 145 | users = try await apiClient.request(UsersEndpoint()) |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | ``` |
| 150 | |
| 151 | ### Testing |
| 152 | |
| 153 | Use `MockAPIClient` for tests: |
| 154 | ```swift |
| 155 | let mockClient = MockAPIClient() |
| 156 | mockClient.mockResponse(for: UsersEndpoint.self, response: [User.mock]) |
| 157 | |
| 158 | let viewModel = UsersViewModel(apiClient: mockClient) |
| 159 | await viewModel.fetch() |
| 160 | |
| 161 | XCTAssertEqual(viewModel.users.count, 1) |
| 162 | ``` |
| 163 | |
| 164 | ## References |
| 165 | |
| 166 | - **networking-patterns.md** - Architecture patterns and best practices |
| 167 | - **templates/** - All template files |
| 168 | - Apple Docs: Swift Concurrency Updates (Swift 6.2) |