Skip to content

Commit 16bafce

Browse files
committed
feat: add support for default timeout, onNotify, and onError
Support for default timeout is added to allow clients to set a timeout for request being made. Default timeout is not set by default. Timeout is disabled by setting the timeout to undefined and enabled by setting the timeout to a number to specify the timeout parameter in ms. onNotify is used to receive notifications and onError is used to receive errors that can't be attributed to a specific request.
1 parent db75f47 commit 16bafce

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

templates/typescript/static/_package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"author": "",
1414
"license": "Apache-2.0",
1515
"dependencies": {
16-
"@open-rpc/client-js": "^1.1.1",
16+
"@open-rpc/client-js": "^1.2.2",
1717
"@open-rpc/meta-schema": "^1.5.3",
1818
"@open-rpc/schema-utils-js": "^1.11.4",
1919
"lodash": "^4.17.15"

templates/typescript/templated/exported-class.template.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { template } from "lodash";
22

33
export 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';
66
import _ from "lodash";
77
import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
88
import { 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

Comments
 (0)