Extremely simple client/server communication

Hi everyone! :smiley:

I have spent the last weeks writing a framework with the goal of radically reducing the need to write communication code between server and client. Let me introduce you to SwiftyBridges:

The server defines one or more APIs:

struct IceCreamAPI: APIDefinition {
    var request: Request
    
    public func getAllFlavors() -> [IceCreamFlavor] {
        [
            IceCreamFlavor(name "Chocolate"),
            IceCreamFlavor(name "Vanilla"),
        ]
    }
}

Then, the API definitions are registered and a single route is configured via Vapor:

let apiRouter = APIRouter()
apiRouter.register(IceCreamAPI.self)
app.post("api") { req -> EventLoopFuture<Response> in
    apiRouter.handle(req)
}

As a next step, SwiftyBridges generates all communication code for server and client.

The client can then simply call the API methods:

let api = IceCreamAPI(url: serverURL)
let flavors: [IceCreamFlavor] = try await api.getAllFlavors()

That's all the code you need!


Authentication can be added with a few additional lines:

// Server:
struct IceCreamAPI: APIDefinition {
    static let middlewares: [Middleware] = [
        UserToken.authenticator(),
    ]
    
    var request: Request
    var user: User
    
    init(request: Request) throws {
        self.request = request
        self.user = try request.auth.require(User.self)
    }
    
    ...
}

// Client:
let userToken: String = ...
let api = IceCreamAPI(url: serverURL, bearerToken: userToken)

Additional infos as well as example apps can be found on GitHub.

I hope you like the framework. I appreciate anyone who tries it out and gives me feedback. I am very interested in any problems or suggestions that come up. Thank you! :slightly_smiling_face:

4 Likes