Skip to content

Commit

Permalink
Add types (#92)
Browse files Browse the repository at this point in the history
* Create index.d.ts

Co-authored-by: Erik Marks <[email protected]>
  • Loading branch information
Jack-Works and rekmarks authored Aug 19, 2020
1 parent de5fbfc commit fe825d2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
70 changes: 70 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
}
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>
}
/**
* Initializes a MetamaskInpageProvider and (optionally) sets it on 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
/**
* 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 interface JsonRpcResponse {
jsonrpc: string
id: string | number
result?: unknown
error?: {
code: number
message: string
data?: unknown
}
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "6.2.0",
"description": "A JavaScript Ethereum provider that connects over a WebExtension port.",
"main": "index.js",
"type": "index.d.ts",
"scripts": {
"test": "jest",
"coverage": "jest --coverage",
Expand All @@ -27,7 +28,8 @@
},
"homepage": "https://github.com/MetaMask/inpage-provider#readme",
"files": [
"src/"
"src/",
"index.d.ts"
],
"dependencies": {
"eth-rpc-errors": "^2.1.1",
Expand Down

0 comments on commit fe825d2

Please sign in to comment.