@honestjs/rpc-plugin 1.0.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.
@@ -0,0 +1,337 @@
1
+ import { RouteInfo, ParameterMetadata, IPlugin, Application } from 'honestjs';
2
+ import { Hono } from 'hono';
3
+ import { Type } from 'ts-morph';
4
+
5
+ /**
6
+ * Parameter metadata with enhanced type information
7
+ */
8
+ interface ParameterMetadataWithType extends ParameterMetadata {
9
+ readonly type: string;
10
+ readonly required: boolean;
11
+ readonly name: string;
12
+ }
13
+ /**
14
+ * Extended route information with comprehensive type data
15
+ */
16
+ interface ExtendedRouteInfo extends Omit<RouteInfo, 'parameters' | 'method'> {
17
+ readonly returns?: string;
18
+ readonly parameters?: readonly ParameterMetadataWithType[];
19
+ readonly method: string;
20
+ readonly fullPath: string;
21
+ }
22
+ /**
23
+ * Controller groups for organization
24
+ */
25
+ type ControllerGroups = Map<string, readonly ExtendedRouteInfo[]>;
26
+ /**
27
+ * Route parameter information for client generation
28
+ * Extends ParameterMetadataWithType for consistency
29
+ */
30
+ interface RouteParameter extends ParameterMetadataWithType {
31
+ }
32
+
33
+ /**
34
+ * Schema with comprehensive type information
35
+ */
36
+ interface SchemaInfo {
37
+ readonly type: string;
38
+ readonly schema: Record<string, any>;
39
+ readonly typescriptType?: string;
40
+ }
41
+ /**
42
+ * Generated client file information
43
+ */
44
+ interface GeneratedClientInfo {
45
+ readonly clientFile: string;
46
+ readonly generatedAt: string;
47
+ }
48
+
49
+ /**
50
+ * Clean separation of concerns for request options
51
+ */
52
+ type RequestOptions<TParams = never, TQuery = never, TBody = never, THeaders = never> = (TParams extends never ? {
53
+ params?: never;
54
+ } : {
55
+ params: TParams;
56
+ }) & (TQuery extends never ? {
57
+ query?: never;
58
+ } : {
59
+ query?: TQuery;
60
+ }) & (TBody extends never ? {
61
+ body?: never;
62
+ } : {
63
+ body: TBody;
64
+ }) & (THeaders extends never ? {
65
+ headers?: never;
66
+ } : {
67
+ headers: THeaders;
68
+ });
69
+ /**
70
+ * API Response wrapper
71
+ */
72
+ interface ApiResponse<T = any> {
73
+ data: T;
74
+ message?: string;
75
+ success: boolean;
76
+ }
77
+ /**
78
+ * API Error class
79
+ */
80
+ declare class ApiError extends Error {
81
+ statusCode: number;
82
+ constructor(statusCode: number, message: string);
83
+ }
84
+
85
+ /**
86
+ * Configuration options for the RPCPlugin
87
+ */
88
+ interface RPCPluginOptions {
89
+ readonly controllerPattern?: string;
90
+ readonly tsConfigPath?: string;
91
+ readonly outputDir?: string;
92
+ readonly generateOnInit?: boolean;
93
+ }
94
+ /**
95
+ * Comprehensive RPC plugin that combines route analysis, schema generation, and client generation
96
+ */
97
+ declare class RPCPlugin implements IPlugin {
98
+ private readonly controllerPattern;
99
+ private readonly tsConfigPath;
100
+ private readonly outputDir;
101
+ private readonly generateOnInit;
102
+ private readonly routeAnalyzer;
103
+ private readonly schemaGenerator;
104
+ private readonly clientGenerator;
105
+ private readonly analyzedRoutes;
106
+ private readonly analyzedSchemas;
107
+ private generatedInfo;
108
+ constructor(options?: RPCPluginOptions);
109
+ /**
110
+ * Validates the plugin configuration
111
+ */
112
+ private validateConfiguration;
113
+ /**
114
+ * Called after all modules are registered
115
+ */
116
+ afterModulesRegistered: (app: Application, hono: Hono) => Promise<void>;
117
+ /**
118
+ * Main analysis method that coordinates all three components
119
+ */
120
+ private analyzeEverything;
121
+ /**
122
+ * Manually trigger analysis (useful for testing or re-generation)
123
+ */
124
+ analyze(): Promise<void>;
125
+ /**
126
+ * Get the analyzed routes
127
+ */
128
+ getRoutes(): readonly ExtendedRouteInfo[];
129
+ /**
130
+ * Get the analyzed schemas
131
+ */
132
+ getSchemas(): readonly SchemaInfo[];
133
+ /**
134
+ * Get the generation info
135
+ */
136
+ getGenerationInfo(): GeneratedClientInfo | null;
137
+ /**
138
+ * Cleanup resources to prevent memory leaks
139
+ */
140
+ dispose(): void;
141
+ /**
142
+ * Plugin lifecycle cleanup
143
+ */
144
+ onDestroy(): void;
145
+ /**
146
+ * Logs a message with the plugin prefix
147
+ */
148
+ private log;
149
+ /**
150
+ * Logs an error with the plugin prefix
151
+ */
152
+ private logError;
153
+ }
154
+
155
+ /**
156
+ * Service for generating TypeScript RPC clients
157
+ */
158
+ declare class ClientGeneratorService {
159
+ private readonly outputDir;
160
+ constructor(outputDir: string);
161
+ /**
162
+ * Generates the TypeScript RPC client
163
+ */
164
+ generateClient(routes: readonly ExtendedRouteInfo[], schemas: readonly SchemaInfo[]): Promise<GeneratedClientInfo>;
165
+ /**
166
+ * Generates the main client file with types included
167
+ */
168
+ private generateClientFile;
169
+ /**
170
+ * Generates the client TypeScript content with types included
171
+ */
172
+ private generateClientContent;
173
+ /**
174
+ * Generates controller methods for the client
175
+ */
176
+ private generateControllerMethods;
177
+ /**
178
+ * Extracts the proper return type from route analysis
179
+ */
180
+ private extractReturnType;
181
+ /**
182
+ * Generates schema types from integrated schema generation
183
+ */
184
+ private generateSchemaTypes;
185
+ /**
186
+ * Groups routes by controller for better organization
187
+ */
188
+ private groupRoutesByController;
189
+ /**
190
+ * Analyzes route parameters to determine their types and usage
191
+ */
192
+ private analyzeRouteParameters;
193
+ }
194
+
195
+ /**
196
+ * Service for analyzing controller methods and extracting type information
197
+ */
198
+ declare class RouteAnalyzerService {
199
+ private readonly controllerPattern;
200
+ private readonly tsConfigPath;
201
+ constructor(controllerPattern: string, tsConfigPath: string);
202
+ private projects;
203
+ /**
204
+ * Analyzes controller methods to extract type information
205
+ */
206
+ analyzeControllerMethods(): Promise<ExtendedRouteInfo[]>;
207
+ /**
208
+ * Creates a new ts-morph project
209
+ */
210
+ private createProject;
211
+ /**
212
+ * Cleanup resources to prevent memory leaks
213
+ */
214
+ dispose(): void;
215
+ /**
216
+ * Finds controller classes in the project
217
+ */
218
+ private findControllerClasses;
219
+ /**
220
+ * Processes all routes and extracts type information
221
+ */
222
+ private processRoutes;
223
+ /**
224
+ * Creates an extended route with type information
225
+ */
226
+ private createExtendedRoute;
227
+ /**
228
+ * Gets the return type of a method
229
+ */
230
+ private getReturnType;
231
+ /**
232
+ * Gets parameters with their types
233
+ */
234
+ private getParametersWithTypes;
235
+ }
236
+
237
+ /**
238
+ * Service for generating JSON schemas from TypeScript types used in controllers
239
+ */
240
+ declare class SchemaGeneratorService {
241
+ private readonly controllerPattern;
242
+ private readonly tsConfigPath;
243
+ constructor(controllerPattern: string, tsConfigPath: string);
244
+ private projects;
245
+ /**
246
+ * Generates JSON schemas from types used in controllers
247
+ */
248
+ generateSchemas(): Promise<SchemaInfo[]>;
249
+ /**
250
+ * Creates a new ts-morph project
251
+ */
252
+ private createProject;
253
+ /**
254
+ * Cleanup resources to prevent memory leaks
255
+ */
256
+ dispose(): void;
257
+ /**
258
+ * Collects types from controller files
259
+ */
260
+ private collectTypesFromControllers;
261
+ /**
262
+ * Collects types from a single method
263
+ */
264
+ private collectTypesFromMethod;
265
+ /**
266
+ * Processes collected types to generate schemas
267
+ */
268
+ private processTypes;
269
+ /**
270
+ * Generates schema for a specific type
271
+ */
272
+ private generateSchemaForType;
273
+ }
274
+
275
+ /**
276
+ * Builds the full path with parameter placeholders
277
+ */
278
+ declare function buildFullPath(basePath: string, parameters: readonly ParameterMetadata[]): string;
279
+ /**
280
+ * Builds the full API path using route information
281
+ */
282
+ declare function buildFullApiPath(route: ExtendedRouteInfo): string;
283
+
284
+ /**
285
+ * Maps JSON schema types to TypeScript types
286
+ */
287
+ declare function mapJsonSchemaTypeToTypeScript(schema: Record<string, any>): string;
288
+ /**
289
+ * Generates TypeScript interface from JSON schema
290
+ */
291
+ declare function generateTypeScriptInterface(typeName: string, schema: Record<string, any>): string;
292
+
293
+ /**
294
+ * Safely converts a value to string, handling symbols and other types
295
+ */
296
+ declare function safeToString(value: unknown): string;
297
+ /**
298
+ * Converts a string to camelCase
299
+ */
300
+ declare function camelCase(str: string): string;
301
+
302
+ /**
303
+ * Extracts a named type from a TypeScript type
304
+ */
305
+ declare function extractNamedType(type: Type): string | null;
306
+ /**
307
+ * Generates type imports for the client
308
+ */
309
+ declare function generateTypeImports(routes: readonly any[]): string;
310
+
311
+ /**
312
+ * Default configuration options for the RPCPlugin
313
+ */
314
+ declare const DEFAULT_OPTIONS: {
315
+ readonly controllerPattern: "src/modules/*/*.controller.ts";
316
+ readonly tsConfigPath: "tsconfig.json";
317
+ readonly outputDir: "./generated/rpc";
318
+ readonly generateOnInit: true;
319
+ };
320
+ /**
321
+ * Log prefix for the RPC plugin
322
+ */
323
+ declare const LOG_PREFIX = "[ RPCPlugin ]";
324
+ /**
325
+ * Built-in TypeScript types that should not be imported
326
+ */
327
+ declare const BUILTIN_UTILITY_TYPES: Set<string>;
328
+ /**
329
+ * Built-in TypeScript types that should be skipped
330
+ */
331
+ declare const BUILTIN_TYPES: Set<string>;
332
+ /**
333
+ * Generic type names that should be unwrapped
334
+ */
335
+ declare const GENERIC_TYPES: Set<string>;
336
+
337
+ export { ApiError, type ApiResponse, BUILTIN_TYPES, BUILTIN_UTILITY_TYPES, ClientGeneratorService, type ControllerGroups, DEFAULT_OPTIONS, type ExtendedRouteInfo, GENERIC_TYPES, type GeneratedClientInfo, LOG_PREFIX, type ParameterMetadataWithType, RPCPlugin, type RPCPluginOptions, type RequestOptions, RouteAnalyzerService, type RouteParameter, SchemaGeneratorService, type SchemaInfo, buildFullApiPath, buildFullPath, camelCase, extractNamedType, generateTypeImports, generateTypeScriptInterface, mapJsonSchemaTypeToTypeScript, safeToString };