Skip to content

Commit

Permalink
Initial add
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Nov 18, 2017
0 parents commit b0c7e3a
Show file tree
Hide file tree
Showing 49 changed files with 1,594 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"presets": [
"env",
"stage-0"
],
"plugins": [
"transform-flow-strip-types",
"transform-class-properties",
"transform-object-rest-spread",
"transform-runtime"
]
}
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_name: travis-pro
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true
[*]
indent_style=space
indent_size=2
tab_width=2
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=120
insert_final_newline=true
35 changes: 35 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"env": {
"browser": true,
"mocha": true,
"node": true
},
"extends": [
"semistandard",
"plugin:flowtype/recommended"
],
"globals": {
"expect": true
},
"parser": "babel-eslint",
"plugins": [
"flowtype"
],
"rules": {
"curly": ["error", "all"],
"newline-after-var": ["error", "always"],
"no-alert": "error",
"no-debugger": "error",
"no-duplicate-imports": ["error", {
"includeExports": true
}],
"object-curly-spacing": ["error", "always"],
"object-property-newline": 0,
"one-var-declaration-per-line": ["error", "always"],
"padded-blocks": ["error", {
"blocks": "never",
"classes": "never",
"switches": "never"
}]
}
}
10 changes: 10 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[ignore]

[include]

[libs]

[options]
esproposal.decorators=ignore
module.ignore_non_literal_requires=true
unsafe.enable_getters_and_setters=true
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage/
lib/
node_modules/
tmp/
4 changes: 4 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
instrumentation:
root: src
extensions:
- .js
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
coverage
scripts
src
test
tmp
.babelrc
.coveralls.yml
.editorconfig
.eslintrc.json
.flowconfig
.gitignore
.istanbul.yml
.travis.yml
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "8"
script: ./scripts/travis.sh
6 changes: 6 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ISC License (ISC)
Copyright 2017 Jaco Greeff

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.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Polkadot JavaScript API

[![Build Status](https://travis-ci.org/polkadot-js/api.svg?branch=master)](https://travis-ci.org/polkadot-js/api)
[![Coverage Status](https://coveralls.io/repos/github/polkadot-js/api/badge.svg?branch=master)](https://coveralls.io/github/polkadot-js/api?branch=master)
[![Dependency Status](https://david-dm.org/polkadot-js/api.svg)](https://david-dm.org/polkadot-js/api)
[![devDependency Status](https://david-dm.org/polkadot-js/api/dev-status.svg)](https://david-dm.org/polkadot-js/api#info=devDependencies)

## Introduction

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.

## Usage

Installation -

```
npm install --save @polkadot/api
```

Initialisation -

```
import Api from '@polkadot/api';
const provider = new Api.HttpProvider('http://127.0.0.1:9933');
const api = new Api(provider);
```

Making calls -

```
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/jsonrpc](https://github.com/polkadot-js/jsonrpc) repository.
75 changes: 75 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "@polkadot/api",
"version": "0.2.0",
"description": "A JavaScript wrapper for the Polkadot JsonRPC interface",
"main": "lib/index.js",
"keywords": [
"Polkadot",
"JsonRPC"
],
"author": "Jaco Greeff <[email protected]>",
"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#readme",
"scripts": {
"build": "npm run build:js",
"build:js": "rimraf lib && babel src --out-dir lib --ignore *.spec.js,types.js",
"check": "npm run check:flow && npm run check:lint",
"check:flow": "flow check",
"check:lint": "eslint src",
"ci:makeshift": "makeshift",
"ci:coveralls": "coveralls < coverage/lcov.info",
"test": "cross-env NODE_ENV=test babel-istanbul cover _mocha 'src/*.spec.js' 'src/**/*.spec.js'"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"babel-istanbul": "^0.12.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow": "^6.23.0",
"babel-preset-stage-0": "^6.24.1",
"chai": "^4.1.2",
"coveralls": "^3.0.0",
"cross-env": "^5.1.1",
"eslint": "^4.11.0",
"eslint-config-semistandard": "^11.0.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-flowtype": "^2.39.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"flow-bin": "^0.59.0",
"makeshift": "^1.1.0",
"mocha": "^4.0.1",
"mock-socket": "^7.1.0",
"nock": "^9.1.0",
"rimraf": "^2.6.2",
"sinon": "^4.1.2",
"sinon-chai": "^2.14.0"
},
"dependencies": {
"@polkadot/jsonrpc": "^0.2.0",
"babel-runtime": "^6.26.0",
"isomorphic-fetch": "^2.2.1",
"ws": "^3.3.1"
}
}
56 changes: 56 additions & 0 deletions scripts/travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/bin/bash
# ISC, Copyright 2017 Jaco Greeff

set -e

function setupGit () {
REPO=$1

echo "Setting up GitHub config for $REPO"

git config push.default simple
git config merge.ours.driver true
git config user.name "Travis CI"
git config user.email "$COMMIT_AUTHOR_EMAIL"
git remote set-url origin https://${GH_TOKEN}@github.com/${REPO}.git > /dev/null 2>&1
}

echo "Running code checks & build"

npm run check
npm run build
npm run test
npm run ci:coveralls

# Pull requests and commits to other branches shouldn't try to deploy, just build to verify
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "master" ]; then
echo "Branch check completed"

exit 0
fi

echo "Setting up GitHub config"

setupGit ${TRAVIS_REPO_SLUG}

if [ -n "$(git status --untracked-files=no --porcelain)" ]; then
echo "Adding build artifacts"

git add .
git commit -m "[CI Skip] Build artifacts"
fi

echo "Publishing to npm"

npm run ci:makeshift
npm --no-git-tag-version version
npm version patch -m "[CI Skip] Version bump"
npm publish

echo "Final push to GitHub"

git push --quiet origin HEAD:refs/heads/$TRAVIS_BRANCH > /dev/null 2>&1

echo "Release completed"

exit 0
71 changes: 71 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

import type { InterfaceDefinition } from '@polkadot/jsonrpc/src/types';
import type { ApiInterface } from './types';
import type { ProviderInterface } from './provider/types';

const interfaces = require('@polkadot/jsonrpc');
const { callSignature } = require('@polkadot/jsonrpc/lib/util');
const { formatInputs, formatOutput } = require('./format');
const { HttpProvider } = require('./provider');
const { isFunction } = require('./util/is');

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(`${callSignature(rpcName, inputs, output)}:: ${error.message}`);
}
};

return container;
}, {});
}

static HttpProvider = HttpProvider;
};
Loading

0 comments on commit b0c7e3a

Please sign in to comment.