Skip to content

Commit 3b22a28

Browse files
committed
Use single arg for full url or ProviderUrl objects for TLNetwork
Add default url parameters for TLNetwork
1 parent 007259f commit 3b22a28

File tree

7 files changed

+77
-102
lines changed

7 files changed

+77
-102
lines changed

docs/guides.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,8 @@ The user therefore needs some coins ([TLC](https://explore.tlbc.trustlines.found
5858
5959
```javascript
6060
const laika = new TLNetwork({
61-
relayProviderUrlObject: {
62-
protocol: 'https',
63-
wsProtocol: 'wss',
64-
host: 'relay0.testnet.trustlines.network',
65-
path: '/api/v1'
66-
},
67-
messagingProviderUrlObject: {
68-
protocol: 'https',
69-
wsProtocol: 'wss',
70-
host: 'relay0.testnet.trustlines.network',
71-
path: '/api/v1'
72-
},
61+
relayUrl: 'https://relay0.testnet.trustlines.network/api/v1',
62+
messagingUrl: 'https://relay0.testnet.trustlines.network/api/v1',
7363
walletType: 'ethers'
7464
})
7565

@@ -85,18 +75,8 @@ An additional step of deploying the identity contract of the newly created user
8575

8676
```javascript
8777
const laika = new TLNetwork({
88-
relayProviderUrlObject: {
89-
protocol: 'https',
90-
wsProtocol: 'wss',
91-
host: 'relay0.testnet.trustlines.network',
92-
path: '/api/v1'
93-
},
94-
messagingProviderUrlObject: {
95-
protocol: 'https',
96-
wsProtocol: 'wss',
97-
host: 'relay0.testnet.trustlines.network',
98-
path: '/api/v1'
99-
},
78+
relayUrl: 'https://relay0.testnet.trustlines.network/api/v1',
79+
messagingUrl: 'https://relay0.testnet.trustlines.network/api/v1',
10080
walletType: 'identity',
10181
identityFactoryAddress: '0x8D2720877Fa796E3C3B91BB91ad6CfcC07Ea249E',
10282
identityImplementationAddress: '0x8BEe92893D3ec62e5B3EBBe4e536A60Fd9AFc9D7'

src/TLNetwork.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,33 @@ import { IdentityWallet } from './wallets/IdentityWallet'
3535
* It contains all of the library's functionality and all calls to the library should be made through a `TLNetwork` instance.
3636
*/
3737
export class TLNetwork {
38-
private static validateConfig(config: TLNetworkConfig): void {
39-
if (
40-
config.relayProviderUrlObject &&
41-
(config.relayApiUrl || config.relayWsApiUrl)
42-
) {
43-
throw new Error(
44-
`Invalid input config; cannot input both relay provider url object and full urls`
45-
)
46-
}
47-
if (
48-
config.messagingProviderUrlObject &&
49-
(config.messagingApiUrl || config.messagingWsApiUrl)
50-
) {
51-
throw new Error(
52-
`Invalid input config; cannot input both messaging provider url object and full urls`
53-
)
54-
}
38+
private static getApiUrl(
39+
apiUrl: string | ProviderUrl,
40+
defaultUrlParameters: ProviderUrl
41+
) {
42+
return this.buildUrl(apiUrl, defaultUrlParameters, utils.buildApiUrl)
43+
}
44+
45+
private static getWsUrl(
46+
wsUrl: string | ProviderUrl,
47+
defaultUrlParameters: ProviderUrl
48+
) {
49+
return this.buildUrl(wsUrl, defaultUrlParameters, utils.buildWsApiUrl)
50+
}
51+
52+
private static buildUrl(
53+
url: string | ProviderUrl,
54+
defaultUrlParameters: ProviderUrl,
55+
buildUrlFunction
56+
) {
57+
return typeof url === 'string'
58+
? url
59+
: buildUrlFunction({
60+
protocol: url.protocol || defaultUrlParameters.protocol,
61+
host: url.host || defaultUrlParameters.host,
62+
port: url.port || defaultUrlParameters.port,
63+
path: url.path || defaultUrlParameters.path
64+
})
5565
}
5666
/**
5767
* User instance containing all user/keystore related methods.
@@ -124,38 +134,31 @@ export class TLNetwork {
124134
* @param config Configuration object. See [[TLNetworkConfig]] for more information.
125135
*/
126136
constructor(config: TLNetworkConfig = {}) {
127-
TLNetwork.validateConfig(config)
128-
const defaultProviderUrl: ProviderUrl = {
129-
protocol: 'http',
130-
host: 'localhost',
131-
port: '',
132-
path: '',
133-
wsProtocol: 'ws'
134-
}
135137
const {
136-
relayProviderUrlObject = config.relayProviderUrlObject ||
137-
defaultProviderUrl,
138-
messagingProviderUrlObject = config.messagingProviderUrlObject ||
139-
defaultProviderUrl,
140-
relayApiUrl,
141-
relayWsApiUrl,
142-
messagingApiUrl,
143-
messagingWsApiUrl,
138+
relayUrl = {},
139+
messagingUrl = {},
144140
web3Provider,
145141
identityFactoryAddress,
146142
identityImplementationAddress,
147143
walletType = WALLET_TYPE_ETHERS,
148144
chainId
149145
} = config
150146

147+
const defaultUrlParameters: ProviderUrl = {
148+
protocol: 'http',
149+
port: '',
150+
path: '',
151+
host: 'localhost'
152+
}
153+
151154
this.setProviders(
152155
new RelayProvider(
153-
relayApiUrl || utils.buildApiUrl(relayProviderUrlObject),
154-
relayWsApiUrl || utils.buildWsApiUrl(relayProviderUrlObject)
156+
TLNetwork.getApiUrl(relayUrl, defaultUrlParameters),
157+
TLNetwork.getWsUrl(relayUrl, defaultUrlParameters)
155158
),
156159
new Provider(
157-
messagingApiUrl || utils.buildApiUrl(messagingProviderUrlObject),
158-
messagingWsApiUrl || utils.buildWsApiUrl(messagingProviderUrlObject)
160+
TLNetwork.getApiUrl(messagingUrl, defaultUrlParameters),
161+
TLNetwork.getWsUrl(messagingUrl, defaultUrlParameters)
159162
)
160163
)
161164

src/typings.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,20 @@ import { Options as ReconnectingOptions } from 'reconnecting-websocket'
77
*/
88
export interface TLNetworkConfig {
99
/**
10-
* Url object for the trustline provider
10+
* ProviderUrl object or full url for the relay api
1111
*/
12-
relayProviderUrlObject?: ProviderUrl
12+
relayUrl?: string | ProviderUrl
1313
/**
14-
* Url object for the messaging provider
14+
* ProviderUrl object or full url for the messaging api
1515
*/
16-
messagingProviderUrlObject?: ProviderUrl
16+
messagingUrl?: string | ProviderUrl
1717
/**
1818
* Web3 provider
1919
*/
2020
web3Provider?: any
2121
/**
2222
* Full URL for trustline rest api
2323
*/
24-
relayApiUrl?: string
25-
/**
26-
* Full URL for trustline WebSocket api
27-
*/
28-
relayWsApiUrl?: string
29-
/**
30-
* Full URL for messaging rest api
31-
*/
32-
messagingApiUrl?: string
33-
/**
34-
* Full URL for messaging WebSocket api
35-
*/
36-
messagingWsApiUrl?: string
37-
/**
38-
* Wallet type to use, either "WalletTypeEthers" or "WalletTypeIdentity".
39-
*/
4024
walletType?: string
4125
/**
4226
* Address of the identity factory
@@ -69,10 +53,6 @@ export interface ProviderUrl {
6953
* Base path for the relay api
7054
*/
7155
path?: string
72-
/**
73-
* Protocol for WebSockets
74-
*/
75-
wsProtocol?: string
7656
}
7757

7858
/**

src/utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ export const buildApiUrl = (UrlObject: ProviderUrl): string => {
434434
* @param UrlObject.path relay api base endpoint
435435
*/
436436
export const buildWsApiUrl = (UrlObject: ProviderUrl): string => {
437-
return `${UrlObject.wsProtocol}://${UrlObject.host}${UrlObject.port &&
438-
`:${UrlObject.port}`}${UrlObject.path && `/${trimUrl(UrlObject.path)}`}`
437+
return `${UrlObject.protocol === 'https' ? 'wss' : 'ws'}://${
438+
UrlObject.host
439+
}${UrlObject.port && `:${UrlObject.port}`}${UrlObject.path &&
440+
`/${trimUrl(UrlObject.path)}`}`
439441
}
440442

441443
/**

tests/Fixtures.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@ export const providerUrl: ProviderUrl = {
2929
host: process.env.RELAY_HOSTNAME || 'localhost',
3030
path: 'api/v1/',
3131
port: 5000,
32-
protocol: 'http',
33-
wsProtocol: 'ws'
32+
protocol: 'http'
3433
}
3534

3635
export const tlNetworkConfig: TLNetworkConfig = {
37-
relayProviderUrlObject: providerUrl,
38-
messagingProviderUrlObject: providerUrl,
36+
relayUrl: providerUrl,
37+
messagingUrl: providerUrl,
3938
chainId: end2endChainId
4039
}
4140

4241
export const tlNetworkConfigIdentity: TLNetworkConfig = {
43-
relayProviderUrlObject: providerUrl,
44-
messagingProviderUrlObject: providerUrl,
42+
relayUrl: providerUrl,
43+
messagingUrl: providerUrl,
4544
walletType: WALLET_TYPE_IDENTITY,
4645
identityFactoryAddress: identityConfig.identityProxyFactory,
4746
identityImplementationAddress: identityConfig.identityImplementation,

tests/e2e/Identity.test.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,9 @@ describe('e2e', () => {
4040
let trustlinesNetwork2: TLNetwork
4141

4242
before(async () => {
43-
const relayApiUrl = utils.buildApiUrl(
44-
tlNetworkConfig.relayProviderUrlObject
45-
)
46-
const relayWsUrl = utils.buildWsApiUrl(
47-
tlNetworkConfig.relayProviderUrlObject
48-
)
49-
relayProvider = new RelayProvider(relayApiUrl, relayWsUrl)
50-
5143
trustlinesNetwork = new TLNetwork(tlNetworkConfigIdentity)
5244
trustlinesNetwork2 = new TLNetwork(tlNetworkConfigIdentity)
45+
relayProvider = trustlinesNetwork.relayProvider
5346

5447
identityWallet = trustlinesNetwork.wallet
5548
})

tests/unit/TLNetwork.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,39 @@ describe('unit', () => {
99
it('should load default configuration', () => {
1010
const tlNetwork = new TLNetwork()
1111
expect(tlNetwork.relayProvider.ApiUrl).to.equal('http://localhost')
12+
expect(tlNetwork.relayProvider.WsApiUrl).to.equal('ws://localhost')
13+
expect(tlNetwork.messagingProvider.ApiUrl).to.equal('http://localhost')
14+
expect(tlNetwork.messagingProvider.WsApiUrl).to.equal('ws://localhost')
1215
})
1316

1417
it('should load custom configuration', () => {
1518
const tlNetwork = new TLNetwork({
16-
relayProviderUrlObject: {
19+
relayUrl: {
1720
host: '192.168.0.59',
1821
path: 'api/v1',
1922
port: 5000,
2023
protocol: 'https'
24+
},
25+
messagingUrl: {
26+
host: '192.168.0.1',
27+
path: 'messaging/api/v1',
28+
port: 4000,
29+
protocol: 'http'
2130
}
2231
})
2332
expect(tlNetwork).to.be.an('object')
2433
expect(tlNetwork.relayProvider.ApiUrl).to.equal(
2534
'https://192.168.0.59:5000/api/v1'
2635
)
36+
expect(tlNetwork.relayProvider.WsApiUrl).to.equal(
37+
'wss://192.168.0.59:5000/api/v1'
38+
)
39+
expect(tlNetwork.messagingProvider.ApiUrl).to.equal(
40+
'http://192.168.0.1:4000/messaging/api/v1'
41+
)
42+
expect(tlNetwork.messagingProvider.WsApiUrl).to.equal(
43+
'ws://192.168.0.1:4000/messaging/api/v1'
44+
)
2745
})
2846
})
2947
})

0 commit comments

Comments
 (0)