@@ -19,6 +19,13 @@ import ArgsBuilder from './ArgsBuilder'
1919import Constants from './Constants'
2020import StorageManager from './StorageManager'
2121import Network from './Network'
22+ import EventEmitter from './EventEmitter'
23+
24+ interface HostConfig {
25+ apiHost : string ;
26+ apiPath : string ;
27+ devServerHost : string ;
28+ }
2229
2330export default class LeanplumRequest {
2431 private cooldownTimeout = null
@@ -34,8 +41,11 @@ export default class LeanplumRequest {
3441 public versionName : string
3542
3643 constructor (
44+ private events : EventEmitter ,
3745 private network = new Network ( )
38- ) { }
46+ ) {
47+ this . loadHostConfig ( )
48+ }
3949
4050 public get userId ( ) : string | undefined {
4151 return this . userIdValue ?? this . loadLocal < string > ( Constants . DEFAULT_KEYS . USER_ID ) ?? this . deviceId
@@ -90,9 +100,8 @@ export default class LeanplumRequest {
90100 }
91101
92102 if ( params . body ( ) ) {
93- this . network . ajax (
94- 'POST' ,
95- `${ this . apiPath } ?${ argsBuilder . build ( ) } ` ,
103+ this . sendRequest (
104+ `?${ argsBuilder . build ( ) } ` ,
96105 params . body ( ) ,
97106 success ,
98107 error ,
@@ -115,9 +124,8 @@ export default class LeanplumRequest {
115124 . add ( Constants . PARAMS . ACTION , Constants . METHODS . MULTI )
116125 . add ( Constants . PARAMS . TIME , ( new Date ( ) . getTime ( ) / 1000 ) . toString ( ) . toString ( ) )
117126 . build ( )
118- this . network . ajax (
119- 'POST' ,
120- `${ this . apiPath } ?${ multiRequestArgs } ` ,
127+ this . sendRequest (
128+ `?${ multiRequestArgs } ` ,
121129 requestData ,
122130 success ,
123131 error ,
@@ -179,6 +187,12 @@ export default class LeanplumRequest {
179187 return ( count > 0 ) ? response ?. response ?. [ count - 1 ] : null
180188 }
181189
190+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
191+ public getFirstResponse ( response ) : any {
192+ const count = response ?. response ?. length ?? 0
193+ return ( count > 0 ) ? response ?. response ?. [ 0 ] : null
194+ }
195+
182196 public isResponseSuccess ( response ) : boolean {
183197 return Boolean ( response ?. success )
184198 }
@@ -191,6 +205,34 @@ export default class LeanplumRequest {
191205 this . saveLocal ( Constants . DEFAULT_KEYS . COUNT , count )
192206 }
193207
208+ private sendRequest ( query : string , data : string , success : Function , error : Function , queued : boolean ) : void {
209+ this . network . ajax (
210+ 'POST' ,
211+ `${ this . apiPath } ${ query } ` ,
212+ data ,
213+ ( response ) => {
214+ const methodResponse = this . getFirstResponse ( response )
215+ if ( ! methodResponse . success && methodResponse . apiHost ) {
216+ const { apiHost, apiPath, devServerHost } = methodResponse
217+
218+ this . saveLocal ( Constants . DEFAULT_KEYS . HOST_CONFIG , JSON . stringify ( {
219+ apiHost,
220+ apiPath,
221+ devServerHost,
222+ } ) )
223+ this . apiPath = `https://${ apiHost } /${ apiPath } `
224+ this . sendRequest ( query , data , success , error , queued )
225+
226+ this . events . emit ( 'updateDevServerHost' , devServerHost )
227+ } else if ( success ) {
228+ success ( response )
229+ }
230+ } ,
231+ error ,
232+ queued
233+ )
234+ }
235+
194236 // eslint-disable-next-line @typescript-eslint/no-explicit-any
195237 private popUnsentRequests ( ) : any [ ] {
196238 const requestData = [ ]
@@ -213,6 +255,15 @@ export default class LeanplumRequest {
213255 return requestData
214256 }
215257
258+ private loadHostConfig ( ) : void {
259+ const hostConfig = JSON . parse ( this . loadLocal < string > ( Constants . DEFAULT_KEYS . HOST_CONFIG ) || 'null' )
260+ if ( hostConfig ) {
261+ const { apiHost, apiPath, devServerHost } = hostConfig
262+ this . apiPath = `https://${ apiHost } /${ apiPath } `
263+ this . events . emit ( 'updateDevServerHost' , devServerHost )
264+ }
265+ }
266+
216267 private loadLocal < T > ( key : string ) : T {
217268 return StorageManager . get ( key )
218269 }
0 commit comments