Skip to content

Commit

Permalink
provider types moved back to web3-core-helpers package to not break e…
Browse files Browse the repository at this point in the history
…xisting code and during moving the tests improved
  • Loading branch information
nivida committed Dec 18, 2019
1 parent a46dbcd commit fb1d422
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 173 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 130 additions & 0 deletions packages/web3-core-helpers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import * as net from "net";

/**
* @file index.d.ts
* @author Josh Stevens <joshstevens19@hotmail.co.uk>
Expand Down Expand Up @@ -65,3 +67,131 @@ export class errors {
static InvalidResponse(result: Error): Error;
static ConnectionTimeout(ms: string): Error;
}

export class IpcProviderBase {
constructor(path: string, net: net.Server);

responseCallbacks: any;
notificationCallbacks: any;
connected: boolean;
connection: any;

addDefaultEvents(): void;

supportsSubscriptions(): boolean;

send(
payload: JsonRpcPayload,
callback: (error: Error | null, result?: JsonRpcResponse) => void
): void;

on(type: string, callback: () => void): void;

once(type: string, callback: () => void): void;

removeListener(type: string, callback: () => void): void;

removeAllListeners(type: string): void;

reset(): void;

reconnect(): void;
}

export class WebsocketProviderBase {
constructor(host: string, options?: WebsocketProviderOptions);

requestQueue: Map<string, RequestItem>;
responseQueue: Map<string, RequestItem>;
connected: boolean;
connection: any;

supportsSubscriptions(): boolean;

send(
payload: JsonRpcPayload,
callback: (error: Error | null, result?: JsonRpcResponse) => void
): void;

on(type: string, callback: () => void): void;

once(type: string, callback: () => void): void;

removeListener(type: string, callback: () => void): void;

removeAllListeners(type: string): void;

reset(): void;

disconnect(code: number, reason: string): void;

connect(): void;

reconnect(): void;
}

export class HttpProviderBase {
constructor(host: string, options?: HttpProviderOptions);

host: string;
connected: boolean;

supportsSubscriptions(): boolean;

send(
payload: JsonRpcPayload,
callback: (error: Error | null, result?: JsonRpcResponse) => void
): void;

disconnect(): boolean;
}

export interface HttpProviderOptions {
keepAlive?: boolean;
timeout?: number;
headers?: HttpHeader[];
withCredentials?: boolean;
}

export interface HttpHeader {
name: string;
value: string;
}

export interface WebsocketProviderOptions {
host?: string;
timeout?: number;
reconnectDelay?: number;
headers?: any;
protocol?: string;
clientConfig?: string;
requestOptions?: any;
origin?: string;
reconnect?: ReconnectOptions;
}

export interface ReconnectOptions {
auto?: boolean;
delay?: number;
maxAttempts?: boolean;
onTimeout?: boolean;
}

export interface RequestItem {
payload: JsonRpcPayload;
callback: (error: any, result: any) => void;
}

export interface JsonRpcPayload {
jsonrpc: string;
method: string;
params: any[];
id?: string | number;
}

export interface JsonRpcResponse {
jsonrpc: string;
id: number;
result?: any;
error?: string;
}
44 changes: 3 additions & 41 deletions packages/web3-providers-http/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,8 @@
* @date 2018
*/

export class HttpProvider {
constructor(host: string, options?: HttpProviderOptions);
import {HttpProviderBase} from 'web3-core-helpers';

host: string;
connected: boolean;
export {HttpProviderOptions, HttpHeader, JsonRpcPayload, JsonRpcResponse} from 'web3-core-helpers';

supportsSubscriptions(): boolean;

send(
payload: JsonRpcPayload,
callback: (error: Error | null, result?: JsonRpcResponse) => void
): void;

disconnect(): boolean;
}

export interface HttpProviderOptions {
keepAlive?: boolean;
timeout?: number;
headers?: HttpHeader[];
withCredentials?: boolean;
}

export interface HttpHeader {
name: string;
value: string;
}

// Duplicated in ws, ipc, and http provider package
export interface JsonRpcPayload {
jsonrpc: string;
method: string;
params: any[];
id?: string | number;
}

