A lightweight Swift Package for typesafe URL construction and HTTP request abstraction, with strict Swift concurrency support.
- Swift Tools Version: 6.0 or later
- Swift: 6.0 or later
- Xcode: 15.4 or later
Add the package dependency to your app’s Package.swift:
// swift-tools-version:6.0
import PackageDescription
let package = Package(
name: "YourPackage",
dependencies: [
.package(url: "https://github.com/EmilioOjeda/Networking.git", from: "1.0.0"),
],
targets: [
.target(
name: "YourPackage",
dependencies: [
.product(name: "Url", package: "Networking"),
.product(name: "Request", package: "Networking")
]
)
]
)Then import modules as needed:
import Url
// and / or
import RequestA DSL for building, validating, and manipulating URL instances in a type-safe, composable way.
Core Type: Url
Components:
Scheme― Type-safe schemes (.http,.https, etc.)Host― Hostname wrapperPort― TCP/UDP port wrapper with presets (.http,.https, etc.)Path― Normalized URL path segmentsQuery&Param― Query parameters via literals or a@QueryBuilder
Examples:
// Build "https://api.app.com:443/v1/posts?limit=20&sort=asc"
let url = Url {
Scheme(.https)
Host("api.app.com")
Port(.https)
Path("/v1/posts")
Query {
Param("limit", "20")
Param("sort", "asc")
}
}A result-builder–powered wrapper around URLRequest, assembling components into a full HTTP request.
Core Type: Request
Components:
Url― URL path and queryMethod― HTTP methods (.get,.post, etc.)Header&Headers― Single and multiple header values with common constructorsBody― Encodable payloads (JSON,FormUrlEncoded)
Examples:
// GET request
let getRequest = Request {
Url(string: "https://api.app.com/v1/posts")
Method(.get)
Accept("application/json")
}
// POST request with JSON body
struct Post: Encodable { let title: String; let body: String }
let postRequest = Request {
Url(string: "https://api.app.com/v1/posts")
Method(.post)
ContentType(.application("json"))
JSON(Post(title: "Hello", body: "World"))
}