Skip to content

Commit 145b477

Browse files
Merge pull request #496 from autonomys/fix/expose-onEveryOpen
fix(rpc): expose `onEveryOpen` in RPC Client Callbacks
2 parents 17d10b3 + 3b59694 commit 145b477

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

packages/utility/rpc/src/rpc/api/definition.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import Websocket from 'websocket'
33
import { randomId } from '../../utils'
44
import { createRpcClient } from '../client'
55
import { createRpcServer } from '../server'
6-
import { Message, MessageQuery, RpcParams, TypedRpcNotificationHandler } from '../types'
6+
import {
7+
Message,
8+
MessageQuery,
9+
RpcClientCallbacks,
10+
RpcParams,
11+
TypedRpcNotificationHandler,
12+
} from '../types'
713
import { RpcError } from '../utils'
814
import {
915
ApiDefinition,
@@ -20,9 +26,12 @@ import {
2026
} from './typing'
2127

2228
export const createApiDefinition = <S extends ApiDefinition>(serverDefinition: S) => {
23-
const createClient = <Client extends WsClientType<S>>(
24-
clientParams: Parameters<typeof createRpcClient>[0],
25-
): ApiDefinitionClient<S> => {
29+
const createClient = <Client extends WsClientType<S>>(clientParams: {
30+
endpoint: string
31+
callbacks: RpcClientCallbacks
32+
reconnectInterval?: number | null
33+
debug?: boolean
34+
}): ApiDefinitionClient<S> => {
2635
const client = createRpcClient(clientParams)
2736

2837
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -189,7 +198,7 @@ export const createApiDefinition = <S extends ApiDefinition>(serverDefinition: S
189198
callbacks,
190199
}: {
191200
handlers: ApiServerHandlers<S>
192-
callbacks: Parameters<typeof createRpcClient>[0]['callbacks']
201+
callbacks: RpcClientCallbacks
193202
}): ApiMockServerClient<S> => {
194203
const eventEmitter = new EventEmitter()
195204

packages/utility/rpc/src/rpc/client.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
MessageQuery,
1010
MessageResponse,
1111
MessageResponseQuery,
12+
RpcClientCallbacks,
1213
} from './types'
1314

1415
export const createRpcClient = ({
@@ -18,14 +19,7 @@ export const createRpcClient = ({
1819
debug = false,
1920
}: {
2021
endpoint: string
21-
callbacks: {
22-
onEveryOpen?: () => void
23-
onFirstOpen?: () => void
24-
onReconnection?: () => void
25-
onError?: (error: Error) => void
26-
onClose?: (event: Websocket.ICloseEvent) => void
27-
onWrongMessage?: (responder: (message: MessageResponseQuery) => void) => void
28-
}
22+
callbacks: RpcClientCallbacks
2923
reconnectInterval?: number | null
3024
debug?: boolean
3125
}): ClientRPC => {

packages/utility/rpc/src/rpc/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import Websocket from 'websocket'
23
import { connection } from 'websocket'
34
import { z } from 'zod'
45
import { PromiseOr } from '../utils/types'
@@ -38,6 +39,15 @@ export type MessageResponseQuery = Omit<MessageResponse, 'id' | 'jsonrpc'> & {
3839
jsonrpc?: string
3940
}
4041

42+
export type RpcClientCallbacks = {
43+
onEveryOpen?: () => void
44+
onFirstOpen?: () => void
45+
onReconnection?: () => void
46+
onError?: (error: Error) => void
47+
onClose?: (event: Websocket.ICloseEvent) => void
48+
onWrongMessage?: (responder: (message: MessageResponseQuery) => void) => void
49+
}
50+
4151
type SuccessResponse<T> = {
4252
jsonrpc: string
4353
id: number

0 commit comments

Comments
 (0)