{"id":20590857,"url":"https://github.com/alickbass/sweetrouter","last_synced_at":"2025-04-14T22:35:51.140Z","repository":{"id":62456333,"uuid":"85182048","full_name":"alickbass/SweetRouter","owner":"alickbass","description":"Declarative URL routing in swift","archived":false,"fork":false,"pushed_at":"2017-12-29T13:56:33.000Z","size":79,"stargazers_count":45,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T07:46:27.008Z","etag":null,"topics":["declarative","endpoints","router","swift"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alickbass.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-16T10:14:40.000Z","updated_at":"2024-12-19T19:44:46.000Z","dependencies_parsed_at":"2022-11-02T00:17:12.658Z","dependency_job_id":null,"html_url":"https://github.com/alickbass/SweetRouter","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSweetRouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSweetRouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSweetRouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alickbass%2FSweetRouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alickbass","download_url":"https://codeload.github.com/alickbass/SweetRouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248974642,"owners_count":21192186,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["declarative","endpoints","router","swift"],"created_at":"2024-11-16T07:38:27.622Z","updated_at":"2025-04-14T22:35:51.109Z","avatar_url":"https://github.com/alickbass.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SweetRouter\nDeclarative URL routing in swift for Alamofire and not only :)\n\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) \n[![Build Status](https://travis-ci.org/alickbass/SweetRouter.svg?branch=master)](https://travis-ci.org/alickbass/SweetRouter)\n[![codecov](https://codecov.io/gh/alickbass/SweetRouter/branch/master/graph/badge.svg)](https://codecov.io/gh/alickbass/SweetRouter)\n\n## Integration\n\n### CocoaPods\n\nYou can use CocoaPods to install SweetRouter by adding it to your Podfile:\n\n```swift\nplatform :ios, '8.0'\nuse_frameworks!\n\ntarget 'MyApp' do\npod 'SweetRouter'\nend\n```\n\n### Carthage\n\nYou can use Carthage to install SweetRouter by adding it to your Cartfile:\n\n```swift\ngithub \"alickbass/SweetRouter\"\n```\n\n## Definitions\n\nImagine that you use the following `URLs` within your App\n\n```\nhttps://myservercom.com:123/api/new/signIn\nhttps://myservercom.com:123/api/new/signOut\nhttps://myservercom.com:123/api/new/posts?date=today\u0026userId=id\n```\n\nEvery `URL` in the list is called an **`Endpoint`**\n\n### Endpoint\n\nEndpoint has the following structure:\n\n```\n                               Endpoint\n┌─────────────────────────────────┴────────────────────────────────────┐\nhttps://myservercom.com:123/api/new/posts?date=today\u0026userId=id#paragraph\n└────────────────┬────────────────┘└────────────────┬──────────────────┘\n            Environment                           Route\n```\n\nEndpoint is represented with `EndpointType` `protocol`.\n\n### Environment\n\nEnvironment has the following structure:\n\n```\n            Environment\n┌────────────────┴─────────────────┐\nhttps://myservercom.com:123/api/new/posts?date=today\u0026userId=id#paragraph\n└─┬─┘  └───────┬───────┘└┬┘└─────┬─┘\nscheme        host     port default path\n```\n\n**Examples of Environment**\n\n```swift\nURL.Env(.https, \"mytestserver.com\").at(\"api\", \"new\") // https://mytestserver.com/api/new/\nURL.Env(IP(127, 0, 0, 1), 8080) // http://127.0.01:8080\nURL.Env.localhost(4001) // http://localhost:4001\n```\n\n### Route\nRoute has the following structure:\n\n```\n                                                   Route\n                                   ┌─────────────────┴─────────────────┐\nhttps://myservercom.com:123/api/new/posts?date=today\u0026userId=id#paragraph\n                                   └──┬──┘└────────┬─────────┘└───┬────┘\n                                     path        query         fragment\n```\n\n**Example of Route**\n\n```swift\n// /api/new/posts?date=today\u0026userId=id#paragraph\nURL.Route(at: \"api\", \"new\", \"posts\").query((\"date\", \"today\"), (\"userId\", \"id\")).fragment(\"paragraph\")\n```\n\n## Example of usage\n\nHere is an example of the Router for some back-end `API`:\n\n```swift\nenum Api: EndpointType {\n    enum Environment: EnvironmentType {\n        case localhost\n        case test\n        case production\n        \n        var value: URL.Env {\n            switch self {\n            case .localhost: return .localhost(8080)\n            case .test: return .init(IP(126, 251, 20, 32))\n            case .production: return .init(.https, \"myproductionserver.com\", 3000)\n            }\n        }\n    }\n    \n    enum Route: RouteType {\n        case auth, me\n        case posts(for: Date)\n        \n        var route: URL.Route {\n            switch self {\n            case .me: return .init(at: \"me\")\n            case .auth: return .init(at: \"auth\")\n            case let .posts(for: date):\n                return URL.Route(at: \"posts\").query((\"date\", date), (\"userId\", \"someId\"))\n            }\n        }\n    }\n    \n    static let current: Environment = .localhost\n}\n```\n\nAnd in our application we would use it like this:\n\n```swift\nprint(Router\u003cApi\u003e(at: .me).url) // http://localhost:8080/me\nprint(Router\u003cApi\u003e(.test, at: .auth).url) // http://126.251.20.32/auth\nprint(Router\u003cApi\u003e(.production, at: .posts(for: Date())).url) // https://myproductionserver.com:3000/posts?date=12.04.2017\u0026userId=someId\n```\n\n## What if I have only one environment?\n\nIt 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:\n\n```swift\nstruct Auth: EndpointType {\n    enum Route: RouteType {\n        case signIn, signOut\n        \n        var route: URL.Route {\n            switch self {\n            case .signIn: return .init(path: [\"signIn\"])\n            case .signOut: return .init(path: [\"signOut\"])\n            }\n        }\n    }\n    \n    static let current = URL.Env(.https, \"auth.server.com\", 8080).at(\"api\", \"new\")\n}\n```\n\nAnd use it like this:\n\n```swift\nprint(Router\u003cAuth\u003e(at: .signIn).url) // https://auth.server.com:8080/api/new/signIn\nprint(Router\u003cAuth\u003e(at: .signOut).url) // https://auth.server.com:8080/api/new/signOut\n```\n\n## How to use with Alamofire?\n\nAdd the following code somewhere in your app:\n\n```swift\nimport Alamofire\nimport SweetRouter\n\nextension Router: URLConvertible {\n    public func asURL() throws -\u003e URL {\n        return url\n    }\n}\n```\n\nAnd then you can use the same Routers like this:\n\n```swift\nAlamofire.request(Router\u003cAuth\u003e(at: .signIn))\n```\n\nAs easy as that 😉\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falickbass%2Fsweetrouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falickbass%2Fsweetrouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falickbass%2Fsweetrouter/lists"}