• Stars
    star
    190
  • Rank 196,398 (Top 4 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿฏ Syntactic sugar for Moya

MoyaSugar

Swift CI CocoaPods

Syntactic sugar for Moya.

Why?

Moya is an elegant network abstraction layer which abstracts API endpoints gracefully with enum. However, it would become massive when the application is getting larger. Whenever you add an endpoint, you should write code at least 5 places: enum case, method, path, parameters and parameterEncoding property. It makes you scroll down again and again. Besides, if you would like to set different HTTP header fields, you should customize endpointClosure of MoyaProvider.

If you're as lazy as I am, MoyaSugar is for you.

At a Glance

Forget about method, path, parameters, parameterEncoding and endpointClosure. Use route, params, httpHeaderFields instead.

extension MyService: SugarTargetType {
  var route: Route {
    return .get("/me")
  }

  var params: Parameters? {
    return JSONEncoding() => [
      "username": "devxoul",
      "password": "****",
    ]
  }

  var headers: [String: String]? {
    return ["Accept": "application/json"]
  }
}

Use MoyaSugarProvider instead of MoyaProvider.

let provider = MoyaSugarProvider<MyService>()
let provider = RxMoyaSugarProvider<MyService>() // If you're using Moya/RxSwift

Complete Example

import Moya
import Moyasugar

enum GitHubAPI {
  case url(String)
  case userRepos(owner: String)
  case createIssue(owner: String, repo: String, title: String, body: String?)
  case editIssue(owner: String, repo: String, number: Int, title: String?, body: String?)
}

extension GitHubAPI : SugarTargetType {

  /// method + path
  var route: Route {
    switch self {
    case .url(let urlString):
      return .get(urlString)

    case .userRepos(let owner):
      return .get("/users/\(owner)/repos")

    case .createIssue(let owner, let repo, _, _):
      return .post("/repos/\(owner)/\(repo)/issues")

    case .editIssue(let owner, let repo, let number, _, _):
      return .patch("/repos/\(owner)/\(repo)/issues/\(number)")
    }
  }

  // override default url building behavior
  var url: URL {
    switch self {
    case .url(let urlString):
      return URL(string: urlString)!
    default:
      return self.defaultURL
    }
  }

  /// encoding + parameters
  var params: Parameters? {
    switch self {
    case .url:
      return nil

    case .userRepos:
      return nil

    case .createIssue(_, _, let title, let body):
      return JSONEncoding() => [
        "title": title,
        "body": body,
      ]

    case .editIssue(_, _, _, let title, let body):
      // Use `URLEncoding()` as default when not specified
      return [
        "title": title,
        "body": body,
      ]
    }
  }

  var headers: [String: String]? {
    return [
      "Accept": "application/json"
    ]
  }

}

APIs

  • ๐Ÿ”— protocol SugarTargetType

    - extension GitHubAPI: TargetType
    + extension GitHubAPI: SugarTargetType
  • ๐Ÿ”— var route: Route

    Returns Route which contains HTTP method and URL path information.

    - var method: Method { get }
    - var path: String { get }
    + var route: Route { get }

    Example:

    var route: Route {
      return .get("/me")
    }
  • ๐Ÿ”— var url: URL

    Returns the request URL. It retuens defaultURL as default, which is the combination of baseURL and path. Implement this property to return custom url. See #6 for detail.

  • ๐Ÿ”— var params: Parameters?

    Returns Parameters which contains parameter encoding and values.

    - var parameters: [String: Any]? { get }
    + var params: Parameters? { get }

    Example:

    var params: Parameters? {
      return JSONEncoding() => [
        "username": "devxoul",
        "password": "****",
      ]
    }

    Note: If the MoyaSugarProvider is initialized with an endpointClosure parameter, HTTP header fields of endpointClosure will be used. This is how to extend the target's HTTP header fields in endpointClosure:

    let endpointClosure: (GitHubAPI) -> Endpoint<GitHubAPI> = { target in
      let defaultEndpoint = MoyaProvider.defaultEndpointMapping(for: target)
      return defaultEndpoint
        .adding(newHTTPHeaderFields: ["Authorization": "MySecretToken"])
    }

Requirements

Same with Moya

Installation

  • Using CocoaPods:

    pod 'MoyaSugar'
    pod 'MoyaSugar/RxSwift' # Use with RxSwift

MoyaSugar currently doesn't cupport Carthage.

Contributing

$ TEST=1 swift package generate-xcodeproj
$ open MoyaSugar.xcodeproj

License

MoyaSugar is under MIT license. See the LICENSE file for more info.

More Repositories

1

Then

โœจ Super sweet syntactic sugar for Swift initializers
Swift
4,045
star
2

URLNavigator

โ›ต๏ธ Elegant URL Routing for Swift
Swift
3,128
star
3

Toaster

๐Ÿž Toast for Swift
Swift
1,668
star
4

UITextView-Placeholder

A missing placeholder for UITextView
Objective-C
1,458
star
5

RxTodo

iOS Todo Application using RxSwift and ReactorKit
Swift
1,291
star
6

SwiftyImage

๐ŸŽจ Generate image resources in Swift
Swift
1,075
star
7

SwiftUITodo

An example to-do list app using SwiftUI which is introduced in WWDC19
Swift
715
star
8

Umbrella

โ˜‚๏ธ Analytics abstraction layer for Swift
Swift
607
star
9

Drrrible

Dribbble for iOS using ReactorKit
Swift
514
star
10

Pure

Pure DI in Swift
Swift
366
star
11

CocoaSeeds

Git Submodule Alternative for Cocoa.
Ruby
341
star
12

RxViewController

RxSwift wrapper for UIViewController and NSViewController
Swift
331
star
13

SwipeBack

Enable iOS 7+ swipe-to-back when custom back button is set.
Objective-C
321
star
14

UICollectionViewFlexLayout

A drop-in replacement for UICollectionViewFlowLayout
Swift
296
star
15

Carte

๐Ÿด Open source license notice view generator for Swift
Ruby
189
star
16

graygram-ios

[40์‹œ๊ฐ„๋งŒ์— Swift๋กœ iOS ์•ฑ ๋งŒ๋“ค๊ธฐ] ํ‘๋ฐฑ ์‚ฌ์ง„ ์ „์šฉ ์†Œ์…œ ๋ฏธ๋””์–ด ์•ฑ
Swift
180
star
17

ReusableKit

Generic reusables for UICollectionView and UITableView
Swift
158
star
18

UINavigationItem-Margin

Margins for UINavigationItem
Objective-C
158
star
19

allkdic

์˜ฌใ…‹์‚ฌ์ „ - ๋งฅ์—์„œ ๋‹จ์ถ•ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์˜์–ด์‚ฌ์ „์ด ๋™‡!!!!
Swift
128
star
20

SwiftyColor

๐ŸŽจ The most sexy way to use colors in Swift.
Swift
120
star
21

RxCodable

RxSwift wrapper for Codable
Swift
113
star
22

RxExpect

RxSwift testing framework
Swift
105
star
23

korail

๐Ÿš† An unofficial Korail API for Python.
Python
90
star
24

Stubber

A minimal method stub for Swift
Swift
86
star
25

Cleverbot

iOS Messaging Application using Cleverbot and ReactorKit
Swift
78
star
26

Blockchain

A blockchain simulator written in Swift
Swift
65
star
27

LetsGitHubSearch

Let'Swift 18 Workshop - Let's TDD
Swift
63
star
28

CancelBag

A DisposeBag for Combine
Swift
53
star
29

TouchAreaInsets

Expand UIView touch area just like a charm
Objective-C
50
star
30

UIViewController-NavigationBar

[Deprecated] UIViewController with its own navigation bar. It provides an availability for the smooth push/pop animations between view controllers which have different navigation bar styles.
Swift
50
star
31

ios-with-swift-in-40-hours

40์‹œ๊ฐ„๋งŒ์— Swift๋กœ iOS ์•ฑ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค์–ด๋ด…์‹œ๋‹ค.
49
star
32

SectionReactor

A ReactorKit extension for managing table view and collection view sections with RxDataSources
Swift
45
star
33

SafeCollection

Safe Collection for Swift
Swift
42
star
34

Fallback

๐Ÿ’Š Syntactic sugar for Swift do-try-catch
Swift
42
star
35

AsyncBlockOperation

NSOperation subclass for async block.
Objective-C
41
star
36

CGFloatLiteral

CGFloat(10) becomes 10.f
Swift
35
star
37

Endpoint

๐Ÿš€ Elegant API Abstraction for Swift
Swift
35
star
38

RxJSON

RxSwift wrapper for JSON
Swift
33
star
39

ASCollectionFlexLayout

A custom collection layout that allows to use Texture layout specs in ASCollectionNode.
Swift
27
star
40

next-route-map

๐Ÿš Routes for Next.js
TypeScript
26
star
41

Date.swift

๐Ÿ“… A Swift Date Library
Swift
26
star
42

ImageEffects

Bring UIImageEffects (WWDC 2013) to UIImage category with handy interface.
Objective-C
25
star
43

Todobox

[40์‹œ๊ฐ„๋งŒ์— Swift๋กœ iOS ์•ฑ ๋งŒ๋“ค๊ธฐ] ํ•  ์ผ ๋ชฉ๋ก ๊ด€๋ฆฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
Swift
24
star
44

RxReusable

Reusable cells and views for RxSwift
Swift
23
star
45

AlertReactor

A ReactorKit extension for UIAlertController
Swift
23
star
46

UIButton-BackgroundContentMode

A missing background content mode for UIButton.
Objective-C
22
star
47

sketch-export-sizes-generator

๐Ÿ’Ž A simple plugin for Sketch that generates export sizes of layers.
20
star
48

RxDelegateCells

[Don't use this] UITableView Cell Height and UICollectionView Cell Size for RxSwift
Swift
19
star
49

Immutable

Missing non-mutating functions in Swift
Swift
18
star
50

PureSwinject

Auto register Pure factories to Swinject
Swift
18
star
51

graygram-web

Python
16
star
52

daumpostcode

๋‹ค์Œ ์šฐํŽธ๋ฒˆํ˜ธ ์„œ๋น„์Šค๋ฅผ ๋ณ„๋„์˜ ํŒจํ‚ค์ง€ ์„ค์น˜ ์—†์ด๋„ ๋„ค์ดํ‹ฐ๋ธŒ ์•ฑ์—์„œ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
HTML
16
star
53

Swiftproj

A command-line tool for managing Xcode project with Swift Package Manager.
Ruby
15
star
54

Meal

[40์‹œ๊ฐ„๋งŒ์— Swift๋กœ iOS ์•ฑ ๋งŒ๋“ค๊ธฐ] ์ „๊ตญ ์ดˆ/์ค‘/๊ณ ๋“ฑํ•™๊ต ๊ธ‰์‹ ์กฐํšŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜
Swift
15
star
55

SwinjectSafeAuto

SwinjectSafeAuto allows to auto-register services to the container and verify the required services are properly registered.
Swift
14
star
56

FastLogin

2018 FAST CAMPUS DEV SEMINAR ์‹ค์Šต ์•ฑ
Swift
14
star
57

NSOperationQueue-CompletionBlock

Completion block for NSOperationQueue.
Objective-C
13
star
58

graphql-codegen-typescript-fixtures

A plugin for graphql-code-generator that generates TypeScript fixtures for testing.
TypeScript
12
star
59

Kodium

Better Korean typo for medium.com
JavaScript
11
star
60

Estimator

Planning Poker application that communicates over BLE (Bluetoothe Low Energy)
Swift
10
star
61

MapViewVisibleAnnotations

Displays which annotations are currently visible in the specific area.
Swift
9
star
62

jira-velocity-chart-completion-rate

Displays completion rate in JIRA velocity chart.
JavaScript
9
star
63

Slots

Dynamic contents management for Swift.
Swift
9
star
64

github-monospace-editor

GitHub Monospace Editor Userscript
JavaScript
9
star
65

flask-restdoc

Flask-Restdoc is a simple tool that generates RESTful API documentation automatically from python files.
Python
9
star
66

notion-badge

Embed App Store badge in Notion
HTML
9
star
67

SwinjectFactory

Experimental Swinject extension for factory injection.
Swift
8
star
68

if_

Swift if-else statement as an expression (Experimental)
Swift
8
star
69

flask-errorhandler

Error handlers for Flask blueprints.
Python
7
star
70

ModelKit

[deprecated] Experimental Model framework for Swift
Swift
7
star
71

shlee322-fb-troll

์ƒํ˜์ด์˜ ํŽ˜๋ถ ๊ธ€๋งˆ๋‹ค ์ผํ•˜๋ผ๋Š” ๋Œ“๊ธ€์„ ๋‹ฌ์•„๋ณด์•„์š” ^์˜ค^
Python
7
star
72

adorable

M๊™ผฬˆaฬ†ฬŽkฬŠฬˆeฬŒฬˆ tฬฬˆeฬ‘ฬˆxฬ†ฬˆt๊™ผฬˆ aฬŠฬˆdฬŒฬˆoฬ„ฬˆrฬฬˆaฬ‘ฬˆbฬ†ฬˆl๊™ผฬˆeฬ†ฬŽ
HTML
7
star
73

resume

My rรฉsumรฉ
Makefile
6
star
74

Playground

A playground for an ideal project
HCL
6
star
75

Bagel

GIF converter for Mac
Swift
5
star
76

RxGitHubSearch

An example GitHub search app for my lecture
Swift
5
star
77

xcode-project-template

Swift
5
star
78

staccato

Staccato ๋ชจ์ž„ ์†Œ๊ฐœ ํŽ˜์ด์ง€
Python
5
star
79

fbmarkdown

Chrome extension that enable markdown on facebook.
JavaScript
5
star
80

JLModel

JLModel allows you to manage models in very simple way.
Objective-C
5
star
81

blog

์–ธ์  ๊ฐ„ ์“ฐ๊ฒ ์ง€?
CSS
4
star
82

TypedKey

Statically-typed key for Swift.
Swift
4
star
83

Hippo-iOS

Objective-C
4
star
84

devxoul.github.io

HTML
4
star
85

naver-cafe-chat-notification

๋„ค์ด๋ฒ„ ์นดํŽ˜ ์ƒˆ ์ฑ„ํŒ… ๊ฐœ์ˆ˜๋ฅผ ๋ธŒ๋ผ์šฐ์ € ํƒ€์ดํ‹€๋กœ ์•Œ๋ ค์ฃผ๋Š” ์Šคํฌ๋ฆฝํŠธ.
JavaScript
4
star
86

fabric-verbose

Python
4
star
87

TistoryMarkdown

A Markdown plugin for Tistory.
4
star
88

4SharedDownloader

You can download files from 4shared.com without delay.
ActionScript
4
star
89

help-me-rx-tableview-cell-height

Help me!
Swift
3
star
90

macsafe

Keep your MacBook from thieves.
Python
3
star
91

NAR

Nginx Auto Reloader
JavaScript
2
star
92

Editag

The easiest way to edit id3 tag.
ActionScript
2
star
93

ImageCrawler

JavaScript
2
star
94

ovenbreak

Utils for ovenapp.io
Makefile
2
star
95

JLChromeTabController

A chrome-like tab bar for iOS.
Objective-C
2
star
96

Evermind

Evermind is a handy tool that makes it easy to mindmap your idea from your work, study, and everyday life.
ActionScript
2
star
97

JLUtils

Objective-C
1
star
98

FastcampusFrame

Swift
1
star
99

Sonaki

Objective-C
1
star
100

Calendar

Infinite-scrolling calendar example
Objective-C
1
star