SwiftyEndpoint is a lightweight, protocol-oriented Swift package that helps you build type-safe API endpoints with ease. It provides a clean and flexible way to define your API endpoints, handle configurations, and manage query parameters.
You can install the SwiftyEndpoint via Swift Package Manager.
- In Xcode, install the SwiftyEndpoint by navigating to File > Add Packages
- And enter the GitHub link:
https://github.com/cembaykara/SwiftyEndpoint.git
Add SwiftyEndpoint to your Package.swift
:
dependencies: [
.package(url: "https://github.com/cembaykara/SwiftyEndpoint.git", from: "1.0.0")
]
First, create a configuration that conforms to SwiftyConfiguration
:
import SwiftyEndpoint
struct NetworkConfiguration: SwiftyConfiguration {
let host: String? = "api.example.com"
let port: Int?
let disableSecureConnection: Bool = false
init(port: Int? = nil) {
self.port = port
}
}
Define your endpoints by conforming to SwiftyEndpoint
:
enum MoviesEndpoint: SwiftyEndpoint {
case mostPopular
case topRated
static let configuration: NetworkConfiguration = NetworkConfiguration()
static var basePath: String { "/api/v2" }
var path: String {
switch self {
case .mostPopular: "/most_popular"
case .topRated: "/top_rated"
}
}
}
Create query parameters by conforming to SwiftyOptions
:
enum MovieOptions: SwiftyOptions {
case page(Int)
case language(String)
func toQueryParameter() -> URLQueryItem {
switch self {
case .page(let value): URLQueryItem(name: "page", value: String(value))
case .language(let value): URLQueryItem(name: "language", value: value)
}
}
}
// Basic usage
let popularMoviesURL = MovieEndpoint.mostPopular.url()
// Result: https://api.example.com/api/v2/most_popular
// With query parameters
let options: [MovieOptions] = [.page(1), .language("en-US") ]
let urlWithParams = MovieEndpoint.popular.url(with: options)
// Result: https://api.example.com/api/v1/movies/most_popular?page=1&language=en-US
Generate base URLs without endpoint-specific paths:
let baseURL = MovieEndpoint.baseURL()
// Result: https://api.example.com/api/v1
You can customize URL construction using the url(with:custom:)
method:
let customURL = endpoint.url(with: options) { components, path in
var modified = components
modified.path = path + "/custom"
return modified
}
Contributions are welcome! Please feel free to submit a Pull Request.