forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery-jsonrpcclient.d.ts
103 lines (95 loc) · 4.67 KB
/
jquery-jsonrpcclient.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Type definitions for jquery.jsonrpc 0.7.0
// Project: https://github.com/Textalk/jquery.jsonrpcclient.js
// Definitions by: Maksim Karelov <https://github.com/Ty3uK>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../jquery/jquery.d.ts" />
interface JsonRpcClientOptions extends JQueryAjaxSettings {
ajaxUrl: string;
headers?: {[key:string]: any};
socketUrl?: string;
onmessage?: (ev: MessageEvent) => void;
onopen?: (ev: Event) => void;
onclose?: (ev: CloseEvent) => void;
onerror?: (ev: Event) => void;
getSockect?: (onmessageCb: () => void) => WebSocket;
/**
* Sets timeout for calls in milliseconds.
* Works with WebSocket as well as AJAX.
*/
timeout?: number;
}
interface JsonRpcClient {
/**
* @fn call
* @memberof JsonRpcClient
*
* @param {string} method The method to run on JSON-RPC server.
* @param {object|array} params The params; an array or object.
* @param {function} successCb A callback for successful request.
* @param {function} errorCb A callback for error.
*
* @return {object} Returns the deferred object that $.ajax returns or {null} for websockets
*/
call(method: string, params: Object | Array<any>, successCb: (result: any) => void, errorCb: (result: any) => void): JQueryDeferred<JQueryXHR>;
/**
* Notify sends a command to the server that won't need a response. In http, there is probably
* an empty response - that will be dropped, but in ws there should be no response at all.
*
* This is very similar to call, but has no id and no handling of callbacks.
*
* @fn notify
* @memberof JsonRpcClient
*
* @param {string} method The method to run on JSON-RPC server.
* @param {object|array} params The params; an array or object.
*
* @return {object} Returns the deferred object that $.ajax returns or {null} for websockets
*/
notify(method: string, params: Object | Array<any>): JQueryDeferred<JQueryXHR>;
/**
* Make a batch-call by using a callback.
*
* The callback will get an object "batch" as only argument. On batch, you can call the methods
* "call" and "notify" just as if it was a normal JsonRpcClient object, and all calls will be
* sent as a batch call then the callback is done.
*
* @fn batch
* @memberof JsonRpcClient
*
* @param {function} callback This function will get a batch handler to run call and notify on.
* @param {function} allDoneCb A callback function to call after all results have been handled.
* @param {function} errorCb A callback function to call if there is an error from the server.
* Note, that batch calls should always get an overall success, and the
* only error
*/
batch(callback: (batch: JsonRpcClient) => void, allDoneCb: (result: any) => void, errorCb: (error: any) => void): void;
}
interface JsonRpcClientFactory {
/**
* @fn new
* @memberof JsonRpcClient
*
* @param {object} options An object stating the backends:
* ajaxUrl A url (relative or absolute) to a http(s) backend.
* headers An object that will be passed along to $.ajax in options.headers
* xhrFields An object that will be passed along to $.ajax in options.xhrFields
* socketUrl A url (relative of absolute) to a ws(s) backend.
* onmessage A socket message handler for other messages (non-responses).
* onopen A socket onopen handler. (Not used for custom getSocket.)
* onclose A socket onclose handler. (Not used for custom getSocket.)
* onerror A socket onerror handler. (Not used for custom getSocket.)
* getSocket A function returning a WebSocket or null.
* It must take an onmessage_cb and bind it to the onmessage event
* (or chain it before/after some other onmessage handler).
* Or, it could return null if no socket is available.
* The returned instance must have readyState <= 1, and if less than 1,
* react to onopen binding.
* timeout (optional) A number of ms to wait before timing out and failing a
* call. If specified a setTimeout will be used to keep track of calls
* made through a websocket.
*/
new (options?: JsonRpcClientOptions): JsonRpcClient;
}
interface JQueryStatic {
JsonRpcClient: JsonRpcClientFactory;
}