Elevate your app’s connectivity with NetworkKit — a small, modular network layer that supports Combine (on Apple platforms), async/await, withCheckedThrowingContinuation, and closures, with Swift concurrency (Sendable, strict concurrency checks) in mind.
- Swift: 5.9+
- Platforms: iOS 15+, macOS 12+, watchOS 8+, tvOS 15+, visionOS 1+
Combine-backed APIs are available only where Combine exists (Apple platforms). The core protocol and async/closure APIs build on Linux and other environments without Combine.
- Combine —
NetworkService.sendRequest(endpoint:type:)returnsAnyPublisher(Apple platforms only). - Async/await (native URLSession) —
sendRequest(urlStr:)usesURLSession’s asyncdata(from:). - Async/await + continuation —
sendRequest(endpoint:)bridgesURLSession.dataTaskwithwithCheckedThrowingContinuation. - Closures —
sendRequest(endpoint:resultHandler:)with a@Sendablecompletion handler. - Injectable session —
NetworkService(configuration:)for tests (e.g. customURLSessionConfiguration/URLProtocol).
Xcode: File → Add Package Dependencies… → enter the repository URL.
Package.swift:
.package(url: "https://github.com/sabapathy7/NetworkKit.git", from: "1.0.8")Use the Releases page and set from: to the lowest version you support (or pin an exact revision if you prefer).
Define types that conform to EndPoint, then use NetworkService:
import NetworkKit
let service = NetworkService()
// Async/await — URL string (native URLSession)
let dict: [String: String] = try await service.sendRequest(urlStr: "https://api.example.com/v1/config")
// Async/await — endpoint (withCheckedThrowingContinuation + dataTask)
let user: User = try await service.sendRequest(endpoint: UserEndpoint.profile)
// Closure
service.sendRequest(endpoint: UserEndpoint.profile) { (result: Result<User, NetworkError>) in
switch result {
case .success(let user): print(user)
case .failure(let error): print(error.customMessage)
}
}
#if canImport(Combine)
import Combine
// Combine (Apple platforms)
var cancellables = Set<AnyCancellable>()
service.sendRequest(endpoint: UserEndpoint.profile, type: User.self)
.sink(receiveCompletion: { _ in }, receiveValue: { print($0) })
.store(in: &cancellables)
#endifNetworkable covers URL async, endpoint async, and the closure API. The Combine publisher lives on NetworkService so the protocol stays portable without Combine:
public protocol Networkable: Sendable {
func sendRequest<T: Decodable & Sendable>(urlStr: String) async throws -> T
func sendRequest<T: Decodable & Sendable>(endpoint: EndPoint) async throws -> T
func sendRequest<T: Decodable & Sendable>(
endpoint: EndPoint,
resultHandler: @Sendable @escaping (Result<T, NetworkError>) -> Void
)
}git clone https://github.com/sabapathy7/NetworkKit.git
cd NetworkKit
swift build
swift test- Swift Package Index: sabapathy7/NetworkKit
- Repository: github.com/sabapathy7/NetworkKit
Issues and pull requests are welcome on GitHub.
MIT — see LICENSE.