@honestjs/rpc-plugin 1.1.0 → 1.1.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 +100 -44
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
#
|
|
1
|
+
# RPC Plugin
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
## Features
|
|
7
|
-
|
|
8
|
-
- **Route Analysis**: Automatically analyzes controller methods and extracts type information using ts-morph
|
|
9
|
-
- **Schema Generation**: Generates JSON schemas and TypeScript interfaces from types used in controllers
|
|
10
|
-
- **Client Generation**: Creates a fully-typed TypeScript RPC client with proper parameter typing
|
|
11
|
-
- **Type Safety**: Full TypeScript support with generated types and interfaces
|
|
3
|
+
The RPC Plugin automatically analyzes your HonestJS controllers and generates a fully-typed TypeScript RPC client with
|
|
4
|
+
proper parameter typing.
|
|
12
5
|
|
|
13
6
|
## Installation
|
|
14
7
|
|
|
@@ -20,24 +13,18 @@ yarn add @honestjs/rpc-plugin
|
|
|
20
13
|
pnpm add @honestjs/rpc-plugin
|
|
21
14
|
```
|
|
22
15
|
|
|
23
|
-
##
|
|
24
|
-
|
|
25
|
-
### Basic Setup
|
|
16
|
+
## Basic Setup
|
|
26
17
|
|
|
27
18
|
```typescript
|
|
28
19
|
import { RPCPlugin } from '@honestjs/rpc-plugin'
|
|
29
20
|
import { Application } from 'honestjs'
|
|
30
21
|
|
|
31
22
|
const app = new Application({
|
|
32
|
-
plugins: [
|
|
33
|
-
new RPCPlugin({
|
|
34
|
-
outputDir: './generated/rpc'
|
|
35
|
-
})
|
|
36
|
-
]
|
|
23
|
+
plugins: [RPCPlugin]
|
|
37
24
|
})
|
|
38
25
|
```
|
|
39
26
|
|
|
40
|
-
|
|
27
|
+
## Configuration Options
|
|
41
28
|
|
|
42
29
|
```typescript
|
|
43
30
|
interface RPCPluginOptions {
|
|
@@ -87,17 +74,6 @@ apiClient.setDefaultHeaders({
|
|
|
87
74
|
'X-API-Key': 'your-api-key',
|
|
88
75
|
Authorization: 'Bearer your-jwt-token'
|
|
89
76
|
})
|
|
90
|
-
|
|
91
|
-
// Use with custom fetch function (e.g., for testing or custom logic)
|
|
92
|
-
const customFetch = (input: RequestInfo | URL, init?: RequestInit) => {
|
|
93
|
-
console.log('Making request to:', input)
|
|
94
|
-
return fetch(input, init)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const apiClientWithCustomFetch = new ApiClient('http://localhost:3000', {
|
|
98
|
-
fetchFn: customFetch,
|
|
99
|
-
defaultHeaders: { 'X-Custom-Header': 'value' }
|
|
100
|
-
})
|
|
101
77
|
```
|
|
102
78
|
|
|
103
79
|
The generated `client.ts` file contains everything you need:
|
|
@@ -220,14 +196,6 @@ expect(mockFetch).toHaveBeenCalledWith('http://test.com/api/v1/users/123', expec
|
|
|
220
196
|
- Creates parameter validation and typing
|
|
221
197
|
- Builds the complete RPC client with proper error handling
|
|
222
198
|
|
|
223
|
-
## Benefits of the Unified Approach
|
|
224
|
-
|
|
225
|
-
- **No Duplication**: Single source of truth for all type information
|
|
226
|
-
- **Tight Coupling**: Components share data directly without file I/O
|
|
227
|
-
- **Better Performance**: Eliminates redundant analysis and file generation
|
|
228
|
-
- **Consistent Types**: All generated code uses the same type definitions
|
|
229
|
-
- **Easier Maintenance**: Single plugin to configure and maintain
|
|
230
|
-
|
|
231
199
|
## Example Generated Output
|
|
232
200
|
|
|
233
201
|
### Generated Client
|
|
@@ -272,12 +240,100 @@ const rpcPlugin = new RPCPlugin()
|
|
|
272
240
|
await rpcPlugin.analyze() // Manually trigger analysis and generation
|
|
273
241
|
```
|
|
274
242
|
|
|
275
|
-
##
|
|
243
|
+
## Advanced Usage
|
|
244
|
+
|
|
245
|
+
### Custom Controller Pattern
|
|
246
|
+
|
|
247
|
+
If your controllers follow a different file structure:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
new RPCPlugin({
|
|
251
|
+
controllerPattern: 'src/controllers/**/*.controller.ts',
|
|
252
|
+
outputDir: './src/generated/api'
|
|
253
|
+
})
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Manual Generation Control
|
|
257
|
+
|
|
258
|
+
Disable automatic generation and control when files are generated:
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
const rpcPlugin = new RPCPlugin({
|
|
262
|
+
generateOnInit: false
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
// Later in your code
|
|
266
|
+
await rpcPlugin.analyze()
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## Integration with HonestJS
|
|
270
|
+
|
|
271
|
+
### Controller Example
|
|
272
|
+
|
|
273
|
+
Here's how your controllers should be structured for optimal RPC generation:
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import { Controller, Post, Get, Body, Param, Query } from 'honestjs'
|
|
277
|
+
|
|
278
|
+
interface CreateUserDto {
|
|
279
|
+
name: string
|
|
280
|
+
email: string
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
interface ListUsersQuery {
|
|
284
|
+
page?: number
|
|
285
|
+
limit?: number
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
@Controller('/users')
|
|
289
|
+
export class UsersController {
|
|
290
|
+
@Post('/')
|
|
291
|
+
async create(@Body() createUserDto: CreateUserDto): Promise<User> {
|
|
292
|
+
// Implementation
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
@Get('/')
|
|
296
|
+
async list(@Query() query: ListUsersQuery): Promise<User[]> {
|
|
297
|
+
// Implementation
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
@Get('/:id')
|
|
301
|
+
async getById(@Param('id') id: string): Promise<User> {
|
|
302
|
+
// Implementation
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### Module Registration
|
|
276
308
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
309
|
+
Ensure your controllers are properly registered in modules:
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import { Module } from 'honestjs'
|
|
313
|
+
import { UsersController } from './users.controller'
|
|
314
|
+
import { UsersService } from './users.service'
|
|
315
|
+
|
|
316
|
+
@Module({
|
|
317
|
+
controllers: [UsersController],
|
|
318
|
+
providers: [UsersService]
|
|
319
|
+
})
|
|
320
|
+
export class UsersModule {}
|
|
321
|
+
```
|
|
280
322
|
|
|
281
|
-
##
|
|
323
|
+
## Error Handling
|
|
282
324
|
|
|
283
|
-
|
|
325
|
+
The generated client includes comprehensive error handling:
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
try {
|
|
329
|
+
const user = await apiClient.users.create({
|
|
330
|
+
body: { name: 'John', email: 'john@example.com' }
|
|
331
|
+
})
|
|
332
|
+
} catch (error) {
|
|
333
|
+
if (error instanceof ApiError) {
|
|
334
|
+
console.error(`API Error ${error.statusCode}: ${error.message}`)
|
|
335
|
+
} else {
|
|
336
|
+
console.error('Unexpected error:', error)
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@honestjs/rpc-plugin",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "RPC plugin for HonestJS framework",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -29,12 +29,12 @@
|
|
|
29
29
|
"directory": "packages/rpc-plugin"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"ts-json-schema-generator": "^2.
|
|
32
|
+
"ts-json-schema-generator": "^2.5.0",
|
|
33
33
|
"ts-morph": "^26.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"honestjs": "^0.1.
|
|
37
|
-
"hono": "^4.
|
|
36
|
+
"honestjs": "^0.1.6",
|
|
37
|
+
"hono": "^4.12.3"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
40
|
"honestjs": "^0.1.0",
|