diff --git a/src/ClientInterface.ts b/src/ClientInterface.ts new file mode 100644 index 0000000..d3fb25e --- /dev/null +++ b/src/ClientInterface.ts @@ -0,0 +1,21 @@ +interface Arguments { + readonly method: string; + readonly params?: readonly unknown[] | object; +} + +export type RequestArguments = Arguments; + +export type NotificationArguments = Arguments; + +export type JSONRPCMessage = RequestArguments | NotificationArguments; + +export interface IClient { + request(args: RequestArguments): Promise; + notify(args: NotificationArguments): Promise; +} + +export interface JSONRpcError extends Error { + message: string; + code: number; + data?: unknown; +} diff --git a/src/Error.ts b/src/Error.ts index 4943883..370b09e 100644 --- a/src/Error.ts +++ b/src/Error.ts @@ -1,10 +1,10 @@ -import { ProviderRpcError } from "./ProviderInterface"; +import { JSONRpcError } from "./ProviderInterface"; export const ERR_TIMEOUT = 7777; export const ERR_MISSIING_ID = 7878; export const ERR_UNKNOWN = 7979; -export class JSONRPCError extends Error implements ProviderRpcError{ +export class JSONRPCError extends Error implements JSONRpcError { public message: string; public code: number; public data: any; diff --git a/src/ProviderInterface.ts b/src/ProviderInterface.ts deleted file mode 100644 index 7fb30a9..0000000 --- a/src/ProviderInterface.ts +++ /dev/null @@ -1,14 +0,0 @@ -export interface ProviderRequestArguments { - readonly method: string; - readonly params?: readonly unknown[] | object; -} - -export interface Provider { - request(args: ProviderRequestArguments): Promise; -} - -export interface ProviderRpcError extends Error { - message: string; - code: number; - data?: unknown; -} diff --git a/src/RequestManager.ts b/src/RequestManager.ts index f8e03ac..a16668f 100644 --- a/src/RequestManager.ts +++ b/src/RequestManager.ts @@ -3,7 +3,7 @@ import { IJSONRPCRequest, IJSONRPCNotification, IBatchRequest } from "./Request" import { JSONRPCError } from "./Error"; import StrictEventEmitter from "strict-event-emitter-types"; import { EventEmitter } from "events"; -import { ProviderRequestArguments, Provider } from "./ProviderInterface"; +import { RequestArguments } from "./ClientInterface"; export type RequestChannel = StrictEventEmitter; @@ -17,7 +17,7 @@ export interface IRequestEvents { * If a transport fails, or times out, move on to the next. */ -class RequestManager implements Provider { +class RequestManager { public transports: Transport[]; public connectPromise: Promise; public batch: IBatchRequest[] = []; @@ -44,11 +44,11 @@ class RequestManager implements Provider { return this.transports[0]; } - public async request(args: ProviderRequestArguments, notification: boolean = false, timeout?: number): Promise { + public async request(requestObject: RequestArguments, notification: boolean = false, timeout?: number): Promise { const internalID = (++this.lastId).toString(); const id = notification ? null : internalID; // naively grab first transport and use it - const payload = {request: this.makeRequest(args.method, args.params || [], id) , internalID}; + const payload = {request: this.makeRequest(requestObject.method, requestObject.params || [], id) , internalID}; if (this.batchStarted) { const result = new Promise((resolve, reject) => { this.batch.push({ resolve, reject, request: payload }); diff --git a/src/index.ts b/src/index.ts index 9f17b8e..bb40e1f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import WebSocketTransport from "./transports/WebSocketTransport"; import PostMessageWindowTransport from "./transports/PostMessageWindowTransport"; import PostMessageIframeTransport from "./transports/PostMessageIframeTransport"; import { JSONRPCError } from "./Error"; -import { Provider, ProviderRequestArguments } from "./ProviderInterface"; +import { IClient, RequestArguments, NotificationArguments } from "./ProviderInterface"; /** * OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and @@ -21,7 +21,7 @@ import { Provider, ProviderRequestArguments } from "./ProviderInterface"; * ``` * */ -class Client implements Provider { +class Client implements IClient { public requestManager: RequestManager; constructor(requestManager: RequestManager) { this.requestManager = requestManager; @@ -71,13 +71,17 @@ class Client implements Provider { * @example * myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar')); */ - public async request(requestObject: ProviderRequestArguments, timeout?: number) { - await this.requestManager.connectPromise; + public async request(requestObject: RequestArguments, timeout?: number) { + if (this.requestManager.connectPromise) { + await this.requestManager.connectPromise; + } return this.requestManager.request(requestObject, false, timeout); } - public async notify(requestObject: ProviderRequestArguments) { - await this.requestManager.connectPromise; + public async notify(requestObject: NotificationArguments) { + if (this.requestManager.connectPromise) { + await this.requestManager.connectPromise; + } return this.requestManager.request(requestObject, true); }