diff --git a/jquery.jsonrpcclient/jquery.jsonrpcclient-tests.ts b/jquery.jsonrpcclient/jquery.jsonrpcclient-tests.ts new file mode 100644 index 000000000..074983562 --- /dev/null +++ b/jquery.jsonrpcclient/jquery.jsonrpcclient-tests.ts @@ -0,0 +1,23 @@ +/// +/// + +var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc' }); +foo.call( + 'bar', ['A parameter', 'B parameter'], + function (result) { alert('Foo bar answered: ' + result.my_answer); }, + function (error) { console.log('There was an error', error); } +); + + +var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc' }); +foo.batch( + function (batch) { + batch.call('bar', ['A parameter', 'B parameter'], function () { }, function () { }); + batch.call('baz', { parameters: 'could be object' }, function () { }, function () { }); + }, + function (all_result_array) { alert('All done.'); }, + function (error_data) { alert('Error in batch response.'); } + ); + +var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc', socketUrl: 'ws://example.com/' }); +foo.call('bar', ['param'], function () { }, function () { }); \ No newline at end of file diff --git a/jquery.jsonrpcclient/jquery.jsonrpcclient.d.ts b/jquery.jsonrpcclient/jquery.jsonrpcclient.d.ts new file mode 100644 index 000000000..de008dc09 --- /dev/null +++ b/jquery.jsonrpcclient/jquery.jsonrpcclient.d.ts @@ -0,0 +1,97 @@ +// Type definitions for jquery.jsonrpc 0.7.0 +// Project: https://github.com/Textalk/jquery.jsonrpcclient.js +// Definitions by: Maksim Karelov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +interface JsonRpcClientOptions extends JQueryAjaxSettings { + ajaxUrl?: string; + headers?: {[key:string]: any}; + sockerUrl?: string; + onmessage?: () => void; + onopen?: () => void; + onclose?: () => void; + onerror?: () => void; + getSockect?: (onmessageCb: () => void) => WebSocket; +} + +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, successCb: (result: any) => void, errorCb: (result: any) => void): JQueryDeferred; + + /** + * 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): JQueryDeferred; + + /** + * 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; +} \ No newline at end of file