Skip to content

Commit

Permalink
add Server.prototype.callp method that returns a promise which closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
tedeh committed Aug 12, 2022
1 parent 1d2308e commit bbed54b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -248,6 +248,7 @@ export interface Server extends events.EventEmitter {
error(code?: number, message?: string, data?: object): JSONRPCError;
call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, originalCallback?: ServerCallCallbackType): void;
call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, context: object, originalCallback?: ServerCallCallbackType): void;
callp(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, context?: object): Promise<JSONRPCResultLike>;
}

export declare const Server: ServerConstructor;
Expand Down
19 changes: 19 additions & 0 deletions lib/server/index.js
Expand Up @@ -333,6 +333,25 @@ Server.prototype.call = function(request, context, originalCallback) {
});
};

/**
* Calls a method on the server returning a promise
* @param {Object|Array|String} request A JSON-RPC request object. Object for single request, Array for batches and String for automatic parsing (using the reviver option)
* @param {Object} [context] Optional context object passed to methods
* @return {Promise<Object>}
*/
Server.prototype.callp = function (...args) {
const self = this;
return new Promise(function (resolve, reject) {
return self.call(...args, function (err, response) {
if (err) {
reject(err);
return;
}
resolve(response);
});
});
};

/**
* Invoke the router
* @param {String} method Method to resolve
Expand Down
1 change: 1 addition & 0 deletions promise/index.d.ts
Expand Up @@ -245,6 +245,7 @@ export interface Server extends events.EventEmitter {
error(code?: number, message?: string, data?: object): JSONRPCError;
call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, originalCallback?: ServerCallCallbackType): void;
call(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, context: object, originalCallback?: ServerCallCallbackType): void;
callp(request: JSONRPCRequestLike | Array<JSONRPCRequestLike>, context?: object): Promise<JSONRPCResultLike>;
}

export declare const Server: ServerConstructor;
Expand Down
18 changes: 18 additions & 0 deletions test/server.test.js
Expand Up @@ -551,6 +551,24 @@ describe('jayson.server', function() {

});

describe('callp', function () {

it('should return a promise that resolves', async function () {
const context = {};
const request = utils.request('add', [10, 20]);
const response = await server.callp(request, context);
await should(response).have.property('result', 30);
});

it('should return a promise that rejects', async function () {
const context = {};
const request = 'invalid request'
const promise = server.callp(request, context);
await should(promise).be.rejected();
});

});

});

});
22 changes: 22 additions & 0 deletions typescript/test.ts
Expand Up @@ -806,6 +806,28 @@ export function test_server_and_method_call () {
});
}

export async function test_server_and_method_callp () {
const server = new jayson.Server();
const request = jayson.Utils.request('add', [1, 2], undefined, {generator: () => Math.random()});
await server.callp(request, {});
await server.callp(request);
const response = await server.callp(request).catch(err => {
console.error(err);
})
console.log(response.result);
}

export async function test_server_and_method_callp_promise () {
const server = new jaysonPromise.Server();
const request = jayson.Utils.request('add', [1, 2], undefined, {generator: () => Math.random()});
await server.callp(request, {});
await server.callp(request);
const response = await server.callp(request).catch(err => {
console.error(err);
})
console.log(response.result);
}

export function test_websocket () {
const wss = new WebSocket.Server({port: 12345});
const server = new jayson.Server();
Expand Down

0 comments on commit bbed54b

Please sign in to comment.