Skip to content

Commit fdbe3c7

Browse files
[feat] added types support for handler function callback
1 parent 3092f0c commit fdbe3c7

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import RPCClient from "./lib/client";
1+
import RPCClient, { IHandlersOption } from "./lib/client";
22
import RPCServer from "./lib/server";
33
export { createRPCError } from "./lib/util";
44
export { createValidator } from "./lib/validator";
55
export { NOREPLY } from "./lib/symbols";
66
export { RPCError, RPCFormatViolationError, RPCFormationViolationError, RPCFrameworkError, RPCGenericError, RPCInternalError, RPCMessageTypeNotSupportedError, RPCNotImplementedError, RPCNotSupportedError, RPCOccurenceConstraintViolationError, RPCOccurrenceConstraintViolationError, RPCPropertyConstraintViolationError, RPCProtocolError, RPCSecurityError, RPCTypeConstraintViolationError, TimeoutError, UnexpectedHttpResponse, WebsocketUpgradeError, } from "./lib/errors";
7-
export { RPCServer, RPCClient };
7+
export { RPCServer, RPCClient, IHandlersOption };

lib/client.d.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ export interface RPC_ClientOptions {
3232
randomisationFactor: number;
3333
};
3434
}
35+
export interface IHandlersOption {
36+
messageId?: string;
37+
method?: string;
38+
params?: Record<string, any>;
39+
signal?: AbortSignal;
40+
reply?: unknown;
41+
}
42+
type IHandlers = ({ params, reply, method, signal, messageId, }: IHandlersOption) => Promise<Record<string, any>>;
3543
declare class RPC_Client extends EventEmitter {
3644
_identity?: string;
37-
_wildcardHandler: Function | null;
38-
_handlers: Map<string, Function>;
45+
_wildcardHandler: IHandlers | null;
46+
_handlers: Map<string, IHandlers>;
3947
_state: number;
4048
_callQueue: Queue;
4149
_ws?: WebSocket;
@@ -122,7 +130,7 @@ declare class RPC_Client extends EventEmitter {
122130
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code CloseEvent codes}
123131
* @returns Promise<Object> - The CloseEvent (code & reason) for closure. May be different from requested code & reason.
124132
*/
125-
close({ code, reason, awaitPending, force, }: {
133+
close({ code, reason, awaitPending, force, }?: {
126134
code?: number;
127135
reason?: string;
128136
awaitPending?: any;
@@ -153,9 +161,6 @@ declare class RPC_Client extends EventEmitter {
153161
* @param {string} [method] - The name of the RPC method to handle.
154162
* @param {Function} handler - A function that can handle incoming calls for this method.
155163
*/
156-
handle(method: string | Function, handler?: ({ params, signal }: {
157-
params: any;
158-
signal: any;
159-
}) => void): void;
164+
handle(method: string | IHandlers, handler?: IHandlers): void;
160165
}
161166
export default RPC_Client;

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)