rpc-express-toolkit 2.3.3 → 3.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.
- package/README.md +221 -9
- package/examples/example.js +17 -18
- package/examples/server-client-example.js +120 -114
- package/examples/ssl-development-example.js +100 -80
- package/package.json +5 -5
- package/src/batch.js +256 -237
- package/src/clients/rpc-client.js +125 -44
- package/src/clients/rpc-client.min.js +1 -1
- package/src/clients/rpc-client.min.mjs +1 -1
- package/src/clients/rpc-client.mjs +159 -96
- package/src/index.d.ts +220 -58
- package/src/index.js +319 -115
- package/src/logger.js +184 -153
- package/src/middleware.js +229 -224
- package/src/validation.js +220 -221
package/src/index.d.ts
CHANGED
|
@@ -1,37 +1,149 @@
|
|
|
1
1
|
// Type definitions for rpc-express-toolkit
|
|
2
2
|
|
|
3
|
-
import type { Router, Request, Response } from
|
|
3
|
+
import type { Router, Request, Response } from 'express';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* JSON-RPC handler function type
|
|
7
7
|
*/
|
|
8
|
-
type JSONRPCHandler<C> = (
|
|
8
|
+
type JSONRPCHandler<C> = (
|
|
9
|
+
req: Request,
|
|
10
|
+
context: C,
|
|
11
|
+
params: any
|
|
12
|
+
) => any | Promise<any>;
|
|
9
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Method configuration object
|
|
16
|
+
*/
|
|
17
|
+
interface MethodConfig<C> {
|
|
18
|
+
handler: JSONRPCHandler<C>;
|
|
19
|
+
schema?: object;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* JSON-RPC error object
|
|
24
|
+
*/
|
|
10
25
|
interface JSONRPCError {
|
|
11
26
|
code: number;
|
|
12
27
|
message: string;
|
|
13
28
|
data?: any;
|
|
14
29
|
}
|
|
15
30
|
|
|
31
|
+
/**
|
|
32
|
+
* JSON-RPC response payload
|
|
33
|
+
*/
|
|
16
34
|
interface JSONRPCResponsePayload {
|
|
17
35
|
id?: string | number | null;
|
|
18
36
|
result?: any;
|
|
19
37
|
error?: JSONRPCError;
|
|
20
38
|
}
|
|
21
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Logging configuration
|
|
42
|
+
*/
|
|
43
|
+
interface LoggingConfig {
|
|
44
|
+
level?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
45
|
+
format?: 'json' | 'simple';
|
|
46
|
+
transports?: Array<'console' | 'file'>;
|
|
47
|
+
file?: {
|
|
48
|
+
filename?: string;
|
|
49
|
+
maxsize?: number;
|
|
50
|
+
maxFiles?: number;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* CORS configuration
|
|
56
|
+
*/
|
|
57
|
+
interface CorsConfig {
|
|
58
|
+
origin?: string | string[] | boolean;
|
|
59
|
+
credentials?: boolean;
|
|
60
|
+
methods?: string[];
|
|
61
|
+
allowedHeaders?: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Rate limiting configuration
|
|
66
|
+
*/
|
|
67
|
+
interface RateLimitConfig {
|
|
68
|
+
windowMs?: number;
|
|
69
|
+
max?: number;
|
|
70
|
+
message?: string;
|
|
71
|
+
standardHeaders?: boolean;
|
|
72
|
+
legacyHeaders?: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Authentication configuration
|
|
77
|
+
*/
|
|
78
|
+
interface AuthConfig {
|
|
79
|
+
required?: boolean;
|
|
80
|
+
verify?: (req: Request) => boolean | Promise<boolean>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Schema validation configuration
|
|
85
|
+
*/
|
|
86
|
+
interface ValidationConfig {
|
|
87
|
+
strict?: boolean;
|
|
88
|
+
coerceTypes?: boolean;
|
|
89
|
+
removeAdditional?: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* RPC endpoint configuration options
|
|
94
|
+
*/
|
|
95
|
+
interface RpcEndpointOptions {
|
|
96
|
+
prefix?: string;
|
|
97
|
+
maxBodySize?: string;
|
|
98
|
+
timeout?: number;
|
|
99
|
+
cors?: boolean | CorsOptions;
|
|
100
|
+
auth?: AuthConfig;
|
|
101
|
+
validation?: ValidationConfig;
|
|
102
|
+
safeStringEnabled?: boolean;
|
|
103
|
+
safeDateEnabled?: boolean;
|
|
104
|
+
crossConfigurationEnabled?: boolean;
|
|
105
|
+
enableSchema?: boolean;
|
|
106
|
+
rateLimit?: RateLimitConfig;
|
|
107
|
+
logging?: LoggingConfig;
|
|
108
|
+
warnOnUnsafeString?: boolean;
|
|
109
|
+
warnOnUnsafeDate?: boolean;
|
|
110
|
+
autoJsonMiddleware?: boolean;
|
|
111
|
+
jsonOptions?: {
|
|
112
|
+
limit?: string;
|
|
113
|
+
strict?: boolean;
|
|
114
|
+
type?: string | string[] | ((req: any) => boolean);
|
|
115
|
+
verify?: (req: any, res: any, buf: Buffer, encoding: string) => void;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* RPC client configuration options
|
|
121
|
+
*/
|
|
122
|
+
interface RpcClientOptions {
|
|
123
|
+
safeStringEnabled?: boolean;
|
|
124
|
+
safeDateEnabled?: boolean;
|
|
125
|
+
warnOnUnsafeString?: boolean;
|
|
126
|
+
warnOnUnsafeDate?: boolean;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Deserialization options for safe prefixes
|
|
131
|
+
*/
|
|
132
|
+
interface DeserializationOptions {
|
|
133
|
+
safeStringEnabled?: boolean;
|
|
134
|
+
safeDateEnabled?: boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
22
137
|
/**
|
|
23
138
|
* The main RpcEndpoint class for JSON-RPC 2.0 endpoints.
|
|
24
|
-
* The generics `<C>` represents the "context" type.
|
|
25
139
|
*/
|
|
26
|
-
declare class RpcEndpoint<C> {
|
|
140
|
+
declare class RpcEndpoint<C = any> {
|
|
27
141
|
/**
|
|
28
142
|
* Constructor.
|
|
29
|
-
*
|
|
30
|
-
* @param router - An Express.Router object
|
|
31
|
-
* @param context - Generic object that will be passed to method handlers
|
|
32
|
-
* @param endpoint - Path on which to respond to JSON-RPC calls (default: "/api")
|
|
33
143
|
*/
|
|
34
144
|
constructor(router: Router, context: C, endpoint?: string);
|
|
145
|
+
constructor(router: Router, context: C, options?: RpcEndpointOptions);
|
|
146
|
+
constructor(router: Router, context: C, endpoint?: string, options?: RpcEndpointOptions);
|
|
35
147
|
|
|
36
148
|
/**
|
|
37
149
|
* Returns the endpoint path, e.g. "/api".
|
|
@@ -41,25 +153,57 @@ declare class RpcEndpoint<C> {
|
|
|
41
153
|
/**
|
|
42
154
|
* Returns the map of all registered methods.
|
|
43
155
|
*/
|
|
44
|
-
get methods(): { [methodName: string]: JSONRPCHandler<C> };
|
|
156
|
+
get methods(): { [methodName: string]: JSONRPCHandler<C> | MethodConfig<C> };
|
|
45
157
|
|
|
46
158
|
/**
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
159
|
+
* Get logger instance
|
|
160
|
+
*/
|
|
161
|
+
get logger(): any;
|
|
162
|
+
|
|
50
163
|
/**
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
164
|
+
* Get middleware manager
|
|
165
|
+
*/
|
|
166
|
+
get middleware(): any;
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Get validator instance
|
|
170
|
+
*/
|
|
171
|
+
get validator(): any;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Add a JSON-RPC method with optional schema validation.
|
|
55
175
|
*/
|
|
56
176
|
addMethod(name: string, handler: JSONRPCHandler<C>): void;
|
|
177
|
+
addMethod(name: string, handler: JSONRPCHandler<C>, schema: object): void;
|
|
178
|
+
addMethod(name: string, config: MethodConfig<C>): void;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Add middleware for specific hooks
|
|
182
|
+
*/
|
|
183
|
+
use(hook: 'beforeCall' | 'afterCall' | 'onError' | 'beforeValidation' | 'afterValidation', middleware: Function): void;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Remove a method
|
|
187
|
+
*/
|
|
188
|
+
removeMethod(name: string): void;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get method configuration
|
|
192
|
+
*/
|
|
193
|
+
getMethod(name: string): JSONRPCHandler<C> | MethodConfig<C> | undefined;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* List all registered methods
|
|
197
|
+
*/
|
|
198
|
+
listMethods(): string[];
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Get metrics data
|
|
202
|
+
*/
|
|
203
|
+
getMetrics(): object;
|
|
57
204
|
|
|
58
205
|
/**
|
|
59
206
|
* Serve client scripts (for calling JSON-RPC from browser).
|
|
60
|
-
*
|
|
61
|
-
* @param router - An Express.Router object
|
|
62
|
-
* @param url - Path from which to serve scripts (default: "/vendor/rpc-client")
|
|
63
207
|
*/
|
|
64
208
|
static serveScripts(router: Router, url?: string): void;
|
|
65
209
|
|
|
@@ -74,63 +218,52 @@ declare class RpcEndpoint<C> {
|
|
|
74
218
|
serializeBigIntsAndDates(value: any): any;
|
|
75
219
|
|
|
76
220
|
/**
|
|
77
|
-
* Recursively deserialize strings representing BigInt and Date
|
|
78
|
-
* (produced by `serializeBigIntsAndDates`) to reconstruct original types.
|
|
221
|
+
* Recursively deserialize strings representing BigInt and Date.
|
|
79
222
|
*/
|
|
80
|
-
deserializeBigIntsAndDates(value: any): any;
|
|
223
|
+
deserializeBigIntsAndDates(value: any, options?: DeserializationOptions): any;
|
|
81
224
|
}
|
|
82
225
|
|
|
83
226
|
/**
|
|
84
227
|
* JSON-RPC 2.0 Client for Node.js environments.
|
|
85
|
-
* Handles BigInt and Date serialization/deserialization automatically.
|
|
86
228
|
*/
|
|
87
229
|
declare class RpcClient {
|
|
88
230
|
/**
|
|
89
231
|
* Client constructor.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
* @param options - Configuration options
|
|
94
|
-
* @param options.rejectUnauthorized - Whether to reject unauthorized SSL certificates (default: true). Set to false for development with self-signed certificates.
|
|
95
|
-
* @param options.ca - Buffer or string of a custom CA certificate to trust (for self-signed CA)
|
|
96
|
-
* @param options.agent - Custom https.Agent instance (advanced usage)
|
|
97
|
-
*/
|
|
98
|
-
constructor(
|
|
99
|
-
endpoint: string,
|
|
100
|
-
defaultHeaders?: Record<string, string>,
|
|
101
|
-
options?: {
|
|
102
|
-
// Opzioni SSL avanzate rimosse: usa process.env.NODE_TLS_REJECT_UNAUTHORIZED per sviluppo
|
|
103
|
-
}
|
|
104
|
-
);
|
|
232
|
+
*/
|
|
233
|
+
constructor(endpoint: string, options?: RpcClientOptions);
|
|
234
|
+
constructor(endpoint: string, defaultHeaders?: Record<string, string>, options?: RpcClientOptions);
|
|
105
235
|
|
|
106
236
|
/**
|
|
107
237
|
* Make a JSON-RPC call to the server.
|
|
108
|
-
*
|
|
109
|
-
* @param method - RPC method name
|
|
110
|
-
* @param params - Parameters to pass to the method
|
|
111
|
-
* @param id - Request ID (optional for notifications)
|
|
112
|
-
* @param overrideHeaders - Headers to override defaults for this request
|
|
113
|
-
* @returns Promise with the RPC call result
|
|
114
238
|
*/
|
|
115
|
-
call(
|
|
239
|
+
call(
|
|
240
|
+
method: string,
|
|
241
|
+
params?: any,
|
|
242
|
+
id?: string | number | null,
|
|
243
|
+
overrideHeaders?: Record<string, string>
|
|
244
|
+
): Promise<any>;
|
|
116
245
|
|
|
117
246
|
/**
|
|
118
247
|
* Make a JSON-RPC notification (no response expected).
|
|
119
|
-
*
|
|
120
|
-
* @param method - RPC method name
|
|
121
|
-
* @param params - Parameters to pass to the method
|
|
122
|
-
* @param overrideHeaders - Headers to override defaults for this request
|
|
123
248
|
*/
|
|
124
|
-
notify(
|
|
249
|
+
notify(
|
|
250
|
+
method: string,
|
|
251
|
+
params?: any,
|
|
252
|
+
overrideHeaders?: Record<string, string>
|
|
253
|
+
): Promise<void>;
|
|
125
254
|
|
|
126
255
|
/**
|
|
127
256
|
* Make a batch JSON-RPC call.
|
|
128
|
-
*
|
|
129
|
-
* @param requests - Array of request objects
|
|
130
|
-
* @param overrideHeaders - Headers to override defaults for this request
|
|
131
|
-
* @returns Promise with array of results
|
|
132
257
|
*/
|
|
133
|
-
batch(
|
|
258
|
+
batch(
|
|
259
|
+
requests: Array<{
|
|
260
|
+
method: string;
|
|
261
|
+
params?: any;
|
|
262
|
+
id?: string | number | null;
|
|
263
|
+
notification?: boolean;
|
|
264
|
+
}>,
|
|
265
|
+
overrideHeaders?: Record<string, string>
|
|
266
|
+
): Promise<any[]>;
|
|
134
267
|
|
|
135
268
|
/**
|
|
136
269
|
* Recursively serialize BigInt and Date to JSON-safe string formats.
|
|
@@ -140,18 +273,47 @@ declare class RpcClient {
|
|
|
140
273
|
/**
|
|
141
274
|
* Recursively deserialize strings representing BigInt and Date.
|
|
142
275
|
*/
|
|
143
|
-
deserializeBigIntsAndDates(value: any): any;
|
|
276
|
+
deserializeBigIntsAndDates(value: any, options?: DeserializationOptions): any;
|
|
144
277
|
}
|
|
145
278
|
|
|
146
279
|
/**
|
|
147
|
-
*
|
|
280
|
+
* Export types for external use
|
|
281
|
+
*/
|
|
282
|
+
export {
|
|
283
|
+
JSONRPCHandler,
|
|
284
|
+
MethodConfig,
|
|
285
|
+
JSONRPCError,
|
|
286
|
+
JSONRPCResponsePayload,
|
|
287
|
+
LoggingConfig,
|
|
288
|
+
CorsConfig,
|
|
289
|
+
RateLimitConfig,
|
|
290
|
+
AuthConfig,
|
|
291
|
+
ValidationConfig,
|
|
292
|
+
RpcEndpointOptions,
|
|
293
|
+
RpcClientOptions,
|
|
294
|
+
DeserializationOptions,
|
|
295
|
+
RpcEndpoint,
|
|
296
|
+
RpcClient
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Main export - RpcEndpoint class with static properties
|
|
148
301
|
*/
|
|
149
302
|
declare const Main: {
|
|
150
303
|
new <C>(router: Router, context: C, endpoint?: string): RpcEndpoint<C>;
|
|
304
|
+
new <C>(router: Router, context: C, options?: RpcEndpointOptions): RpcEndpoint<C>;
|
|
305
|
+
new <C>(router: Router, context: C, endpoint?: string, options?: RpcEndpointOptions): RpcEndpoint<C>;
|
|
151
306
|
prototype: RpcEndpoint<any>;
|
|
152
307
|
serveScripts(router: Router, url?: string): void;
|
|
153
308
|
RpcEndpoint: typeof RpcEndpoint;
|
|
154
309
|
RpcClient: typeof RpcClient;
|
|
310
|
+
Logger: any;
|
|
311
|
+
MiddlewareManager: any;
|
|
312
|
+
builtInMiddlewares: any;
|
|
313
|
+
SchemaValidator: any;
|
|
314
|
+
commonSchemas: any;
|
|
315
|
+
SchemaBuilder: any;
|
|
316
|
+
BatchHandler: any;
|
|
155
317
|
};
|
|
156
318
|
|
|
157
319
|
export = Main;
|