Skip to content

Commit

Permalink
Touch up types (#97)
Browse files Browse the repository at this point in the history
* cleanup types

* add missing semicolons, line breaks

* add send types

* fix initProvider signature

* fix incorrectly optional params

* improve JsonRpcResponse type

* add deprecation tags

* add full docstrings to declarations file
  • Loading branch information
rekmarks authored Aug 19, 2020
1 parent 807e415 commit 6db8c33
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 72 deletions.
216 changes: 160 additions & 56 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,174 @@
import { EventEmitter } from 'events'
import { Duplex } from 'stream'
import { EventEmitter } from 'events';
import { Duplex } from 'stream';

export interface MetamaskInpageProviderOptions {
/**
* The logging API to use.
* @default console
*/
logger?: Pick<Console, 'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace'>
/**
* The maximum number of event listeners.
* @default 100
*/
maxEventListeners?: number
/**
* Whether the provider should send page metadata.
* @default true
*/
shouldSendMetadata?: boolean

/**
* The logging API to use.
* @default console
*/
logger?: Pick<Console, 'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace'>;

/**
* The maximum number of event listeners.
* @default 100
*/
maxEventListeners?: number;

/**
* Whether the provider should send page metadata.
* @default true
*/
shouldSendMetadata?: boolean;
}

export class MetamaskInpageProvider extends EventEmitter {
/**
*
* @param connectionStream A Node.js duplex stream
* @param options An options bag
*/
constructor(connectionStream: Duplex, options?: MetamaskInpageProviderOptions)
readonly isMetaMask: true
readonly selectedAddress: string | null
readonly networkVersion: string | null
readonly chainId: string | undefined
isConnected(): boolean
sendAsync(payload: JsonRpcPayload, callback: (error: Error | null, result?: JsonRpcResponse) => void): void
/** Submits an RPC request to MetaMask for the given method, with the given params. Resolves with the result of the method call, or rejects on error. */
request(args: JsonRpcPayload): Promise<unknown>

/**
* @param connectionStream - A Node.js duplex stream.
* @param options - An options bag.
*/
constructor (connectionStream: Duplex, options?: MetamaskInpageProviderOptions);

/**
* Returns whether the provider can process RPC requests.
*/
isConnected (): boolean;

/**
* Submits an RPC request for the given method, with the given params.
* Resolves with the result of the method call, or rejects on error.
*/
request (args: RequestArguments): Promise<unknown>;

/**
* Submits an RPC request per the given JSON-RPC request object.
*/
sendAsync (
payload: JsonRpcRequest,
callback: (error: Error | null, result?: JsonRpcResponse) => void,
): void;

/**
* Submits an RPC request for the given method, with the given params.
* @deprecated Use {@link request} instead.
*/
send (method: string, params?: unknown[]): Promise<JsonRpcResponse>;

/**
* Submits an RPC request per the given JSON-RPC request object.
* @deprecated Use {@link request} instead.
*/
send (
payload: JsonRpcRequest,
callback: (error: Error | null, result?: JsonRpcResponse) => void,
): void;

/**
* Accepts a JSON-RPC request object, and synchronously returns the cached result
* for the given method. Only supports 4 specific methods.
* @deprecated Use {@link request} instead.
*/
send (payload: SendSyncJsonRpcRequest): JsonRpcResponse;

/**
* Indicating that this provider is a MetaMask provider.
*/
readonly isMetaMask: true;

/**
* The user's currently selected Ethereum address.
* If null, MetaMask is either locked or the user has not permitted any
* addresses to be viewed.
*/
readonly selectedAddress: string | null;

/**
* The network ID of the currently connected Ethereum chain.
* @deprecated Use {@link chainId} instead.
*/
readonly networkVersion: string | null;

/**
* The chain ID of the currently connected Ethereum chain.
* See [chainId.network]{@link https://chainid.network} for more information.
*/
readonly chainId: string | undefined;
}

/**
* Initializes a MetamaskInpageProvider and (optionally) sets it on window.ethereum.
* Initializes a MetamaskInpageProvider and (optionally) assigns it as window.ethereum.
* @returns The initialized provider (whether set or not).
*/
export function initProvider(
opts?: Pick<MetamaskInpageProviderOptions, 'maxEventListeners' | 'shouldSendMetadata'> & {
/** A Node.js duplex stream */
connectionStream?: Duplex
/** Whether the provider should be set as window.ethereum */
shouldSetOnWindow?: boolean
}
): MetamaskInpageProvider
export function initProvider (
options: Pick<MetamaskInpageProviderOptions, 'maxEventListeners' | 'shouldSendMetadata'> & {

/** A Node.js duplex stream. */
connectionStream: Duplex;

/**
* Whether the provider should be set as window.ethereum.
* @default true
*/
shouldSetOnWindow?: boolean;
}
): MetamaskInpageProvider;

/**
* Sets the given provider instance as window.ethereum and dispatches the 'ethereum#initialized' event on window.
* Sets the given provider instance as window.ethereum and dispatches
* the 'ethereum#initialized' event on window.
*
* @param providerInstance - The provider instance.
*/
export function setGlobalProvider(providerInstance: MetamaskInpageProvider): void
export interface JsonRpcPayload {
jsonrpc: string
method: string
params?: unknown[]
id?: string | number
export function setGlobalProvider (providerInstance: MetamaskInpageProvider): void;

export interface RequestArguments {

/** The RPC method to request. */
method: string;

/** The params of the RPC method, if any. */
params?: unknown[];
}
export interface JsonRpcResponse {
jsonrpc: string
id: string | number
result?: unknown
error?: {
code: number
message: string
data?: unknown
}

export interface JsonRpcRequest {

/** The RPC method to request. */
method: string;

/** The params of the RPC method, if any. */
params?: unknown[];

/** For spec compliance; handled if not provided. */
id?: string | number;

/** For spec compliance; handled if not provided. */
jsonrpc?: '2.0';
}

export interface SendSyncJsonRpcRequest extends JsonRpcRequest {
method: 'eth_accounts' | 'eth_coinbase' | 'eth_uninstallFilter' | 'net_version';
}

interface JsonRpcResponseBase {

/** Equal to the corresponding JSON-RPC request object. */
id?: string | number;

/** Equal to the corresponding JSON-RPC request. */
jsonrpc?: '2.0';
}

export interface JsonRpcErrorResponse extends JsonRpcResponseBase {
error: {
code: number;
message: string;
data?: unknown;
};
}

export interface JsonRpcSuccessResponse extends JsonRpcResponseBase {
result: unknown;
}

export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;
14 changes: 7 additions & 7 deletions src/MetamaskInpageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ module.exports = class MetamaskInpageProvider extends SafeEventEmitter {

/**
* @param {Object} connectionStream - A Node.js duplex stream
* @param {Object} opts - An options bag
* @param {ConsoleLike} [opts.logger] - The logging API to use. Default: console
* @param {number} [opts.maxEventListeners] - The maximum number of event
* @param {Object} options - An options bag
* @param {ConsoleLike} [options.logger] - The logging API to use. Default: console
* @param {number} [options.maxEventListeners] - The maximum number of event
* listeners. Default: 100
* @param {boolean} [opts.shouldSendMetadata] - Whether the provider should
* @param {boolean} [options.shouldSendMetadata] - Whether the provider should
* send page metadata. Default: true
*/
constructor (
Expand Down Expand Up @@ -266,14 +266,14 @@ module.exports = class MetamaskInpageProvider extends SafeEventEmitter {
//====================

/**
* Returns whether the inpage provider is connected to MetaMask.
* Returns whether the provider can process RPC requests.
*/
isConnected () {
return this._state.isConnected
}

/**
* Submits an RPC request to MetaMask for the given method, with the given params.
* Submits an RPC request for the given method, with the given params.
* Resolves with the result of the method call, or rejects on error.
*
* @param {Object} args - The RPC request arguments.
Expand Down Expand Up @@ -319,7 +319,7 @@ module.exports = class MetamaskInpageProvider extends SafeEventEmitter {
}

/**
* Submit a JSON-RPC request object and a callback to make an RPC method call.
* Submits an RPC request per the given JSON-RPC request object.
*
* @param {Object} payload - The RPC request object.
* @param {Function} cb - The callback function.
Expand Down
18 changes: 9 additions & 9 deletions src/initProvider.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const MetamaskInpageProvider = require('./MetamaskInpageProvider')

/**
* Initializes a MetamaskInpageProvider and (optionally) sets it on window.ethereum.
*
* @param {Object} opts - An options bag.
* @param {Object} opts.connectionStream - A Node.js stream.
* @param {number} opts.maxEventListeners - The maximum number of event listeners.
* @param {boolean} opts.shouldSendMetadata - Whether the provider should send page metadata.
* @param {boolean} opts.shouldSetOnWindow - Whether the provider should be set as window.ethereum
* @returns {MetamaskInpageProvider | Proxy} The initialized provider (whether set or not).
*/
* Initializes a MetamaskInpageProvider and (optionally) assigns it as window.ethereum.
*
* @param {Object} options - An options bag.
* @param {Object} options.connectionStream - A Node.js stream.
* @param {number} options.maxEventListeners - The maximum number of event listeners.
* @param {boolean} options.shouldSendMetadata - Whether the provider should send page metadata.
* @param {boolean} options.shouldSetOnWindow - Whether the provider should be set as window.ethereum
* @returns {MetamaskInpageProvider | Proxy} The initialized provider (whether set or not).
*/
function initProvider ({
connectionStream,
maxEventListeners = 100,
Expand Down

0 comments on commit 6db8c33

Please sign in to comment.