Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[ignore]
<PROJECT_ROOT>/build
<PROJECT_ROOT>/packages/.*/build

[include]

Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"packages": [
"packages/*"
],
"version": "0.6.12"
"version": "0.7.0"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.14.6",
"@polkadot/dev": "^0.15.1",
"lerna": "^2.5.1"
}
}
8 changes: 4 additions & 4 deletions packages/api-format/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-format",
"version": "0.6.12",
"version": "0.7.0",
"description": "Input/Output formatters for JsonRPC exchange",
"main": "index.js",
"keywords": [
Expand Down Expand Up @@ -30,11 +30,11 @@
"test": "echo \"Tests only available from root wrapper\""
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.6.12"
"@polkadot/api-jsonrpc": "^0.7.0"
},
"dependencies": {
"@polkadot/primitives-json": "^0.4.6",
"@polkadot/util": "^0.11.1",
"@polkadot/primitives-json": "^0.4.10",
"@polkadot/util": "^0.13.1",
"babel-runtime": "^6.26.0"
}
}
2 changes: 1 addition & 1 deletion packages/api-jsonrpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-jsonrpc",
"version": "0.6.12",
"version": "0.7.0",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-jsonrpc/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// of the ISC license. See the LICENSE file for details.
// @flow

import type { InterfaceDefinition } from './types';
import type { InterfaceDefinitions } from './types';

const chain = require('./chain');
const state = require('./state');
Expand All @@ -14,4 +14,4 @@ const state = require('./state');
module.exports = ({
chain,
state
}: { [string]: InterfaceDefinition });
}: InterfaceDefinitions);
6 changes: 6 additions & 0 deletions packages/api-jsonrpc/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ export type InterfaceMethodDefinition = {
output: InterfaceOutputType
};

export type InterfaceTypes = 'chain' | 'state';

export type InterfaceDefinition = {
methods: {
[string]: InterfaceMethodDefinition
}
};

