From 5690a572d3ba52a54c130e57fd3e10c8b21e535e Mon Sep 17 00:00:00 2001 From: peter Date: Wed, 25 Sep 2019 20:53:53 +0200 Subject: [PATCH] v 1.2.1 --- README.md | 35 +++++++++++++++++++++++++++++------ package.json | 2 +- src/Utils.ts | 5 +++-- test/Test.ts | 2 +- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 2dbc206..519268b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,11 @@ client.connect().then(async () => { # Using callbacks -rpclibrary offers a special type of call that can be used with callbacks +rpclibrary offers a special type of call that can be used with callbacks. The callback *has to be the last argument* and may be the only passed function. + +In order to function, some metadata has to be included in the return value of hooks. On success, the function is expected to return a `type SubscriptionResponse = { result: 'Success', uuid: string }` or in case of errors a `type ErrorResposne = { result: 'Error' }`. + +The uuid, as the name implies, is used to uniquely identify the callback for a given invocation and also dictates the name given to the client-side RPC which you should unhook once you're done with it. ```typescript import {Backend, Frontend, Utils} from 'rpclibrary' @@ -52,7 +56,7 @@ const server = new Backend.RPCServer(20000, [{ { name: 'subscribe', hook: async (callback) => { - const resp = Utils.makeSubResponse() + const resp = Utils.makeSubResponse() //Convenience method to generate SubscriptionResponse callbacks.set(resp.uuid, callback); return resp } @@ -77,8 +81,30 @@ client.connect().then(async () => { }) ``` +If you need to include further response data into your `SubscriptionResponse` you can extend it using the server's first generic parameter `SubResType` + +```typescript +new RPCServer<{extension: string}>({ + exportRPCs: () => [ + { + name: 'subscribe', + hook: async (callback) => { + return { + result: 'Success', + uuid: 'very_random_string', + extension: 'your_data_here' + } + } + } + ] +}) + +``` + #Experimental typing support -It is possible to declare pseudo-interfaces for servers and clients +It is possible to declare pseudo-interfaces for servers and clients by using server's second generic parameter. +This feature is currently still in development and considered **unstable and untested**. Use with caution. + ```typescript type MyInterface = RPCInterface<{ Group1: { @@ -120,8 +146,5 @@ new RPCServer<{a:string}, MyInterface>(20001, ) ``` - - - # Documentation [https://gitea.frontblock.me/fw-docs/rpclibrary] diff --git a/package.json b/package.json index dbdf2fc..f29dddc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rpclibrary", - "version": "1.2.0", + "version": "1.2.1", "description": "rpclibrary is a websocket on steroids!", "main": "./js/Index.js", "repository": { diff --git a/src/Utils.ts b/src/Utils.ts index b9442a8..238912e 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -116,9 +116,10 @@ const extractArgs = (f:Function):string[] => { * Simple utility function to create basic {@link SubscriptionResponse} * @param uuid optional uuid to use, otherwise defaults to uuid/v4 */ -export function makeSubResponse(uuid?:string):SubscriptionResponse{ +export function makeSubResponse(extension:T):SubscriptionResponse & T{ return { result: "Success", - uuid: uuid?uuid:uuidv4(), + uuid: uuidv4(), + ...extension } } \ No newline at end of file diff --git a/test/Test.ts b/test/Test.ts index 1f5d29f..64f620c 100644 --- a/test/Test.ts +++ b/test/Test.ts @@ -111,7 +111,7 @@ describe('RPCSocket', () => { if(x === 6) done() else - done(new Error('echo RPC response did not match')) + done(new Error('add RPC response did not match')) }) })