@mingzey/typedrpc 1.1.1 → 1.1.2
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 +20 -10
- package/README.zh-CN.md +49 -36
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
[English](./README.md) / [中文](./README.zh-CN.md)
|
|
1
2
|
# TypedRPC
|
|
2
3
|
|
|
3
4
|
TypeScript-based RPC framework with support for multiple connection types including HTTP, Socket, and SocketIO.
|
|
4
5
|
|
|
6
|
+
## Repository
|
|
7
|
+
|
|
8
|
+
[Github](https://github.com/MingZeY/TypedRPC)
|
|
9
|
+
|
|
5
10
|
## Features
|
|
6
11
|
|
|
7
12
|
- **Type-Safe RPC calls** - Leverage TypeScript's type system for end-to-end type safety
|
|
@@ -22,13 +27,13 @@ npm install typedrpc
|
|
|
22
27
|
### Server Setup
|
|
23
28
|
|
|
24
29
|
```typescript
|
|
25
|
-
import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
|
|
30
|
+
import { TypedRPCServer, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
26
31
|
|
|
27
32
|
// Define your API interface
|
|
28
33
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
29
|
-
|
|
34
|
+
// Service layer - Define your services here or use interface inheritance
|
|
30
35
|
math:{
|
|
31
|
-
|
|
36
|
+
// Method layer - Define your methods here
|
|
32
37
|
add(a:number,b:number):number,
|
|
33
38
|
},
|
|
34
39
|
}>({
|
|
@@ -47,7 +52,7 @@ const server = new TypedRPCServer({
|
|
|
47
52
|
|
|
48
53
|
// Hook service methods
|
|
49
54
|
server.hook('math','add',{
|
|
50
|
-
handler:(a,b)=>a+b,
|
|
55
|
+
handler: (a,b) => a+b,
|
|
51
56
|
});
|
|
52
57
|
|
|
53
58
|
// Start listening
|
|
@@ -59,7 +64,7 @@ server.listen({
|
|
|
59
64
|
### Client Setup
|
|
60
65
|
|
|
61
66
|
```typescript
|
|
62
|
-
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
67
|
+
import { TypedRPCClient, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
63
68
|
// Reuse or import the same API definition
|
|
64
69
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
65
70
|
math:{
|
|
@@ -97,9 +102,11 @@ new TypedRPCServer({
|
|
|
97
102
|
local:ServerAPIDefine,
|
|
98
103
|
connection:{
|
|
99
104
|
provider:new TypedRPCConnectionProviderHTTP(),
|
|
105
|
+
// or use socket connection
|
|
100
106
|
// provider:new TypedRPCConnectionProviderSocket(),
|
|
107
|
+
// or use socketio connection
|
|
101
108
|
// provider:new TypedRPCConnectionProviderSocketIO(),
|
|
102
|
-
//
|
|
109
|
+
// or implaement your own connection provider
|
|
103
110
|
}
|
|
104
111
|
})
|
|
105
112
|
```
|
|
@@ -127,7 +134,7 @@ new TypedRPCServer({
|
|
|
127
134
|
TypedRPC supports middleware for both servers and clients:
|
|
128
135
|
|
|
129
136
|
```typescript
|
|
130
|
-
class MyMiddleware extends
|
|
137
|
+
class MyMiddleware extends TypedRPCMiddleware{
|
|
131
138
|
|
|
132
139
|
async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
133
140
|
// Do something when inbound packet
|
|
@@ -150,7 +157,7 @@ TypedRPC provides a built-in context system that allows you to access request co
|
|
|
150
157
|
|
|
151
158
|
```typescript
|
|
152
159
|
// Server-side code
|
|
153
|
-
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
|
|
160
|
+
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from '@mingzey/typedrpc';
|
|
154
161
|
|
|
155
162
|
// Define service interface
|
|
156
163
|
interface MathServiceInterface {
|
|
@@ -172,7 +179,10 @@ class MathService implements MathServiceInterface, TypedRPCContextAware {
|
|
|
172
179
|
// Inject context using TypedRPCContextSymbol
|
|
173
180
|
[TypedRPCContextSymbol]: TypedRPCContext | null = null;
|
|
174
181
|
|
|
175
|
-
|
|
182
|
+
/**
|
|
183
|
+
* For safety, must use @TypedRPCAPIDefine.method() to mark method as RPC method in service usage
|
|
184
|
+
* You need set experimentalDecorators:true in tsconfig.json
|
|
185
|
+
*/
|
|
176
186
|
@TypedRPCAPIDefine.method()
|
|
177
187
|
add(a: number, b: number): number {
|
|
178
188
|
// Access context
|
|
@@ -227,4 +237,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
|
|
|
227
237
|
|
|
228
238
|
## License
|
|
229
239
|
|
|
230
|
-
MIT License - see the [LICENSE](https://github.com/
|
|
240
|
+
MIT License - see the [LICENSE](https://github.com/MingZeY/TypedRPC/blob/master/LICENSE) file for details.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
[English](./README.md) / [中文](./README.zh-CN.md)
|
|
2
|
+
|
|
1
3
|
# TypedRPC
|
|
2
4
|
|
|
3
5
|
基于 TypeScript 的 RPC 框架,支持多种连接类型,包括 HTTP、Socket 和 SocketIO。
|
|
4
6
|
|
|
7
|
+
## 仓库
|
|
8
|
+
|
|
9
|
+
[Github](https://github.com/MingZeY/TypedRPC)
|
|
10
|
+
|
|
5
11
|
## 特性
|
|
6
12
|
|
|
7
13
|
- **类型安全的 RPC 调用** - 利用 TypeScript 的类型系统实现端到端的类型安全
|
|
@@ -22,22 +28,24 @@ npm install typedrpc
|
|
|
22
28
|
### 服务器设置
|
|
23
29
|
|
|
24
30
|
```typescript
|
|
25
|
-
import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
|
|
31
|
+
import { TypedRPCServer, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
26
32
|
|
|
27
33
|
// 定义 API 接口
|
|
28
34
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
29
|
-
|
|
35
|
+
// 服务层 - 在此处定义服务或使用接口继承
|
|
30
36
|
math:{
|
|
31
|
-
|
|
37
|
+
// 方法层 - 在此处定义方法
|
|
32
38
|
add(a:number,b:number):number,
|
|
33
39
|
},
|
|
34
|
-
}>(
|
|
40
|
+
}>({
|
|
41
|
+
timeout:10 * 1000
|
|
42
|
+
});
|
|
35
43
|
|
|
36
44
|
// 创建服务器实例
|
|
37
45
|
const server = new TypedRPCServer({
|
|
38
46
|
// 让 TypeScript 从 ServerAPIDefine 推断类型
|
|
39
47
|
local:ServerAPIDefine,
|
|
40
|
-
// 连接层 -
|
|
48
|
+
// 连接层 - 在此处使用连接提供者
|
|
41
49
|
// connection:{
|
|
42
50
|
// provider:new TypedRPCConnectionProviderHTTP(),
|
|
43
51
|
// }
|
|
@@ -45,7 +53,7 @@ const server = new TypedRPCServer({
|
|
|
45
53
|
|
|
46
54
|
// 挂钩服务方法
|
|
47
55
|
server.hook('math','add',{
|
|
48
|
-
handler:(a,b)=>a+b,
|
|
56
|
+
handler: (a,b) => a+b,
|
|
49
57
|
});
|
|
50
58
|
|
|
51
59
|
// 开始监听
|
|
@@ -57,7 +65,7 @@ server.listen({
|
|
|
57
65
|
### 客户端设置
|
|
58
66
|
|
|
59
67
|
```typescript
|
|
60
|
-
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
68
|
+
import { TypedRPCClient, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
61
69
|
// 重用或导入相同的 API 定义
|
|
62
70
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
63
71
|
math:{
|
|
@@ -95,9 +103,11 @@ new TypedRPCServer({
|
|
|
95
103
|
local:ServerAPIDefine,
|
|
96
104
|
connection:{
|
|
97
105
|
provider:new TypedRPCConnectionProviderHTTP(),
|
|
106
|
+
// or use socket connection
|
|
98
107
|
// provider:new TypedRPCConnectionProviderSocket(),
|
|
108
|
+
// or use socketio connection
|
|
99
109
|
// provider:new TypedRPCConnectionProviderSocketIO(),
|
|
100
|
-
// provider
|
|
110
|
+
// or implaement your own connection provider
|
|
101
111
|
}
|
|
102
112
|
})
|
|
103
113
|
```
|
|
@@ -125,15 +135,15 @@ new TypedRPCServer({
|
|
|
125
135
|
TypedRPC 支持服务器和客户端的中间件:
|
|
126
136
|
|
|
127
137
|
```typescript
|
|
128
|
-
class MyMiddleware extends
|
|
138
|
+
class MyMiddleware extends TypedRPCMiddleware{
|
|
129
139
|
|
|
130
140
|
async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
131
|
-
//
|
|
141
|
+
// Do something when inbound packet
|
|
132
142
|
return context;
|
|
133
143
|
}
|
|
134
144
|
|
|
135
145
|
async outbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
136
|
-
//
|
|
146
|
+
// Do something when outbound packet
|
|
137
147
|
return context;
|
|
138
148
|
}
|
|
139
149
|
|
|
@@ -147,82 +157,85 @@ TypedRPC 提供内置的上下文系统,允许您在服务方法中访问请
|
|
|
147
157
|
### 示例:在服务中使用上下文
|
|
148
158
|
|
|
149
159
|
```typescript
|
|
150
|
-
//
|
|
151
|
-
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
|
|
160
|
+
// Server-side code
|
|
161
|
+
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from '@mingzey/typedrpc';
|
|
152
162
|
|
|
153
|
-
//
|
|
163
|
+
// Define service interface
|
|
154
164
|
interface MathServiceInterface {
|
|
155
165
|
add(a: number, b: number):number;
|
|
156
166
|
}
|
|
157
167
|
|
|
158
|
-
//
|
|
168
|
+
// Create API definition
|
|
159
169
|
const serverAPIDefine = new TypedRPCAPIDefine<{
|
|
160
170
|
math: MathServiceInterface,
|
|
161
171
|
}>();
|
|
162
172
|
|
|
163
|
-
//
|
|
173
|
+
// Create server instance
|
|
164
174
|
const server = new TypedRPCServer({
|
|
165
175
|
local: serverAPIDefine,
|
|
166
176
|
});
|
|
167
177
|
|
|
168
|
-
//
|
|
178
|
+
// Implement service with context awareness
|
|
169
179
|
class MathService implements MathServiceInterface, TypedRPCContextAware {
|
|
170
|
-
//
|
|
180
|
+
// Inject context using TypedRPCContextSymbol
|
|
171
181
|
[TypedRPCContextSymbol]: TypedRPCContext | null = null;
|
|
172
182
|
|
|
173
|
-
|
|
183
|
+
/**
|
|
184
|
+
* For safety, must use @TypedRPCAPIDefine.method() to mark method as RPC method in service usage
|
|
185
|
+
* You need set experimentalDecorators:true in tsconfig.json
|
|
186
|
+
*/
|
|
174
187
|
@TypedRPCAPIDefine.method()
|
|
175
188
|
add(a: number, b: number): number {
|
|
176
|
-
//
|
|
189
|
+
// Access context
|
|
177
190
|
const context = this[TypedRPCContextSymbol];
|
|
178
191
|
if (!context) {
|
|
179
|
-
throw new Error('
|
|
192
|
+
throw new Error('Context is not available');
|
|
180
193
|
}
|
|
181
194
|
|
|
182
|
-
//
|
|
183
|
-
console.log('
|
|
195
|
+
// Use context information (e.g., connection details, authentication)
|
|
196
|
+
console.log('Request received from:', context.connection);
|
|
184
197
|
|
|
185
198
|
return a + b;
|
|
186
199
|
}
|
|
187
200
|
}
|
|
188
201
|
|
|
189
|
-
//
|
|
202
|
+
// Hook service to server, only methods marked with @TypedRPCAPIDefine.method() will be hooked
|
|
190
203
|
server.hookService('math', new MathService());
|
|
191
204
|
|
|
192
|
-
//
|
|
205
|
+
// Start server
|
|
193
206
|
server.listen({
|
|
194
207
|
port: 3698,
|
|
195
208
|
});
|
|
196
209
|
```
|
|
197
210
|
|
|
198
211
|
```typescript
|
|
199
|
-
//
|
|
212
|
+
// Client-side code
|
|
200
213
|
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
201
214
|
|
|
202
|
-
//
|
|
215
|
+
// Reuse API definition
|
|
203
216
|
const serverAPIDefine = new TypedRPCAPIDefine<{
|
|
204
217
|
math: MathServiceInterface,
|
|
205
218
|
}>();
|
|
206
219
|
|
|
207
|
-
//
|
|
220
|
+
// Create client
|
|
208
221
|
const client = new TypedRPCClient({
|
|
209
222
|
remote: serverAPIDefine,
|
|
210
223
|
});
|
|
211
224
|
|
|
212
|
-
//
|
|
225
|
+
// Connect and make RPC call
|
|
213
226
|
const connection = await client.connect("localhost:3698");
|
|
214
227
|
const api = client.getAPI(connection);
|
|
215
|
-
const result = await api.math.add.call(1, 2); //
|
|
228
|
+
const result = await api.math.add.call(1, 2); // Returns 3
|
|
216
229
|
```
|
|
217
230
|
|
|
218
|
-
##
|
|
231
|
+
## More Usage
|
|
219
232
|
|
|
220
|
-
|
|
233
|
+
see `./test/*.ts` for more usage.
|
|
221
234
|
|
|
222
|
-
##
|
|
235
|
+
## Contributing
|
|
223
236
|
|
|
224
|
-
|
|
237
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
225
238
|
|
|
226
|
-
##
|
|
239
|
+
## License
|
|
227
240
|
|
|
228
|
-
MIT
|
|
241
|
+
MIT License - see the [LICENSE](https://github.com/MingZeY/TypedRPC/blob/master/LICENSE) file for details.
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mingzey/typedrpc",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "TypeScript-based RPC framework with support for multiple connection types including HTTP, Socket, and SocketIO or custom",
|
|
5
5
|
"author": "MingZeY <1552904342@qq.com>",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/MingZeY/TypedRPC.git"
|
|
9
|
+
},
|
|
6
10
|
"keywords": [
|
|
7
11
|
"rpc",
|
|
8
12
|
"typescript"
|