@honestjs/rpc-plugin 1.4.0 → 1.5.0

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
@@ -1,7 +1,7 @@
1
1
  # RPC Plugin
2
2
 
3
- The RPC Plugin automatically analyzes your HonestJS controllers and generates a fully-typed TypeScript RPC client with
4
- proper parameter typing.
3
+ The RPC Plugin automatically analyzes your HonestJS controllers and, by default, generates a fully-typed TypeScript RPC
4
+ client with proper parameter typing. You can also provide custom generators.
5
5
 
6
6
  ## Installation
7
7
 
@@ -35,6 +35,7 @@ interface RPCPluginOptions {
35
35
  readonly tsConfigPath?: string // Path to tsconfig.json (default: 'tsconfig.json')
36
36
  readonly outputDir?: string // Output directory for generated files (default: './generated/rpc')
37
37
  readonly generateOnInit?: boolean // Generate files on initialization (default: true)
38
+ readonly generators?: readonly RPCGenerator[] // Optional list of generators to execute
38
39
  readonly context?: {
39
40
  readonly namespace?: string // Default: 'rpc'
40
41
  readonly keys?: {
@@ -44,6 +45,20 @@ interface RPCPluginOptions {
44
45
  }
45
46
  ```
46
47
 
48
+ ### Generator Behavior
49
+
50
+ - If `generators` is omitted, the plugin uses the built-in `TypeScriptClientGenerator` by default.
51
+ - If `generators` is provided, only those generators are executed.
52
+ - You can still use the built-in TypeScript client generator explicitly:
53
+
54
+ ```typescript
55
+ import { RPCPlugin, TypeScriptClientGenerator } from '@honestjs/rpc-plugin'
56
+
57
+ new RPCPlugin({
58
+ generators: [new TypeScriptClientGenerator('./generated/rpc')]
59
+ })
60
+ ```
61
+
47
62
  ## Application Context Artifact
48
63
 
49
64
  After analysis, RPC plugin publishes this artifact to the application context:
@@ -71,7 +86,7 @@ The plugin generates files in the output directory (default: `./generated/rpc`):
71
86
 
72
87
  | File | Description | When generated |
73
88
  | --------------- | -------------------------------------------- | --------------------- |
74
- | `client.ts` | Type-safe RPC client with all DTOs | Always |
89
+ | `client.ts` | Type-safe RPC client with all DTOs | When TypeScript generator runs |
75
90
  | `.rpc-checksum` | Hash of source files for incremental caching | Always |
76
91
  | `rpc-artifact.json` | Serialized routes/schemas artifact for cache-backed context publishing | Always |
77
92
 
package/dist/index.d.mts CHANGED
@@ -41,10 +41,12 @@ interface SchemaInfo {
41
41
  readonly typescriptType?: string;
42
42
  }
43
43
  /**
44
- * Generated client file information
44
+ * Generated output information for one generator run.
45
45
  */
46
46
  interface GeneratedClientInfo {
47
- readonly clientFile: string;
47
+ readonly generator: string;
48
+ readonly clientFile?: string;
49
+ readonly outputFiles?: readonly string[];
48
50
  readonly generatedAt: string;
49
51
  }
50
52
 
@@ -72,6 +74,22 @@ declare class ApiError extends Error {
72
74
  constructor(statusCode: number, message: string);
73
75
  }
74
76
 
77
+ /**
78
+ * Context passed to each RPC generator.
79
+ */
80
+ interface RPCGeneratorContext {
81
+ readonly outputDir: string;
82
+ readonly routes: readonly ExtendedRouteInfo[];
83
+ readonly schemas: readonly SchemaInfo[];
84
+ }
85
+ /**
86
+ * Contract for custom RPC generators.
87
+ */
88
+ interface RPCGenerator {
89
+ readonly name: string;
90
+ generate(context: RPCGeneratorContext): Promise<GeneratedClientInfo>;
91
+ }
92
+
75
93
  /**
76
94
  * Configuration options for the RPCPlugin
77
95
  */
@@ -80,6 +98,7 @@ interface RPCPluginOptions {
80
98
  readonly tsConfigPath?: string;
81
99
  readonly outputDir?: string;
82
100
  readonly generateOnInit?: boolean;
101
+ readonly generators?: readonly RPCGenerator[];
83
102
  readonly context?: {
84
103
  readonly namespace?: string;
85
104
  readonly keys?: {
@@ -99,11 +118,11 @@ declare class RPCPlugin implements IPlugin {
99
118
  private readonly contextArtifactKey;
100
119
  private readonly routeAnalyzer;
101
120
  private readonly schemaGenerator;
102
- private readonly clientGenerator;
121
+ private readonly generators;
103
122
  private project;
104
123
  private analyzedRoutes;
105
124
  private analyzedSchemas;
106
- private generatedInfo;
125
+ private generatedInfos;
107
126
  private app;
108
127
  constructor(options?: RPCPluginOptions);
109
128
  /**
@@ -135,6 +154,10 @@ declare class RPCPlugin implements IPlugin {
135
154
  * Get the generation info
136
155
  */
137
156
  getGenerationInfo(): GeneratedClientInfo | null;
157
+ /**
158
+ * Get all generation infos
159
+ */
160
+ getGenerationInfos(): readonly GeneratedClientInfo[];
138
161
  /**
139
162
  * Checks whether expected output files exist on disk
140
163
  */
@@ -142,6 +165,8 @@ declare class RPCPlugin implements IPlugin {
142
165
  private getArtifactPath;
143
166
  private writeArtifactToDisk;
144
167
  private loadArtifactFromDisk;
168
+ private runGenerators;
169
+ private hasTypeScriptGenerator;
145
170
  private publishArtifact;
146
171
  private getArtifactContextKey;
147
172
  /**
@@ -159,41 +184,46 @@ declare class RPCPlugin implements IPlugin {
159
184
  }
160
185
 
161
186
  /**
162
- * Service for generating TypeScript RPC clients
187
+ * Built-in generator for TypeScript RPC clients.
163
188
  */
164
- declare class ClientGeneratorService {
189
+ declare class TypeScriptClientGenerator implements RPCGenerator {
165
190
  private readonly outputDir;
191
+ readonly name = "typescript-client";
166
192
  constructor(outputDir: string);
167
193
  /**
168
- * Generates the TypeScript RPC client
194
+ * Generates the TypeScript RPC client.
195
+ */
196
+ generate(context: RPCGeneratorContext): Promise<GeneratedClientInfo>;
197
+ /**
198
+ * Generates the TypeScript RPC client.
169
199
  */
170
200
  generateClient(routes: readonly ExtendedRouteInfo[], schemas: readonly SchemaInfo[]): Promise<GeneratedClientInfo>;
171
201
  /**
172
- * Generates the main client file with types included
202
+ * Generates the main client file with types included.
173
203
  */
174
204
  private generateClientFile;
175
205
  /**
176
- * Generates the client TypeScript content with types included
206
+ * Generates the client TypeScript content with types included.
177
207
  */
178
208
  private generateClientContent;
179
209
  /**
180
- * Generates controller methods for the client
210
+ * Generates controller methods for the client.
181
211
  */
182
212
  private generateControllerMethods;
183
213
  /**
184
- * Extracts the proper return type from route analysis
214
+ * Extracts the proper return type from route analysis.
185
215
  */
186
216
  private extractReturnType;
187
217
  /**
188
- * Generates schema types from integrated schema generation
218
+ * Generates schema types from integrated schema generation.
189
219
  */
190
220
  private generateSchemaTypes;
191
221
  /**
192
- * Groups routes by controller for better organization
222
+ * Groups routes by controller for better organization.
193
223
  */
194
224
  private groupRoutesByController;
195
225
  /**
196
- * Analyzes route parameters to determine their types and usage
226
+ * Analyzes route parameters to determine their types and usage.
197
227
  */
198
228
  private analyzeRouteParameters;
199
229
  }
@@ -277,6 +307,8 @@ declare function readChecksum(outputDir: string): ChecksumData | null;
277
307
  */
278
308
  declare function writeChecksum(outputDir: string, data: ChecksumData): Promise<void>;
279
309
 
310
+ /** Minimal route shape needed to build the full API path (prefix + version + route + path). */
311
+ type RoutePathInput = Pick<ExtendedRouteInfo, 'prefix' | 'version' | 'route' | 'path'>;
280
312
  /**
281
313
  * Builds the full path with parameter placeholders
282
314
  */
@@ -284,7 +316,7 @@ declare function buildFullPath(basePath: string, parameters: readonly ParameterM
284
316
  /**
285
317
  * Builds the full API path using route information
286
318
  */
287
- declare function buildFullApiPath(route: ExtendedRouteInfo): string;
319
+ declare function buildFullApiPath(route: RoutePathInput): string;
288
320
 
289
321
  /**
290
322
  * Maps JSON schema types to TypeScript types
@@ -341,4 +373,4 @@ declare const BUILTIN_TYPES: Set<string>;
341
373
  */
342
374
  declare const GENERIC_TYPES: Set<string>;
343
375
 
344
- 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, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
376
+ export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, 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, TypeScriptClientGenerator, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
package/dist/index.d.ts CHANGED
@@ -41,10 +41,12 @@ interface SchemaInfo {
41
41
  readonly typescriptType?: string;
42
42
  }
43
43
  /**
44
- * Generated client file information
44
+ * Generated output information for one generator run.
45
45
  */
46
46
  interface GeneratedClientInfo {
47
- readonly clientFile: string;
47
+ readonly generator: string;
48
+ readonly clientFile?: string;
49
+ readonly outputFiles?: readonly string[];
48
50
  readonly generatedAt: string;
49
51
  }
50
52
 
@@ -72,6 +74,22 @@ declare class ApiError extends Error {
72
74
  constructor(statusCode: number, message: string);
73
75
  }
74
76
 
77
+ /**
78
+ * Context passed to each RPC generator.
79
+ */
80
+ interface RPCGeneratorContext {
81
+ readonly outputDir: string;
82
+ readonly routes: readonly ExtendedRouteInfo[];
83
+ readonly schemas: readonly SchemaInfo[];
84
+ }
85
+ /**
86
+ * Contract for custom RPC generators.
87
+ */
88
+ interface RPCGenerator {
89
+ readonly name: string;
90
+ generate(context: RPCGeneratorContext): Promise<GeneratedClientInfo>;
91
+ }
92
+
75
93
  /**
76
94
  * Configuration options for the RPCPlugin
77
95
  */
@@ -80,6 +98,7 @@ interface RPCPluginOptions {
80
98
  readonly tsConfigPath?: string;
81
99
  readonly outputDir?: string;
82
100
  readonly generateOnInit?: boolean;
101
+ readonly generators?: readonly RPCGenerator[];
83
102
  readonly context?: {
84
103
  readonly namespace?: string;
85
104
  readonly keys?: {
@@ -99,11 +118,11 @@ declare class RPCPlugin implements IPlugin {
99
118
  private readonly contextArtifactKey;
100
119
  private readonly routeAnalyzer;
101
120
  private readonly schemaGenerator;
102
- private readonly clientGenerator;
121
+ private readonly generators;
103
122
  private project;
104
123
  private analyzedRoutes;
105
124
  private analyzedSchemas;
106
- private generatedInfo;
125
+ private generatedInfos;
107
126
  private app;
108
127
  constructor(options?: RPCPluginOptions);
109
128
  /**
@@ -135,6 +154,10 @@ declare class RPCPlugin implements IPlugin {
135
154
  * Get the generation info
136
155
  */
137
156
  getGenerationInfo(): GeneratedClientInfo | null;
157
+ /**
158
+ * Get all generation infos
159
+ */
160
+ getGenerationInfos(): readonly GeneratedClientInfo[];
138
161
  /**
139
162
  * Checks whether expected output files exist on disk
140
163
  */
@@ -142,6 +165,8 @@ declare class RPCPlugin implements IPlugin {
142
165
  private getArtifactPath;
143
166
  private writeArtifactToDisk;
144
167
  private loadArtifactFromDisk;
168
+ private runGenerators;
169
+ private hasTypeScriptGenerator;
145
170
  private publishArtifact;
146
171
  private getArtifactContextKey;
147
172
  /**
@@ -159,41 +184,46 @@ declare class RPCPlugin implements IPlugin {
159
184
  }
160
185
 
161
186
  /**
162
- * Service for generating TypeScript RPC clients
187
+ * Built-in generator for TypeScript RPC clients.
163
188
  */
164
- declare class ClientGeneratorService {
189
+ declare class TypeScriptClientGenerator implements RPCGenerator {
165
190
  private readonly outputDir;
191
+ readonly name = "typescript-client";
166
192
  constructor(outputDir: string);
167
193
  /**
168
- * Generates the TypeScript RPC client
194
+ * Generates the TypeScript RPC client.
195
+ */
196
+ generate(context: RPCGeneratorContext): Promise<GeneratedClientInfo>;
197
+ /**
198
+ * Generates the TypeScript RPC client.
169
199
  */
170
200
  generateClient(routes: readonly ExtendedRouteInfo[], schemas: readonly SchemaInfo[]): Promise<GeneratedClientInfo>;
171
201
  /**
172
- * Generates the main client file with types included
202
+ * Generates the main client file with types included.
173
203
  */
174
204
  private generateClientFile;
175
205
  /**
176
- * Generates the client TypeScript content with types included
206
+ * Generates the client TypeScript content with types included.
177
207
  */
178
208
  private generateClientContent;
179
209
  /**
180
- * Generates controller methods for the client
210
+ * Generates controller methods for the client.
181
211
  */
182
212
  private generateControllerMethods;
183
213
  /**
184
- * Extracts the proper return type from route analysis
214
+ * Extracts the proper return type from route analysis.
185
215
  */
186
216
  private extractReturnType;
187
217
  /**
188
- * Generates schema types from integrated schema generation
218
+ * Generates schema types from integrated schema generation.
189
219
  */
190
220
  private generateSchemaTypes;
191
221
  /**
192
- * Groups routes by controller for better organization
222
+ * Groups routes by controller for better organization.
193
223
  */
194
224
  private groupRoutesByController;
195
225
  /**
196
- * Analyzes route parameters to determine their types and usage
226
+ * Analyzes route parameters to determine their types and usage.
197
227
  */
198
228
  private analyzeRouteParameters;
199
229
  }
@@ -277,6 +307,8 @@ declare function readChecksum(outputDir: string): ChecksumData | null;
277
307
  */
278
308
  declare function writeChecksum(outputDir: string, data: ChecksumData): Promise<void>;
279
309
 
310
+ /** Minimal route shape needed to build the full API path (prefix + version + route + path). */
311
+ type RoutePathInput = Pick<ExtendedRouteInfo, 'prefix' | 'version' | 'route' | 'path'>;
280
312
  /**
281
313
  * Builds the full path with parameter placeholders
282
314
  */
@@ -284,7 +316,7 @@ declare function buildFullPath(basePath: string, parameters: readonly ParameterM
284
316
  /**
285
317
  * Builds the full API path using route information
286
318
  */
287
- declare function buildFullApiPath(route: ExtendedRouteInfo): string;
319
+ declare function buildFullApiPath(route: RoutePathInput): string;
288
320
 
289
321
  /**
290
322
  * Maps JSON schema types to TypeScript types
@@ -341,4 +373,4 @@ declare const BUILTIN_TYPES: Set<string>;
341
373
  */
342
374
  declare const GENERIC_TYPES: Set<string>;
343
375
 
344
- 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, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };
376
+ export { ApiError, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, type ChecksumData, 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, TypeScriptClientGenerator, buildFullApiPath, buildFullPath, camelCase, computeHash, extractNamedType, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, readChecksum, safeToString, writeChecksum };