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
5 changes: 4 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
[libs]
./node_modules/@polkadot/dev/flow-typed

[lints]
all=warn

[options]
unsafe.enable_getters_and_setters=true
include_warnings=true
module.name_mapper='^@polkadot/api-\(format\|jsonrpc\|provider\)\(.*\)$' -> '<PROJECT_ROOT>/packages/api-\1/src\2'
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.13.5",
"@polkadot/dev": "^0.14.6",
"lerna": "^2.5.1"
}
}
4 changes: 2 additions & 2 deletions packages/api-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"@polkadot/api-jsonrpc": "^0.6.8"
},
"dependencies": {
"@polkadot/primitives-json": "^0.4.1",
"@polkadot/util": "^0.10.1",
"@polkadot/primitives-json": "^0.4.6",
"@polkadot/util": "^0.11.1",
"babel-runtime": "^6.26.0"
}
}
2 changes: 1 addition & 1 deletion packages/api-format/src/echo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

module.exports = function echo (value: any): any {
module.exports = function echo (value: mixed): mixed {
return value;
};
7 changes: 3 additions & 4 deletions packages/api-format/src/input.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

import type { FormatInputType, InterfaceInputType } from '@polkadot/api-jsonrpc/types';
import type { FormatterFunction } from './types';
import type { InterfaceInputType } from '@polkadot/api-jsonrpc/types';

const accountIdEncode = require('@polkadot/primitives-json/accountId/encode');
const h256Encode = require('@polkadot/primitives-json/h256/encode');
Expand All @@ -12,15 +11,15 @@ const headerHashEncode = require('@polkadot/primitives-json/headerHash/encode');
const echo = require('./echo');
const util = require('./util');

const formatters: { [FormatInputType]: FormatterFunction } = {
const formatters = {
'Address': accountIdEncode,
'CallData': hashEncode,
'H256': h256Encode,
'HeaderHash': headerHashEncode,
'String': echo
};

module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<any>): Array<any> {
module.exports = function formatInputs (inputs: Array<InterfaceInputType>, values: Array<mixed>): Array<mixed> {
const types = inputs.map(({ type }) => type);

return util.formatArray(formatters, types, values);
Expand Down
7 changes: 3 additions & 4 deletions packages/api-format/src/output.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

import type { FormatOutputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
import type { FormatterFunction } from './types';
import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';

const headerDecode = require('@polkadot/primitives-json/header/decode');

const echo = require('./echo');
const util = require('./util');

const formatters: { [FormatOutputType]: FormatterFunction } = {
const formatters = {
'Header': headerDecode,
'OutData': echo,
'StorageData': echo
};

module.exports = function formatOutput (output: InterfaceOutputType, value: any): any {
module.exports = function formatOutput (output: InterfaceOutputType, value: mixed): mixed {
return util.format(formatters, output.type, value);
};
3 changes: 2 additions & 1 deletion packages/api-format/src/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

export type FormatterFunction = (value: any) => any;
// flowlint-next-line unclear-type:off
export type FormatterFunction = (value: any) => mixed;
21 changes: 15 additions & 6 deletions packages/api-format/src/util.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

import type { FormatInputType, FormatOutputType } from '@polkadot/api-jsonrpc/types';
import type { FormatterFunction } from './types';

type FormattersFunctionType = FormatInputType | FormatOutputType;
type FormattersFunctionMap = {
[FormattersFunctionType]: FormatterFunction
};

const isUndefined = require('@polkadot/util/is/undefined');
const echo = require('./echo');

const arrayTypeRegex = /\[\]$/;

function formatSingleType (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
function formatSingleType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: mixed): mixed {
const formatter: FormatterFunction = formatters[type];

if (isUndefined(formatter)) {
Expand All @@ -20,28 +26,31 @@ function formatSingleType (formatters: { [any]: FormatterFunction }, type: strin
try {
return formatter(value);
} catch (error) {
// $FlowFixMe just let JS coerce all it wants to
throw new Error(`Error formatting '${value}' as '${type}': ${error.message}`);
}
}

function formatArrayType (formatters: { [any]: FormatterFunction }, type: string, value: Array<any>): any {
type = type.replace(arrayTypeRegex, '');
function formatArrayType (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: Array<mixed>): mixed {
// flowlint-next-line unclear-type:off
type = ((type.replace(arrayTypeRegex, ''): any): FormattersFunctionType);

return value.map((value) => {
return formatSingleType(formatters, type, value);
});
}

function format (formatters: { [any]: FormatterFunction }, type: string, value: any): any {
function format (formatters: FormattersFunctionMap, type: FormattersFunctionType, value: mixed | Array<mixed>): mixed {
if (arrayTypeRegex.test(type)) {
// $FlowFixMe array type
return formatArrayType(formatters, type, value);
}

return formatSingleType(formatters, type, value);
}

function formatArray (formatters: { [any]: FormatterFunction }, types: Array<string>, values: Array<any>): Array<any> {
return types.map((type, index) => {
function formatArray (formatters: FormattersFunctionMap, types: Array<FormattersFunctionType>, values: Array<mixed>): Array<mixed> {
return types.map((type, index): mixed => {
return format(formatters, type, values[index]);
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"nock": "^9.1.0"
},
"dependencies": {
"@polkadot/util": "^0.10.1",
"@polkadot/util": "^0.11.1",
"babel-runtime": "^6.26.0",
"isomorphic-fetch": "^2.2.1",
"websocket": "^1.0.25"
Expand Down
3 changes: 2 additions & 1 deletion packages/api-provider/src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ module.exports = class HttpProvider extends JsonRpcCoder implements ProviderInte
this._endpoint = endpoint;
}

// flowlint-next-line unsafe-getters-setters:off
get isConnected (): boolean {
return true;
}

async send (method: string, params: Array<any>): Promise<any> {
async send (method: string, params: Array<mixed>): Promise<mixed> {
const body = this.encodeJson(method, params);
const response = await fetch(this._endpoint, {
body,
Expand Down
7 changes: 4 additions & 3 deletions packages/api-provider/src/jsonRpcCoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const isUndefined = require('@polkadot/util/is/undefined');
module.exports = class JsonRpcCoder {
_id: number = 0;

decodeResponse (response: JsonRpcResponse): any {
decodeResponse (response: JsonRpcResponse): mixed {
assert(response, 'Empty response object received');

assert(response.jsonrpc === '2.0', 'Invalid jsonrpc field in decoded object');
Expand All @@ -27,7 +27,7 @@ module.exports = class JsonRpcCoder {
return response.result;
}

encodeObject (method: string, params: Array<any>): JsonRpcRequest {
encodeObject (method: string, params: Array<mixed>): JsonRpcRequest {
return {
id: ++this._id,
jsonrpc: '2.0',
Expand All @@ -36,12 +36,13 @@ module.exports = class JsonRpcCoder {
};
}

encodeJson (method: string, params: Array<any>): string {
encodeJson (method: string, params: Array<mixed>): string {
return JSON.stringify(
this.encodeObject(method, params)
);
}

// flowlint-next-line unsafe-getters-setters:off
get id (): number {
return this._id;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/api-provider/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ export type JsonRpcObject = {

export type JsonRpcRequest = JsonRpcObject & {
method: string;
params: Array<any>;
params: Array<mixed>;
};

export type JsonRpcResponseBase = {
error?: {
code: number,
message: string
};
result?: any;
result?: mixed;
}

export type JsonRpcResponse = JsonRpcObject & JsonRpcResponseBase;

export interface ProviderInterface {
+isConnected: boolean;
send (method: string, params: Array<any>): Promise<any>;
send (method: string, params: Array<mixed>): Promise<mixed>;
}
23 changes: 12 additions & 11 deletions packages/api-provider/src/ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import type { JsonRpcResponse, ProviderInterface } from '../types';

type AwaitingType = {
callback: (error: ?Error, result: any) => void
};

type WsMessageType = {
data: any
callback: (error: ?Error, result: mixed) => void
};

require('./polyfill');
Expand Down Expand Up @@ -37,6 +33,7 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
}
}

// flowlint-next-line unsafe-getters-setters:off
get isConnected (): boolean {
return this._isConnected;
}
Expand Down Expand Up @@ -67,20 +64,24 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
// console.log('connected to', this._endpoint);
this._isConnected = true;

Object.keys(this._queued).forEach((id: string) => {
Object.keys(this._queued).forEach((id) => {
try {
this._websocket.send(
// flowlint-next-line unclear-type:off
this._queued[((id: any): number)]
);

// flowlint-next-line unclear-type:off
delete this._queued[((id: any): number)];
} catch (error) {
console.error(error);
}
});
}

_onMessage = (message: WsMessageType) => {
const response: JsonRpcResponse = JSON.parse(message.data);
_onMessage = (message: MessageEvent): void => {
// flowlint-next-line unclear-type:off
const response: JsonRpcResponse = JSON.parse(((message.data: any): string));
const handler = this._handlers[response.id];

if (!handler) {
Expand All @@ -99,13 +100,13 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
delete this._handlers[response.id];
}

send (method: string, params: Array<any>): Promise<any> {
return new Promise((resolve, reject) => {
send (method: string, params: Array<mixed>): Promise<mixed> {
return new Promise((resolve, reject): void => {
try {
const json = this.encodeJson(method, params);

this._handlers[this.id] = {
callback: (error: ?Error, result: any) => {
callback: (error: ?Error, result: mixed) => {
if (error) {
reject(error);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@polkadot/api-format": "^0.6.8",
"@polkadot/api-jsonrpc": "^0.6.8",
"@polkadot/api-provider": "^0.6.8",
"@polkadot/util": "^0.10.1",
"@polkadot/util": "^0.11.1",
"babel-runtime": "^6.26.0"
}
}
16 changes: 9 additions & 7 deletions packages/api/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

const { formatInputs, formatOutput } = require('@polkadot/api-format');
const interfaces = require('@polkadot/api-jsonrpc');
Expand All @@ -14,8 +14,8 @@ const jsonrpcSignature = require('@polkadot/util/jsonrpc/signature');

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

constructor (provider: ProviderInterface) {
assert(provider && isFunction(provider.send), 'Expected Provider');
Expand All @@ -26,15 +26,17 @@ module.exports = class Api implements ApiInterface {
this._stateInterface = this._createInterface(interfaces, 'state');
}

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

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

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

return Object
Expand All @@ -43,7 +45,7 @@ module.exports = class Api implements ApiInterface {
const { inputs, output } = definition.methods[method];
const rpcName = `${section}_${method}`;

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

Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow

export type ApiInterface$Section = {
[string]: () => Promise<mixed>
};

export interface ApiInterface {
+chain: any;
+state: any;
+chain: ApiInterface$Section;
+state: ApiInterface$Section;
}
Loading