Skip to content

Commit

Permalink
update provider types, add type-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Oct 4, 2023
1 parent 5f9bd1b commit 049d23e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
34 changes: 28 additions & 6 deletions src/json-rpc-provider-types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import { expectAssignable, expectNotAssignable } from 'tsd';
import EthQuery from '@metamask/eth-query';
import { Web3Provider } from '@ethersproject/providers';
import EthQuery from '@metamask/eth-query';
import { expectAssignable, expectNotAssignable } from 'tsd';

import type {
LegacyEthereumProvider
} from '.';
import type { JsonRpcRequest, LegacyEthereumProvider } from '.';

// Known legacy providers

expectAssignable<LegacyEthereumProvider>(new EthQuery({} as any));
expectAssignable<LegacyEthereumProvider>(new Web3Provider({} as any));
expectAssignable<LegacyEthereumProvider>({
send: async (method: string, params: string[]) =>
Promise.resolve([method, params]),
});
expectAssignable<LegacyEthereumProvider>({
// eslint-disable-next-line @typescript-eslint/no-empty-function
send: (_req: JsonRpcRequest, _cb: () => void) => {},
});
expectAssignable<LegacyEthereumProvider>({
send: async (req: JsonRpcRequest, _cb: (_x: null, _result: null) => void) =>
Promise.resolve(req),
});

expectNotAssignable<LegacyEthereumProvider>({ foo: '123' });
expectNotAssignable<LegacyEthereumProvider>({ send: '123' });

expectNotAssignable<LegacyEthereumProvider>({
send: (method: string, params: string[]) => [method, params],
});
expectNotAssignable<LegacyEthereumProvider>({
send: async (
req: JsonRpcRequest,
_cb: (_x: null, _result: undefined) => void,
) => Promise.resolve(req),
});
43 changes: 23 additions & 20 deletions src/json-rpc-provider-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,27 @@ export type EIP1993Provider = SafeEventEmitter & {
* [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193). It is not compliant with
* any Ethereum provider standard.
*/
export type LegacyEthereumProvider = LegacyEthersProvider | LegacyEthJsQueryProvider | LegacyWeb3Provider;
export type LegacyEthereumProvider =
| LegacyEthersProvider
| LegacyEthJsQueryProvider
| LegacyWeb3Provider;

type LegacyWeb3Provider = {
type LegacyEthersProvider = {
/**
* Send a provider request asynchronously.
* Send a provider request asynchronously. (ethers v5 Web3Provider)
*
* @param req - The request to send.
* @param callback - A function that is called upon the success or failure of the request.
* @param method - The RPC method to call.
* @param params - Array with method parameters.
* @returns A promise resolving with the result of the RPC call, or rejecting on failure.
*/
send?<Result extends Json = Json>(
req: Partial<JsonRpcRequest>,
callback: SendAsyncCallback<Result>,
): void;
}
send(method: string, params: any[]): Promise<Json>;
/*
send<Result extends Json = Json>(
method: string,
params: any[],
): Promise<Result>;
*/
};

type LegacyEthJsQueryProvider = {
/**
Expand All @@ -55,24 +62,20 @@ type LegacyEthJsQueryProvider = {
* @param req - The request to send.
* @param callback - A function that is called upon the success or failure of the request.
*/
sendAsync?<Result extends Json = Json>(
sendAsync<Result extends Json = Json>(
req: Partial<JsonRpcRequest>,
callback: SendAsyncCallback<Result>,
): void;
};

type LegacyEthersProvider = {
type LegacyWeb3Provider = {
/**
* Send a provider request asynchronously. (ethers v5 Web3Provider)
* Send a provider request asynchronously.
*
* @param method - The RPC method to call.
* @param params - Array with method parameters.
* @returns - A promise resolving with the result of the RPC call, or rejecting on failure.
* @param req - The request to send.
* @param callback - A function that is called upon the success or failure of the request.
*/
send?<Result extends Json = Json>(
method: string,
params: any[],
): Promise<Result>;
send(req: Partial<JsonRpcRequest>, callback: SendAsyncCallback<Json>): void;
};

type SendAsyncCallback<Result extends Json> = (
Expand Down

0 comments on commit 049d23e

Please sign in to comment.