Skip to content

Commit e40e8c4

Browse files
Building url based on slug or url
1 parent b8adbb0 commit e40e8c4

1 file changed

Lines changed: 54 additions & 36 deletions

File tree

CHReactNativeGatekeeper.tsx

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ export default class CHReactNativeGatekeeper {
8383
this.events.navigation = navigation;
8484
}
8585

86-
async fetchWithTimeout (resource: string, options: RequestInit) {
87-
86+
async fetchWithTimeout(resource: string, options: RequestInit) {
87+
8888
try {
8989
const controller = new AbortController();
9090
const id = setTimeout(() => controller.abort(), this.timeout);
9191
const response = await fetch(resource, {
9292
...options,
93-
signal: controller.signal
94-
});
93+
signal: controller.signal
94+
});
9595
clearTimeout(id);
96-
96+
9797
return response;
9898
} catch (error) {
9999
return error;
100100
}
101101

102-
}
102+
}
103103

104104
/**
105105
*
@@ -119,9 +119,8 @@ export default class CHReactNativeGatekeeper {
119119
// a URL and config given:
120120

121121
return this.fetchWithTimeout(url, config)
122-
.then(async (response) => {
123-
124-
if(response.headers) {
122+
.then(async (response) => {
123+
if (response.headers) {
125124
return {
126125
headers: response.headers,
127126
status: response.status,
@@ -138,38 +137,57 @@ export default class CHReactNativeGatekeeper {
138137
})
139138
}
140139

141-
/**
142-
*
143-
*/
144-
async makeCrowdHandlerRequest() {
145-
this.on({ type: 'onRequestStart' })
146-
let URL: string = `${API_REDIRECT_ENDPOINT}?ch-public-key=${this.CH_KEY}`;
140+
buildRequestURL() : string {
141+
let URL: string;
142+
let saved_token :undefined;
143+
144+
let params: any = [ { name: 'ch-public-key', value: this.CH_KEY } ];
147145

148-
if(this.requestType === 'slug') {
149-
URL = `${URL}&slug=${this.screen_config.slug}`;
146+
if (this.requestType === 'slug') {
147+
params.push({ name: 'slug', value: this.screen_config.slug });
150148
if (this.tokens[this.screen_config.slug]) {
151-
URL = `${API_REDIRECT_ENDPOINT}${this.tokens[this.screen_config.slug]}?ch-public-key=${this.CH_KEY}&slug=${this.screen_config.slug}`;
149+
saved_token = this.tokens[this.screen_config.slug];
152150
}
153151
} else {
154-
URL = `${URL}&url=${this.screen_config.url}`;
155-
if (this.tokens[this.screen_config.url]) {
156-
URL = `${API_REDIRECT_ENDPOINT}${this.tokens[this.screen_config.url]}?ch-public-key=${this.CH_KEY}&url=${this.screen_config.url}`;
152+
params.push({ name: 'url', value: encodeURI(this.screen_config.url) });
153+
if (this.tokens[encodeURI(this.screen_config.url)]) {
154+
saved_token = this.tokens[encodeURI(this.screen_config.url)]
157155
}
158-
}
156+
}
159157

158+
if (!saved_token){
159+
URL = API_REDIRECT_ENDPOINT;
160+
} else {
161+
URL = `${API_REDIRECT_ENDPOINT}${saved_token}`;
162+
}
163+
164+
URL = `${URL}?${params.map((param) => {
165+
return `${param.name}=${param.value}`
166+
}).join('&')}`
167+
168+
return URL;
169+
}
170+
171+
/**
172+
*
173+
*/
174+
async makeCrowdHandlerRequest() {
175+
this.on({ type: 'onRequestStart' })
176+
const URL : string = this.buildRequestURL();
177+
160178
try {
161179
const response = await this.request<CHResponse>(URL, {
162180
method: 'GET'
163-
});
181+
});
164182

165183
let token: any;
166-
if(response && response.url) {
184+
if (response && response.url) {
167185
token = this.getToken(response.url);
168186
}
169187

170188
if (token) {
171189
this.saveToken((this.requestType === 'slug') ? this.screen_config.slug : encodeURI(this.screen_config.url), token);
172-
}
190+
}
173191

174192
if (response && response.status === 200) {
175193

@@ -201,7 +219,7 @@ export default class CHReactNativeGatekeeper {
201219

202220
this.on({ type: 'onRequestEnd' })
203221

204-
} catch (error) {
222+
} catch (error) {
205223

206224
this.on({
207225
type: 'onRedirect',
@@ -255,7 +273,7 @@ export default class CHReactNativeGatekeeper {
255273

256274
} catch (error) {
257275
console.log(error);
258-
276+
259277
}
260278

261279
return token;
@@ -267,23 +285,23 @@ export default class CHReactNativeGatekeeper {
267285

268286
}
269287

270-
handlePostMessage(message: string) {
288+
handlePostMessage(message: string) {
271289
let parts = message.split('=');
272290

273291
try {
274-
let payload = JSON.parse(parts[1]);
275-
292+
let payload = JSON.parse(parts[1]);
293+
276294
switch (parts[0]) {
277295
case 'saveToken':
278-
296+
279297
if (this.requestType === 'slug') {
280298
this.saveToken(payload.slug, payload.token)
281299
}
282-
300+
283301
if (this.requestType === 'url') {
284302
this.saveToken(encodeURI(payload.requestURL), payload.token)
285303
}
286-
304+
287305
break;
288306
case 'promoted':
289307
this.on({
@@ -296,14 +314,14 @@ export default class CHReactNativeGatekeeper {
296314
}
297315
});
298316
break;
299-
317+
300318
default:
301319
break;
302320
}
303-
321+
304322
} catch (error) {
305323
console.log(error);
306-
324+
307325
}
308326
}
309327
}

0 commit comments

Comments
 (0)