Skip to content

Commit 5878365

Browse files
committed
feat(geolocation): geolocation utility
1 parent 898c722 commit 5878365

File tree

9 files changed

+83
-0
lines changed

9 files changed

+83
-0
lines changed

packages/react/.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ SCROLL_TRACKING=<boolean to enable scroll tracking, e.g. false>
1616

1717
#Feature Flags
1818
FOOTER_LOCALE_BUTTON=<Boolean flag to turn on/off the locale selector>
19+
20+
# Geolocation
21+
GEO_API=<endpoint for getting user country by geolocation>

packages/utilities/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GEO_API=https://api.www.s81c.com/webmaster/dbip/

packages/utilities/__mocks__/axios.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
get: jest.fn(() => Promise.resolve({ data: {} })),
3+
};

packages/utilities/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"jsdoc": "rimraf docs && jsdoc -c ./jsdoc.json ./README.md"
3535
},
3636
"dependencies": {
37+
"axios": "^0.19.0",
3738
"window-or-global": "^1.0.1"
3839
},
3940
"devDependencies": {
@@ -69,6 +70,7 @@
6970
"rollup": "^1.10.0",
7071
"rollup-plugin-babel": "^4.3.2",
7172
"rollup-plugin-commonjs": "^9.3.4",
73+
"rollup-plugin-node-builtins": "^2.1.2",
7274
"rollup-plugin-node-resolve": "^4.2.3",
7375
"rollup-plugin-replace": "^2.2.0",
7476
"rollup-plugin-sizes": "^0.5.0",

packages/utilities/scripts/rollup.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const gzip = require('gzip-size');
66

77
const commonjs = require('rollup-plugin-commonjs');
88
const resolve = require('rollup-plugin-node-resolve');
9+
const builtins = require('rollup-plugin-node-builtins');
910
const babel = require('rollup-plugin-babel');
1011
const replace = require('rollup-plugin-replace');
1112
const { terser } = require('rollup-plugin-terser');
@@ -59,6 +60,7 @@ module.exports = {
5960
input: 'src/index.js',
6061
plugins: [
6162
resolve({
63+
browser: true,
6264
mainFields: ['jsnext', 'module', 'main'],
6365
}),
6466
commonjs({
@@ -71,6 +73,7 @@ module.exports = {
7173
replace({
7274
'process.env.NODE_ENV': JSON.stringify(env),
7375
}),
76+
builtins(),
7477
...prodSettings,
7578
],
7679
output: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import mockAxios from 'axios';
2+
import { geolocation } from '../';
3+
4+
mockAxios.get.mockImplementationOnce(() =>
5+
Promise.resolve({
6+
data: {
7+
country: 'us',
8+
},
9+
})
10+
);
11+
12+
describe('Geolocation utility', () => {
13+
it('should return an object with the expected cc and lc', async () => {
14+
Object.defineProperty(window.navigator, 'language', {
15+
writable: true,
16+
value: 'en',
17+
});
18+
19+
const info = await geolocation();
20+
const endpoint = process.env.GEO_API;
21+
const expected = {
22+
cc: 'us',
23+
lc: 'en',
24+
};
25+
expect(info).toStrictEqual(expected);
26+
expect(mockAxios.get).toHaveBeenCalledWith(endpoint, {
27+
headers: {
28+
'Content-Type': 'application/json; charset=utf-8',
29+
},
30+
});
31+
});
32+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import axios from 'axios';
2+
3+
const _endpoint = process.env.GEO_API;
4+
/**
5+
* Utility to retrieve user's country code based on their IP address
6+
* and the language code from the browser language preference
7+
*
8+
* @returns {object} object with cc and lc data
9+
*
10+
*/
11+
async function geolocation() {
12+
const location = await axios
13+
.get(_endpoint, {
14+
headers: {
15+
'Content-Type': 'application/json; charset=utf-8',
16+
},
17+
})
18+
.then(response => response.data);
19+
20+
const cc = location && location.country;
21+
22+
// get language preference from browser
23+
const lc = window.navigator.language;
24+
25+
if (cc && lc) {
26+
return { cc, lc };
27+
} else return;
28+
}
29+
30+
export default geolocation;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright IBM Corp. 2016, 2018
3+
*
4+
* This source code is licensed under the Apache-2.0 license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export { default as geolocation } from './geolocation';

packages/utilities/src/utilities/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77

88
export * from './escaperegexp';
99
export * from './featureflag';
10+
export * from './geolocation';
1011
export * from './serialize';
1112
export * from './settings';

0 commit comments

Comments
 (0)