Examples

Small packages you can run.

Each example is an independent Swift package that depends on the repository root.

CodableRoutes

Define a Pathway model, encode it into a path, and decode it from a URL.

import Foundation
import Pathways

enum AccountKind: String, Codable, Sendable {
    case personal
    case business
}

struct AccountRoute: Pathway {
    static let pattern = "/accounts/:accountID/:kind"

    let accountID: Int
    let kind: AccountKind
}

let route = AccountRoute(accountID: 42, kind: .business)
let path = try PathwayEncoder.shared.encode(route)

guard let url = URL(string: "https://example.com\(path)") else {
    preconditionFailure("The example URL must be valid")
}

let decoded = try PathwayDecoder.shared.decode(AccountRoute.self, from: url)
print("Decoded account: \(decoded.accountID) (\(decoded.kind.rawValue))")
cd Examples/CodableRoutes
swift run

View complete source →

RoutingCenter

Register typed and path-only routes, filter by exact host, read query parameters, and inspect the result from handle(_:).

import Foundation
import Pathways

struct ProductRoute: Pathway {
    static let pattern = "/products/:productID"

    let productID: Int
}

@main enum RoutingCenterExample {
  @MainActor static func main() throws {
    var router = Pathways()

    router.register(host: "example.com", ProductRoute.self) { route, query in
        let campaign = query["campaign", default: "none"]
        print("Open product \(route.productID); campaign: \(campaign)")
    }

    router.register(host: "example.com", path: "/settings") { query in
        let tab = query["tab", default: "general"]
        print("Open settings tab: \(tab)")
    }

    let url = URL(string: "https://example.com/products/42?campaign=summer")!
    let handled = try router.handle(url)
    print("Handled: \(handled)")
  }
}
cd Examples/RoutingCenter
swift run

View complete source →