// Duplicated in ws, ipc, and http provider package
export interface JsonRpcResponse {
jsonrpc: string;
id: number;
result?: any;
error?: string;
}
export class HttpProvider extends HttpProviderBase { }
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
* @date 2018
*/

import { HttpProvider, JsonRpcResponse } from 'web3-providers-http';
import { HttpProvider, JsonRpcResponse, JsonRpcPayload, HttpProviderOptions } from 'web3-providers-http';

const httpProvider = new HttpProvider('http://localhost:8545', {
const payload: JsonRpcPayload = {jsonrpc: '', id: 0, method: '', params: []};

const options: HttpProviderOptions = {
timeout: 20000,
headers: [
{
Expand All @@ -31,13 +33,15 @@ const httpProvider = new HttpProvider('http://localhost:8545', {
}
],
withCredentials: false
});
};

const httpProvider = new HttpProvider('http://localhost:8545', options);

// $ExpectType void
httpProvider.send({} as any, (error: Error | null) => {});
httpProvider.send(payload, (error: Error | null) => {});

// $ExpectType void
httpProvider.send({} as any, (error: Error | null, result: JsonRpcResponse | undefined) => {});
httpProvider.send(payload, (error: Error | null, result: JsonRpcResponse | undefined) => {});

// $ExpectType boolean
httpProvider.disconnect();
47 changes: 3 additions & 44 deletions packages/web3-providers-ipc/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,8 @@
*/

import * as net from "net";
import {IpcProviderBase} from 'web3-core-helpers';

export class IpcProvider {
constructor(path: string, net: net.Server);
export {JsonRpcPayload, JsonRpcResponse} from 'web3-core-helpers';

responseCallbacks: any;
notificationCallbacks: any;
connected: boolean;
connection: any;

addDefaultEvents(): void;

supportsSubscriptions(): boolean;

send(
payload: JsonRpcPayload,
callback: (error: Error | null, result?: JsonRpcResponse) => void
): void;

on(type: string, callback: () => void): void;

once(type: string, callback: () => void): void;

removeListener(type: string, callback: () => void): void;

removeAllListeners(type: string): void;

reset(): void;

reconnect(): void;
}

// Duplicated in ws, ipc, and http provider package
export interface JsonRpcPayload {
jsonrpc: string;
method: string;
params: any[];
id?: string | number;
}

// Duplicated in ws, ipc, and http provider package
export interface JsonRpcResponse {
jsonrpc: string;
id: number;
result?: any;
error?: string;
}
export class IpcProvider extends IpcProviderBase { }
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
*/

import * as net from 'net';
import { IpcProvider, JsonRpcResponse } from 'web3-providers-ipc';
import { IpcProvider, JsonRpcResponse, JsonRpcPayload } from 'web3-providers-ipc';

const ipcProvider = new IpcProvider(
'/Users/myuser/Library/Ethereum/geth.ipc',
new net.Server()
);

const payload: JsonRpcPayload = {jsonrpc: '', id: 0, method: '', params: []};

// $ExpectType any
ipcProvider.responseCallbacks;

Expand All @@ -47,10 +49,10 @@ ipcProvider.addDefaultEvents();
ipcProvider.supportsSubscriptions();

// $ExpectType void
ipcProvider.send({} as any, (error: Error | null) => {});
ipcProvider.send(payload, (error: Error | null) => {});

// $ExpectType void
ipcProvider.send({} as any, (error: Error | null, result: JsonRpcResponse | undefined) => {});
ipcProvider.send(payload, (error: Error | null, result: JsonRpcResponse | undefined) => {});

// $ExpectType void
ipcProvider.on('type', () => {});
Expand Down
3 changes: 2 additions & 1 deletion packages/web3-providers-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"main": "src/index.js",
"dependencies": {
"@web3-js/websocket": "^1.0.29",
"eventemitter3": "^4.0.0"
"eventemitter3": "^4.0.0",
"web3-core-helpers": "1.2.4"
},
"devDependencies": {
"definitelytyped-header-parser": "^1.0.1",
Expand Down
Loading

0 comments on commit fb1d422

Please sign in to comment.