@zdavison/nestjs-rpc-toolkit 0.0.10 → 0.0.12
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 +263 -0
- package/dist/bin/init.js +15 -2
- package/dist/bin/init.js.map +1 -1
- package/dist/decorators/rpc-controller.decorator.d.ts +35 -0
- package/dist/decorators/rpc-controller.decorator.d.ts.map +1 -0
- package/dist/decorators/rpc-controller.decorator.js +54 -0
- package/dist/decorators/rpc-controller.decorator.js.map +1 -0
- package/dist/decorators/rpc-method.decorator.d.ts +22 -1
- package/dist/decorators/rpc-method.decorator.d.ts.map +1 -1
- package/dist/decorators/rpc-method.decorator.js +32 -22
- package/dist/decorators/rpc-method.decorator.js.map +1 -1
- package/dist/generators/rpc-types-generator.d.ts +1 -1
- package/dist/generators/rpc-types-generator.d.ts.map +1 -1
- package/dist/generators/rpc-types-generator.js +164 -55
- package/dist/generators/rpc-types-generator.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/rpc/rpc-client.d.ts +15 -0
- package/dist/rpc/rpc-client.d.ts.map +1 -0
- package/dist/rpc/rpc-client.js +44 -0
- package/dist/rpc/rpc-client.js.map +1 -0
- package/dist/rpc/typed-message-bus.d.ts +11 -0
- package/dist/rpc/typed-message-bus.d.ts.map +1 -1
- package/dist/rpc/typed-message-bus.js +2 -1
- package/dist/rpc/typed-message-bus.js.map +1 -1
- package/dist/transport/{in-memory.client.d.ts → in-process.client.d.ts} +2 -2
- package/dist/transport/in-process.client.d.ts.map +1 -0
- package/dist/transport/{in-memory.client.js → in-process.client.js} +8 -7
- package/dist/transport/in-process.client.js.map +1 -0
- package/dist/transport/{in-memory.transport.d.ts → in-process.transport.d.ts} +3 -3
- package/dist/transport/in-process.transport.d.ts.map +1 -0
- package/dist/transport/{in-memory.transport.js → in-process.transport.js} +10 -7
- package/dist/transport/in-process.transport.js.map +1 -0
- package/dist/transport/index.d.ts +2 -2
- package/dist/transport/index.d.ts.map +1 -1
- package/dist/transport/index.js +2 -2
- package/dist/transport/index.js.map +1 -1
- package/dist/types/serializable.d.ts +35 -0
- package/dist/types/serializable.d.ts.map +1 -0
- package/dist/types/serializable.js +7 -0
- package/dist/types/serializable.js.map +1 -0
- package/package.json +1 -1
- package/dist/module-base/index.d.ts +0 -15
- package/dist/module-base/index.d.ts.map +0 -1
- package/dist/module-base/index.js +0 -38
- package/dist/module-base/index.js.map +0 -1
- package/dist/transport/in-memory.client.d.ts.map +0 -1
- package/dist/transport/in-memory.client.js.map +0 -1
- package/dist/transport/in-memory.transport.d.ts.map +0 -1
- package/dist/transport/in-memory.transport.js.map +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# @zdavison/nestjs-rpc-toolkit
|
|
2
|
+
|
|
3
|
+
A TypeScript toolkit for type-safe RPC calls in NestJS monorepos. Enables seamless inter-service communication with compile-time type safety and automatic type generation.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/js/%40zdavison%2Fnestjs-rpc-toolkit)
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🛡️ **Type Safety**: Full TypeScript support with generated types for RPC calls
|
|
10
|
+
- 🏗️ **Decorator-Based**: Simple `@RpcController` and `@RpcMethod` decorators
|
|
11
|
+
- 🔄 **Auto-Generation**: Automatic TypeScript type generation from your RPC methods
|
|
12
|
+
- 📦 **Monorepo Ready**: Designed for NestJS monorepos with wildcard package scanning
|
|
13
|
+
- 🚀 **Multiple Transports**: In-memory for development, TCP for production microservices
|
|
14
|
+
- ⚡ **Zero Config**: Smart defaults with optional customization
|
|
15
|
+
|
|
16
|
+
## Quick Start
|
|
17
|
+
|
|
18
|
+
### 1. Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @zdavison/nestjs-rpc-toolkit
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### 2. Define RPC Methods
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { RpcController, RpcMethod } from '@zdavison/nestjs-rpc-toolkit';
|
|
28
|
+
|
|
29
|
+
@RpcController('user') // Creates 'user.*' RPC patterns
|
|
30
|
+
export class UserService {
|
|
31
|
+
@RpcMethod()
|
|
32
|
+
async findOne(params: { id: string }): Promise<User> {
|
|
33
|
+
// RPC pattern: 'user.findOne'
|
|
34
|
+
return this.userRepository.findById(params.id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@RpcMethod()
|
|
38
|
+
async create(params: CreateUserDto): Promise<User> {
|
|
39
|
+
// RPC pattern: 'user.create'
|
|
40
|
+
return this.userRepository.create(params);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### 3. Initialize RPC Package
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npx @zdavison/nestjs-rpc-toolkit bootstrap
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
This creates:
|
|
52
|
+
- RPC package structure
|
|
53
|
+
- Configuration files
|
|
54
|
+
- Type generation scripts
|
|
55
|
+
- TypeScript setup
|
|
56
|
+
|
|
57
|
+
### 4. Generate Types
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm run generate:types
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Generates type-safe interfaces:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
// Generated in your RPC package
|
|
67
|
+
export interface UserDomain {
|
|
68
|
+
findOne(params: { id: string }): Promise<User>;
|
|
69
|
+
create(params: CreateUserDto): Promise<User>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface IRpcClient {
|
|
73
|
+
user: UserDomain;
|
|
74
|
+
product: ProductDomain;
|
|
75
|
+
// ... other domains
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 5. Make Type-Safe RPC Calls
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { MessageBus } from '@your-org/rpc'; // Your generated RPC package
|
|
83
|
+
|
|
84
|
+
@Injectable()
|
|
85
|
+
export class ProductService {
|
|
86
|
+
constructor(private readonly rpc: MessageBus) {}
|
|
87
|
+
|
|
88
|
+
async getProductWithOwner(productId: string) {
|
|
89
|
+
// Type-safe RPC calls with auto-completion
|
|
90
|
+
const product = await this.rpc.send('product.findOne', { id: productId });
|
|
91
|
+
const owner = await this.rpc.send('user.findOne', { id: product.ownerId });
|
|
92
|
+
|
|
93
|
+
return { product, owner };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Documentation
|
|
99
|
+
|
|
100
|
+
### @RpcController Decorator
|
|
101
|
+
|
|
102
|
+
The `@RpcController` decorator marks classes containing RPC methods:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Explicit prefix
|
|
106
|
+
@RpcController('user')
|
|
107
|
+
export class UserService { }
|
|
108
|
+
|
|
109
|
+
// Auto-inferred from class name
|
|
110
|
+
@RpcController() // Infers 'user' from 'UserService'
|
|
111
|
+
export class UserService { }
|
|
112
|
+
|
|
113
|
+
@RpcController() // Infers 'product' from 'ProductApplication'
|
|
114
|
+
export class ProductApplication { }
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Auto-inference rules:**
|
|
118
|
+
- Removes suffixes: `Service`, `Application`, `Handler`, `Repository`
|
|
119
|
+
- Converts to lowercase: `UserService` → `user`
|
|
120
|
+
|
|
121
|
+
### @RpcMethod Decorator
|
|
122
|
+
|
|
123
|
+
Marks methods as RPC endpoints:
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
@RpcController('user')
|
|
127
|
+
export class UserService {
|
|
128
|
+
@RpcMethod()
|
|
129
|
+
async findAll(): Promise<User[]> {
|
|
130
|
+
// Pattern: 'user.findAll'
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
@RpcMethod()
|
|
134
|
+
async findOne(params: { id: string }): Promise<User> {
|
|
135
|
+
// Pattern: 'user.findOne'
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Configuration
|
|
141
|
+
|
|
142
|
+
Create `nestjs-rpc-toolkit.config.json`:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"packages": [
|
|
147
|
+
"packages/modules/*",
|
|
148
|
+
"apps/specific-service"
|
|
149
|
+
],
|
|
150
|
+
"outputDir": "packages/lib-rpc/src"
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
**Package paths support:**
|
|
155
|
+
- Glob patterns: `packages/modules/*`
|
|
156
|
+
- Specific paths: `apps/user-service`
|
|
157
|
+
- Multiple patterns in array
|
|
158
|
+
|
|
159
|
+
### Transport Options
|
|
160
|
+
|
|
161
|
+
#### In-Process Transport (Development)
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { InProcessTransport } from '@zdavison/nestjs-rpc-toolkit';
|
|
165
|
+
|
|
166
|
+
@Module({
|
|
167
|
+
imports: [
|
|
168
|
+
InProcessTransport.forRoot(),
|
|
169
|
+
],
|
|
170
|
+
})
|
|
171
|
+
export class AppModule {}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### TCP Transport (Production)
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { TcpTransport } from '@zdavison/nestjs-rpc-toolkit';
|
|
178
|
+
|
|
179
|
+
@Module({
|
|
180
|
+
imports: [
|
|
181
|
+
TcpTransport.forRoot({
|
|
182
|
+
host: 'localhost',
|
|
183
|
+
port: 3001,
|
|
184
|
+
}),
|
|
185
|
+
],
|
|
186
|
+
})
|
|
187
|
+
export class AppModule {}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Examples
|
|
191
|
+
|
|
192
|
+
The `examples/` directory contains a complete monorepo setup:
|
|
193
|
+
|
|
194
|
+
```bash
|
|
195
|
+
cd examples
|
|
196
|
+
pnpm install
|
|
197
|
+
pnpm generate-rpc # Generate RPC types
|
|
198
|
+
pnpm build # Build all packages
|
|
199
|
+
pnpm dev # Start API in development mode
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Example Structure
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
examples/
|
|
206
|
+
├── apps/api/ # Main NestJS application
|
|
207
|
+
├── modules/
|
|
208
|
+
│ ├── user-module/ # User domain module
|
|
209
|
+
│ └── auth-module/ # Auth domain module
|
|
210
|
+
├── lib-rpc/ # Generated RPC types package
|
|
211
|
+
└── package.json
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Serialization Requirements
|
|
215
|
+
|
|
216
|
+
All RPC parameters and return types must be JSON-serializable for TCP transport:
|
|
217
|
+
|
|
218
|
+
✅ **Allowed:**
|
|
219
|
+
- Primitives: `string`, `number`, `boolean`, `null`
|
|
220
|
+
- Plain objects: `{ name: string, age: number }`
|
|
221
|
+
- Arrays: `string[]`, `User[]`
|
|
222
|
+
|
|
223
|
+
❌ **Avoid:**
|
|
224
|
+
- Functions, callbacks
|
|
225
|
+
- Class instances (use plain objects/interfaces)
|
|
226
|
+
- `Buffer`, `Map`, `Set`
|
|
227
|
+
- `undefined` (use `null` instead)
|
|
228
|
+
- DOM elements
|
|
229
|
+
|
|
230
|
+
## API Reference
|
|
231
|
+
|
|
232
|
+
### Core Classes
|
|
233
|
+
|
|
234
|
+
- **`MessageBus<T>`**: Type-safe RPC client
|
|
235
|
+
- **`RpcTypesGenerator`**: Generates TypeScript types from RPC methods
|
|
236
|
+
- **`InProcessTransport`**: In-process transport for development
|
|
237
|
+
- **`TcpTransport`**: TCP transport for production
|
|
238
|
+
|
|
239
|
+
### Decorators
|
|
240
|
+
|
|
241
|
+
- **`@RpcController(prefix?)`**: Marks RPC controller classes
|
|
242
|
+
- **`@RpcMethod(pattern?)`**: Marks RPC endpoint methods
|
|
243
|
+
|
|
244
|
+
## Contributing
|
|
245
|
+
|
|
246
|
+
1. Fork the repository
|
|
247
|
+
2. Create your feature branch: `git checkout -b feature/my-feature`
|
|
248
|
+
3. Commit your changes: `git commit -am 'Add some feature'`
|
|
249
|
+
4. Push to the branch: `git push origin feature/my-feature`
|
|
250
|
+
5. Submit a pull request
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
MIT
|
|
255
|
+
|
|
256
|
+
## Support
|
|
257
|
+
|
|
258
|
+
- [GitHub Issues](https://github.com/zdavison/nestjs-rpc-toolkit/issues)
|
|
259
|
+
- [Documentation](https://github.com/zdavison/nestjs-rpc-toolkit)
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
**Made for NestJS monorepos** 🚀
|
package/dist/bin/init.js
CHANGED
|
@@ -58,7 +58,7 @@ class RpcPackageInitializer {
|
|
|
58
58
|
console.log('3. Build the package with "npm run build"');
|
|
59
59
|
}
|
|
60
60
|
async promptForConfig() {
|
|
61
|
-
const packagePath = await this.question('Where would you like to create the RPC package? ', 'packages/rpc');
|
|
61
|
+
const packagePath = await this.question('Where would you like to create the RPC package? ', 'packages/lib-rpc');
|
|
62
62
|
const projectName = path.basename(this.cwd);
|
|
63
63
|
const defaultPackageName = `@${projectName}/rpc`;
|
|
64
64
|
const packageName = await this.question('What should the package name be? ', defaultPackageName);
|
|
@@ -83,9 +83,11 @@ class RpcPackageInitializer {
|
|
|
83
83
|
}
|
|
84
84
|
async createRpcPackage(config) {
|
|
85
85
|
const fullPackagePath = path.resolve(this.cwd, config.packagePath);
|
|
86
|
+
// Create directory structure
|
|
86
87
|
await this.ensureDirectory(fullPackagePath);
|
|
87
88
|
await this.ensureDirectory(path.join(fullPackagePath, 'src'));
|
|
88
89
|
await this.ensureDirectory(path.join(fullPackagePath, 'scripts'));
|
|
90
|
+
// Create files
|
|
89
91
|
await this.createPackageJson(fullPackagePath, config);
|
|
90
92
|
await this.createTsConfig(fullPackagePath);
|
|
91
93
|
await this.createRpcConfig(fullPackagePath, config);
|
|
@@ -112,7 +114,7 @@ class RpcPackageInitializer {
|
|
|
112
114
|
"generate:types": "ts-node scripts/generate-all-rpc-types.ts"
|
|
113
115
|
},
|
|
114
116
|
dependencies: {
|
|
115
|
-
"@zdavison/nestjs-rpc-toolkit": "^0.0.
|
|
117
|
+
"@zdavison/nestjs-rpc-toolkit": "^0.0.12",
|
|
116
118
|
"@nestjs/common": "^10.0.0",
|
|
117
119
|
"@nestjs/microservices": "^10.0.0"
|
|
118
120
|
},
|
|
@@ -127,12 +129,15 @@ class RpcPackageInitializer {
|
|
|
127
129
|
fs.writeFileSync(filePath, JSON.stringify(packageJson, null, 2));
|
|
128
130
|
}
|
|
129
131
|
findRootTsConfig() {
|
|
132
|
+
// Search for root tsconfig files starting from repo root
|
|
130
133
|
const possiblePaths = [
|
|
131
134
|
'tsconfig.base.json',
|
|
132
135
|
'tsconfig.json'
|
|
133
136
|
];
|
|
137
|
+
// Find git root or use current working directory
|
|
134
138
|
let searchDir = this.cwd;
|
|
135
139
|
try {
|
|
140
|
+
// Try to find git root by looking for .git directory
|
|
136
141
|
let current = this.cwd;
|
|
137
142
|
while (current !== path.dirname(current)) {
|
|
138
143
|
if (fs.existsSync(path.join(current, '.git'))) {
|
|
@@ -143,6 +148,7 @@ class RpcPackageInitializer {
|
|
|
143
148
|
}
|
|
144
149
|
}
|
|
145
150
|
catch {
|
|
151
|
+
// If git root search fails, use current directory
|
|
146
152
|
}
|
|
147
153
|
for (const tsConfigPath of possiblePaths) {
|
|
148
154
|
const fullPath = path.join(searchDir, tsConfigPath);
|
|
@@ -153,9 +159,11 @@ class RpcPackageInitializer {
|
|
|
153
159
|
return null;
|
|
154
160
|
}
|
|
155
161
|
async createTsConfig(packagePath) {
|
|
162
|
+
// Try to find a root tsconfig to extend from
|
|
156
163
|
const rootTsConfig = this.findRootTsConfig();
|
|
157
164
|
let tsConfig;
|
|
158
165
|
if (rootTsConfig) {
|
|
166
|
+
// Calculate relative path from package to root tsconfig
|
|
159
167
|
const relativePath = path.relative(packagePath, rootTsConfig);
|
|
160
168
|
tsConfig = {
|
|
161
169
|
extends: relativePath,
|
|
@@ -170,6 +178,7 @@ class RpcPackageInitializer {
|
|
|
170
178
|
};
|
|
171
179
|
}
|
|
172
180
|
else {
|
|
181
|
+
// Create a standalone valid tsconfig
|
|
173
182
|
tsConfig = {
|
|
174
183
|
compilerOptions: {
|
|
175
184
|
target: "ES2020",
|
|
@@ -221,10 +230,12 @@ generator.generate();
|
|
|
221
230
|
fs.writeFileSync(filePath, script);
|
|
222
231
|
}
|
|
223
232
|
async createSourceFiles(packagePath) {
|
|
233
|
+
// Create index.ts
|
|
224
234
|
const indexContent = `export * from './all.rpc.gen';
|
|
225
235
|
export * from './typed-message-bus';
|
|
226
236
|
`;
|
|
227
237
|
fs.writeFileSync(path.join(packagePath, 'src', 'index.ts'), indexContent);
|
|
238
|
+
// Create typed-message-bus.ts
|
|
228
239
|
const messageBusContent = `import { MessageBus as BaseMessageBus, IMessageBus } from '@zdavison/nestjs-rpc-toolkit';
|
|
229
240
|
import { AllRpcMethods } from './all.rpc.gen';
|
|
230
241
|
import { Injectable } from '@nestjs/common';
|
|
@@ -241,6 +252,7 @@ export class MessageBus extends BaseMessageBus<AllRpcMethods> implements ITypedM
|
|
|
241
252
|
}
|
|
242
253
|
`;
|
|
243
254
|
fs.writeFileSync(path.join(packagePath, 'src', 'typed-message-bus.ts'), messageBusContent);
|
|
255
|
+
// Create placeholder all.rpc.gen.ts
|
|
244
256
|
const placeholderContent = `// This file will be generated by the RPC types generator
|
|
245
257
|
// Run 'npm run generate:types' to generate the actual types
|
|
246
258
|
|
|
@@ -252,6 +264,7 @@ export interface AllRpcMethods {
|
|
|
252
264
|
}
|
|
253
265
|
}
|
|
254
266
|
exports.RpcPackageInitializer = RpcPackageInitializer;
|
|
267
|
+
// Main execution
|
|
255
268
|
if (require.main === module) {
|
|
256
269
|
const initializer = new RpcPackageInitializer();
|
|
257
270
|
initializer.init().catch((error) => {
|
package/dist/bin/init.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/bin/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,2CAA6B;AAC7B,uCAAyB;AACzB,mDAAqC;AAQrC,MAAM,qBAAqB;IAIzB;QACE,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;QACzB,IAAI,CAAC,EAAE,GAAG,QAAQ,CAAC,eAAe,CAAC;YACjC,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,MAAM,EAAE,OAAO,CAAC,MAAM;SACvB,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,IAAI;QACR,OAAO,CAAC,GAAG,CAAC,qCAAqC,CAAC,CAAC;QAEnD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAC5C,MAAM,IAAI,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;QAEpC,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;QAEhB,OAAO,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;QACzD,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;QAC7B,OAAO,CAAC,GAAG,CAAC,4GAA4G,CAAC,CAAC;QAC1H,OAAO,CAAC,GAAG,CAAC,uDAAuD,CAAC,CAAC;QACrE,OAAO,CAAC,GAAG,CAAC,2CAA2C,CAAC,CAAC;IAC3D,CAAC;IAEO,KAAK,CAAC,eAAe;QAC3B,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,QAAQ,CACrC,kDAAkD,EAClD,
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/bin/init.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,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OAAO,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;IAC5B,MAAM,WAAW,GAAG,IAAI,qBAAqB,EAAE,CAAC;IAChD,WAAW,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QACjC,OAAO,CAAC,KAAK,CAAC,mCAAmC,EAAE,KAAK,CAAC,CAAC;QAC1D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
/**
|
|
3
|
+
* RPC Controller decorator for classes that contain RPC methods.
|
|
4
|
+
* This decorator applies the NestJS @Controller decorator internally
|
|
5
|
+
* and sets metadata for RPC method discovery and pattern generation.
|
|
6
|
+
*
|
|
7
|
+
* @param prefix - Optional module prefix for RPC patterns (e.g., 'user', 'product').
|
|
8
|
+
* If not provided, infers from class name by removing common suffixes:
|
|
9
|
+
* Service, Application, Handler, Repository
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* @RpcController('user')
|
|
13
|
+
* export class UserService {
|
|
14
|
+
* @RpcMethod()
|
|
15
|
+
* async findAll(): Promise<User[]> {
|
|
16
|
+
* // Pattern: auto-generated as 'user.findAll'
|
|
17
|
+
* return this.users;
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @RpcController() // Infers 'user' from UserService
|
|
22
|
+
* export class UserService { ... }
|
|
23
|
+
*
|
|
24
|
+
* @RpcController() // Infers 'product' from ProductApplication
|
|
25
|
+
* export class ProductApplication { ... }
|
|
26
|
+
*
|
|
27
|
+
* @RpcController() // Infers 'order' from OrderHandler
|
|
28
|
+
* export class OrderHandler { ... }
|
|
29
|
+
*
|
|
30
|
+
* @RpcController() // Infers 'customer' from CustomerRepository
|
|
31
|
+
* export class CustomerRepository { ... }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function RpcController(prefix?: string): ClassDecorator;
|
|
35
|
+
//# sourceMappingURL=rpc-controller.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-controller.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/rpc-controller.decorator.ts"],"names":[],"mappings":"AACA,OAAO,kBAAkB,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,cAAc,CAkB7D"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RpcController = RpcController;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
/**
|
|
7
|
+
* RPC Controller decorator for classes that contain RPC methods.
|
|
8
|
+
* This decorator applies the NestJS @Controller decorator internally
|
|
9
|
+
* and sets metadata for RPC method discovery and pattern generation.
|
|
10
|
+
*
|
|
11
|
+
* @param prefix - Optional module prefix for RPC patterns (e.g., 'user', 'product').
|
|
12
|
+
* If not provided, infers from class name by removing common suffixes:
|
|
13
|
+
* Service, Application, Handler, Repository
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* @RpcController('user')
|
|
17
|
+
* export class UserService {
|
|
18
|
+
* @RpcMethod()
|
|
19
|
+
* async findAll(): Promise<User[]> {
|
|
20
|
+
* // Pattern: auto-generated as 'user.findAll'
|
|
21
|
+
* return this.users;
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @RpcController() // Infers 'user' from UserService
|
|
26
|
+
* export class UserService { ... }
|
|
27
|
+
*
|
|
28
|
+
* @RpcController() // Infers 'product' from ProductApplication
|
|
29
|
+
* export class ProductApplication { ... }
|
|
30
|
+
*
|
|
31
|
+
* @RpcController() // Infers 'order' from OrderHandler
|
|
32
|
+
* export class OrderHandler { ... }
|
|
33
|
+
*
|
|
34
|
+
* @RpcController() // Infers 'customer' from CustomerRepository
|
|
35
|
+
* export class CustomerRepository { ... }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function RpcController(prefix) {
|
|
39
|
+
return function (target) {
|
|
40
|
+
// Apply the NestJS Controller decorator
|
|
41
|
+
(0, common_1.Controller)()(target);
|
|
42
|
+
// Set RPC-specific metadata for pattern generation
|
|
43
|
+
let modulePrefix = prefix;
|
|
44
|
+
if (!modulePrefix) {
|
|
45
|
+
// Infer from class name: UserService -> 'user', ProductApplication -> 'product', OrderHandler -> 'order'
|
|
46
|
+
modulePrefix = target.name
|
|
47
|
+
.replace(/(Service|Application|Handler|Repository)$/, '')
|
|
48
|
+
.toLowerCase();
|
|
49
|
+
}
|
|
50
|
+
Reflect.defineMetadata('rpc:module', modulePrefix, target);
|
|
51
|
+
return target;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=rpc-controller.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rpc-controller.decorator.js","sourceRoot":"","sources":["../../src/decorators/rpc-controller.decorator.ts"],"names":[],"mappings":";;AAmCA,sCAkBC;AArDD,2CAA4C;AAC5C,4BAA0B;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,SAAgB,aAAa,CAAC,MAAe;IAC3C,OAAO,UAAU,MAAW;QAC1B,wCAAwC;QACxC,IAAA,mBAAU,GAAE,CAAC,MAAM,CAAC,CAAC;QAErB,mDAAmD;QACnD,IAAI,YAAY,GAAG,MAAM,CAAC;QAC1B,IAAI,CAAC,YAAY,EAAE,CAAC;YAClB,yGAAyG;YACzG,YAAY,GAAG,MAAM,CAAC,IAAI;iBACvB,OAAO,CAAC,2CAA2C,EAAE,EAAE,CAAC;iBACxD,WAAW,EAAE,CAAC;QACnB,CAAC;QAED,OAAO,CAAC,cAAc,CAAC,YAAY,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;QAE3D,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
import { SerializableRpcMethod } from '../types/serializable';
|
|
1
2
|
import 'reflect-metadata';
|
|
2
|
-
|
|
3
|
+
/**
|
|
4
|
+
* RPC Method decorator with compile-time serialization validation.
|
|
5
|
+
* Ensures all parameters and return types are JSON-serializable for TCP transport.
|
|
6
|
+
* Pattern is automatically generated from the class name or @Controller decorator.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* @RpcMethod()
|
|
11
|
+
* async findUser(id: string): Promise<User> {
|
|
12
|
+
* // ✅ Valid: string param, User return type are serializable
|
|
13
|
+
* // Pattern: auto-generated as 'user.findUser' or 'controllerName.findUser'
|
|
14
|
+
* return this.userRepo.findById(id);
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* @RpcMethod()
|
|
18
|
+
* async invalidMethod(callback: (data: string) => void): Promise<HTMLElement> {
|
|
19
|
+
* // ❌ Error: callback and HTMLElement are not serializable
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function RpcMethod<T extends (...args: any[]) => any>(): (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<SerializableRpcMethod<T>> | void;
|
|
3
24
|
//# sourceMappingURL=rpc-method.decorator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-method.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/rpc-method.decorator.ts"],"names":[],"mappings":"AAEA,OAAO,kBAAkB,CAAC;AAE1B,wBAAgB,SAAS,CAAC,
|
|
1
|
+
{"version":3,"file":"rpc-method.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/rpc-method.decorator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,kBAAkB,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK,CAC9D,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,MAAM,EACnB,UAAU,EAAE,uBAAuB,CAAC,CAAC,CAAC,KACnC,uBAAuB,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAyC5D"}
|
|
@@ -4,31 +4,38 @@ exports.RpcMethod = RpcMethod;
|
|
|
4
4
|
const microservices_1 = require("@nestjs/microservices");
|
|
5
5
|
const rpc_registry_1 = require("../rpc/rpc-registry");
|
|
6
6
|
require("reflect-metadata");
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* RPC Method decorator with compile-time serialization validation.
|
|
9
|
+
* Ensures all parameters and return types are JSON-serializable for TCP transport.
|
|
10
|
+
* Pattern is automatically generated from the class name or @Controller decorator.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* @RpcMethod()
|
|
15
|
+
* async findUser(id: string): Promise<User> {
|
|
16
|
+
* // ✅ Valid: string param, User return type are serializable
|
|
17
|
+
* // Pattern: auto-generated as 'user.findUser' or 'controllerName.findUser'
|
|
18
|
+
* return this.userRepo.findById(id);
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* @RpcMethod()
|
|
22
|
+
* async invalidMethod(callback: (data: string) => void): Promise<HTMLElement> {
|
|
23
|
+
* // ❌ Error: callback and HTMLElement are not serializable
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function RpcMethod() {
|
|
8
28
|
return function (target, propertyKey, descriptor) {
|
|
9
29
|
const originalMethod = descriptor.value;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
actualPattern = pattern;
|
|
15
|
-
module = pattern.split('.')[0];
|
|
16
|
-
methodName = pattern.split('.')[1];
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
let controllerDecorator = Reflect.getMetadata('path', target.constructor) ||
|
|
20
|
-
Reflect.getMetadata(Symbol.for('path'), target.constructor) ||
|
|
21
|
-
Reflect.getMetadata('PATH_METADATA', target.constructor);
|
|
22
|
-
if (controllerDecorator) {
|
|
23
|
-
module = controllerDecorator;
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
const className = target.constructor.name;
|
|
27
|
-
module = className.replace(/Service$/, '').toLowerCase();
|
|
28
|
-
}
|
|
29
|
-
methodName = pattern || propertyKey;
|
|
30
|
-
actualPattern = `${module}.${methodName}`;
|
|
30
|
+
// Get module prefix from @RpcController decorator metadata
|
|
31
|
+
const module = Reflect.getMetadata('rpc:module', target.constructor);
|
|
32
|
+
if (!module) {
|
|
33
|
+
throw new Error(`@RpcMethod can only be used in classes decorated with @RpcController. Class: ${target.constructor.name}`);
|
|
31
34
|
}
|
|
35
|
+
// Use method name for the pattern
|
|
36
|
+
const methodName = propertyKey;
|
|
37
|
+
const actualPattern = `${module}.${methodName}`;
|
|
38
|
+
// Register in RPC registry
|
|
32
39
|
const metadata = {
|
|
33
40
|
pattern: actualPattern,
|
|
34
41
|
module,
|
|
@@ -37,12 +44,15 @@ function RpcMethod(pattern) {
|
|
|
37
44
|
propertyKey,
|
|
38
45
|
};
|
|
39
46
|
rpc_registry_1.rpcRegistry.registerMethod(metadata);
|
|
47
|
+
// Create wrapper method that handles array-based payloads
|
|
40
48
|
descriptor.value = function (args) {
|
|
49
|
+
// If args is an array, spread it as arguments, otherwise pass as single argument
|
|
41
50
|
if (Array.isArray(args)) {
|
|
42
51
|
return originalMethod.apply(this, args);
|
|
43
52
|
}
|
|
44
53
|
return originalMethod.call(this, args);
|
|
45
54
|
};
|
|
55
|
+
// Apply the NestJS MessagePattern decorator
|
|
46
56
|
(0, microservices_1.MessagePattern)(actualPattern)(target, propertyKey, descriptor);
|
|
47
57
|
};
|
|
48
58
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-method.decorator.js","sourceRoot":"","sources":["../../src/decorators/rpc-method.decorator.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"rpc-method.decorator.js","sourceRoot":"","sources":["../../src/decorators/rpc-method.decorator.ts"],"names":[],"mappings":";;AAyBA,8BA6CC;AAtED,yDAA6E;AAC7E,sDAAqE;AAErE,4BAA0B;AAE1B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,SAAS;IAKvB,OAAO,UACL,MAAW,EACX,WAAmB,EACnB,UAAsC;QAEtC,MAAM,cAAc,GAAG,UAAU,CAAC,KAAM,CAAC;QAEzC,2DAA2D;QAC3D,MAAM,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,YAAY,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;QAErE,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,gFAAgF,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC;QAC7H,CAAC;QAED,kCAAkC;QAClC,MAAM,UAAU,GAAG,WAAW,CAAC;QAC/B,MAAM,aAAa,GAAG,GAAG,MAAM,IAAI,UAAU,EAAE,CAAC;QAEhD,2BAA2B;QAC3B,MAAM,QAAQ,GAAsB;YAClC,OAAO,EAAE,aAAa;YACtB,MAAM;YACN,UAAU;YACV,MAAM,EAAE,MAAM,CAAC,WAAW;YAC1B,WAAW;SACZ,CAAC;QACF,0BAAW,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;QAErC,0DAA0D;QAC1D,UAAU,CAAC,KAAK,GAAG,UAAqB,IAAW;YACjD,iFAAiF;YACjF,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;gBACxB,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC1C,CAAC;YACD,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACzC,CAAQ,CAAC;QAET,4CAA4C;QAC5C,IAAA,8BAAkB,EAAC,aAAa,CAAC,CAAC,MAAM,EAAE,WAAW,EAAE,UAAU,CAAC,CAAC;IACrE,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export interface RpcGenerationConfig {
|
|
2
|
+
/** Package paths to scan for RPC methods. Supports glob patterns like 'packages/modules/*' */
|
|
2
3
|
packages: string[];
|
|
3
4
|
outputDir: string;
|
|
4
5
|
}
|
|
@@ -29,7 +30,6 @@ export declare class RpcTypesGenerator {
|
|
|
29
30
|
private getModuleForFile;
|
|
30
31
|
private isInternalType;
|
|
31
32
|
private processMethod;
|
|
32
|
-
private extractPattern;
|
|
33
33
|
private generateTypesFile;
|
|
34
34
|
private generateModuleTypesFile;
|
|
35
35
|
private generateMainTypesFile;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc-types-generator.d.ts","sourceRoot":"","sources":["../../src/generators/rpc-types-generator.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,mBAAmB;
|
|
1
|
+
{"version":3,"file":"rpc-types-generator.d.ts","sourceRoot":"","sources":["../../src/generators/rpc-types-generator.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,mBAAmB;IAClC,8FAA8F;IAC9F,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;CACpB;AAiBD,qBAAa,iBAAiB;IAShB,OAAO,CAAC,OAAO;IAR3B,OAAO,CAAC,QAAQ,CAAmC;IACnD,OAAO,CAAC,UAAU,CAAuB;IACzC,OAAO,CAAC,UAAU,CAA+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