First stage renames, rpc-{core,provider,rx} (#220)
* Docs renames (& removal for re-gen) * Rename api, api-provider & api-rx * Apoi -> Rpc, RxApi -> RpcRx (classes) * Add .nodoc * Bump version * Link updates (summary) * Cleanup deleted (master version) * Deleted re-instated * More... ;) * Types... * Revert .nodoc
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import createCoder from './index';
|
||||
|
||||
describe('decodeResponse', () => {
|
||||
let coder;
|
||||
|
||||
beforeEach(() => {
|
||||
coder = createCoder();
|
||||
});
|
||||
|
||||
it('expects a non-empty input object', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse()
|
||||
).toThrow(/Empty response/);
|
||||
});
|
||||
|
||||
it('expects a valid jsonrpc field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({})
|
||||
).toThrow(/Invalid jsonrpc/);
|
||||
});
|
||||
|
||||
it('expects a valid id field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ jsonrpc: '2.0' })
|
||||
).toThrow(/Invalid id/);
|
||||
});
|
||||
|
||||
it('expects a valid result field', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' })
|
||||
).toThrow(/No result/);
|
||||
});
|
||||
|
||||
it('throws any error found', () => {
|
||||
expect(
|
||||
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0', error: { code: 123, message: 'test error' } })
|
||||
).toThrow(/\[123\]: test error/);
|
||||
});
|
||||
|
||||
it('returns the result', () => {
|
||||
expect(
|
||||
coder.decodeResponse({ id: 1, jsonrpc: '2.0', result: 'some result' })
|
||||
).toEqual('some result');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { JsonRpcResponse, JsonRpcResponseBase$Error } from '../../types';
|
||||
import { RpcCoderState } from './types';
|
||||
|
||||
import assert from '@polkadot/util/assert';
|
||||
import isNumber from '@polkadot/util/is/number';
|
||||
import isUndefined from '@polkadot/util/is/undefined';
|
||||
|
||||
function checkError (error?: JsonRpcResponseBase$Error) {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { code, message } = error;
|
||||
|
||||
throw new Error(`[${code}]: ${message}`);
|
||||
}
|
||||
|
||||
export default function decodeResponse (self: RpcCoderState, response: JsonRpcResponse): any {
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import createCoder from './index';
|
||||
|
||||
describe('encodeJson', () => {
|
||||
let coder;
|
||||
|
||||
beforeEach(() => {
|
||||
coder = createCoder();
|
||||
});
|
||||
|
||||
it('encodes a valid JsonRPC JSON string', () => {
|
||||
expect(
|
||||
coder.encodeJson('method', 'params')
|
||||
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { RpcCoderState } from './types';
|
||||
|
||||
import encodeObject from './encodeObject';
|
||||
|
||||
export default function encodeJson (self: RpcCoderState, method: string, params: Array<any>): string {
|
||||
return JSON.stringify(
|
||||
encodeObject(self, method, params)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import createCoder from './index';
|
||||
|
||||
describe('encodeObject', () => {
|
||||
let coder;
|
||||
|
||||
beforeEach(() => {
|
||||
coder = createCoder();
|
||||
});
|
||||
|
||||
it('starts with id === 0 (nothing sent)', () => {
|
||||
expect(coder.getId()).toEqual(0);
|
||||
});
|
||||
|
||||
it('encodes a valid JsonRPC object', () => {
|
||||
expect(
|
||||
coder.encodeObject('method', 'params')
|
||||
).toEqual({
|
||||
id: 1,
|
||||
jsonrpc: '2.0',
|
||||
method: 'method',
|
||||
params: 'params'
|
||||
});
|
||||
expect(coder.getId()).toEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { JsonRpcRequest } from '../../types';
|
||||
import { RpcCoderState } from './types';
|
||||
|
||||
export default function encodeObject (self: RpcCoderState, method: string, params: Array<any>): JsonRpcRequest {
|
||||
return {
|
||||
id: ++self.id,
|
||||
jsonrpc: '2.0',
|
||||
method,
|
||||
params
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { JsonRpcRequest, JsonRpcResponse } from '../../types';
|
||||
import { RpcCoder, RpcCoderState } from './types';
|
||||
|
||||
import decodeResponse from './decodeResponse';
|
||||
import encodeJson from './encodeJson';
|
||||
import encodeObject from './encodeObject';
|
||||
|
||||
export default function rpcCoder (): RpcCoder {
|
||||
const self: RpcCoderState = {
|
||||
id: 0
|
||||
};
|
||||
|
||||
return {
|
||||
decodeResponse: (response: JsonRpcResponse): any =>
|
||||
decodeResponse(self, response),
|
||||
encodeJson: (method: string, params: Array<any>): string =>
|
||||
encodeJson(self, method, params),
|
||||
encodeObject: (method: string, params: Array<any>): JsonRpcRequest =>
|
||||
encodeObject(self, method, params),
|
||||
getId: (): number =>
|
||||
self.id
|
||||
};
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2018 @polkadot/rpc-provider authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { JsonRpcRequest, JsonRpcResponse } from '../../types';
|
||||
|
||||
export type RpcCoder = {
|
||||
decodeResponse: (response: JsonRpcResponse) => any,
|
||||
encodeObject: (method: string, params: Array<any>) => JsonRpcRequest,
|
||||
encodeJson: (method: string, params: Array<any>) => string,
|
||||
getId: () => number
|
||||
};
|
||||
|
||||
export type RpcCoderState = {
|
||||
id: number
|
||||
};
|
||||
Reference in New Issue
Block a user