@@ -26,10 +26,12 @@ export default class CHReactNativeGatekeeper {
2626 navigation : null ,
2727 } ;
2828 screen_config : any ;
29+ timeout : number ;
2930
30- constructor ( config : { CH_KEY : string } , events : CHEvents , debug : boolean ) {
31+ constructor ( config : { CH_KEY : string } , events : CHEvents , timeout : number , debug : boolean ) {
3132 this . CH_KEY = config . CH_KEY ;
32- this . debug = debug ;
33+ this . debug = debug || false ;
34+ this . timeout = timeout || 3000 ; // default timeout setting of 3 seconds
3335 this . events = {
3436 ...this . events ,
3537 ...events ,
@@ -44,7 +46,6 @@ export default class CHReactNativeGatekeeper {
4446 * @returns Promise
4547 */
4648 redirectOrWait ( config : { slug : string } ) {
47-
4849 if ( ! config . slug ) {
4950
5051 this . on ( {
@@ -76,6 +77,27 @@ export default class CHReactNativeGatekeeper {
7677 this . events . navigation = navigation ;
7778 }
7879
80+ async fetchWithTimeout ( resource : string , options : RequestInit ) {
81+
82+ try {
83+ const controller = new AbortController ( ) ;
84+ const id = setTimeout ( ( ) => controller . abort ( ) , this . timeout ) ;
85+
86+ const response = await fetch ( resource , {
87+ ...options ,
88+ signal : controller . signal
89+ } ) ;
90+
91+
92+ clearTimeout ( id ) ;
93+
94+ return response ;
95+ } catch ( error ) {
96+ return error ;
97+ }
98+
99+ }
100+
79101 /**
80102 *
81103 * @param url
@@ -92,13 +114,24 @@ export default class CHReactNativeGatekeeper {
92114 ) : Promise < CHResponse > {
93115 // Inside, we call the `fetch` function with
94116 // a URL and config given:
95- return fetch ( url , config )
96- . then ( async ( response ) => {
97- return {
98- headers : response . headers ,
99- status : response . status ,
100- url : response . url ,
101- } as CHResponse ;
117+
118+ return this . fetchWithTimeout ( url , config )
119+ . then ( async ( response ) => {
120+
121+ if ( response . headers ) {
122+ return {
123+ headers : response . headers ,
124+ status : response . status ,
125+ url : response . url ,
126+ } as CHResponse ;
127+ } else {
128+ return {
129+ headers : undefined ,
130+ status : 504 ,
131+ url : undefined ,
132+ } as CHResponse ;
133+ }
134+
102135 } )
103136 }
104137
@@ -115,15 +148,20 @@ export default class CHReactNativeGatekeeper {
115148 try {
116149 const response = await this . request < CHResponse > ( URL , {
117150 method : 'GET'
118- } ) ;
119-
120- const token = this . getToken ( response . url ) ;
151+ } ) ;
152+
153+ let token : string ;
154+ if ( response && response . url ) {
155+ token = this . getToken ( response . url ) ;
156+
157+ if ( token ) {
158+ this . saveToken ( this . screen_config . slug , token ) ;
159+ }
121160
122- if ( token ) {
123- this . saveToken ( this . screen_config . slug , token ) ;
124161 }
125162
126- if ( response . status === 200 ) {
163+
164+ if ( response && response . status === 200 ) {
127165
128166 this . on ( {
129167 type : 'onWait' ,
@@ -153,7 +191,7 @@ export default class CHReactNativeGatekeeper {
153191
154192 this . on ( { type : 'onRequestEnd' } )
155193
156- } catch ( error ) {
194+ } catch ( error ) {
157195
158196 this . on ( {
159197 type : 'onRedirect' ,
0 commit comments