Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Provides an easy way to manage namespace of multiple URL endpoints in Swift.

License

NotificationsYou must be signed in to change notification settings

devyhan/URLRouter

Repository files navigation

maincodecov

What's URLRouter 📟

URLRouter is provides an easy way to manage multiple URL endpoints in Swift.It provides a simple interface for managing multiple endpoints and allows developers to interact with them in a single, unified manner.It also provides a way for developers to create custom endpoints DSL(Domain-Specific Languages) and to manage their own settings for each endpoint.Additionally, it provides a way to track the status of each endpoint and to easily detect any changes or updates that have been made.

Similar to Swift Evolution'sRegex builder DSL, URL string literal and a more powerful pattern result builder to help make Swift URL string processing fast and easy and without mistakes. Ultimately, withURLRouter, changes are easy to detect and useful for maintenance.

🤔Ask questions you’re wondering abouthere.
💡Share ideashere.

Installation 📦

  • UsingSwift Package Manager

    import PackageDescriptionletpackage=Package(  name:"SomeApp",  dependencies:[.Package(url:"https://github.com/devyhan/URLRouter", majorVersion:"<LATEST_RELEASES_VERSION>"),])

Configure URLRouter 📝

Implement URLs Namespace

  • To implement URLs namespace we create a new type that will house the domain and behavior of the URLs by conforming toURLRoutable.
import URLRouterpublicenumURLs:URLRoutable{...}

HttpHeader declaration

  • UsingHeaderBuilder tohttpHeader declaration.
Request{...Header{Field("HEADERVALUE", forKey:"HEADERKEY")Field("HEADERVALUE1", forKey:"HEADERKEY1")Field("HEADERVALUE2", forKey:"HEADERKEY2")...}...}
  • UsingDictionary tohttpHeader declaration.
Request{...Header(["HEADERKEY":"HEADERVALUE","HEADERKEY1":"HEADERVALUE1","HEADERKEY2":"HEADERVALUE2",...])...}

HttpBody declaration

  • UsingHeaderBuilder tohttpHeader declaration.
Request{...Body{Field("VALUE", forKey:"KEY")Field("VALUE1", forKey:"KEY1")Field("VALUE2", forKey:"KEY2")...}...}
  • UsingDictionary<String, Any> tohttpHeader declaration.
Request{...Body(["KEY":"VALUE","KEY1":"VALUE1","KEY2":"VALUE2",...])...}

HttpMethod declaration

  • UsingMethod(_ method:) tohttpMethod declaration.
Request{...Method(.get)...}
  • Usingstatic let method: tohttpMethod declaration.
Request{...Method.get...}

URL declaration

  • UsingURL(_ url:) toURL declaration.
Request{...URL("https://www.baseurl.com/comments?postId=1")...}
  • UsingURLBuilder toURL declaration andURLComponents declaration.
Request{...URL{Scheme(.https)Host("www.baseurl.com")Path("comments")Query("postId", value:"1")}...}// https://www.baseurl.com/comments?postId=1
  • UsingBaseURL(_ url:) forURL override.
Request{BaseURL("https://www.baseurl.com")URL{Path("comments")Query("postId", value:"1")}}// https://www.baseurl.com/comments?postId=1Router{BaseURL("https://www.baseurl.com")Request{URL{Scheme(.https)Host("www.overrideurl.com")Path("comments")Query("postId", value:"1")}}}// https://www.overrideurl.com/comments?postId=1

URL Scheme declaration

  • UsingScheme(_ scheme:) toScheme declaration.
Request{...URL{Scheme(.https)...}...}
  • Usingstatic let scheme: toScheme declaration.
Request{...URL{Scheme.https...}...}

URL Query declaration

  • UsingDictionary<String, String?> toQuery declaration.
Request{...URL{Query(["first":"firstQuery","second":"secondQuery",...])}...}
  • UsingQuery(_, value:) toQuery declaration.
Request{...URL{Query("test", value:"query")Query("test", value:"query")...}...}
  • UsingField(_, forKey:) toQuery declaration.
Request{...URL{Query{Field("firstQuery", forKey:"first")Field("secondQuery", forKey:"second")...}...}...}