export type InterfaceDefinitions = {
[InterfaceTypes]: InterfaceDefinition
};
4 changes: 2 additions & 2 deletions packages/api-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-provider",
"version": "0.6.12",
"version": "0.7.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
Expand Down Expand Up @@ -34,7 +34,7 @@
"nock": "^9.1.0"
},
"dependencies": {
"@polkadot/util": "^0.11.1",
"@polkadot/util": "^0.13.1",
"babel-runtime": "^6.26.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
Expand Down
1 change: 1 addition & 0 deletions packages/api-provider/src/jsonRpcCoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = class JsonRpcCoder {

throw new Error(`[${code}]: ${message}`);
}

assert(!isUndefined(response.result), 'No result found in JsonRpc response');

return response.result;
Expand Down
18 changes: 3 additions & 15 deletions packages/api-provider/src/ws/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,28 +207,16 @@ describe('Ws', () => {
});

describe('queued', () => {
let sendPromise;

beforeEach(() => {
ws = createWs([{
it('sends messages when connected', () => {
const ws = createWs([{
id: 1,
method: 'test_queue',
reply: {
result: 'ok'
}
}], false);
sendPromise = ws.send('test_queue', []);
});

it('sends messages when connected', () => {
ws.connect();

return sendPromise.then((result) => {
expect(result).toEqual('ok');
});
});
const sendPromise = ws.send('test_queue', []);

it('sends messages when connected (errors)', () => {
ws.connect();

return sendPromise.then((result) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "0.6.12",
"version": "0.7.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
Expand Down Expand Up @@ -30,10 +30,10 @@
"test": "echo \"Tests only available from root wrapper\""
},
"dependencies": {
"@polkadot/api-format": "^0.6.12",
"@polkadot/api-jsonrpc": "^0.6.12",
"@polkadot/api-provider": "^0.6.12",
"@polkadot/util": "^0.11.1",
"@polkadot/api-format": "^0.7.0",
"@polkadot/api-jsonrpc": "^0.7.0",
"@polkadot/api-provider": "^0.7.0",
"@polkadot/util": "^0.13.1",
"babel-runtime": "^6.26.0"
}
}
53 changes: 6 additions & 47 deletions packages/api/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,22 @@
// @flow

import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { InterfaceDefinition } from '@polkadot/api-jsonrpc/types';
import type { ApiInterface, ApiInterface$Section } from './types';

const { formatInputs, formatOutput } = require('@polkadot/api-format');
const interfaces = require('@polkadot/api-jsonrpc');
const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const isFunction = require('@polkadot/util/is/function');
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');

const createInterface = require('./create/interface');

module.exports = class Api implements ApiInterface {
_provider: ProviderInterface;
_chainInterface: ApiInterface$Section;
_stateInterface: ApiInterface$Section;
chain: ApiInterface$Section;
state: ApiInterface$Section;

constructor (provider: ProviderInterface) {
assert(provider && isFunction(provider.send), 'Expected Provider');

this._provider = provider;

this._chainInterface = this._createInterface(interfaces, 'chain');
this._stateInterface = this._createInterface(interfaces, 'state');
}

// flowlint-next-line unsafe-getters-setters:off
get chain (): ApiInterface$Section {
return this._chainInterface;
}

// flowlint-next-line unsafe-getters-setters:off
get state (): ApiInterface$Section {
return this._stateInterface;
}

_createInterface (definitions: { [string]: InterfaceDefinition }, section: string): ApiInterface$Section {
const definition = definitions[section];

return Object
.keys(definition.methods)
.reduce((container, method: string) => {
const { inputs, output } = definition.methods[method];
const rpcName = `${section}_${method}`;

container[method] = async (..._params: Array<mixed>): Promise<mixed> => {
try {
assert(inputs.length === _params.length, `${inputs.length} params expected, found ${_params.length} instead`);

const params = formatInputs(inputs, _params);
const result = await this._provider.send(rpcName, params);

return formatOutput(output, result);
} catch (error) {
throw new ExtError(`${jsonrpcSignature(rpcName, inputs, output)}:: ${error.message}`, (error: ExtError).code);
}
};

return container;
}, {});
this.chain = createInterface(provider, interfaces.chain, 'chain');
this.state = createInterface(provider, interfaces.state, 'state');
}
};
84 changes: 9 additions & 75 deletions packages/api/src/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

const isFunction = require('@polkadot/util/is/function');

const Api = require('./api');

describe('Api', () => {
Expand All @@ -25,81 +23,17 @@ describe('Api', () => {
sendSpy.mockRestore();
});

describe('constructor', () => {
it('requires a provider with a send method', () => {
expect(
() => new Api({})
).toThrow(/Expected Provider/);
});

it('sets up the chain interface', () => {
expect(api.chain).toBeDefined();
});

it('sets up the state interface', () => {
expect(api.state).toBeDefined();
});
it('requires a provider with a send method', () => {
expect(
() => new Api({})
).toThrow(/Expected Provider/);
});

describe('_createInterface', () => {
let container;

beforeEach(() => {
container = api._createInterface({
test: {
methods: {
blah: {
inputs: [
{ name: 'foo', type: 'Address' }
],
output: { type: 'Address' }
},
bleh: {
inputs: [],
output: { type: 'Address' }
}
}
}
}, 'test');
});

describe('method expansion', () => {
it('adds the specified methods to the interface', () => {
expect(Object.keys(container)).toEqual(['blah', 'bleh']);
});

it('had function calls for the attached methods', () => {
expect(isFunction(container.blah)).toEqual(true);
expect(isFunction(container.bleh)).toEqual(true);
});
});

describe('calling', () => {
it('wraps errors with the call signature', () => {
return container
.blah()
.catch((error) => {
expect(error.message).toMatch(/test_blah \(foo: Address\): Address/);
});
});

it('checks for mismatched parameters', () => {
return container
.bleh(1)
.catch((error) => {
expect(error.message).toMatch(/0 params expected, found 1 instead/);
});
});
it('sets up the chain interface', () => {
expect(api.chain).toBeDefined();
});

it('calls the provider with the correct parameters', () => {
return container
.blah('0x123')
.then(() => {
expect(provider.send).toHaveBeenCalledWith('test_blah', [
'0x0123'
]);
});
});
});
it('sets up the state interface', () => {
expect(api.state).toBeDefined();
});
});
37 changes: 37 additions & 0 deletions packages/api/src/create/interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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 '@polkadot/api-jsonrpc/types';
import type { ProviderInterface } from '@polkadot/api-provider/types';
import type { ApiInterface$Section } from '../types';

const { formatInputs, formatOutput } = require('@polkadot/api-format');
const assert = require('@polkadot/util/assert');
const ExtError = require('@polkadot/util/ext/error');
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');

module.exports = function createInterface (provider: ProviderInterface, definition: InterfaceDefinition, section: string): ApiInterface$Section {
return Object
.keys(definition.methods)
.reduce((container, method: string) => {
const { inputs, output } = definition.methods[method];
const rpcName = `${section}_${method}`;

container[method] = async (..._params: Array<mixed>): Promise<mixed> => {
try {
assert(inputs.length === _params.length, `${inputs.length} params expected, found ${_params.length} instead`);

const params = formatInputs(inputs, _params);
const result = await provider.send(rpcName, params);

return formatOutput(output, result);
} catch (error) {
throw new ExtError(`${jsonrpcSignature(rpcName, inputs, output)}:: ${error.message}`, (error: ExtError).code);
}
};

return container;
}, ({}: $Shape<ApiInterface$Section>));
};
Loading