@honestjs/rpc-plugin 1.3.0 → 1.4.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 CHANGED
@@ -18,10 +18,13 @@ pnpm add @honestjs/rpc-plugin
18
18
  ```typescript
19
19
  import { RPCPlugin } from '@honestjs/rpc-plugin'
20
20
  import { Application } from 'honestjs'
21
+ import AppModule from './app.module'
21
22
 
22
- const app = new Application({
23
- plugins: [RPCPlugin]
23
+ const { hono } = await Application.create(AppModule, {
24
+ plugins: [new RPCPlugin()]
24
25
  })
26
+
27
+ export default hono
25
28
  ```
26
29
 
27
30
  ## Configuration Options
@@ -32,18 +35,36 @@ interface RPCPluginOptions {
32
35
  readonly tsConfigPath?: string // Path to tsconfig.json (default: 'tsconfig.json')
33
36
  readonly outputDir?: string // Output directory for generated files (default: './generated/rpc')
34
37
  readonly generateOnInit?: boolean // Generate files on initialization (default: true)
35
- readonly openapi?: OpenApiOptions | boolean // Enable OpenAPI spec generation (default: false)
38
+ readonly context?: {
39
+ readonly namespace?: string // Default: 'rpc'
40
+ readonly keys?: {
41
+ readonly artifact?: string // Default: 'artifact'
42
+ }
43
+ }
36
44
  }
45
+ ```
46
+
47
+ ## Application Context Artifact
37
48
 
38
- interface OpenApiOptions {
39
- readonly title?: string // API title (default: 'API')
40
- readonly version?: string // API version (default: '1.0.0')
41
- readonly description?: string // API description (default: '')
42
- readonly servers?: readonly { url: string; description?: string }[] // Server list
43
- readonly outputFile?: string // Output filename (default: 'openapi.json')
49
+ After analysis, RPC plugin publishes this artifact to the application context:
50
+
51
+ ```typescript
52
+ type RpcArtifact = {
53
+ routes: ExtendedRouteInfo[]
54
+ schemas: SchemaInfo[]
44
55
  }
45
56
  ```
46
57
 
58
+ Default key is `'rpc.artifact'` (from `context.namespace + '.' + context.keys.artifact`). This enables direct integration with API docs:
59
+
60
+ ```typescript
61
+ import { ApiDocsPlugin } from '@honestjs/api-docs-plugin'
62
+
63
+ const { hono } = await Application.create(AppModule, {
64
+ plugins: [new RPCPlugin(), new ApiDocsPlugin({ artifact: 'rpc.artifact' })]
65
+ })
66
+ ```
67
+
47
68
  ## What It Generates
48
69
 
49
70
  The plugin generates files in the output directory (default: `./generated/rpc`):
@@ -51,8 +72,8 @@ The plugin generates files in the output directory (default: `./generated/rpc`):
51
72
  | File | Description | When generated |
52
73
  | --------------- | -------------------------------------------- | --------------------- |
53
74
  | `client.ts` | Type-safe RPC client with all DTOs | Always |
54
- | `openapi.json` | OpenAPI 3.0.3 specification | When `openapi` is set |
55
75
  | `.rpc-checksum` | Hash of source files for incremental caching | Always |
76
+ | `rpc-artifact.json` | Serialized routes/schemas artifact for cache-backed context publishing | Always |
56
77
 
57
78
  ### TypeScript RPC Client (`client.ts`)
58
79
 
@@ -190,46 +211,6 @@ const testApiClient = new ApiClient('http://test.com', {
190
211
  expect(mockFetch).toHaveBeenCalledWith('http://test.com/api/v1/users/123', expect.objectContaining({ method: 'GET' }))
191
212
  ```
192
213
 
