-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from open-rpc/feat/provider-interface
Feat/provider interface
- Loading branch information
Showing
12 changed files
with
191 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import RequestManager from "./RequestManager"; | ||
import { JSONRPCError } from "./Error"; | ||
import { IClient, RequestArguments, NotificationArguments } from "./ClientInterface"; | ||
|
||
/** | ||
* OpenRPC Client JS is a browser-compatible JSON-RPC client with multiple transports and | ||
* multiple request managers to enable features like round-robin or fallback-by-position. | ||
* | ||
* @example | ||
* ```typescript | ||
* import { RequestManager, HTTPTransport, Client } from '@open-rpc/client-js'; | ||
* const transport = new HTTPTransport('http://localhost:3333'); | ||
* const client = new Client(new RequestManager([transport])); | ||
* const result = await client.request({method: 'addition', params: [2, 2]}); | ||
* // => { jsonrpc: '2.0', id: 1, result: 4 } | ||
* ``` | ||
* | ||
*/ | ||
class Client implements IClient { | ||
public requestManager: RequestManager; | ||
constructor(requestManager: RequestManager) { | ||
this.requestManager = requestManager; | ||
} | ||
|
||
/** | ||
* Initiates [[RequestManager.startBatch]] in order to build a batch call. | ||
* | ||
* Subsequent calls to [[Client.request]] will be added to the batch. Once [[Client.stopBatch]] is called, the | ||
* promises for the [[Client.request]] will then be resolved. If the [[RequestManager]] already has a batch in | ||
* progress, this method is a noop. | ||
* | ||
* @example | ||
* myClient.startBatch(); | ||
* myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar')); | ||
* myClient.request({method: "foo", params: ["baz"]}).then(() => console.log('foobaz')); | ||
* myClient.stopBatch(); | ||
*/ | ||
public startBatch(): void { | ||
return this.requestManager.startBatch(); | ||
} | ||
|
||
/** | ||
* Initiates [[RequestManager.stopBatch]] in order to finalize and send the batch to the underlying transport. | ||
* | ||
* [[Client.stopBatch]] will send the [[Client.request]] calls made since the last [[Client.startBatch]] call. For | ||
* that reason, [[Client.startBatch]] MUST be called before [[Client.stopBatch]]. | ||
* | ||
* @example | ||
* myClient.startBatch(); | ||
* myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar')); | ||
* myClient.request({method: "foo", params: ["baz"]}).then(() => console.log('foobaz')); | ||
* myClient.stopBatch(); | ||
*/ | ||
public stopBatch(): void { | ||
return this.requestManager.stopBatch(); | ||
} | ||
|
||
/** | ||
* A JSON-RPC call is represented by sending a Request object to a Server. | ||
* | ||
* @param requestObject.method A String containing the name of the method to be invoked. Method names that begin with the word rpc | ||
* followed by a period character (U+002E or ASCII 46) are reserved for rpc-internal methods and extensions and | ||
* MUST NOT be used for anything else. | ||
* @param requestObject.params A Structured value that holds the parameter values to be used during the invocation of the method. | ||
* | ||
* @example | ||
* myClient.request({method: "foo", params: ["bar"]}).then(() => console.log('foobar')); | ||
*/ | ||
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: NotificationArguments) { | ||
if (this.requestManager.connectPromise) { | ||
await this.requestManager.connectPromise; | ||
} | ||
return this.requestManager.request(requestObject, true); | ||
} | ||
|
||
public onNotification(callback: (data: any) => void) { | ||
this.requestManager.requestChannel.addListener("notification", callback); | ||
} | ||
|
||
public onError(callback: (data: JSONRPCError) => void) { | ||
this.requestManager.requestChannel.addListener("error", callback); | ||
} | ||
} | ||
|
||
export default Client; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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<unknown>; | ||
notify(args: NotificationArguments): Promise<unknown>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.