@@ -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 */
3737export 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
0 commit comments