Skip to content

MarvinNazari/NetworkRequest

Repository files navigation

NetworkRequest

A tiny, type-safe, dependency-free HTTP request builder for Swift.

Swift Platforms SPM License CI

NetworkRequest describes an HTTP request and how to parse its response as a single, immutable value. It does not execute itself — URLSession, async/await, Combine, or any mock you like is in charge of dispatch. The library just gives you a strongly-typed, composable description of the work.

Quick start

import NetworkRequest

struct User: Decodable, Sendable {
    let id: Int
    let name: String
}

struct APIError: Decodable, Error, Sendable {
    let message: String
}

let me = NetworkRequest<User, APIError>(
    url: URL(string: "https://api.example.com/me"),
    additionalHeaderFields: ["Authorization": "Bearer \(token)"]
)

let (data, response) = try await URLSession.shared.data(for: me.urlRequest())
let user = try me.parse(data, response)

A non-2xx response decodes the body as APIError and throws it — your do/catch block sees a typed error.

Why NetworkRequest?

  • Type-safe. A NetworkRequest<User, APIError> carries both the success and the failure shape in its type.
  • Execution-agnostic. No baked-in transport. Use URLSession, mock it, swap it for Combine, plug in your own.
  • Sendable-clean. Builds cleanly under Swift 6 strict concurrency and conditionally conforms to Sendable when its generic parameters do.
  • Zero dependencies. Just Foundation.
  • Built-in cURL debugging. request.cURLCommand reproduces a request on the command line.

Installation

Add the package to your Package.swift:

dependencies: [
    .package(url: "https://github.com/MarvinNazari/NetworkRequest", from: "1.0.0"),
],
targets: [
    .target(name: "MyApp", dependencies: ["NetworkRequest"]),
]

In Xcode: File ▸ Add Package Dependencies… and paste the URL.

Documentation

Full API reference and articles (Getting Started, A Real-World Example, Recipes, Building Requests, Parsing Responses, Request Bodies) are published as Documentation.

The same documentation is bundled as a DocC catalog inside the package; in Xcode, choose Product ▸ Build Documentation to browse it locally.

Requirements

  • Swift 6.2 toolchain (Xcode 17+)
  • iOS 13+, macOS 10.15+, tvOS 13+, watchOS 6+

License

MIT — see LICENSE.

About

A tiny, type-safe, dependency-free HTTP request builder for Swift. Builds clean under Swift 6 strict concurrency.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages