Expose WS subscribe/unsubscribe (#63)
* Expose WS subscribe * Support unsubscribe * Align unsubscribe with method * Basic e2e sub test * Cleanup parameter positions * Small update * Handle non-supplied in output formatter
This commit is contained in:
@@ -3,27 +3,40 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { JsonRpcResponse } from '../../types';
|
||||
import type { JsonRpcResponse, JsonRpcResponseBase$Error } from '../../types';
|
||||
import type { RpcCoderState } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
const isNumber = require('@polkadot/util/is/number');
|
||||
const isUndefined = require('@polkadot/util/is/undefined');
|
||||
|
||||
module.exports = function decodeResponse (self: RpcCoderState, response: JsonRpcResponse): mixed {
|
||||
assert(response, 'Empty response object received');
|
||||
|
||||
assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');
|
||||
|
||||
assert(isNumber(response.id), 'Invalid id field in decoded object');
|
||||
|
||||
if (response.error) {
|
||||
const { code, message } = response.error;
|
||||
|
||||
throw new Error(`[${code}]: ${message}`);
|
||||
function checkError (error?: JsonRpcResponseBase$Error) {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
assert(!isUndefined(response.result), 'No result found in JsonRpc response');
|
||||
const { code, message } = error;
|
||||
|
||||
throw new Error(`[${code}]: ${message}`);
|
||||
}
|
||||
|
||||
module.exports = function decodeResponse (self: RpcCoderState, response: JsonRpcResponse): mixed {
|
||||
assert(response, 'Empty response object received');
|
||||
assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');
|
||||
|
||||
const isSubscription = !isUndefined(response.params) && !isUndefined(response.method);
|
||||
|
||||
assert(isNumber(response.id) || (isSubscription && isNumber(response.params.subscription)), 'Invalid id field in decoded object');
|
||||
|
||||
checkError(response.error);
|
||||
|
||||
assert(!isUndefined(response.result) || isSubscription, 'No result found in JsonRpc response');
|
||||
|
||||
if (isSubscription) {
|
||||
checkError(response.params.error);
|
||||
|
||||
return response.params.result;
|
||||
}
|
||||
|
||||
return response.result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user