Skip to content

Commit 0641e65

Browse files
authored
Merge pull request #178 from vu3th/fix/connect-options-types
Fix/connect options types
2 parents 74342a1 + 6fe1cb9 commit 0641e65

File tree

5 files changed

+22
-16
lines changed

5 files changed

+22
-16
lines changed

app/components/content/Eip6963.client.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const providerList = computed(() => {
1414
})
1515
})
1616
17-
async function onClickWallet(rdns?: RDNS) {
17+
async function onClickWallet(rdns: RDNS) {
1818
useVueDappModal().close()
1919
await connectTo('BrowserWallet', { target: 'rdns', rdns })
2020
}

packages/core/src/browserWalletConnector.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class BrowserWalletConnector extends Connector<EIP1193Provider, BrowserWa
3737
useEIP6963().subscribe()
3838
}
3939

40-
async connect(options: ConnectOptions) {
40+
async connect(options: ConnectOptions<'BrowserWallet'>) {
4141
const { target, rdns, timeout } = options
4242

4343
const { provider, info } = this.getProvider(target, rdns)

packages/core/src/services/connect.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { computed, readonly } from 'vue'
22
import { useStore } from '../store'
3-
import { ConnectOptions, ConnectorName, ProviderTarget, RDNS } from '../types'
3+
import { ConnectOptions, ConnectorName, ProviderTarget } from '../types'
44
import { AutoConnectError, ConnectError, ConnectorNotFoundError } from '../errors'
55
import { normalizeChainId } from '../utils'
66
import {
@@ -24,7 +24,7 @@ export function useConnect(pinia?: any) {
2424
walletStore.wallet.providerTarget = null
2525
}
2626

27-
async function connectTo(connectorName: ConnectorName | string, options?: ConnectOptions) {
27+
async function connectTo<T extends ConnectorName>(connectorName: T, options: ConnectOptions<T>) {
2828
walletStore.wallet.error = ''
2929
walletStore.wallet.status = 'connecting'
3030

@@ -36,11 +36,8 @@ export function useConnect(pinia?: any) {
3636
const { provider, account, chainId, info } = await connector.connect(options)
3737

3838
if (connector.name === 'BrowserWallet') {
39-
if (!info) throw new Error('BrowserWallet connector requires provider info')
40-
if (!options?.target) throw new Error('BrowserWallet connector requires target')
41-
42-
walletStore.wallet.providerInfo = info
43-
walletStore.wallet.providerTarget = options?.target
39+
walletStore.wallet.providerInfo = info || null
40+
walletStore.wallet.providerTarget = options?.target || null
4441
}
4542

4643
walletStore.wallet.connector = connector
@@ -114,12 +111,14 @@ export function useConnect(pinia?: any) {
114111
const browserWallet = walletStore.connectors.find(conn => conn.name === 'BrowserWallet')
115112
if (!browserWallet) return
116113

117-
let options: ConnectOptions
114+
let options: ConnectOptions<'BrowserWallet'>
118115

119116
switch (target) {
120117
case 'window.ethereum':
121118
if (!isWindowEthereumAvailable) return
122-
options = { target: 'window.ethereum' }
119+
options = {
120+
target: 'window.ethereum',
121+
}
123122
break
124123
case 'rdns':
125124
const lastRdns = getLastConnectedBrowserWallet()
@@ -132,7 +131,7 @@ export function useConnect(pinia?: any) {
132131
}
133132

134133
try {
135-
await connectTo(browserWallet.name, options)
134+
await connectTo('BrowserWallet', options)
136135
} catch (err: any) {
137136
throw new AutoConnectError(err)
138137
}

packages/core/src/types/connector.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ export type ConnectorData<Provider = any> = {
1111
info?: EIP6963ProviderInfo // Only available for BrowserWalletConnector
1212
}
1313

14-
export type ConnectOptions = {
14+
export type ConnectOptionsBase = {
1515
target?: ProviderTarget
1616
rdns?: string
1717
timeout?: number
1818
}
1919

20+
type ConnectOptionsForBrowserWallet<T extends ProviderTarget = ProviderTarget> = ConnectOptionsBase &
21+
(T extends 'rdns' ? { target: T; rdns: string } : { target: T })
22+
23+
export type ConnectOptions<T extends ConnectorName> = T extends 'BrowserWallet'
24+
? ConnectOptionsForBrowserWallet
25+
: ConnectOptionsBase | undefined
26+
2027
export abstract class Connector<Provider = EIP1193Provider, Options = any> {
2128
// Connector name
2229
abstract readonly name: string
@@ -27,7 +34,7 @@ export abstract class Connector<Provider = EIP1193Provider, Options = any> {
2734
this.options = options
2835
}
2936

30-
abstract connect(optionsOrTimeout?: ConnectOptions | number): Promise<ConnectorData>
37+
abstract connect<T extends ConnectorName>(optionsOrTimeout?: ConnectOptions<T> | number): Promise<ConnectorData>
3138

3239
abstract getProvider(): Promise<Provider> | { provider: EIP1193Provider; info?: EIP6963ProviderInfo }
3340

packages/modal/src/VueDappModal.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ watch(modalOpen, async () => {
9393
}
9494
})
9595
96-
async function onClickWallet(connName: ConnectorName, options?: ConnectOptions) {
96+
async function onClickWallet<T extends ConnectorName>(connName: T, options?: ConnectOptions<T>) {
9797
try {
9898
closeModal()
99-
await connectTo(connName, options)
99+
await connectTo<ConnectorName>(connName, options)
100100
} catch (err: any) {
101101
emit('connectError', err)
102102
}

0 commit comments

Comments
 (0)