Move to lerna repo (#19)
* Pull api-* packages into lerna repo * Bootstrap with hoist * Update jest alias config * Add api-jsonrpc * Pull in jsonrpc * Update badges * Don't hoist, use workspaces * .eslintrc to root * Cleanups, build from root * Shorthand commands * Update to @polkadot/dev 0.9.1 * Update description
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
ISC License (ISC)
|
||||
|
||||
Copyright 2017 Jaco Greeff <jacogr@gmail.com>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
@@ -0,0 +1,37 @@
|
||||
[](https://david-dm.org/polkadot-js/api?path=packages/api)
|
||||
[](https://david-dm.org/polkadot-js/api?path=packages/api#info=devDependencies)
|
||||
|
||||
# @polkadot/api
|
||||
|
||||
Warning - currently this does not actually do all that much, it is an attempt to put into code some thoughts about how to maintain the endpoints. This library provides a clean wrapper around all the methods exposed by a Polkadot network client. Methods are auto-generated for the [JsonRPC interface definitions](https://github.com/polkadot-js/api/packages/api-jsonrpc).
|
||||
|
||||
## Usage
|
||||
|
||||
Installation -
|
||||
|
||||
```
|
||||
npm install --save @polkadot/api
|
||||
```
|
||||
|
||||
Initialisation -
|
||||
|
||||
```js
|
||||
import Api from '@polkadot/api';
|
||||
import HttpProvider from '@polkadot/api-provider/http';
|
||||
|
||||
const provider = new HttpProvider('http://127.0.0.1:9933');
|
||||
const api = new Api(provider);
|
||||
```
|
||||
|
||||
Making calls -
|
||||
|
||||
```js
|
||||
api.chain
|
||||
.getHeader('0x1234567890')
|
||||
.then((header) => console.log(header))
|
||||
.catch((error) => console.error(error));
|
||||
```
|
||||
|
||||
## Available methods
|
||||
|
||||
For a list of currently exposed methods, see the [@polkadot/api-jsonrpc](https://github.com/polkadot-js/api/packages/api-jsonrpc#readme) repository.
|
||||
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"name": "@polkadot/api",
|
||||
"version": "0.5.0",
|
||||
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
"Polkadot",
|
||||
"JsonRPC"
|
||||
],
|
||||
"author": "Jaco Greeff <jacogr@gmail.com>",
|
||||
"license": "ISC",
|
||||
"engines": {
|
||||
"node": ">=6.4"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/polkadot-js/api.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/polkadot-js/api/issues"
|
||||
},
|
||||
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-babel",
|
||||
"check": "eslint src && flow check",
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"devDependencies": {
|
||||
"@polkadot/dev": "^0.9.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@polkadot/api-format": "^0.5.0",
|
||||
"@polkadot/api-jsonrpc": "^0.5.0",
|
||||
"@polkadot/api-provider": "^0.5.0",
|
||||
"@polkadot/util": "^0.5.15",
|
||||
"babel-runtime": "^6.26.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
import type { ProviderInterface } from '@polkadot/api-provider/types';
|
||||
import type { InterfaceDefinition } from '@polkadot/api-jsonrpc/types';
|
||||
import type { ApiInterface } from './types';
|
||||
|
||||
const { formatInputs, formatOutput } = require('@polkadot/api-format');
|
||||
const interfaces = require('@polkadot/api-jsonrpc');
|
||||
const isFunction = require('@polkadot/util/is/function');
|
||||
const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');
|
||||
|
||||
module.exports = class Api implements ApiInterface {
|
||||
_provider: ProviderInterface;
|
||||
_chainInterface: any = null;
|
||||
_stateInterface: any = null;
|
||||
|
||||
constructor (provider: ProviderInterface) {
|
||||
if (!provider) {
|
||||
throw new Error('Instantiate the Api with `new Api(new Provider(...))`');
|
||||
}
|
||||
|
||||
if (!isFunction(provider.send)) {
|
||||
throw new Error('Supplied provider does not expose send method');
|
||||
}
|
||||
|
||||
this._provider = provider;
|
||||
|
||||
this._chainInterface = this._createInterface(interfaces, 'chain');
|
||||
this._stateInterface = this._createInterface(interfaces, 'state');
|
||||
}
|
||||
|
||||
get chain (): any {
|
||||
return this._chainInterface;
|
||||
}
|
||||
|
||||
get state (): any {
|
||||
return this._stateInterface;
|
||||
}
|
||||
|
||||
_createInterface (definitions: { [string]: InterfaceDefinition }, section: string): any {
|
||||
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<any>): any => {
|
||||
try {
|
||||
if (inputs.length !== _params.length) {
|
||||
throw new Error(`${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 Error(`${jsonrpcSignature(rpcName, inputs, output)}:: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
return container;
|
||||
}, {});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,111 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
/* global jest */
|
||||
|
||||
const isFunction = require('@polkadot/util/is/function');
|
||||
|
||||
const Api = require('./api');
|
||||
|
||||
describe('Api', () => {
|
||||
let api;
|
||||
let provider;
|
||||
let sendSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
provider = {
|
||||
send: (method, params) => {
|
||||
return Promise.resolve(params[0]);
|
||||
}
|
||||
};
|
||||
sendSpy = jest.spyOn(provider, 'send');
|
||||
api = new Api(provider);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
sendSpy.mockRestore();
|
||||
});
|
||||
|
||||
describe('constructor', () => {
|
||||
it('requires a provider', () => {
|
||||
expect(
|
||||
() => new Api()
|
||||
).toThrow(/Instantiate the Api/);
|
||||
});
|
||||
|
||||
it('requires a provider with a send method', () => {
|
||||
expect(
|
||||
() => new Api({})
|
||||
).toThrow(/does not expose send/);
|
||||
});
|
||||
|
||||
it('sets up the chain interface', () => {
|
||||
expect(api.chain).toBeDefined();
|
||||
});
|
||||
|
||||
it('sets up the state interface', () => {
|
||||
expect(api.state).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
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('calls the provider with the correct parameters', () => {
|
||||
return container
|
||||
.blah('0x123')
|
||||
.then(() => {
|
||||
expect(provider.send).toHaveBeenCalledWith('test_blah', [
|
||||
'0x0000000000000000000000000000000000000123'
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const Api = require('./api');
|
||||
|
||||
module.exports = Api;
|
||||
@@ -0,0 +1,9 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
const Api = require('./index');
|
||||
|
||||
describe('index', () => {
|
||||
it('exports default Api', () => {
|
||||
expect(Api).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
export interface ApiInterface {
|
||||
+chain: any;
|
||||
+state: any;
|
||||
}
|
||||
Reference in New Issue
Block a user