CallableKit provides typesafe rpc with Swift server
public protocol EchoServiceProtocol {
func hello(request: EchoHelloRequest) async throws -> EchoHelloResponse
}
public struct EchoHelloRequest: Codable, Sendable {
public init(name: String) {
self.name = name
}
public var name: String
}
public struct EchoHelloResponse: Codable, Sendable {
public init(message: String) {
self.message = message
}
public var message: String
}
Run code generation
Server implements thet protocol and register to routes.
import Vapor
struct EchoService: EchoServiceProtocol {
func hello(request: EchoHelloRequest) async throws -> EchoHelloResponse {
return .init(message: "Hello, \(request.name)!")
}
}
let app = Application()
defer { app.shutdown() }
let echoProvider = EchoServiceProvider { _ in // EchoServiceProvider is generated type
EchoService()
}
try app.register(collection: echoProvider)
try app.run()
let client: some StubClientProtocol = FoundationHTTPStubClient(
baseURL: URL(string: "http://127.0.0.1:8080")!,
)
// .echo.hello is generated extension
let res = try await client.echo.hello(request: .init(name: "Swift"))
print(res.message) // Hello, Swift!
const stub = createStubClient("http://127.0.0.1:8080");
const echoClient = bindEcho(stub);
const res = await echoClient.hello({ name: "TypeScript" });
console.log(res.message); // Hello, TypeScript!
// ^? res: { message: string }
Swift types are coverted to TS types powered by CodableToTypeScript
$ swift run codegen Sources/APIDefinition \
--client_out Sources/Client/Gen \
--vapor_out Sources/Server/Gen \
--ts_out TSClient/src/Gen \
Mint is useful to checkout and run.
or
Use from package plugin (see example)
Add plugin target in your Package.swift (or add dedicated Package.swift for independency)
dependencies: [
...
.package(url: "https://github.com/sidepelican/CallableKit", from: "1.0.0"),
],
targets: [
...
.plugin(
name: "CodegenPlugin",
capability: .command(
intent: .custom(verb: "codegen", description: "Generate codes from Sources/APIDefinition"),
permissions: [.writeToPackageDirectory(reason: "Place generated code")]
),
dependencies: [
.product(name: "codegen", package: "CallableKit"),
]
)
]
$ swift run codegen
link |
Stars: 0 |
Last commit: 2 weeks ago |
Full Changelog: https://github.com/sidepelican/CallableKit/compare/1.2.1...1.3.0
72177c002bed5a22b028448739b5a199a53eec9b : Remove force coercing to string type of ID suffix type
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics