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:
Jaco Greeff
2017-11-28 18:03:48 +01:00
committed by GitHub
parent bf6880d5b0
commit e8ccc9aa79
64 changed files with 2378 additions and 136 deletions
+15
View File
@@ -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.
+14
View File
@@ -0,0 +1,14 @@
[![Dependency Status](https://david-dm.org/polkadot-js/api.svg?path=packages/api-format)](https://david-dm.org/polkadot-js/api?path=packages/api-format)
[![devDependency Status](https://david-dm.org/polkadot-js/api/dev-status.svg?path=packages/api-format)](https://david-dm.org/polkadot-js/api?path=packages/api-format#info=devDependencies)
# @polkadot/api-format
Formatters that are used by the application API interface, taking care of transparently formatting parameters (inputs) and the results (output) for requests made over the client RPC interfaces.
## Usage
Installation -
```
npm install --save @polkadot/api-format
```
+40
View File
@@ -0,0 +1,40 @@
{
"name": "@polkadot/api-format",
"version": "0.5.0",
"description": "Input/Output formatters for JsonRPC exchange",
"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-format#readme",
"scripts": {
"build": "polkadot-dev-build-babel",
"check": "eslint src && flow check",
"test": "echo \"Tests only available from root wrapper\""
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.5.0",
"@polkadot/dev": "^0.9.1"
},
"dependencies": {
"@polkadot/util": "^0.5.15",
"babel-runtime": "^6.26.0"
}
}
+10
View File
@@ -0,0 +1,10 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const formatInputs = require('./input');
const formatOutput = require('./output');
module.exports = {
formatInputs,
formatOutput
};
+10
View File
@@ -0,0 +1,10 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const { formatH160 } = require('./hex');
// TODO: Currently the format assumes 160-bit values (like Ethereum)
// this will probably change along the way as things get firmed up
module.exports = function format (value: ?string): string {
return formatH160(value);
};
@@ -0,0 +1,13 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./address');
describe('input/address', () => {
describe('format', () => {
it('pads to H160 value', () => {
expect(
format('0x1234567890')
).toEqual('0x0000000000000000000000000000001234567890');
});
});
});
+33
View File
@@ -0,0 +1,33 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const { hexAddPrefix, hexHasPrefix, hexStripPrefix } = require('@polkadot/util/hex');
const H64_ZERO: string = '00000000000000000000000000000000';
const H128_ZERO: string = `${H64_ZERO}${H64_ZERO}`;
const H256_ZERO: string = `${H128_ZERO}${H128_ZERO}`;
// const H512_ZERO: string = `${H256_ZERO}${H256_ZERO}`;
function leftHexPad (value: ?string, bitLength: number): string {
const length = 2 * bitLength / 8;
if (hexHasPrefix(value)) {
value = hexStripPrefix(value);
}
return hexAddPrefix(`${H256_ZERO}${value || ''}`.slice(-length));
}
function formatH160 (value: ?string): string {
return leftHexPad(value, 160);
}
function formatH256 (value: ?string): string {
return leftHexPad(value, 256);
}
module.exports = {
leftHexPad,
formatH160,
formatH256
};
+53
View File
@@ -0,0 +1,53 @@
// ISC, Copyright 2017 Jaco Greeff
const { leftHexPad, formatH160, formatH256 } = require('./hex');
describe('input/hex', () => {
describe('leftHexPad', () => {
it('padds to the required length', () => {
expect(
leftHexPad('0x123', 16)
).toEqual('0x0123');
});
it('padds to the required length (no prefix)', () => {
expect(
leftHexPad('123', 16)
).toEqual('0x0123');
});
it('pads null values correctly', () => {
expect(
leftHexPad(null, 16)
).toEqual('0x0000');
});
});
describe('formatH160', () => {
it('pads to 40 bytes', () => {
expect(
formatH160('0x1234567890')
).toEqual('0x0000000000000000000000000000001234567890');
});
it('pads null values correctly', () => {
expect(
formatH160(null)
).toMatch(/^0x(00){20,}$/);
});
});
describe('formatH256', () => {
it('pads to 64 bytes', () => {
expect(
formatH256('0x1234567890')
).toEqual('0x0000000000000000000000000000000000000000000000000000001234567890');
});
it('pads null values correctly', () => {
expect(
formatH256(null)
).toMatch(/^0x(00){32,}$/);
});
});
});
+24
View File
@@ -0,0 +1,24 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatInputType, InterfaceInputType } from '@polkadot/api-jsonrpc/types';
import type { FormatterFunction } from '../types';
const formatAddress = require('./address');
const { formatH256 } = require('./hex');
const formatNoop = require('../noop');
const util = require('../util');
const formatters: { [FormatInputType]: FormatterFunction } = {
'Address': formatAddress,
'CallData': formatNoop,
'H256': formatH256,
'HeaderHash': formatH256,
'String': formatNoop
};
module.exports = function format (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
const types = inputs.map(({ type }) => type);
return util.formatArray(formatters, types, values);
};
@@ -0,0 +1,22 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./index');
describe('input', () => {
describe('format', () => {
it('formats each value in an array', () => {
expect(
format(
[
{ name: 'foo', type: 'Address' },
{ name: 'bar', type: 'H256' }
],
['0x1234', '0xabcd']
)
).toEqual([
'0x0000000000000000000000000000000000001234',
'0x000000000000000000000000000000000000000000000000000000000000abcd'
]);
});
});
});
+6
View File
@@ -0,0 +1,6 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
module.exports = function format (value: any): any {
return value;
};
+13
View File
@@ -0,0 +1,13 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./noop');
describe('noop', () => {
it('returns input value as output value', () => {
const input = { 'some': 'object' };
expect(
format(input)
).toEqual(input);
});
});
+18
View File
@@ -0,0 +1,18 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatOutputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
import type { FormatterFunction } from '../types';
const formatNoop = require('../noop');
const util = require('../util');
const formatters: { [FormatOutputType]: FormatterFunction } = {
'Header': formatNoop,
'OutData': formatNoop,
'StorageData': formatNoop
};
module.exports = function format (output: InterfaceOutputType, value: any): any {
return util.format(formatters, output.type, value);
};
@@ -0,0 +1,13 @@
// ISC, Copyright 2017 Jaco Greeff
const format = require('./index');
describe('output', () => {
describe('format', () => {
it('formats the value', () => {
expect(
format('Header', 'test')
).toEqual('test');
});
});
});
+4
View File
@@ -0,0 +1,4 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
export type FormatterFunction = (value: any) => any;
+49
View File
@@ -0,0 +1,49 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
import type { FormatterFunction } from './types';
const { isUndefined } = require('@polkadot/util/is');
const formatNoop = require('./noop');
const arrayTypeRegex = /\[\]$/;
function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
const isArrayType: boolean = arrayTypeRegex.test(type);
const formatter: FormatterFunction = isArrayType
? formatters[type.replace(arrayTypeRegex, '')]
: formatters[type];
if (isUndefined(formatter)) {
console.warn(`Unable to find default formatter for '${type}', falling back to noop`);
return formatNoop(value);
}
if (isArrayType && !Array.isArray(value)) {
throw new Error(`Unable to format non-array '${value}' as '${type}'`);
}
try {
if (isArrayType) {
return (value: Array<any>).map((value) => formatter(value));
} else {
return formatter(value);
}
} catch (error) {
throw new Error(`Error formatting '${value}' as '${type}'': ${error.message}`);
}
}
function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {
return types.map((type, index) => {
const value = values[index];
return format(formatters, type, value);
});
}
module.exports = {
format,
formatArray
};
+86
View File
@@ -0,0 +1,86 @@
// ISC, Copyright 2017 Jaco Greeff
/* global jest */
const { format, formatArray } = require('./util');
const formatNoop = require('./noop');
describe('util', () => {
let formatters;
let noopSpy;
let warnSpy;
beforeEach(() => {
noopSpy = jest.fn(formatNoop);
warnSpy = jest.spyOn(console, 'warn');
formatters = {
'Address': noopSpy,
'Address[]': noopSpy,
'Exception': () => {
throw new Error('something went wrong');
}
};
});
afterEach(() => {
noopSpy.mockRestore();
warnSpy.mockRestore();
});
describe('format', () => {
it('formats unknown types with a fallback', () => {
expect(
format(formatters, 'Unknown', 'test')
).toEqual('test');
});
it('logs a warning with unknown types', () => {
format(formatters, 'Unknown', 'test');
expect(warnSpy).toHaveBeenCalledWith("Unable to find default formatter for 'Unknown', falling back to noop");
});
it('wraps exceptions with the type', () => {
expect(
() => format(formatters, 'Exception', 'test')
).toThrow(/Error formatting 'test' as 'Exception'/);
});
describe('primitives', () => {
it('formats known types using the supplied formatter', () => {
format(formatters, 'Address', '0xaddress');
expect(noopSpy).toHaveBeenCalledWith('0xaddress');
});
});
describe('primitive[]', () => {
it('throws exception with array type, but non-array value', () => {
expect(
() => format(formatters, 'Address[]', 'not-an-array')
).toThrow(/Unable to format non-array/);
});
it('formats using the primitive type', () => {
format(formatters, 'Address[]', ['0x123', '0x234']);
expect(noopSpy).toHaveBeenCalledWith('0x123');
expect(noopSpy).toHaveBeenCalledWith('0x234');
});
it('returns formatted values as arrays', () => {
expect(
format(formatters, 'Address[]', ['0x123', '0x234'])
).toEqual(['0x123', '0x234']);
});
});
});
describe('formatArray', () => {
it('formats each value in an array', () => {
expect(
formatArray(formatters, ['Unknown', 'Unknown'], ['test', 'blah'])
).toEqual(['test', 'blah']);
});
});
});