@@ -2,7 +2,7 @@ import { template } from "lodash";
22
33export default template ( `
44// Code generated by @open-rpc/client-generator DO NOT EDIT.
5- import { RequestManager, WebSocketTransport, HTTPTransport, Client } from '@open-rpc/client-js';
5+ import { RequestManager, WebSocketTransport, HTTPTransport, Client, JSONRPCError } from '@open-rpc/client-js';
66import _ from "lodash";
77import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
88import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils-js";
@@ -23,6 +23,7 @@ export class <%= className %> {
2323 public static openrpcDocument: OpenRPC = <%= JSON.stringify(openrpcDocument) %> ;
2424 public transport: HTTPTransport | WebSocketTransport;
2525 private validator: MethodCallValidator;
26+ private timeout: number | undefined;
2627
2728 constructor(options: Options) {
2829
@@ -49,6 +50,36 @@ export class <%= className %> {
4950 this.rpc = new Client(new RequestManager([this.transport]));
5051 this.validator = new MethodCallValidator(<%= className %>.openrpcDocument);
5152 }
53+ /**
54+ * Adds a JSONRPC notification handler to handle receiving notifications.
55+ * @example
56+ * myClient.onNotification((data)=>console.log(data));
57+ */
58+ public onNotification(callback: (data: any) => void) {
59+ this.rpc.onNotification(callback);
60+ }
61+
62+ /**
63+ * Adds an optional JSONRPCError handler to handle receiving errors that cannot be resolved to a specific request
64+ * @example
65+ * myClient.onError((err: JSONRPCError)=>console.log(err.message));
66+ */
67+ public onError(callback: (data: JSONRPCError) => void) {
68+ this.rpc.onError(callback);
69+ }
70+
71+ /**
72+ * Sets a default timeout in ms for all requests excluding notifications.
73+ * @example
74+ * // 20s timeout
75+ * myClient.setDefaultTimeout(20000);
76+ * // Removes timeout from request
77+ * myClient.setDefaultTimeout(undefined);
78+ */
79+ public setDefaultTimeout(ms?: number) {
80+ this.timeout = ms;
81+ }
82+
5283
5384 /**
5485 * Initiates [[<%= className %>.startBatch]] in order to build a batch call.
@@ -85,6 +116,7 @@ export class <%= className %> {
85116
86117 private request(methodName: string, params: any[]): Promise<any> {
87118 const methodObject = _.find(<%= className %>.openrpcDocument.methods, ({name}) => name === methodName) as MethodObject;
119+ const notification = methodObject.result ? false : true;
88120 const openRpcMethodValidationErrors = this.validator.validate(methodName, params);
89121 if ( openRpcMethodValidationErrors instanceof MethodNotFoundError || openRpcMethodValidationErrors.length > 0) {
90122 return Promise.reject(openRpcMethodValidationErrors);
@@ -96,13 +128,17 @@ export class <%= className %> {
96128 } else {
97129 rpcParams = params;
98130 }
99- return this.rpc.request(methodName, rpcParams);
131+ if(notification) {
132+ return this.rpc.notify(methodName, rpcParams);
133+ }
134+ return this.rpc.request(methodName, rpcParams, this.timeout);
100135 }
101136
102137 <% openrpcDocument.methods.forEach((method) => { %>
103138 /**
104139 * <%= method.summary %>
105140 */
141+ // tslint:disable-next-line:max-line-length
106142 public <%= method.name %>: <%= methodTypings.getTypingNames("typescript", method).method %> = (...params) => {
107143 return this.request("<%= method.name %>", params);
108144 }
0 commit comments