kkrpc 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/__tests__/scripts/api.ts +9 -0
- package/deno.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -86,3 +86,74 @@ const api = rpc.getAPI()
|
|
|
86
86
|
const sum = await api.add(1, 2)
|
|
87
87
|
expect(sum).toBe(3)
|
|
88
88
|
```
|
|
89
|
+
|
|
90
|
+
### HTTP Example
|
|
91
|
+
|
|
92
|
+
Codesandbox: https://codesandbox.io/p/live/4a349334-0b04-4352-89f9-cf1955553ae7
|
|
93
|
+
|
|
94
|
+
#### `api.ts`
|
|
95
|
+
|
|
96
|
+
Define API type and implementation.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
export type API = {
|
|
100
|
+
echo: (message: string) => Promise<string>
|
|
101
|
+
add: (a: number, b: number) => Promise<number>
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const api: API = {
|
|
105
|
+
echo: (message) => {
|
|
106
|
+
return Promise.resolve(message)
|
|
107
|
+
},
|
|
108
|
+
add: (a, b) => {
|
|
109
|
+
return Promise.resolve(a + b)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### `server.ts`
|
|
115
|
+
|
|
116
|
+
Server only requires a one-time setup, then it won't need to be touched again.
|
|
117
|
+
All the API implementation is in `api.ts`.
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { HTTPServerIO, RPCChannel } from "kkrpc"
|
|
121
|
+
import { api, type API } from "./api"
|
|
122
|
+
|
|
123
|
+
const serverIO = new HTTPServerIO()
|
|
124
|
+
const serverRPC = new RPCChannel<API, API>(serverIO, api)
|
|
125
|
+
|
|
126
|
+
const server = Bun.serve({
|
|
127
|
+
port: 3000,
|
|
128
|
+
async fetch(req) {
|
|
129
|
+
const url = new URL(req.url)
|
|
130
|
+
if (url.pathname === "/rpc") {
|
|
131
|
+
const res = await serverIO.handleRequest(await req.text())
|
|
132
|
+
return new Response(res, {
|
|
133
|
+
headers: { "Content-Type": "application/json" }
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
return new Response("Not found", { status: 404 })
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
console.log(`Start server on port: ${server.port}`)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
#### `client.ts`
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { HTTPClientIO, RPCChannel } from "kkrpc"
|
|
146
|
+
import { api, type API } from "./api"
|
|
147
|
+
|
|
148
|
+
const clientIO = new HTTPClientIO({
|
|
149
|
+
url: "http://localhost:3000/rpc"
|
|
150
|
+
})
|
|
151
|
+
const clientRPC = new RPCChannel<{}, API>(clientIO, api)
|
|
152
|
+
const clientAPI = clientRPC.getAPI()
|
|
153
|
+
|
|
154
|
+
const echoResponse = await clientAPI.echo("hello")
|
|
155
|
+
console.log("echoResponse", echoResponse)
|
|
156
|
+
|
|
157
|
+
const sum = await clientAPI.add(2, 3)
|
|
158
|
+
console.log("Sum: ", sum)
|
|
159
|
+
```
|
package/__tests__/scripts/api.ts
CHANGED
|
@@ -10,6 +10,9 @@ export interface API {
|
|
|
10
10
|
grade2: {
|
|
11
11
|
multiply(a: number, b: number, callback?: (result: number) => void): Promise<number>
|
|
12
12
|
}
|
|
13
|
+
grade3: {
|
|
14
|
+
divide(a: number, b: number, callback?: (result: number) => void): Promise<number>
|
|
15
|
+
}
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
|
|
@@ -33,6 +36,12 @@ export const apiMethods: API = {
|
|
|
33
36
|
callback?.(a * b)
|
|
34
37
|
return a * b
|
|
35
38
|
}
|
|
39
|
+
},
|
|
40
|
+
grade3: {
|
|
41
|
+
divide: async (a: number, b: number, callback?: (result: number) => void) => {
|
|
42
|
+
callback?.(a / b)
|
|
43
|
+
return a / b
|
|
44
|
+
}
|
|
36
45
|
}
|
|
37
46
|
}
|
|
38
47
|
}
|
package/deno.json
CHANGED