@@ -9,9 +9,11 @@ CallableKit provides typesafe rpc with Swift server
99
1010# Example
1111
12- - Define interface protocol and share this module for server and client.
12+ Define interface protocol and share the module to server and client.
13+ Interface protocols must be annotated with ` @Callable `
1314
1415``` swift
16+ @Callable
1517public protocol EchoServiceProtocol {
1618 func hello (request : EchoHelloRequest) async throws -> EchoHelloResponse
1719}
@@ -31,11 +33,11 @@ public struct EchoHelloResponse: Codable, Sendable {
3133}
3234```
3335
34- - Run code generation
35-
36- - Server implements thet protocol and register to routes.
36+ Server implements thet protocol and register to routes.
3737
3838``` swift
39+ import APIDefinition // Your interface module
40+ import CallableKitVaporTransport
3941import Vapor
4042
4143struct EchoService : EchoServiceProtocol {
@@ -44,34 +46,41 @@ struct EchoService: EchoServiceProtocol {
4446 }
4547}
4648
47- let app = Application ()
48- defer { app.shutdown () }
49- let echoProvider = EchoServiceProvider { _ in // EchoServiceProvider is generated type
49+ let app = try await Application.make ()
50+
51+ // Swift macro generates `configureEchoServiceProtocol`
52+ configureEchoServiceProtocol (transport : VaporTransport (router : app.routes ) { _ in
5053 EchoService ()
51- }
52- try app.register (collection : echoProvider)
53- try app.run ()
54+ })
55+
56+ try await app.execute ()
57+ try await app.asyncShutdown ()
5458```
5559
56- - Client can call the functions through stub client. Using the same protocol.
60+ Client can call the functions through stub client. Using the same protocol.
5761
5862``` swift
59- let client: some StubClientProtocol = FoundationHTTPStubClient (
60- baseURL : URL (string : " http://127.0.0.1:8080" )! ,
63+ import APIDefinition
64+ import CallableKitURLSessionStub
65+
66+ let stubClient: some StubClientProtocol = URLSessionStubClient (
67+ baseURL : URL (string : " http://127.0.0.1:8080" )!
6168)
62- // .echo.hello is generated extension
63- let res = try await client.echo .hello (request : .init (name : " Swift" ))
69+ // EchoServiceProtocolStub is also generated by Swift macro
70+ let echoClient = EchoServiceProtocolStub (client : stubClient)
71+ let res = try await echoClient.hello (request : .init (name : " Swift" ))
6472print (res.message ) // Hello, Swift!
6573```
6674
67- - TypeScript client also supported!
75+ TypeScript client is also supported.
76+ It needs manual code generation.
77+
6878
6979``` ts
7080const stub = createStubClient (" http://127.0.0.1:8080" );
7181const echoClient = bindEcho (stub );
72- const res = await echoClient .hello ({ name: " TypeScript" });
82+ const res/* : { message: string } */ = await echoClient .hello ({ name: " TypeScript" });
7383console .log (res .message ); // Hello, TypeScript!
74- // ^? res: { message: string }
7584```
7685
7786Swift types are coverted to TS types powered by [ CodableToTypeScript] ( https://github.com/omochi/CodableToTypeScript )
@@ -82,23 +91,21 @@ Swift types are coverted to TS types powered by [CodableToTypeScript](https://gi
8291
8392``` sh
8493$ swift run codegen Sources/APIDefinition \
85- --client_out Sources/Client/Gen \
86- --vapor_out Sources/Server/Gen \
8794 --ts_out TSClient/src/Gen \
8895```
8996
9097 [ Mint] ( https://github.com/yonaskolb/Mint ) is useful to checkout and run.
9198
92- or
99+ or
93100
94101- Use from package plugin (see [ example] ( https://github.com/sidepelican/CallableKit/tree/main/example ) )
95-
102+
96103 Add plugin target in your Package.swift (or add dedicated Package.swift for independency)
97104
98105``` swift
99106 dependencies: [
100107 ...
101- .package (url : " https://github.com/sidepelican/CallableKit" , from : " 1.0.0" ),
108+ .package (url : " https://github.com/sidepelican/CallableKit.git " , from : " 1.0.0" ),
102109 ],
103110 targets: [
104111 ...
0 commit comments