-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathuserCountry.js
29 lines (24 loc) · 1.04 KB
/
userCountry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import constants from '../../src/lib/constants';
const { defaultCountry } = constants;
export const fetchUserCountry = () => {
const geoLocationUrl = 'https://www.paypalobjects.com/muse/noop.js';
return fetch(geoLocationUrl, { mode: 'no-cors' })
.then(res => {
// we need this function to workaround a chrome issue
// https://stackoverflow.com/questions/45816743/how-to-solve-this-caution-request-is-not-finished-yet-in-chrome
const bodyNoOp = () => {
};
res.text().then(bodyNoOp);
// If country cannot be determined show the default config
// This can happen if Akamai header 'x-client-location' is missing in noop.js response
const country = res.headers.get('x-client-location');
if (country === null) {
return defaultCountry;
}
return country;
})
.catch(() => {
// Fallback to US in case noop.js request blocked
return defaultCountry;
});
}