193
- ## OpenAPI Spec Generation
194
-
195
- The plugin can produce an OpenAPI 3.0.3 JSON specification alongside the client. This is useful for Swagger UI, documentation portals, and third-party integrations. No extra dependencies are needed — the spec is built from the same route and schema data used for client generation.
196
-
197
- ### Enable with defaults
198
-
199
- Pass `true` to use all default values:
200
-
201
- ```typescript
202
- new RPCPlugin({
203
- openapi: true
204
- })
205
- ```
206
-
207
- ### Custom options
208
-
209
- ```typescript
210
- new RPCPlugin({
211
- openapi: {
212
- title: 'My Service API',
213
- version: '2.1.0',
214
- description: 'User management service',
215
- servers: [
216
- { url: 'https://api.example.com', description: 'Production' },
217
- { url: 'http://localhost:3000', description: 'Local' }
218
- ],
219
- outputFile: 'api-spec.json'
220
- }
221
- })
222
- ```
223
-
224
- The generated spec includes:
225
-
226
- - **Paths** derived from your controller routes
227
- - **Parameters** (path, query) with correct types
228
- - **Request bodies** referencing component schemas for DTOs
229
- - **Responses** with schema references and array type support
230
- - **Component schemas** extracted from `ts-json-schema-generator` output
231
- - **Tags** derived from controller names
232
-
233
214
  ## Hash-based Caching
234
215
 
235
216
  On startup the plugin hashes all controller source files (SHA-256) and stores the checksum in `.rpc-checksum` inside the output directory. On subsequent runs, if the hash matches and the expected output files already exist, the expensive analysis and generation pipeline is skipped entirely. This significantly reduces startup time in large projects.
@@ -274,18 +255,11 @@ You can also delete `.rpc-checksum` from the output directory to clear the cache
274
255
  - Creates parameter validation and typing
275
256
  - Builds the complete RPC client with proper error handling
276
257
 
277
- ### 4. OpenAPI Generation (optional)
278
-
279
- - Converts route and schema data to OpenAPI 3.0.3 paths and components
280
- - Maps `@Param()`, `@Query()`, and `@Body()` decorators to the correct OpenAPI parameter locations
281
- - References component schemas via `$ref` for DTOs
282
- - Outputs a single JSON file ready for Swagger UI or any OpenAPI-compatible tool
283
-
284
- ### 5. Incremental Caching
258
+ ### 4. Incremental Caching
285
259
 
286
260
  - Hashes all matched controller files after glob resolution
287
261
  - Compares against the stored `.rpc-checksum`
288
- - Skips steps 1–4 when files are unchanged and output already exists
262
+ - Skips steps 1–3 when files are unchanged and output already exists
289
263
 
290
264
  ## Example Generated Output
291
265
 
package/dist/index.d.mts CHANGED
@@ -72,16 +72,6 @@ declare class ApiError extends Error {
72
72
  constructor(statusCode: number, message: string);
73
73
  }
74
74
 
75
- interface OpenApiOptions {
76
- readonly title?: string;
77
- readonly version?: string;
78
- readonly description?: string;
79
- readonly servers?: readonly {
80
- url: string;
81
- description?: string;
82
- }[];
83
- readonly outputFile?: string;
84
- }
85
75
  /**
86
76
  * Configuration options for the RPCPlugin
87
77
  */
@@ -90,7 +80,12 @@ interface RPCPluginOptions {
90
80
  readonly tsConfigPath?: string;
91
81
  readonly outputDir?: string;
92
82
  readonly generateOnInit?: boolean;
93
- readonly openapi?: OpenApiOptions | boolean;
83
+ readonly context?: {
84
+ readonly namespace?: string;
85
+ readonly keys?: {
86
+ readonly artifact?: string;
87
+ };
88
+ };
94
89
  }
