Update with flow lint (#46)

* Flow lint warning fixes

* Flow updates fro new flow-bin
This commit is contained in:
Jaco Greeff
2017-12-30 21:54:22 +01:00
committed by GitHub
parent 800f869c9f
commit 4053064288
17 changed files with 504 additions and 374 deletions
+4 -1
View File
@@ -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'
+1 -1
View File
@@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.13.5",
"@polkadot/dev": "^0.14.6",
"lerna": "^2.5.1"
}
}
+2 -2
View File
@@ -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"
}
}
+1 -1
View File
@@ -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;
};
+3 -4
View File
@@ -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');
@@ -12,7 +11,7 @@ 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,
@@ -20,7 +19,7 @@ const formatters: { [FormatInputType]: FormatterFunction } = {
'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);
+3 -4
View File
@@ -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);
};
+2 -1
View File
@@ -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;
+15 -6
View File
@@ -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)) {
@@ -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]);
});
}
+1 -1
View File
@@ -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"
+2 -1
View File
@@ -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,
+4 -3
View File
@@ -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');
@@ -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',
@@ -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;
}
+3 -3
View File
@@ -8,7 +8,7 @@ export type JsonRpcObject = {
export type JsonRpcRequest = JsonRpcObject & {
method: string;
params: Array<any>;
params: Array<mixed>;
};
export type JsonRpcResponseBase = {
@@ -16,12 +16,12 @@ export type JsonRpcResponseBase = {
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>;
}
+12 -11
View File
@@ -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');
@@ -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;
}
@@ -67,11 +64,14 @@ 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);
@@ -79,8 +79,9 @@ module.exports = class WsProvider extends JsonRpcCoder implements ProviderInterf
});
}
_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) {
@@ -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 {
+1 -1
View File
@@ -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"
}
}
+9 -7
View File
@@ -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');
@@ -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');
@@ -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
@@ -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`);
+6 -2
View File
@@ -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;
}
+435 -325
View File
File diff suppressed because it is too large Load Diff