{"id":13871986,"url":"https://github.com/pointfreeco/swift-url-routing","last_synced_at":"2025-05-15T16:06:53.764Z","repository":{"id":37698177,"uuid":"487614471","full_name":"pointfreeco/swift-url-routing","owner":"pointfreeco","description":"A bidirectional router with more type safety and less fuss.","archived":false,"fork":false,"pushed_at":"2025-02-24T18:19:05.000Z","size":7161,"stargazers_count":374,"open_issues_count":5,"forks_count":30,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-08T01:44:50.553Z","etag":null,"topics":["bidirectional","invertible","parsing","printing","reversible","router","routing"],"latest_commit_sha":null,"homepage":"https://swiftpackageindex.com/pointfreeco/swift-url-routing/main/documentation/urlrouting","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/pointfreeco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-01T18:39:21.000Z","updated_at":"2025-05-03T23:05:37.000Z","dependencies_parsed_at":"2024-07-23T02:18:31.231Z","dependency_job_id":"cd9aefdf-fca4-45aa-a66f-6086cf2f8e19","html_url":"https://github.com/pointfreeco/swift-url-routing","commit_stats":{"total_commits":37,"total_committers":12,"mean_commits":"3.0833333333333335","dds":0.3783783783783784,"last_synced_commit":"9924e8be183e7895ebc579f2aaebaebb78bc3c66"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pointfreeco%2Fswift-url-routing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pointfreeco%2Fswift-url-routing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pointfreeco%2Fswift-url-routing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pointfreeco%2Fswift-url-routing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pointfreeco","download_url":"https://codeload.github.com/pointfreeco/swift-url-routing/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254374475,"owners_count":22060611,"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":["bidirectional","invertible","parsing","printing","reversible","router","routing"],"created_at":"2024-08-05T23:00:31.864Z","updated_at":"2025-05-15T16:06:53.740Z","avatar_url":"https://github.com/pointfreeco.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":[],"readme":"# URL Routing\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fpointfreeco%2Fswift-url-routing%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/pointfreeco/swift-url-routing)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fpointfreeco%2Fswift-url-routing%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/pointfreeco/swift-url-routing)\n\nA bidirectional URL router with more type safety and less fuss. This library is built with [Parsing][swift-parsing].\n\n---\n\n* [Motivation](#Motivation)\n* [Getting started](#Getting-started)\n* [Documentation](#Documentation)\n* [License](#License)\n\n## Learn More\n\nThis library was discussed in an [episode](http://pointfree.co/episodes/ep187-tour-of-parser-printers-url-routing) of [Point-Free](http://pointfree.co), a video series exploring functional programming and the Swift programming and the Swift language, hosted by [Brandon Williams](https://twitter.com/mbrandonw) and [Stephen Celis](https://twitter.com/stephencelis).\n\n\u003ca href=\"http://pointfree.co/episodes/ep187-tour-of-parser-printers-url-routing\"\u003e\n  \u003cimg alt=\"video poster image\" src=\"https://d3rccdn33rt8ze.cloudfront.net/episodes/0187.jpeg\" width=\"600\"\u003e\n\u003c/a\u003e\n\n## Motivation\n\nURL routing is a ubiquitous problem in both client-side and server-side applications:\n\n* Clients, such as iOS applications, need to route URLs for deep-linking, which amounts to picking apart a URL in order to figure out where to navigate the user in the app.\n* Servers, such as [Vapor][vapor] applications, also need to pick apart URL requests to figure out what page to serve, but also need to _generate_ valid URLs for linking within the website.\n\nThis library provides URL routing function for both client and server applications, and does so in a composable, type-safe manner.\n\n## Getting Started\n\nTo use the library you first begin with a domain modeling exercise. You model a route enum that represents each URL you want to recognize in your application, and each case of the enum holds the data you want to extract from the URL.\n\nFor example, if we had screens in our Books application that represent showing all books, showing a particular book, and searching books, we can model this as an enum:\n\n```swift\nenum AppRoute {\n  case books\n  case book(id: Int)\n  case searchBooks(query: String, count: Int = 10)\n}\n```\n\nNotice that we only encode the data we want to extract from the URL in these cases. There are no details of where this data lives in the URL, such as whether it comes from path parameters, query parameters or POST body data.\n\nThose details are determined by the router, which can be constructed with the tools shipped in this library. Its purpose is to transform an incoming URL into the `AppRoute` type. For example:\n\n```swift\nimport URLRouting\n\nlet appRouter = OneOf {\n  // GET /books\n  Route(.case(AppRoute.books)) {\n    Path { \"books\" }\n  }\n\n  // GET /books/:id\n  Route(.case(AppRoute.book(id:))) {\n    Path { \"books\"; Digits() }\n  }\n\n  // GET /books/search?query=:query\u0026count=:count\n  Route(.case(AppRoute.searchBooks(query:count:))) {\n    Path { \"books\"; \"search\" }\n    Query {\n      Field(\"query\")\n      Field(\"count\", default: 10) { Digits() }\n    }\n  }\n}\n```\n\nThis router describes at a high-level how to pick apart the path components, query parameters, and more from a URL in order to transform it into an `AppRoute`.\n\nOnce this router is defined you can use it to implement deep-linking logic in your application. You can implement a single function that accepts a `URL`, use the router's `match` method to transform it into an `AppRoute`, and then switch on the route to handle each deep link destination:\n\n```swift\nfunc handleDeepLink(url: URL) throws {\n  switch try appRouter.match(url: url) {\n  case .books:\n    // navigate to books screen\n\n  case let .book(id: id):\n    // navigate to book with id\n\n  case let .searchBooks(query: query, count: count):\n    // navigate to search screen with query and count\n  }\n}\n```\n\nThis kind of routing is incredibly useful in client side iOS applications, but it can also be used in server-side applications. Even better, it can automatically transform `AppRoute` values back into URL's which is handy for linking to various parts of your website:\n\n```swift\nappRouter.path(for: .searchBooks(query: \"Blob Bio\"))\n// \"/books/search?query=Blob%20Bio\"\n```\n\n```swift\nul {\n  for book in books {\n    li {\n      a {\n        book.title\n      }\n      .href(appRouter.path(for: .book(id: book.id)))\n    }\n  }\n}\n```\n```html\n\u003cul\u003e\n  \u003cli\u003e\u003ca href=\"/books/1\"\u003eBlob Autobiography\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/books/2\"\u003eBlobbed around the world\u003c/a\u003e\u003c/li\u003e\n  \u003cli\u003e\u003ca href=\"/books/3\"\u003eBlob's guide to success\u003c/a\u003e\u003c/li\u003e\n\u003c/ul\u003e\n```\n\nFor [Vapor][vapor] bindings to URL Routing, see the [Vapor Routing][vapor-routing] package.\n\n## Documentation\n\nThe documentation for releases and main are available [here](https://swiftpackageindex.com/pointfreeco/swift-url-routing/main/documentation/urlrouting).\n\n## License\n\nThis library is released under the MIT license. See [LICENSE](LICENSE) for details.\n\n[swift-parsing]: http://github.com/pointfreeco/swift-parsing\n[vapor-routing]: http://github.com/pointfreeco/vapor-routing\n[vapor]: http://vapor.codes\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpointfreeco%2Fswift-url-routing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpointfreeco%2Fswift-url-routing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpointfreeco%2Fswift-url-routing/lists"}