Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/utility/rpc/src/rpc/api/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import Websocket from 'websocket'
import { randomId } from '../../utils'
import { createRpcClient } from '../client'
import { createRpcServer } from '../server'
import { Message, MessageQuery, RpcParams, TypedRpcNotificationHandler } from '../types'
import {
Message,
MessageQuery,
RpcClientCallbacks,
RpcParams,
TypedRpcNotificationHandler,
} from '../types'
import { RpcError } from '../utils'
import {
ApiDefinition,
Expand All @@ -20,9 +26,12 @@ import {
} from './typing'

export const createApiDefinition = <S extends ApiDefinition>(serverDefinition: S) => {
const createClient = <Client extends WsClientType<S>>(
clientParams: Parameters<typeof createRpcClient>[0],
): ApiDefinitionClient<S> => {
const createClient = <Client extends WsClientType<S>>(clientParams: {
endpoint: string
callbacks: RpcClientCallbacks
reconnectInterval?: number | null
debug?: boolean
}): ApiDefinitionClient<S> => {
const client = createRpcClient(clientParams)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -189,7 +198,7 @@ export const createApiDefinition = <S extends ApiDefinition>(serverDefinition: S
callbacks,
}: {
handlers: ApiServerHandlers<S>
callbacks: Parameters<typeof createRpcClient>[0]['callbacks']
callbacks: RpcClientCallbacks
}): ApiMockServerClient<S> => {
const eventEmitter = new EventEmitter()

Expand Down
10 changes: 2 additions & 8 deletions packages/utility/rpc/src/rpc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MessageQuery,
MessageResponse,
MessageResponseQuery,
RpcClientCallbacks,
} from './types'

export const createRpcClient = ({
Expand All @@ -18,14 +19,7 @@ export const createRpcClient = ({
debug = false,
}: {
endpoint: string
callbacks: {
onEveryOpen?: () => void
onFirstOpen?: () => void
onReconnection?: () => void
onError?: (error: Error) => void
onClose?: (event: Websocket.ICloseEvent) => void
onWrongMessage?: (responder: (message: MessageResponseQuery) => void) => void
}
callbacks: RpcClientCallbacks
reconnectInterval?: number | null
debug?: boolean
}): ClientRPC => {
Expand Down
10 changes: 10 additions & 0 deletions packages/utility/rpc/src/rpc/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import Websocket from 'websocket'
import { connection } from 'websocket'
import { z } from 'zod'
import { PromiseOr } from '../utils/types'
Expand Down Expand Up @@ -38,6 +39,15 @@ export type MessageResponseQuery = Omit<MessageResponse, 'id' | 'jsonrpc'> & {
jsonrpc?: string
}

export type RpcClientCallbacks = {
onEveryOpen?: () => void
onFirstOpen?: () => void
onReconnection?: () => void
onError?: (error: Error) => void
onClose?: (event: Websocket.ICloseEvent) => void
onWrongMessage?: (responder: (message: MessageResponseQuery) => void) => void
}

type SuccessResponse<T> = {
jsonrpc: string
id: number
Expand Down
Loading