Bump dev (w/ eslint 8) (#4070)
* Bump dev (w/ eslint 8) * Additional linting * Additional * More lint fixes * Last batch * Additional * Cleanups
This commit is contained in:
@@ -12,7 +12,7 @@ describe('encodeJson', (): void => {
|
||||
|
||||
it('encodes a valid JsonRPC JSON string', (): void => {
|
||||
expect(
|
||||
coder.encodeJson('method', 'params')
|
||||
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":"params"}');
|
||||
coder.encodeJson('method', ['params'])
|
||||
).toEqual('{"id":1,"jsonrpc":"2.0","method":"method","params":["params"]}');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,7 +46,7 @@ export class RpcCoder {
|
||||
return response.result;
|
||||
}
|
||||
|
||||
public encodeJson (method: string, params: any | any[]): string {
|
||||
public encodeJson (method: string, params: unknown[]): string {
|
||||
return stringify(
|
||||
this.encodeObject(method, params)
|
||||
);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/rpc-provider authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types';
|
||||
import type { JsonRpcResponse, ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '../types';
|
||||
|
||||
import { assert, logger } from '@polkadot/util';
|
||||
import { fetch } from '@polkadot/x-fetch';
|
||||
@@ -103,7 +103,7 @@ export class HttpProvider implements ProviderInterface {
|
||||
/**
|
||||
* @summary Send HTTP POST Request with Body to configured HTTP Endpoint.
|
||||
*/
|
||||
public async send <T = any> (method: string, params: unknown[]): Promise<T> {
|
||||
public async send <T> (method: string, params: unknown[]): Promise<T> {
|
||||
const body = this.#coder.encodeJson(method, params);
|
||||
const response = await fetch(this.#endpoint, {
|
||||
body,
|
||||
@@ -118,8 +118,7 @@ export class HttpProvider implements ProviderInterface {
|
||||
|
||||
assert(response.ok, () => `[${response.status}]: ${response.statusText}`);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const result = await response.json();
|
||||
const result = (await response.json()) as JsonRpcResponse;
|
||||
|
||||
return this.#coder.decodeResponse(result) as T;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/rpc-provider authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Request } from '../../test/mockWs';
|
||||
|
||||
import { mockWs } from '../../test/mockWs';
|
||||
import { Mock } from './../mock/types';
|
||||
import { WsProvider } from './';
|
||||
@@ -10,7 +12,7 @@ const TEST_WS_URL = 'ws://localhost-index.spec.ts:9977';
|
||||
let provider: WsProvider | null;
|
||||
let mock: Mock;
|
||||
|
||||
function createWs (requests: any[], autoConnect = 1000, headers?: Record<string, string>): WsProvider {
|
||||
function createWs (requests: Request[], autoConnect = 1000, headers?: Record<string, string>): WsProvider {
|
||||
mock = mockWs(requests, TEST_WS_URL);
|
||||
provider = new WsProvider(TEST_WS_URL, autoConnect, headers);
|
||||
|
||||
|
||||
@@ -337,7 +337,7 @@ export class WsProvider implements ProviderInterface {
|
||||
|
||||
#emit = (type: ProviderInterfaceEmitted, ...args: unknown[]): void => {
|
||||
this.#eventemitter.emit(type, ...args);
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketClose = (event: CloseEvent): void => {
|
||||
const error = new Error(`disconnected from ${this.#endpoints[this.#endpointIndex]}: ${event.code}:: ${event.reason || getWSErrorString(event.code)}`);
|
||||
@@ -369,12 +369,12 @@ export class WsProvider implements ProviderInterface {
|
||||
});
|
||||
}, this.#autoConnectMs);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketError = (error: Event): void => {
|
||||
l.debug(() => ['socket error', error]);
|
||||
this.#emit('error', error);
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketMessage = (message: MessageEvent<string>): void => {
|
||||
l.debug(() => ['received', message.data]);
|
||||
@@ -384,7 +384,7 @@ export class WsProvider implements ProviderInterface {
|
||||
return isUndefined(response.method)
|
||||
? this.#onSocketMessageResult(response)
|
||||
: this.#onSocketMessageSubscribe(response);
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketMessageResult = (response: JsonRpcResponse): void => {
|
||||
const handler = this.#handlers[response.id];
|
||||
@@ -422,7 +422,7 @@ export class WsProvider implements ProviderInterface {
|
||||
}
|
||||
|
||||
delete this.#handlers[response.id];
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketMessageSubscribe = (response: JsonRpcResponse): void => {
|
||||
const method = ALIASES[response.method as string] || response.method || 'invalid';
|
||||
@@ -448,7 +448,7 @@ export class WsProvider implements ProviderInterface {
|
||||
} catch (error) {
|
||||
handler.callback(error as Error, undefined);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#onSocketOpen = (): boolean => {
|
||||
assert(!isNull(this.#websocket), 'WebSocket cannot be null in onOpen');
|
||||
@@ -461,7 +461,7 @@ export class WsProvider implements ProviderInterface {
|
||||
this.#resubscribe();
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
#resubscribe = (): void => {
|
||||
const subscriptions = this.#subscriptions;
|
||||
@@ -484,5 +484,5 @@ export class WsProvider implements ProviderInterface {
|
||||
l.error(error);
|
||||
}
|
||||
})).catch(l.error);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Constructor } from '@polkadot/types/types';
|
||||
import type { Request } from '../../test/mockWs';
|
||||
|
||||
import { mockWs } from '../../test/mockWs';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
@@ -14,7 +15,7 @@ const TEST_WS_URL = 'ws://localhost-send.spec.ts:9965';
|
||||
let provider: WsProvider | null;
|
||||
let mock: Mock;
|
||||
|
||||
function createMock (requests: any[]): void {
|
||||
function createMock (requests: Request[]): void {
|
||||
mock = mockWs(requests, TEST_WS_URL);
|
||||
}
|
||||
|
||||
@@ -89,7 +90,8 @@ describe('send', (): void => {
|
||||
code: 666,
|
||||
message: 'error'
|
||||
},
|
||||
id: 1
|
||||
id: 1,
|
||||
method: 'something'
|
||||
}]);
|
||||
|
||||
return createWs().then((ws) =>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Constructor } from '@polkadot/types/types';
|
||||
import type { Request } from '../../test/mockWs';
|
||||
|
||||
import { mockWs } from '../../test/mockWs';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
@@ -14,7 +15,7 @@ const TEST_WS_URL = 'ws://localhost-subscribe.test.ts:9933';
|
||||
let provider: WsProvider | null;
|
||||
let mock: Mock;
|
||||
|
||||
function createMock (requests: any[]): void {
|
||||
function createMock (requests: Request[]): void {
|
||||
mock = mockWs(requests, TEST_WS_URL);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Constructor } from '@polkadot/types/types';
|
||||
import type { Request } from '../../test/mockWs';
|
||||
|
||||
import { mockWs } from '../../test/mockWs';
|
||||
import { Global, Mock } from './../mock/types';
|
||||
@@ -14,7 +15,7 @@ const TEST_WS_URL = 'ws://localhost-unsubscribe.test.ts:9933';
|
||||
let provider: WsProvider | null;
|
||||
let mock: Mock;
|
||||
|
||||
function createMock (requests: any): void {
|
||||
function createMock (requests: Request[]): void {
|
||||
mock = mockWs(requests, TEST_WS_URL);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { Server, WebSocket } from 'mock-socket';
|
||||
import { stringify } from '@polkadot/util';
|
||||
|
||||
interface Scope {
|
||||
body: { [index: string]: Record<string, any> };
|
||||
body: { [index: string]: Record<string, unknown> };
|
||||
requests: number;
|
||||
server: Server;
|
||||
done: any;
|
||||
@@ -27,12 +27,22 @@ interface ReplyDef {
|
||||
};
|
||||
}
|
||||
|
||||
interface RpcBase {
|
||||
id: number;
|
||||
jsonrpc: '2.0';
|
||||
}
|
||||
|
||||
type RpcError = RpcBase & ErrorDef;
|
||||
type RpcReply = RpcBase & { result: unknown };
|
||||
|
||||
export type Request = { method: string } & (ErrorDef | ReplyDef);
|
||||
|
||||
global.WebSocket = WebSocket as typeof global.WebSocket;
|
||||
|
||||
export const TEST_WS_URL = 'ws://localhost:9955';
|
||||
|
||||
// should be JSONRPC def return
|
||||
function createError ({ error: { code, message }, id }: ErrorDef): any {
|
||||
function createError ({ error: { code, message }, id }: ErrorDef): RpcError {
|
||||
return {
|
||||
error: {
|
||||
code,
|
||||
@@ -44,7 +54,7 @@ function createError ({ error: { code, message }, id }: ErrorDef): any {
|
||||
}
|
||||
|
||||
// should be JSONRPC def return
|
||||
function createReply ({ id, reply: { result } }: ReplyDef): any {
|
||||
function createReply ({ id, reply: { result } }: ReplyDef): RpcReply {
|
||||
return {
|
||||
id,
|
||||
jsonrpc: '2.0',
|
||||
@@ -53,7 +63,7 @@ function createReply ({ id, reply: { result } }: ReplyDef): any {
|
||||
}
|
||||
|
||||
// scope definition returned
|
||||
export function mockWs (requests: ({ method: string } & ErrorDef)[], wsUrl: string = TEST_WS_URL): Scope {
|
||||
export function mockWs (requests: Request[], wsUrl: string = TEST_WS_URL): Scope {
|
||||
const server = new Server(wsUrl);
|
||||
|
||||
let requestCount = 0;
|
||||
@@ -69,16 +79,13 @@ export function mockWs (requests: ({ method: string } & ErrorDef)[], wsUrl: stri
|
||||
};
|
||||
|
||||
server.on('connection', (socket): void => {
|
||||
// FIXME This whole any mess is a mess
|
||||
socket.on('message', (body: any): void => {
|
||||
const request: any = requests[requestCount];
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
|
||||
const response: any = request.error
|
||||
? createError(request)
|
||||
: createReply(request);
|
||||
socket.on('message', (body): void => {
|
||||
const request = requests[requestCount];
|
||||
const response = (request as ErrorDef).error
|
||||
? createError(request as ErrorDef)
|
||||
: createReply(request as ReplyDef);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
|
||||
scope.body[request.method] = body;
|
||||
scope.body[request.method] = body as unknown as Record<string, unknown>;
|
||||
requestCount++;
|
||||
|
||||
socket.send(stringify(response));
|
||||
|
||||
Reference in New Issue
Block a user