Skip to content

Commit

Permalink
Merge pull request #3 from decentraland/feat/expose-connection-data
Browse files Browse the repository at this point in the history
feat: expose connection data
  • Loading branch information
cazala authored Feb 12, 2021
2 parents 2ccb663 + 31ade72 commit 66ecb82
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,29 @@ connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN)
connection.disconnect()
```

### .getConnectionData()

Returns the data used for the last successfull [.connect()](#connect) call. It's used by [.tryPreviousConnection](#trypreviousconnection) to determine which connection to use. Check [ConnectionData](#ConnectionData) for more info on the returned type

**Definition**

```typescript
getConnectionData(): ConnectionData | undefined
```

**Usage**

```typescript
const connection = new ConnectionManager(new Storage())
connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN)

// (...)

const connectionData = connection.getConnectionData() // => connectionData is ConnectionData
```



### .getAvialableProviders()

Returns the providers available for connection. If for example no `window` object is found, `ProviderType.INJECTED` will not be returned on the list
Expand Down Expand Up @@ -201,6 +224,15 @@ export type ConnectionResponse = {
}
```
### ConnectionData
```typescript
export type ConnectionData = {
providerType: ProviderType
chainId: ChainId
}
```
# Example
```typescript
Expand Down
8 changes: 4 additions & 4 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ConnectionManager {
chainId: ChainId = ChainId.MAINNET
): Promise<ConnectionResponse> {
this.setConnectionData(providerType, chainId)
this.connector = this.getConnector(providerType, chainId)
this.connector = this.buildConnector(providerType, chainId)

const {
provider,
Expand Down Expand Up @@ -86,12 +86,12 @@ export class ConnectionManager {
providerType: ProviderType,
chainId: ChainId = ChainId.MAINNET
): Promise<Provider> {
const connector = this.getConnector(providerType, chainId)
const connector = this.buildConnector(providerType, chainId)
const provider = await connector.getProvider()
return this.toProvider(provider)
}

getConnector(
buildConnector(
providerType: ProviderType,
chainId: ChainId
): AbstractConnector {
Expand All @@ -107,7 +107,7 @@ export class ConnectionManager {
}
}

private getConnectionData(): ConnectionData | undefined {
getConnectionData(): ConnectionData | undefined {
const { storageKey } = getConfiguration()
const connectionData = this.storage.get(storageKey)
return connectionData ? JSON.parse(connectionData) : undefined
Expand Down
42 changes: 30 additions & 12 deletions test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('ConnectionManager', () => {
describe('#connect', () => {
it('should set the connector', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
sinon.stub(connectionManager, 'buildConnector').returns(stubConnector)

expect(connectionManager.connector).to.eq(undefined)
await connectionManager.connect(ProviderType.INJECTED)
Expand All @@ -48,7 +48,7 @@ describe('ConnectionManager', () => {
it('should activate the connector', async () => {
const stubConnector = new StubConnector()
const getConnectorStub = sinon
.stub(connectionManager, 'getConnector')
.stub(connectionManager, 'buildConnector')
.returns(stubConnector)
const activateStub = sinon.stub(stubConnector, 'activate').callThrough()

Expand All @@ -60,7 +60,7 @@ describe('ConnectionManager', () => {

it('should return the connection data', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
sinon.stub(connectionManager, 'buildConnector').returns(stubConnector)

const result = await connectionManager.connect(
ProviderType.INJECTED,
Expand All @@ -82,7 +82,7 @@ describe('ConnectionManager', () => {

it('should not patch the provider with the request method if it already exists', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
sinon.stub(connectionManager, 'buildConnector').returns(stubConnector)

const result = await connectionManager.connect(
ProviderType.INJECTED,
Expand All @@ -104,7 +104,7 @@ describe('ConnectionManager', () => {
it('should store the last provider and chain', async () => {
const stubConnector = new StubConnector()
const configuration = getConfiguration()
sinon.stub(connectionManager, 'getConnector').returns(stubConnector)
sinon.stub(connectionManager, 'buildConnector').returns(stubConnector)

await connectionManager.connect(ProviderType.INJECTED, ChainId.KOVAN)

Expand All @@ -128,7 +128,7 @@ describe('ConnectionManager', () => {
it('should connect to the last supplied provider', async () => {
const stubConnector = new StubConnector()
const getConnectorStub = sinon
.stub(connectionManager, 'getConnector')
.stub(connectionManager, 'buildConnector')
.returns(stubConnector)

await connectionManager.connect(ProviderType.FORTMATIC)
Expand All @@ -154,6 +154,24 @@ describe('ConnectionManager', () => {
})
})

describe('#getConnectionData', () => {
it('should return the data used on the last successful connection', async () => {
const stubConnector = new StubConnector()
sinon.stub(connectionManager, 'buildConnector').returns(stubConnector)

await connectionManager.connect(ProviderType.INJECTED, ChainId.KOVAN)

expect(connectionManager.getConnectionData()).to.deep.eq({
providerType: ProviderType.INJECTED,
chainId: ChainId.KOVAN
})
})

it('should return undefined if no connection happneed', () => {
expect(connectionManager.getConnectionData()).to.eq(undefined)
})
})

describe('#disconnect', () => {
it('should not do anything if no connector exists', () => {
expect(connectionManager.disconnect()).not.to.eventually.throw()
Expand Down Expand Up @@ -214,7 +232,7 @@ describe('ConnectionManager', () => {
const provider = { send: () => {} }

const getConnectorStub = sinon
.stub(connectionManager, 'getConnector')
.stub(connectionManager, 'buildConnector')
.returns(stubConnector)
const getProviderStub = sinon
.stub(stubConnector, 'getProvider')
Expand Down Expand Up @@ -276,16 +294,16 @@ describe('ConnectionManager', () => {
})
})

describe('#getConnector', () => {
describe('#buildConnector', () => {
it('should throw if an invalid provider type is supplied', () => {
const providerType = 'Invalid Provider Type' as any
expect(() =>
connectionManager.getConnector(providerType, ChainId.MAINNET)
connectionManager.buildConnector(providerType, ChainId.MAINNET)
).to.throw(`Invalid provider ${providerType}`)
})

it('should return an instance of FortmaticConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
const connector = connectionManager.buildConnector(
ProviderType.FORTMATIC,
ChainId.KOVAN
)
Expand All @@ -294,7 +312,7 @@ describe('ConnectionManager', () => {
})

it('should return an instance of InjectedConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
const connector = connectionManager.buildConnector(
ProviderType.INJECTED,
ChainId.KOVAN
)
Expand All @@ -303,7 +321,7 @@ describe('ConnectionManager', () => {
})

it('should return an instance of WalletConnectConnector for the supplied chain', () => {
const connector = connectionManager.getConnector(
const connector = connectionManager.buildConnector(
ProviderType.WALLET_CONNECT,
ChainId.KOVAN
)
Expand Down

0 comments on commit 66ecb82

Please sign in to comment.