ts-game-decorators 1.0.0 → 1.0.1
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 +79 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# express-socket-decorators
|
|
2
|
+
|
|
3
|
+
A TypeScript library for auto-routing Express APIs and Socket.IO events using decorators. Simplifies backend code for scalable Node.js applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
- Decorators for Express route/controller (@RouterController, @Get, @Post, @Authen)
|
|
7
|
+
- Decorators for Socket.IO event handler (@SocketService, @OnEvent, @OnDisconnect, @OnError, @AuthenSocket)
|
|
8
|
+
- Unified `initServer` function to bootstrap Express + Socket.IO + Redis adapter
|
|
9
|
+
- TypeScript-first, auto .d.ts
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
```sh
|
|
13
|
+
npm install express-socket-decorators
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage Example
|
|
17
|
+
|
|
18
|
+
### 1. API Controller
|
|
19
|
+
```ts
|
|
20
|
+
import { RouterController, Get, Post, Authen } from 'express-socket-decorators';
|
|
21
|
+
|
|
22
|
+
@RouterController('/api/public')
|
|
23
|
+
export class PublicAPI {
|
|
24
|
+
@Get('/hello')
|
|
25
|
+
hello(req, res) {
|
|
26
|
+
res.json({ msg: 'Hello world!' });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
@RouterController('/api/private')
|
|
31
|
+
@Authen()
|
|
32
|
+
export class PrivateAPI {
|
|
33
|
+
@Get('/profile')
|
|
34
|
+
profile(req, res) {
|
|
35
|
+
res.json({ user: req.userId });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. Socket Service
|
|
41
|
+
```ts
|
|
42
|
+
import { SocketService, OnEvent, OnDisconnect, AuthenSocket } from 'express-socket-decorators';
|
|
43
|
+
|
|
44
|
+
@SocketService()
|
|
45
|
+
export class GameSocketService {
|
|
46
|
+
@OnEvent('startGame')
|
|
47
|
+
@AuthenSocket()
|
|
48
|
+
start(socket, data) {
|
|
49
|
+
// ...
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
@OnDisconnect()
|
|
53
|
+
disconnect(socket) {
|
|
54
|
+
// ...
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 3. Bootstrap Server
|
|
60
|
+
```ts
|
|
61
|
+
import { initServer } from 'express-socket-decorators';
|
|
62
|
+
import { PublicAPI, PrivateAPI } from './api';
|
|
63
|
+
import { GameSocketService } from './socket';
|
|
64
|
+
import { authAPIToken } from './middleware/auth';
|
|
65
|
+
import { createRedisAdapter } from './config/redis';
|
|
66
|
+
|
|
67
|
+
initServer({
|
|
68
|
+
port: 3000,
|
|
69
|
+
apiControllers: [PublicAPI, PrivateAPI],
|
|
70
|
+
socketServices: [GameSocketService],
|
|
71
|
+
authAPIMiddleware: authAPIToken,
|
|
72
|
+
createRedisAdapter,
|
|
73
|
+
publicPath: __dirname + '/public',
|
|
74
|
+
onReady: () => console.log('Server ready!')
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
MIT
|
package/package.json
CHANGED