95
90
  /**
96
91
  * Comprehensive RPC plugin that combines route analysis, schema generation, and client generation
@@ -100,17 +95,17 @@ declare class RPCPlugin implements IPlugin {
100
95
  private readonly tsConfigPath;
101
96
  private readonly outputDir;
102
97
  private readonly generateOnInit;
98
+ private readonly contextNamespace;
99
+ private readonly contextArtifactKey;
103
100
  private readonly routeAnalyzer;
104
101
  private readonly schemaGenerator;
105
102
  private readonly clientGenerator;
106
- private readonly openApiGenerator;
107
- private readonly openApiOptions;
108
103
  private project;
109
104
  private analyzedRoutes;
110
105
  private analyzedSchemas;
111
106
  private generatedInfo;
107
+ private app;
112
108
  constructor(options?: RPCPluginOptions);
113
- private resolveOpenApiOptions;
114
109
  /**
115
110
  * Validates the plugin configuration
116
111
  */
@@ -144,6 +139,11 @@ declare class RPCPlugin implements IPlugin {
144
139
  * Checks whether expected output files exist on disk
145
140
  */
146
141
  private outputFilesExist;
142
+ private getArtifactPath;
143
+ private writeArtifactToDisk;
144
+ private loadArtifactFromDisk;
145
+ private publishArtifact;
146
+ private getArtifactContextKey;
147
147
  /**
148
148
  * Cleanup resources to prevent memory leaks
149
149
  */
@@ -198,42 +198,6 @@ declare class ClientGeneratorService {
198
198
  private analyzeRouteParameters;
199
199
  }
200
200
 
201
- interface ResolvedOpenApiOptions {
202
- readonly title: string;
203
- readonly version: string;
204
- readonly description: string;
205
- readonly servers: readonly {
206
- url: string;
207
- description?: string;
208
- }[];
209
- readonly outputFile: string;
210
- }
211
- /**
212
- * Service for generating OpenAPI 3.0.3 specifications from analyzed routes and schemas
213
- */
214
- declare class OpenApiGeneratorService {
215
- private readonly outputDir;
216
- constructor(outputDir: string);
217
- generateSpec(routes: readonly ExtendedRouteInfo[], schemas: readonly SchemaInfo[], options: ResolvedOpenApiOptions): Promise<string>;
218
- private buildSpec;
219
- private buildOperation;
220
- private buildParameters;
221
- private buildRequestBody;
222
- private buildResponses;
223
- private resolveResponseSchema;
224
- private buildSchemaMap;
225
- /**
226
- * Converts Express-style `:param` path to OpenAPI `{param}` syntax
227
- */
228
- private toOpenApiPath;
229
- private tsTypeToJsonSchema;
230
- /**
231
- * Extracts the base type name from a TS type string, stripping
232
- * wrappers like `Partial<...>`, `...[]`, `Promise<...>`.
233
- */
234
- private extractBaseTypeName;
235
- }
236
-
237
201
  /**
238
202
  * Service for analyzing controller methods and extracting type information
239
203
  */
@@ -313,6 +277,8 @@ declare function readChecksum(outputDir: string): ChecksumData | null;
313
277
  */
314
278
  declare function writeChecksum(outputDir: string, data: ChecksumData): Promise<void>;
315
279
 
280
+ /** Minimal route shape needed to build the full API path (prefix + version + route + path). */
281
+ type RoutePathInput = Pick<ExtendedRouteInfo, 'prefix' | 'version' | 'route' | 'path'>;
316
282
  /**
317
283
  * Builds the full path with parameter placeholders
318
284
  */
@@ -320,7 +286,7 @@ declare function buildFullPath(basePath: string, parameters: readonly ParameterM
320
286
  /**
321
287
  * Builds the full API path using route information
322
288
  */
323
- declare function buildFullApiPath(route: ExtendedRouteInfo): string;
289
+ declare function buildFullApiPath(route: RoutePathInput): string;
324
290
 
325
291
  /**
326
292
  * Maps JSON schema types to TypeScript types
@@ -353,6 +319,12 @@ declare const DEFAULT_OPTIONS: {
353
319
  readonly tsConfigPath: "tsconfig.json";
354
320
  readonly outputDir: "./generated/rpc";
355
321
  readonly generateOnInit: true;
322
+ readonly context: {
323
+ readonly namespace: "rpc";
324
+ readonly keys: {
325
+ readonly artifact: "artifact";
326
+ };
327
+ };
356
328
  };
357
329
  /**
358
330
  * Log prefix for the RPC plugin
@@ -371,4 +343,4 @@ declare const BUILTIN_TYPES: Set<string>;
371
343
  */
372
344
  declare const GENERIC_TYPES: Set<string>;
373
345
 
374
- export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, ClientGeneratorService, type ControllerGroups, DEFAULT_OPTIONS, type ExtendedRouteInfo, type FetchFunction, GENERIC_TYPES, type GeneratedClientInfo, LOG_PREFIX, OpenApiGeneratorService, type OpenApiOptions, type ParameterMetadataWithType, RPCPlugin, type RPCPluginOptions, type RequestOptions, RouteAnalyzerService, type RouteParameter, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
346
+ export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, ClientGeneratorService, type ControllerGroups, DEFAULT_OPTIONS, type ExtendedRouteInfo, type FetchFunction, GENERIC_TYPES, type GeneratedClientInfo, LOG_PREFIX, type ParameterMetadataWithType, RPCPlugin, type RPCPluginOptions, type RequestOptions, RouteAnalyzerService, type RouteParameter, type RoutePathInput, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
package/dist/index.d.ts CHANGED
@@ -72,16 +72,6 @@ declare class ApiError extends Error {
72
72
  constructor(statusCode: number, message: string);
73
73
  }
74
74
 
75
- interface OpenApiOptions {
76
- readonly title?: string;
77
- readonly version?: string;
78
- readonly description?: string;
79
- readonly servers?: readonly {
80
- url: string;
81
- description?: string;
82
- }[];
83
- readonly outputFile?: string;
84
- }
85
75
  /**
86
76
  * Configuration options for the RPCPlugin
87
77
  */
@@ -90,7 +80,12 @@ interface RPCPluginOptions {
90
80
  readonly tsConfigPath?: string;
91
81
  readonly outputDir?: string;
92
82
  readonly generateOnInit?: boolean;
93
- readonly openapi?: OpenApiOptions | boolean;
83
+ readonly context?: {
84
+ readonly namespace?: string;
85
+ readonly keys?: {
86
+ readonly artifact?: string;
87
+ };
88
+ };
94
89
  }
95
90
  /**
96
91
  * Comprehensive RPC plugin that combines route analysis, schema generation, and client generation
@@ -100,17 +95,17 @@ declare class RPCPlugin implements IPlugin {
100
95
  private readonly tsConfigPath;
101
96
  private readonly outputDir;
102
97
  private readonly generateOnInit;
98
+ private readonly contextNamespace;
99
+ private readonly contextArtifactKey;
103
100
  private readonly routeAnalyzer;
104
101
  private readonly schemaGenerator;
105
102
  private readonly clientGenerator;
106
- private readonly openApiGenerator;
107
- private readonly openApiOptions;
108
103
  private project;
109
104
  private analyzedRoutes;
110
105
  private analyzedSchemas;
111
106
  private generatedInfo;
107
+ private app;
112
108
  constructor(options?: RPCPluginOptions);
113
- private resolveOpenApiOptions;
114
109
  /**
115
110
  * Validates the plugin configuration
116
111
  */
@@ -144,6 +139,11 @@ declare class RPCPlugin implements IPlugin {
144
139
  * Checks whether expected output files exist on disk
145
140
  */
146
141
  private outputFilesExist;
142
+ private getArtifactPath;
143
+ private writeArtifactToDisk;
144
+ private loadArtifactFromDisk;
145
+ private publishArtifact;
146
+ private getArtifactContextKey;
147
147
  /**
148
148
  * Cleanup resources to prevent memory leaks
149
149
  */
@@ -198,42 +198,6 @@ declare class ClientGeneratorService {
198
198
  private analyzeRouteParameters;
199
199
  }
200
200
 
201
- interface ResolvedOpenApiOptions {
202
- readonly title: string;
203
- readonly version: string;
204
- readonly description: string;
205
- readonly servers: readonly {
206
- url: string;
207
- description?: string;
208
- }[];
209
- readonly outputFile: string;
210
- }
211
- /**
212
- * Service for generating OpenAPI 3.0.3 specifications from analyzed routes and schemas
213
- */
214
- declare class OpenApiGeneratorService {
215
- private readonly outputDir;
216
- constructor(outputDir: string);
217
- generateSpec(routes: readonly ExtendedRouteInfo[], schemas: readonly SchemaInfo[], options: ResolvedOpenApiOptions): Promise<string>;
218
- private buildSpec;
219
- private buildOperation;
220
- private buildParameters;
221
- private buildRequestBody;
222
- private buildResponses;
223
- private resolveResponseSchema;
224
- private buildSchemaMap;
225
- /**
226
- * Converts Express-style `:param` path to OpenAPI `{param}` syntax
227
- */
228
- private toOpenApiPath;
229
- private tsTypeToJsonSchema;
230
- /**
231
- * Extracts the base type name from a TS type string, stripping
232
- * wrappers like `Partial<...>`, `...[]`, `Promise<...>`.
233
- */
234
- private extractBaseTypeName;
235
- }
236
-
237
201
  /**
238
202
  * Service for analyzing controller methods and extracting type information
239
203
  */
@@ -313,6 +277,8 @@ declare function readChecksum(outputDir: string): ChecksumData | null;
313
277
  */
314
278
  declare function writeChecksum(outputDir: string, data: ChecksumData): Promise<void>;
315
279
 
280
+ /** Minimal route shape needed to build the full API path (prefix + version + route + path). */
281
+ type RoutePathInput = Pick<ExtendedRouteInfo, 'prefix' | 'version' | 'route' | 'path'>;
316
282
  /**
317
283
  * Builds the full path with parameter placeholders
318
284
  */
@@ -320,7 +286,7 @@ declare function buildFullPath(basePath: string, parameters: readonly ParameterM
320
286
  /**
321
287
  * Builds the full API path using route information
322
288
  */
323
- declare function buildFullApiPath(route: ExtendedRouteInfo): string;
289
+ declare function buildFullApiPath(route: RoutePathInput): string;
324
290
 
325
291
  /**
326
292
  * Maps JSON schema types to TypeScript types
@@ -353,6 +319,12 @@ declare const DEFAULT_OPTIONS: {
353
319
  readonly tsConfigPath: "tsconfig.json";
354
320
  readonly outputDir: "./generated/rpc";
355
321
  readonly generateOnInit: true;
322
+ readonly context: {
323
+ readonly namespace: "rpc";
324
+ readonly keys: {
325
+ readonly artifact: "artifact";
326
+ };
327
+ };
356
328
  };
357
329
  /**
358
330
  * Log prefix for the RPC plugin
@@ -371,4 +343,4 @@ declare const BUILTIN_TYPES: Set<string>;
371
343
  */
372
344
  declare const GENERIC_TYPES: Set<string>;
373
345
 
374
- export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, ClientGeneratorService, type ControllerGroups, DEFAULT_OPTIONS, type ExtendedRouteInfo, type FetchFunction, GENERIC_TYPES, type GeneratedClientInfo, LOG_PREFIX, OpenApiGeneratorService, type OpenApiOptions, type ParameterMetadataWithType, RPCPlugin, type RPCPluginOptions, type RequestOptions, RouteAnalyzerService, type RouteParameter, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
346
+ export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, ClientGeneratorService, type ControllerGroups, DEFAULT_OPTIONS, type ExtendedRouteInfo, type FetchFunction, GENERIC_TYPES, type GeneratedClientInfo, LOG_PREFIX, type ParameterMetadataWithType, RPCPlugin, type RPCPluginOptions, type RequestOptions, RouteAnalyzerService, type RouteParameter, type RoutePathInput, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };