Add api.extra.* (test) methods (#55)

This commit is contained in:
Jaco Greeff
2018-03-15 16:33:21 +01:00
committed by GitHub
parent 56ec979655
commit 74f14160bd
10 changed files with 101 additions and 29 deletions
+4 -1
View File
@@ -5,15 +5,18 @@
import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
const bnDecode = require('@polkadot/primitives-json/bn/decode');
const headerDecode = require('@polkadot/primitives-json/header/decode');
const echo = require('./echo');
const util = require('./util');
const formatters = {
'BlockNumber': bnDecode,
'Header': headerDecode,
'OutData': echo,
'StorageData': echo
'StorageData': echo,
'U64': bnDecode
};
module.exports = function formatOutput (output: InterfaceOutputType, value: mixed): mixed {
@@ -0,0 +1,18 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getBlockNumber
@signature extra_getBlockNumber (): BlockNumber
@summary Retrieves the best blockNumber
@description
Returns the best blockNumber available on the client
*/
module.exports = ({
inputs: [],
output: { type: 'BlockNumber' }
}: InterfaceMethodDefinition);
@@ -0,0 +1,18 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceMethodDefinition } from '../types';
/**
@name getClientTime
@signature extra_getClientTime (): U64
@summary Retrieves the current time
@description
Returns the current time on the running node
*/
module.exports = ({
inputs: [],
output: { type: 'U64' }
}: InterfaceMethodDefinition);
+19
View File
@@ -0,0 +1,19 @@
// Copyright 2017-2018 Jaco Greeff
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinition } from '../types';
const getBlockNumber = require('./getBlockNumber');
const getClientTime = require('./getClientTime');
/**
@summary Extended methods not available in all clients.
*/
module.exports = ({
methods: {
getBlockNumber,
getClientTime
}
}: InterfaceDefinition);
+2
View File
@@ -6,6 +6,7 @@
import type { InterfaceDefinitions } from './types';
const chain = require('./chain');
const extra = require('./extra');
const state = require('./state');
/**
@@ -13,5 +14,6 @@ const state = require('./state');
*/
module.exports = ({
chain,
extra,
state
}: InterfaceDefinitions);
+2 -2
View File
@@ -5,7 +5,7 @@
export type FormatInputType = 'Address' | 'CallData' | 'H256' | 'HeaderHash' | 'String';
export type FormatOutputType = 'Header' | 'OutData' | 'StorageData';
export type FormatOutputType = 'BlockNumber' | 'Header' | 'OutData' | 'StorageData' | 'U64';
export type InterfaceInputType = {
name: string,
@@ -22,7 +22,7 @@ export type InterfaceMethodDefinition = {
output: InterfaceOutputType
};
export type InterfaceTypes = 'chain' | 'state';
export type InterfaceTypes = 'chain' | 'extra' | 'state';
export type InterfaceDefinition$Methods = {
[string]: InterfaceMethodDefinition
+7 -2
View File
@@ -3,15 +3,20 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceDefinition } from '@polkadot/api-jsonrpc/types';
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface$Section } from '../types';
const interfaces = require('@polkadot/api-jsonrpc');
const createMethod = require('./method');
const createSubscribe = require('./subscribe');
module.exports = function createInterface (provider: ProviderInterface, { methods }: InterfaceDefinition, section: string): ApiInterface$Section {
console.log('interfaces', interfaces);
module.exports = function createInterface (provider: ProviderInterface, section: InterfaceTypes): ApiInterface$Section {
const exposed: $Shape<ApiInterface$Section> = {};
const { methods } = interfaces[section];
exposed.subscribe = createSubscribe(provider, section, methods);
exposed.unsubscribe = provider.unsubscribe;
+18 -15
View File
@@ -4,6 +4,23 @@
const isFunction = require('@polkadot/util/is/function');
jest.mock('./@polkadot/api-jsonrpc', () => ({
test: {
methods: {
blah: {
inputs: [
{ name: 'foo', type: 'Address' }
],
output: { type: 'Address' }
},
bleh: {
inputs: [],
output: { type: 'Address' }
}
}
}
}));
const createInterface = require('./interface');
describe('createInterface', () => {
@@ -11,26 +28,12 @@ describe('createInterface', () => {
let provider;
beforeEach(() => {
const definition = {
methods: {
blah: {
inputs: [
{ name: 'foo', type: 'Address' }
],
output: { type: 'Address' }
},
bleh: {
inputs: [],
output: { type: 'Address' }
}
}
};
provider = {
send: jest.fn((method, params) => {
return Promise.resolve(params[0]);
})
};
container = createInterface(provider, definition, 'test');
container = createInterface(provider, 'test');
});
it('adds the specified methods to the interface', () => {
+10 -7
View File
@@ -3,20 +3,23 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface } from './types';
const interfaces = require('@polkadot/api-jsonrpc');
const assert = require('@polkadot/util/assert');
const isFunction = require('@polkadot/util/is/function');
const createInterface = require('./create/interface');
module.exports = function api (provider: ProviderInterface): ApiInterface {
assert(provider && isFunction(provider.send), 'Expected Provider');
const ALL_INTERFACES: Array<InterfaceTypes> = ['chain', 'extra', 'state'];
return {
chain: createInterface(provider, interfaces.chain, 'chain'),
state: createInterface(provider, interfaces.state, 'state')
};
module.exports = function api (provider: ProviderInterface): ApiInterface {
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
return ALL_INTERFACES.reduce((result, type: InterfaceTypes) => {
result[type] = createInterface(provider, type);
return result;
}, ({}: $Shape<ApiInterface>));
};
+3 -2
View File
@@ -3,6 +3,8 @@
// of the ISC license. See the LICENSE file for details.
// @flow
import type { InterfaceTypes } from '@polkadot/api-jsonrpc/types';
export type ApiInterface$Section$Method = (...params: Array<mixed>) => Promise<mixed>;
export type ApiInterface$Section = {
@@ -13,6 +15,5 @@ export type ApiInterface$Section = {
};
export type ApiInterface = {
chain: ApiInterface$Section,
state: ApiInterface$Section
[InterfaceTypes]: ApiInterface$Section
}