@mingzey/typedrpc 1.1.1 → 1.1.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 +30 -17
- package/README.zh-CN.md +59 -43
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
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
|
-
- **Type-Safe RPC calls** - Leverage TypeScript's type system for end-to-end type safety
|
|
8
|
-
- **Multiple connection types** - Support for HTTP, Socket, and SocketIO connections, or custom connection providers
|
|
9
|
-
- **Middleware support** - Extensible middleware system for request/response handling
|
|
10
|
-
- **Context-aware** - Built-in context system for passing data between handlers
|
|
11
|
-
- **Bidirectional communication** - Support for two-way RPC calls between client and server, capability depends on connection type
|
|
12
|
-
- **Easy to use** - Simple API for defining services and methods
|
|
12
|
+
- 🛡️ **Type-Safe RPC calls** - Leverage TypeScript's type system for end-to-end type safety
|
|
13
|
+
- 🔌 **Multiple connection types** - Support for HTTP, Socket, and SocketIO connections, or custom connection providers
|
|
14
|
+
- 🔄 **Middleware support** - Extensible middleware system for request/response handling
|
|
15
|
+
- 📝 **Context-aware** - Built-in context system for passing data between handlers
|
|
16
|
+
- 🔁 **Bidirectional communication** - Support for two-way RPC calls between client and server, capability depends on connection type
|
|
17
|
+
- 📦 **Easy to use** - Simple API for defining services and methods
|
|
18
|
+
- 🚀 **Zero dependencies** - No external library dependencies, only depends on TypeScript standard library
|
|
19
|
+
- 🌐 **Frontend and backend compatible** - Can be used in Node.js backend and browser frontend
|
|
20
|
+
- 📈 **High extensibility** - Can be easily integrated into frameworks like Electron, Express as an RPC solution
|
|
13
21
|
|
|
14
22
|
## Installation
|
|
15
23
|
|
|
16
24
|
```bash
|
|
17
|
-
npm install typedrpc
|
|
25
|
+
npm install @mingzey/typedrpc
|
|
18
26
|
```
|
|
19
27
|
|
|
20
28
|
## Basic Usage
|
|
@@ -22,13 +30,13 @@ npm install typedrpc
|
|
|
22
30
|
### Server Setup
|
|
23
31
|
|
|
24
32
|
```typescript
|
|
25
|
-
import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
|
|
33
|
+
import { TypedRPCServer, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
26
34
|
|
|
27
35
|
// Define your API interface
|
|
28
36
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
29
|
-
|
|
37
|
+
// Service layer - Define your services here or use interface inheritance
|
|
30
38
|
math:{
|
|
31
|
-
|
|
39
|
+
// Method layer - Define your methods here
|
|
32
40
|
add(a:number,b:number):number,
|
|
33
41
|
},
|
|
34
42
|
}>({
|
|
@@ -47,7 +55,7 @@ const server = new TypedRPCServer({
|
|
|
47
55
|
|
|
48
56
|
// Hook service methods
|
|
49
57
|
server.hook('math','add',{
|
|
50
|
-
handler:(a,b)=>a+b,
|
|
58
|
+
handler: (a,b) => a+b,
|
|
51
59
|
});
|
|
52
60
|
|
|
53
61
|
// Start listening
|
|
@@ -59,7 +67,7 @@ server.listen({
|
|
|
59
67
|
### Client Setup
|
|
60
68
|
|
|
61
69
|
```typescript
|
|
62
|
-
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
70
|
+
import { TypedRPCClient, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
63
71
|
// Reuse or import the same API definition
|
|
64
72
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
65
73
|
math:{
|
|
@@ -97,9 +105,11 @@ new TypedRPCServer({
|
|
|
97
105
|
local:ServerAPIDefine,
|
|
98
106
|
connection:{
|
|
99
107
|
provider:new TypedRPCConnectionProviderHTTP(),
|
|
108
|
+
// or use socket connection
|
|
100
109
|
// provider:new TypedRPCConnectionProviderSocket(),
|
|
110
|
+
// or use socketio connection
|
|
101
111
|
// provider:new TypedRPCConnectionProviderSocketIO(),
|
|
102
|
-
//
|
|
112
|
+
// or implaement your own connection provider
|
|
103
113
|
}
|
|
104
114
|
})
|
|
105
115
|
```
|
|
@@ -127,7 +137,7 @@ new TypedRPCServer({
|
|
|
127
137
|
TypedRPC supports middleware for both servers and clients:
|
|
128
138
|
|
|
129
139
|
```typescript
|
|
130
|
-
class MyMiddleware extends
|
|
140
|
+
class MyMiddleware extends TypedRPCMiddleware{
|
|
131
141
|
|
|
132
142
|
async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
133
143
|
// Do something when inbound packet
|
|
@@ -150,7 +160,7 @@ TypedRPC provides a built-in context system that allows you to access request co
|
|
|
150
160
|
|
|
151
161
|
```typescript
|
|
152
162
|
// Server-side code
|
|
153
|
-
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
|
|
163
|
+
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from '@mingzey/typedrpc';
|
|
154
164
|
|
|
155
165
|
// Define service interface
|
|
156
166
|
interface MathServiceInterface {
|
|
@@ -172,7 +182,10 @@ class MathService implements MathServiceInterface, TypedRPCContextAware {
|
|
|
172
182
|
// Inject context using TypedRPCContextSymbol
|
|
173
183
|
[TypedRPCContextSymbol]: TypedRPCContext | null = null;
|
|
174
184
|
|
|
175
|
-
|
|
185
|
+
/**
|
|
186
|
+
* For safety, must use @TypedRPCAPIDefine.method() to mark method as RPC method in service usage
|
|
187
|
+
* You need set experimentalDecorators:true in tsconfig.json
|
|
188
|
+
*/
|
|
176
189
|
@TypedRPCAPIDefine.method()
|
|
177
190
|
add(a: number, b: number): number {
|
|
178
191
|
// Access context
|
|
@@ -227,4 +240,4 @@ Contributions are welcome! Please feel free to submit a Pull Request.
|
|
|
227
240
|
|
|
228
241
|
## License
|
|
229
242
|
|
|
230
|
-
MIT License - see the [LICENSE](https://github.com/
|
|
243
|
+
MIT License - see the [LICENSE](https://github.com/MingZeY/TypedRPC/blob/master/LICENSE) file for details.
|
package/README.zh-CN.md
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
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
|
-
- **类型安全的 RPC 调用** - 利用 TypeScript 的类型系统实现端到端的类型安全
|
|
8
|
-
- **多种连接类型** - 支持 HTTP、Socket 和 SocketIO 连接,或自定义`TypedRPCConnectionProvider`
|
|
9
|
-
- **中间件支持** - 可扩展的中间件系统,用于请求/响应处理
|
|
10
|
-
- **上下文感知** - 内置上下文系统,用于在处理程序之间传递数据
|
|
11
|
-
- **双向通信** - 支持客户端和服务器之间的双向 RPC 调用,能力取决于连接类型
|
|
12
|
-
- **易于使用** - 用于定义服务和方法的简单 API
|
|
13
|
+
- 🛡️ **类型安全的 RPC 调用** - 利用 TypeScript 的类型系统实现端到端的类型安全
|
|
14
|
+
- 🔌 **多种连接类型** - 支持 HTTP、Socket 和 SocketIO 连接,或自定义`TypedRPCConnectionProvider`
|
|
15
|
+
- 🔄 **中间件支持** - 可扩展的中间件系统,用于请求/响应处理
|
|
16
|
+
- 📝 **上下文感知** - 内置上下文系统,用于在处理程序之间传递数据
|
|
17
|
+
- 🔁 **双向通信** - 支持客户端和服务器之间的双向 RPC 调用,能力取决于连接类型
|
|
18
|
+
- 📦 **易于使用** - 用于定义服务和方法的简单 API
|
|
19
|
+
- 🚀 **零依赖** - 不依赖任何外部库,仅依赖 TypeScript 标准库
|
|
20
|
+
- 🌐 **前后端通用** - 可在 Node.js 后端和浏览器前端使用
|
|
21
|
+
- 📈 **高拓展性** - 可轻松集成入 Electron、Express 等框架作为 RPC 调用方案
|
|
13
22
|
|
|
14
23
|
## 安装
|
|
15
24
|
|
|
16
25
|
```bash
|
|
17
|
-
npm install typedrpc
|
|
26
|
+
npm install @mingzey/typedrpc
|
|
18
27
|
```
|
|
19
28
|
|
|
20
29
|
## 基本使用
|
|
@@ -22,22 +31,24 @@ npm install typedrpc
|
|
|
22
31
|
### 服务器设置
|
|
23
32
|
|
|
24
33
|
```typescript
|
|
25
|
-
import { TypedRPCServer, TypedRPCAPIDefine } from 'typedrpc';
|
|
34
|
+
import { TypedRPCServer, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
26
35
|
|
|
27
36
|
// 定义 API 接口
|
|
28
37
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
29
|
-
|
|
38
|
+
// 服务层 - 在此处定义服务或使用接口继承
|
|
30
39
|
math:{
|
|
31
|
-
|
|
40
|
+
// 方法层 - 在此处定义方法
|
|
32
41
|
add(a:number,b:number):number,
|
|
33
42
|
},
|
|
34
|
-
}>(
|
|
43
|
+
}>({
|
|
44
|
+
timeout:10 * 1000
|
|
45
|
+
});
|
|
35
46
|
|
|
36
47
|
// 创建服务器实例
|
|
37
48
|
const server = new TypedRPCServer({
|
|
38
49
|
// 让 TypeScript 从 ServerAPIDefine 推断类型
|
|
39
50
|
local:ServerAPIDefine,
|
|
40
|
-
// 连接层 -
|
|
51
|
+
// 连接层 - 在此处使用连接提供者
|
|
41
52
|
// connection:{
|
|
42
53
|
// provider:new TypedRPCConnectionProviderHTTP(),
|
|
43
54
|
// }
|
|
@@ -45,7 +56,7 @@ const server = new TypedRPCServer({
|
|
|
45
56
|
|
|
46
57
|
// 挂钩服务方法
|
|
47
58
|
server.hook('math','add',{
|
|
48
|
-
handler:(a,b)=>a+b,
|
|
59
|
+
handler: (a,b) => a+b,
|
|
49
60
|
});
|
|
50
61
|
|
|
51
62
|
// 开始监听
|
|
@@ -57,7 +68,7 @@ server.listen({
|
|
|
57
68
|
### 客户端设置
|
|
58
69
|
|
|
59
70
|
```typescript
|
|
60
|
-
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
71
|
+
import { TypedRPCClient, TypedRPCAPIDefine } from '@mingzey/typedrpc';
|
|
61
72
|
// 重用或导入相同的 API 定义
|
|
62
73
|
const ServerAPIDefine = new TypedRPCAPIDefine<{
|
|
63
74
|
math:{
|
|
@@ -95,9 +106,11 @@ new TypedRPCServer({
|
|
|
95
106
|
local:ServerAPIDefine,
|
|
96
107
|
connection:{
|
|
97
108
|
provider:new TypedRPCConnectionProviderHTTP(),
|
|
109
|
+
// or use socket connection
|
|
98
110
|
// provider:new TypedRPCConnectionProviderSocket(),
|
|
111
|
+
// or use socketio connection
|
|
99
112
|
// provider:new TypedRPCConnectionProviderSocketIO(),
|
|
100
|
-
// provider
|
|
113
|
+
// or implaement your own connection provider
|
|
101
114
|
}
|
|
102
115
|
})
|
|
103
116
|
```
|
|
@@ -125,15 +138,15 @@ new TypedRPCServer({
|
|
|
125
138
|
TypedRPC 支持服务器和客户端的中间件:
|
|
126
139
|
|
|
127
140
|
```typescript
|
|
128
|
-
class MyMiddleware extends
|
|
141
|
+
class MyMiddleware extends TypedRPCMiddleware{
|
|
129
142
|
|
|
130
143
|
async inbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
131
|
-
//
|
|
144
|
+
// Do something when inbound packet
|
|
132
145
|
return context;
|
|
133
146
|
}
|
|
134
147
|
|
|
135
148
|
async outbound(context: TypedRPCContext): Promise<TypedRPCContext> {
|
|
136
|
-
//
|
|
149
|
+
// Do something when outbound packet
|
|
137
150
|
return context;
|
|
138
151
|
}
|
|
139
152
|
|
|
@@ -147,82 +160,85 @@ TypedRPC 提供内置的上下文系统,允许您在服务方法中访问请
|
|
|
147
160
|
### 示例:在服务中使用上下文
|
|
148
161
|
|
|
149
162
|
```typescript
|
|
150
|
-
//
|
|
151
|
-
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from 'typedrpc';
|
|
163
|
+
// Server-side code
|
|
164
|
+
import { TypedRPCServer, TypedRPCAPIDefine, TypedRPCContextSymbol, type TypedRPCContext, type TypedRPCContextAware } from '@mingzey/typedrpc';
|
|
152
165
|
|
|
153
|
-
//
|
|
166
|
+
// Define service interface
|
|
154
167
|
interface MathServiceInterface {
|
|
155
168
|
add(a: number, b: number):number;
|
|
156
169
|
}
|
|
157
170
|
|
|
158
|
-
//
|
|
171
|
+
// Create API definition
|
|
159
172
|
const serverAPIDefine = new TypedRPCAPIDefine<{
|
|
160
173
|
math: MathServiceInterface,
|
|
161
174
|
}>();
|
|
162
175
|
|
|
163
|
-
//
|
|
176
|
+
// Create server instance
|
|
164
177
|
const server = new TypedRPCServer({
|
|
165
178
|
local: serverAPIDefine,
|
|
166
179
|
});
|
|
167
180
|
|
|
168
|
-
//
|
|
181
|
+
// Implement service with context awareness
|
|
169
182
|
class MathService implements MathServiceInterface, TypedRPCContextAware {
|
|
170
|
-
//
|
|
183
|
+
// Inject context using TypedRPCContextSymbol
|
|
171
184
|
[TypedRPCContextSymbol]: TypedRPCContext | null = null;
|
|
172
185
|
|
|
173
|
-
|
|
186
|
+
/**
|
|
187
|
+
* For safety, must use @TypedRPCAPIDefine.method() to mark method as RPC method in service usage
|
|
188
|
+
* You need set experimentalDecorators:true in tsconfig.json
|
|
189
|
+
*/
|
|
174
190
|
@TypedRPCAPIDefine.method()
|
|
175
191
|
add(a: number, b: number): number {
|
|
176
|
-
//
|
|
192
|
+
// Access context
|
|
177
193
|
const context = this[TypedRPCContextSymbol];
|
|
178
194
|
if (!context) {
|
|
179
|
-
throw new Error('
|
|
195
|
+
throw new Error('Context is not available');
|
|
180
196
|
}
|
|
181
197
|
|
|
182
|
-
//
|
|
183
|
-
console.log('
|
|
198
|
+
// Use context information (e.g., connection details, authentication)
|
|
199
|
+
console.log('Request received from:', context.connection);
|
|
184
200
|
|
|
185
201
|
return a + b;
|
|
186
202
|
}
|
|
187
203
|
}
|
|
188
204
|
|
|
189
|
-
//
|
|
205
|
+
// Hook service to server, only methods marked with @TypedRPCAPIDefine.method() will be hooked
|
|
190
206
|
server.hookService('math', new MathService());
|
|
191
207
|
|
|
192
|
-
//
|
|
208
|
+
// Start server
|
|
193
209
|
server.listen({
|
|
194
210
|
port: 3698,
|
|
195
211
|
});
|
|
196
212
|
```
|
|
197
213
|
|
|
198
214
|
```typescript
|
|
199
|
-
//
|
|
215
|
+
// Client-side code
|
|
200
216
|
import { TypedRPCClient, TypedRPCAPIDefine } from 'typedrpc';
|
|
201
217
|
|
|
202
|
-
//
|
|
218
|
+
// Reuse API definition
|
|
203
219
|
const serverAPIDefine = new TypedRPCAPIDefine<{
|
|
204
220
|
math: MathServiceInterface,
|
|
205
221
|
}>();
|
|
206
222
|
|
|
207
|
-
//
|
|
223
|
+
// Create client
|
|
208
224
|
const client = new TypedRPCClient({
|
|
209
225
|
remote: serverAPIDefine,
|
|
210
226
|
});
|
|
211
227
|
|
|
212
|
-
//
|
|
228
|
+
// Connect and make RPC call
|
|
213
229
|
const connection = await client.connect("localhost:3698");
|
|
214
230
|
const api = client.getAPI(connection);
|
|
215
|
-
const result = await api.math.add.call(1, 2); //
|
|
231
|
+
const result = await api.math.add.call(1, 2); // Returns 3
|
|
216
232
|
```
|
|
217
233
|
|
|
218
|
-
##
|
|
234
|
+
## More Usage
|
|
219
235
|
|
|
220
|
-
|
|
236
|
+
see `./test/*.ts` for more usage.
|
|
221
237
|
|
|
222
|
-
##
|
|
238
|
+
## Contributing
|
|
223
239
|
|
|
224
|
-
|
|
240
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
225
241
|
|
|
226
|
-
##
|
|
242
|
+
## License
|
|
227
243
|
|
|
228
|
-
MIT
|
|
244
|
+
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.3",
|
|
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"
|
|
@@ -17,7 +21,7 @@
|
|
|
17
21
|
"scripts": {
|
|
18
22
|
"test": "tsx test/test.ts",
|
|
19
23
|
"build": "tsc -p tsconfig.build.json",
|
|
20
|
-
"check": "tsc
|
|
24
|
+
"check": "tsc --noEmit"
|
|
21
25
|
},
|
|
22
26
|
"devDependencies": {
|
|
23
27
|
"@types/express": "^5.0.6",
|