How to configure and useURLRouter in a real world project?

  • Just create URLRouter.swift in your project! Happy hacking! 😁
import URLRouterenumURLs:URLRoutable{  // DOC: https://docs.github.com/ko/rest/repos/repos?apiVersion=2022-11-28#list-organization-repositoriescase listOrganizationRepositories(organizationName:String)  // DOC: https://docs.github.com/ko/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repositorycase createAnOrganizationRepository(organizationName:String, repositoryInfo:RepositoryInfo)  // DOC: https://docs.github.com/ko/rest/search?apiVersion=2022-11-28#search-repositoriescase searchRepositories(query:String)case deeplink(path:String="home")structRepositoryInfo{letname:Stringletdescription:StringlethomePage:Stringlet`private`:BoollethasIssues:BoollethasProjects:BoollethasWiki:Bool}varrouter:URLRouter{URLRouter{BaseURL("http://api.github.com")switchself{caselet.listOrganizationRepositories(organizationName):Request{Method.postHeader{Field("application/vnd.github+json", forKey:"Accept")Field("Bearer <YOUR-TOKEN>", forKey:"Authorization")Field("2022-11-28", forKey:"X-GitHub-Api-Version")}URL{Path("orgs/\(organizationName)/repos")}}caselet.createAnOrganizationRepository(organizationName, repositoryInfo):Request{Method.postHeader{Field("application/vnd.github+json", forKey:"Accept")Field("Bearer <YOUR-TOKEN>", forKey:"Authorization")Field("2022-11-28", forKey:"X-GitHub-Api-Version")}URL{Path("orgs/\(organizationName)/repos")}Body{Field(repositoryInfo.name, forKey:"name")Field(repositoryInfo.description, forKey:"description")Field(repositoryInfo.homePage, forKey:"homepage")Field(repositoryInfo.private, forKey:"private")Field(repositoryInfo.hasIssues, forKey:"has_issues")Field(repositoryInfo.hasProjects, forKey:"has_projects")Field(repositoryInfo.hasWiki, forKey:"has_wiki")}}caselet.searchRepositories(query):Request{Method.getHeader{Field("application/vnd.github+json", forKey:"Accept")Field("Bearer <YOUR-TOKEN>", forKey:"Authorization")Field("2022-11-28", forKey:"X-GitHub-Api-Version")}URL{Path("search/repositories")Query("q", value: query)}}caselet.deeplink(path):URL{Scheme.custom("example-deeplink")Host("detail")Path(path)Query{Field("postId", forKey:"1")Field("createdAt", forKey:"2021-04-27T04:39:54.261Z")}}}}}}// http://api.github.com/orgs/organization/reposletlistOrganizationRepositoriesUrl=URLs.listOrganizationRepositories(organizationName:"organization").router?.urlRequest?.url// http://api.github.com/search/repositories?q=urlrouterletsearchRepositoriesUrl=URLs.searchRepositories(query:"urlrouter").router?.urlRequest?.url// example-deeplink://detail/comments?1=postId&2021-04-27T04:39:54.261Z=createdAletdeeplink=URLs.deeplink(path:"detail").router.url
  • UsingURLRouter to provideURLRequest.
letrepositoryInfo:URLs.RepositoryInfo=.init(name:"Hello-World", description:"This is your first repository", homePage:"https://github.com", private:false, hasIssues:true, hasProjects:true, hasWiki:false)letrequest=URLs.createAnOrganizationRepository(organizationName:"SomeOrganization", repositoryInfo: repositoryInfo).router?.urlRequestURLSession.shared.dataTask(with: request){ data, response, error in...
  • UsingURLRouter to provide deeplinkURL and check to match thisURL.
class AppDelegate:UIResponder,UIApplicationDelegate{...func application(_ app:UIApplication, open url:URL, options:[UIApplication.OpenURLOptionsKey:Any])->Bool{letdetailDeeplink=URLs.deeplink(path:"detail").router.urlif detailDeeplink== url{...}...

License

URLRouter is under MIT license. See theLICENSE file for more info.


GitHub release (latest SemVer)Twitter Follow @devyhan93

About

Provides an easy way to manage namespace of multiple URL endpoints in Swift.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp