Skip to content

Commit 36bce2f

Browse files
authored
Merge pull request #38 from sidepelican/documentation
Update document
2 parents fa377ef + 16b0fb0 commit 36bce2f

6 files changed

Lines changed: 39 additions & 32 deletions

File tree

README.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1517
public 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
3941
import Vapor
4042

4143
struct 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"))
6472
print(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
7080
const stub = createStubClient("http://127.0.0.1:8080");
7181
const echoClient = bindEcho(stub);
72-
const res = await echoClient.hello({ name: "TypeScript" });
82+
const res/*: { message: string }*/ = await echoClient.hello({ name: "TypeScript" });
7383
console.log(res.message); // Hello, TypeScript!
74-
// ^? res: { message: string }
7584
```
7685

7786
Swift 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
...
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
@usableFromInline internal struct CallableKitEmpty: Codable {
1+
@usableFromInline internal struct CallableKitEmpty: Codable, Sendable {
22
@usableFromInline init() {}
33
}

Sources/CallableKit/ServiceTransport.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
public protocol ServiceTransport<Service> {
22
associatedtype Service
33

4-
func register<Request: Decodable>(
4+
func register<Request: Decodable & Sendable>(
55
path: String,
66
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> (Request) async throws -> Void
77
)
88

9-
func register<Response: Encodable>(
9+
func register<Response: Encodable & Sendable>(
1010
path: String,
1111
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> () async throws -> Response
1212
)
@@ -16,14 +16,14 @@ public protocol ServiceTransport<Service> {
1616
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> () async throws -> Void
1717
)
1818

19-
func register<Request: Decodable, Response: Encodable>(
19+
func register<Request: Decodable & Sendable, Response: Encodable & Sendable>(
2020
path: String,
2121
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> (Request) async throws -> Response
2222
)
2323
}
2424

2525
extension ServiceTransport {
26-
@inlinable public func register<Request: Decodable>(
26+
@inlinable public func register<Request: Decodable & Sendable>(
2727
path: String,
2828
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> (Request) async throws -> Void
2929
) {
@@ -37,7 +37,7 @@ extension ServiceTransport {
3737
}
3838
}
3939

40-
@inlinable public func register<Response: Encodable>(
40+
@inlinable public func register<Response: Encodable & Sendable>(
4141
path: String,
4242
methodSelector: @escaping @Sendable (Service.Type) -> (Service) -> () async throws -> Response
4343
) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct ErrorFrame: Decodable, CustomStringConvertible, LocalizedError {
1010
var errorDescription: String? { description }
1111
}
1212

13-
@main struct Main {
13+
@main struct ClientMain {
1414
static func main() async throws {
1515
let client: some StubClientProtocol = URLSessionStubClient(
1616
baseURL: URL(string: "http://127.0.0.1:8080")!,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct ErrorMiddleware<Context: RequestContext>: MiddlewareProtocol {
2323
}
2424
}
2525

26-
@main struct Main {
26+
@main struct HBMain {
2727
static func main() async throws {
2828
let router = Router()
2929
router.add(middleware: ErrorMiddleware())

example/Sources/VaporServer/main.swift renamed to example/Sources/VaporServer/VaporMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import CallableKitVaporTransport
33
import Vapor
44
import Service
55

6-
@main struct Main {
6+
@main struct VaporMain {
77
static func main() async throws {
88
let app = try await Application.make()
99

0 commit comments

Comments
 (0)