Swiftpack.co -  jrsaruo/Firework as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
jrsaruo/Firework
Firework is a wrapper for Alamofire.
.package(url: "https://github.com/jrsaruo/Firework.git", from: "1.0.0")

Firework

Firework is a wrapper for Alamofire.

Requirements

  • iOS 10.0+ / macOS 10.12+ / tvOS 10.0+ / watchOS 3.0+
  • Xcode 11+
  • Swift 5.1+

Usage

1. Define endpoints

extension Endpoint {
    // Base endpoint
    static var base: Endpoint { "https://some.api.com" }
    
    // Health check API endpoint
    static var healthCheck: Endpoint { base / "health" } // https://some.api.com/health
    
    // User API endpoints
    static var user: Endpoint { base / "user" }
    static var login: Endpoint { user / "login" }
    static var profile: Endpoint { user / "profile" } // https://some.api.com/user/profile
}

2. Send requests

e.g. Send a GET request

// A GET request type
struct HealthCheckRequest: GETRequest {
    var endpoint: Endpoint { .healthCheck }
}

// Send a request
let request = HealthCheckRequest()
APIClient.send(request) { result in
    switch result {
    case .success:
        print("Healthy!")
    case .failure(let error):
        print(error)
    }
}

e.g. Send a POST request and decode the response JSON

// A type corresponding to response JSON such as `{ "name": "...", "age": ... }`
struct Profile: Decodable {
    let name: String
    let age: Int
}

// A POST request type
struct ProfileRequest: POSTRequest, DecodingRequest {
    typealias Response = Profile
    let userID: Int
    
    var endpoint: Endpoint { .profile }
    var body: [String: Any] {
        ["user_id": userID]
    }
}

// Send a request
let request = ProfileRequest(userID: 100)
APIClient.send(request, decodingCompletion: { result in
    switch result {
    case .success(let profile):
        // Type of `profile` is `Profile`
        print("User name:", profile.name)
    case .failure(let error):
        print("Error:", error)
    }
})

Using Firework in your project

To use the Firework library in a SwiftPM project, add the following line to the dependencies in your Package.swift file:

.package(url: "https://github.com/jrsaruo/Firework", from "1.0.0"),

and add Firework as a dependency for your target:

.target(name: "<target>", dependencies: [
    .product(name: "Firework", package: "Firework"),
    // other dependencies
]),

FInally, add import Firework in your source code.

GitHub

link
Stars: 0
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Dependencies

Release Notes

First Launch of Firework!
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API