Api & RxApi now created via new (#153)

* Api & RxApi now created via new

* Bump version
This commit is contained in:
Jaco Greeff
2018-09-14 17:32:58 +02:00
committed by GitHub
parent d9ab009917
commit 35b4f9a095
23 changed files with 106 additions and 102 deletions
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.28.35"
"version": "0.29.0"
}
+1 -1
View File
@@ -21,6 +21,6 @@
},
"devDependencies": {
"@polkadot/dev": "^0.20.19",
"@polkadot/ts": "^0.1.27"
"@polkadot/ts": "^0.1.28"
}
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-format",
"version": "0.28.35",
"version": "0.29.0",
"description": "Input/Output formatters for JsonRPC exchange",
"main": "index.js",
"keywords": [
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-format#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/jsonrpc": "^0.28.35",
"@polkadot/primitives": "^0.28.35",
"@polkadot/jsonrpc": "^0.29.0",
"@polkadot/primitives": "^0.29.0",
"@polkadot/util": "^0.30.3",
"@polkadot/util-keyring": "^0.30.3"
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-provider",
"version": "0.28.35",
"version": "0.29.0",
"description": "Transport providers for the API",
"main": "index.js",
"keywords": [
@@ -34,7 +34,7 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/storage": "^0.28.35",
"@polkadot/storage": "^0.29.0",
"@polkadot/util": "^0.30.3",
"@polkadot/util-crypto": "^0.30.3",
"@polkadot/util-keyring": "^0.30.3",
+2 -2
View File
@@ -4,7 +4,7 @@
import storage from '@polkadot/storage';
import createApi from '../../api/src';
import Api from '../../api/src';
import Ws from '../src/ws';
describe.skip('e2e', () => {
@@ -12,7 +12,7 @@ describe.skip('e2e', () => {
beforeEach(() => {
jest.setTimeout(30000);
api = createApi(new Ws('ws://127.0.0.1:9944'));
api = new Api(new Ws('ws://127.0.0.1:9944'));
});
it('subscribes to chain_newHead', (done) => {
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api-rx",
"version": "0.28.35",
"version": "0.29.0",
"description": "An RxJs wrapper around the Polkadot JS API",
"main": "index.js",
"keywords": [
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-rx#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/api": "^0.28.35",
"@polkadot/api-provider": "^0.28.35",
"@polkadot/api": "^0.29.0",
"@polkadot/api-provider": "^0.29.0",
"@types/rx": "^4.1.1",
"rxjs": "^6.3.2"
}
-16
View File
@@ -1,16 +0,0 @@
// Copyright 2017-2018 @polkadot/api-rx authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface } from '@polkadot/api-provider/types';
import { BehaviorSubject } from 'rxjs';
export default function connected (provider: ProviderInterface): BehaviorSubject<boolean> {
const subject = new BehaviorSubject(provider.isConnected());
provider.on('connected', () => subject.next(true));
provider.on('disconnected', () => subject.next(false));
return subject;
}
-20
View File
@@ -1,20 +0,0 @@
// Copyright 2017-2018 @polkadot/api-rx authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { ProviderInterface } from '@polkadot/api-provider/types';
import { RxApiInterface } from '../types';
import { BehaviorSubject } from 'rxjs';
import createConnected from './connected';
export default function exposed (provider: ProviderInterface): RxApiInterface {
const connected = createConnected(provider);
// @ts-ignore as in types.d.ts, not sure what to do here
return {
isConnected: (): BehaviorSubject<boolean> =>
connected
};
}
+22 -4
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import isFunction from '@polkadot/util/is/function';
jest.mock('@polkadot/api-provider/ws', () => class {
isConnected = () => true;
on = () => true;
@@ -9,14 +11,30 @@ jest.mock('@polkadot/api-provider/ws', () => class {
});
jest.mock('./interface', () => (api, sectionName) => sectionName);
const createApi = require('./index').default;
const Api = require('./index').default;
describe('Api', () => {
let api;
beforeEach(() => {
api = new Api();
});
describe('createApi', () => {
it('creates an instance with all sections', () => {
expect(
Object.keys(createApi())
Object
.keys(api)
.filter((key) =>
!key.startsWith('_')
)
).toEqual([
'isConnected', 'author', 'chain', 'state', 'system'
'author', 'chain', 'state', 'system'
]);
});
it('has isConnected', () => {
expect(
isFunction(api.isConnected)
).toBe(true);
});
});
+26 -11
View File
@@ -2,24 +2,39 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Interface$Sections } from '@polkadot/jsonrpc/types';
import { ApiInterface } from '@polkadot/api/types';
import { ProviderInterface } from '@polkadot/api-provider/types';
import { RxApiInterface } from './types';
import { RxApiInterface, RxApiInterface$Section } from './types';
import createApi from '@polkadot/api/index';
import interfaces from '@polkadot/jsonrpc/index';
import { BehaviorSubject } from 'rxjs';
import Api from '@polkadot/api/index';
import Ws from '@polkadot/api-provider/ws';
import createExposed from './api';
import defaults from './defaults';
import createInterface from './interface';
export default function rxApi (provider: ProviderInterface = new Ws(defaults.WS_URL)): RxApiInterface {
const api = createApi(provider);
export default class RxApi implements RxApiInterface {
private _api: ApiInterface;
private _isConnected: BehaviorSubject<boolean>;
readonly author: RxApiInterface$Section;
readonly chain: RxApiInterface$Section;
readonly state: RxApiInterface$Section;
readonly system: RxApiInterface$Section;
return Object.keys(interfaces).reduce((result, type) => {
result[type as Interface$Sections] = createInterface(api, type as Interface$Sections);
constructor (provider: ProviderInterface = new Ws(defaults.WS_URL)) {
this._api = new Api(provider);
this._isConnected = new BehaviorSubject(provider.isConnected());
return result;
}, createExposed(provider));
provider.on('connected', () => this._isConnected.next(true));
provider.on('disconnected', () => this._isConnected.next(false));
this.author = createInterface(this._api, 'author');
this.chain = createInterface(this._api, 'chain');
this.state = createInterface(this._api, 'state');
this.system = createInterface(this._api, 'system');
}
isConnected (): BehaviorSubject<boolean> {
return this._isConnected;
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ export type RxApiInterface$Section = {
};
type RxApiInterface$Keys = {
[key in keyof Interfaces]: RxApiInterface$Section;
readonly [key in keyof Interfaces]: RxApiInterface$Section;
};
export type RxApiInterface = RxApiInterface$Keys & {
+2 -2
View File
@@ -25,11 +25,11 @@ npm install --save @polkadot/api
Initialisation -
```js
import createApi from '@polkadot/api';
import Api from '@polkadot/api';
import WsProvider from '@polkadot/api-provider/ws';
const provider = new WsProvider('http://127.0.0.1:9944');
const api = createApi(provider);
const api = new Api(provider);
```
Making calls -
+5 -5
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/api",
"version": "0.28.35",
"version": "0.29.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "index.js",
"keywords": [
@@ -30,10 +30,10 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/api-format": "^0.28.35",
"@polkadot/api-provider": "^0.28.35",
"@polkadot/jsonrpc": "^0.28.35",
"@polkadot/params": "^0.28.35",
"@polkadot/api-format": "^0.29.0",
"@polkadot/api-provider": "^0.29.0",
"@polkadot/jsonrpc": "^0.29.0",
"@polkadot/params": "^0.29.0",
"@polkadot/util": "^0.30.3"
}
}
+2 -2
View File
@@ -4,7 +4,7 @@
import storage from '@polkadot/storage';
import createApi from '../index';
import Api from '../index';
const ADDR_ONE = '5EhmTa7fL6SdjgKXo9g6hetR6nHnRAmrtisoGFWEESjzECtY';
const ADDR_TWO = '5FjbQAHSptRU4Q7Py6zDsRnE7UkpZikJVg5nm1tmu3WuZXKd';
@@ -31,7 +31,7 @@ describe('formatResult', () => {
})
)
};
api = createApi(provider);
api = new Api(provider);
});
it('encodes key (with params), decoding response', () => {
+3 -3
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import createApi from './index';
import Api from './index';
describe('Api', () => {
let api;
@@ -16,7 +16,7 @@ describe('Api', () => {
}
};
sendSpy = jest.spyOn(provider, 'send');
api = createApi(provider);
api = new Api(provider);
});
afterEach(() => {
@@ -25,7 +25,7 @@ describe('Api', () => {
it('requires a provider with a send method', () => {
expect(
() => createApi({})
() => new Api({})
).toThrow(/Expected Provider/);
});
+16 -9
View File
@@ -2,22 +2,29 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Interface$Sections } from '@polkadot/jsonrpc/types';
import { ProviderInterface } from '@polkadot/api-provider/types';
import { ApiInterface } from './types';
import { ApiInterface, ApiInterface$Section } from './types';
import interfaces from '@polkadot/jsonrpc/index';
import assert from '@polkadot/util/assert';
import isFunction from '@polkadot/util/is/function';
import createInterface from './create/interface';
export default function api (provider: ProviderInterface): ApiInterface {
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
export default class Api implements ApiInterface {
private _provider: ProviderInterface;
readonly author: ApiInterface$Section;
readonly chain: ApiInterface$Section;
readonly state: ApiInterface$Section;
readonly system: ApiInterface$Section;
return Object.keys(interfaces).reduce((exposed, type) => {
exposed[type as Interface$Sections] = createInterface(provider, type as Interface$Sections);
constructor (provider: ProviderInterface) {
assert(provider && isFunction(provider.send), 'Expected Provider to API create');
return exposed;
}, {} as ApiInterface);
this._provider = provider;
this.author = createInterface(this._provider, 'author');
this.chain = createInterface(this._provider, 'chain');
this.state = createInterface(this._provider, 'state');
this.system = createInterface(this._provider, 'system');
}
}
+1 -1
View File
@@ -16,5 +16,5 @@ export type ApiInterface$Section = {
};
export type ApiInterface = {
[key in keyof Interfaces]: ApiInterface$Section
readonly [key in keyof Interfaces]: ApiInterface$Section
}
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/extrinsics",
"version": "0.28.35",
"version": "0.29.0",
"main": "index.js",
"repository": "https://github.com/polkadot-js/api.git",
"author": "Jaco Greeff <jacogr@gmail.com>",
@@ -11,8 +11,8 @@
"license": "ISC",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/params": "^0.28.35",
"@polkadot/primitives": "^0.28.35",
"@polkadot/params": "^0.29.0",
"@polkadot/primitives": "^0.29.0",
"@polkadot/util": "^0.30.3"
}
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/jsonrpc",
"version": "0.28.35",
"version": "0.29.0",
"description": "Method definitions for the Polkadot RPC layer",
"main": "index.js",
"engines": {
@@ -29,7 +29,7 @@
},
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-jsonrpc#readme",
"dependencies": {
"@polkadot/params": "^0.28.35",
"@polkadot/params": "^0.29.0",
"babel-runtime": "^6.26.0"
}
}
+4 -4
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/params",
"version": "0.28.35",
"version": "0.29.0",
"description": "Type defintions for parameters as passed in calls",
"main": "index.js",
"engines": {
@@ -29,9 +29,9 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-params#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/extrinsics": "^0.28.35",
"@polkadot/params": "^0.28.35",
"@polkadot/primitives": "^0.28.35",
"@polkadot/extrinsics": "^0.29.0",
"@polkadot/params": "^0.29.0",
"@polkadot/primitives": "^0.29.0",
"@polkadot/util": "^0.30.3",
"@polkadot/util-keyring": "^0.30.3"
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/primitives",
"version": "0.28.35",
"version": "0.29.0",
"description": "Type defintions for the Polkadot network",
"main": "index.js",
"engines": {
+3 -3
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/storage",
"version": "0.28.35",
"version": "0.29.0",
"description": "Generic wrapper wrapper around state storage",
"main": "index.js",
"engines": {
@@ -30,8 +30,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-storage#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/params": "^0.28.35",
"@polkadot/primitives": "^0.28.35",
"@polkadot/params": "^0.29.0",
"@polkadot/primitives": "^0.29.0",
"@polkadot/util": "^0.30.3",
"@polkadot/util-crypto": "^0.30.3",
"@polkadot/util-keyring": "^0.30.3"
+3 -3
View File
@@ -1225,9 +1225,9 @@
"@polkadot/util-crypto" "^0.30.3"
"@polkadot/util-rlp" "^0.30.3"
"@polkadot/ts@^0.1.27":
version "0.1.27"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.27.tgz#88ddd9e90f4c5c98996ecd433670839eaf530d73"
"@polkadot/ts@^0.1.28":
version "0.1.28"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.28.tgz#c3f2f4ae3864447a2938765d8fae953ed71aaa5e"
"@polkadot/util-crypto@^0.30.3":
version "0.30.3"