Latest 1.0.0
License MIT
Platforms ios 8.0, osx 10.11, watchos 2.0, tvos 9.0, requires ARC

Declarative URL routing in swift for Alamofire and not only :)

Carthage compatible
Build Status



You can use CocoaPods to install SweetRouter by adding it to your Podfile:

platform :ios, '8.0'

target 'MyApp' do
pod 'SweetRouter'


You can use Carthage to install SweetRouter by adding it to your Cartfile:

github "alickbass/SweetRouter"


Imagine that you use the following URLs within your App

Every URL in the list is called an Endpoint


Endpoint has the following structure:

            Environment                           Route

Endpoint is represented with EndpointType protocol.


Environment has the following structure:

└─┬─┘  └───────┬───────┘└┬┘└─────┬─┘
scheme        host     port default path

Examples of Environment

URL.Env(.https, "").at("api", "new") //
URL.Env(IP(127, 0, 0, 1), 8080) // http://127.0.01:8080
URL.Env.localhost(4001) // http://localhost:4001


Route has the following structure:

                                     path        query         fragment

Example of Route

// /api/new/posts?date=today&userId=id#paragraph
URL.Route(at: "api", "new", "posts").query(("date", "today"), ("userId", "id")).fragment("paragraph")

Example of usage

Here is an example of the Router for some back-end API:

enum Api: EndpointType {
    enum Environment: EnvironmentType {
        case localhost
        case test
        case production

        var value: URL.Env {
            switch self {
            case .localhost: return .localhost(8080)
            case .test: return .init(IP(126, 251, 20, 32))
            case .production: return .init(.https, "", 3000)

    enum Route: RouteType {
        case auth, me
        case posts(for: Date)

        var route: URL.Route {
            switch self {
            case .me: return .init(at: "me")
            case .auth: return .init(at: "auth")
            case let .posts(for: date):
                return URL.Route(at: "posts").query(("date", date), ("userId", "someId"))

    static let current: Environment = .localhost

And in our application we would use it like this:

print(Router<Api>(at: .me).url) // http://localhost:8080/me
print(Router<Api>(.test, at: .auth).url) //
print(Router<Api>(.production, at: .posts(for: Date())).url) //

What if I have only one environment?

It can often happen, that you will be using some third-party API and you will have only access to Production environment. So in this case your Router will look something like this:

struct Auth: EndpointType {
    enum Route: RouteType {
        case signIn, signOut

        var route: URL.Route {
            switch self {
            case .signIn: return .init(path: ["signIn"])
            case .signOut: return .init(path: ["signOut"])

    static let current = URL.Env(.https, "", 8080).at("api", "new")

And use it like this:

print(Router<Auth>(at: .signIn).url) //
print(Router<Auth>(at: .signOut).url) //

How to use with Alamofire?

Add the following code somewhere in your app:

import Alamofire
import SweetRouter

extension Router: URLConvertible {
    public func asURL() throws -> URL {
        return url

And then you can use the same Routers like this:

Alamofire.request(Router<Auth>(at: .signIn))

As easy as that 😉

Latest podspec

    "name": "SweetRouter",
    "version": "1.0.0",
    "summary": "Declarative URL routing in swift for Alamofire and not only :)",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "Oleksii Dykan": "[email protected]"
    "platforms": {
        "ios": "8.0",
        "osx": "10.11",
        "watchos": "2.0",
        "tvos": "9.0"
    "requires_arc": true,
    "source": {
        "git": "",
        "tag": "1.0.0",
        "branch": "master"
    "source_files": "SweetRouter/*.swift",
    "pod_target_xcconfig": {
        "SWIFT_VERSION": "4"
    "pushed_with_swift_version": "3.0"

Pin It on Pinterest

Share This