Skip to content

Commit c634e6a

Browse files
Add axios
1 parent a80ff32 commit c634e6a

File tree

6 files changed

+82
-3
lines changed

6 files changed

+82
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"axios": "^0.19.1",
910
"babel-plugin-transform-remove-console": "^6.9.4",
1011
"customize-cra": "^0.9.1",
1112
"hookrouter": "^1.2.3",

src/constants/configs.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ const projectEnv = 'local';
33
// const projectEnv = 'stg';
44
// const projectEnv = 'prod';
55

6-
let domain;
6+
let domain, baseAPI;
77

88
switch (projectEnv) {
99
case 'dev':
1010
domain = 'your-dev-domain';
11+
baseAPI = 'your-dev-api-url';
1112
break;
1213
case 'stg':
1314
domain = 'your-stg-domain';
15+
baseAPI = 'your-stg-api-url';
1416
break;
1517
case 'prod':
1618
domain = 'your-prod-domain';
19+
baseAPI = 'your-prod-api-url';
1720
break;
1821
default:
1922
domain = 'localhost'; // local
23+
baseAPI = 'http://jsonplaceholder.typicode.com/';
2024
break;
2125
}
2226

2327
export default {
24-
domain
28+
domain,
29+
baseAPI
2530
};

src/constants/cookies.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const LANGUAGE_TRANS = 'language_trans';
2+
export const ACCESS_TOKEN = 'access_token';

src/helpers/api.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import axios from 'axios';
2+
import Cookie from 'js-cookie';
3+
import Config from '../constants/configs';
4+
import { ACCESS_TOKEN } from '../constants/cookies';
5+
6+
const api = axios.create({
7+
baseURL: Config.baseAPI,
8+
timeout: 1000
9+
});
10+
11+
api.interceptors.request.use(
12+
config => {
13+
if (config.baseURL === Config.baseAPI && !config.headers.Authorization) {
14+
const token = Cookie.get(ACCESS_TOKEN);
15+
if (token) {
16+
config.headers.Authorization = `Bearer ${token}`;
17+
}
18+
}
19+
return config;
20+
},
21+
error => Promise.reject(error)
22+
);
23+
24+
api.interceptors.response.use(
25+
response => {
26+
// Any status code that lie within the range of 2xx cause this function to trigger
27+
// Do something with response data
28+
return response;
29+
},
30+
error => Promise.reject(error)
31+
);
32+
33+
export default api;

src/pages/HomePage/HomePage.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { FormattedMessage } from 'react-intl';
33
import Navigation from '../../components/Navigation/Navigation';
44
import Title from '../../components/Title/Title';
5+
import api from '../../helpers/api';
56
import './HomePage.scss';
67

78
const HomePage = () => {
9+
const testApi = async () => {
10+
console.log('test api');
11+
try {
12+
const { data } = await api.request({
13+
method: 'get',
14+
url: 'users/1'
15+
});
16+
console.log({ data });
17+
} catch (error) {
18+
console.log('error to get users: ', error);
19+
}
20+
};
21+
22+
useEffect(() => {
23+
testApi();
24+
}, []);
25+
826
return (
927
<div className="home-page-container">
1028
<Navigation />

yarn.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,6 +2004,13 @@ aws4@^1.8.0:
20042004
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.9.0.tgz#24390e6ad61386b0a747265754d2a17219de862c"
20052005
integrity sha512-Uvq6hVe90D0B2WEnUqtdgY1bATGz3mw33nH9Y+dmA+w5DHvUmBgkr5rM/KCHpCsiFNRUfokW/szpPPgMK2hm4A==
20062006

2007+
axios@^0.19.1:
2008+
version "0.19.1"
2009+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.1.tgz#8a6a04eed23dfe72747e1dd43c604b8f1677b5aa"
2010+
integrity sha512-Yl+7nfreYKaLRvAvjNPkvfjnQHJM1yLBY3zhqAwcJSwR/6ETkanUgylgtIvkvz0xJ+p/vZuNw8X7Hnb7Whsbpw==
2011+
dependencies:
2012+
follow-redirects "1.5.10"
2013+
20072014
axobject-query@^2.0.2:
20082015
version "2.1.1"
20092016
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.1.1.tgz#2a3b1271ec722d48a4cd4b3fcc20c853326a49a7"
@@ -3362,6 +3369,13 @@ debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.0, debug@^2.6.8, debug@^2.6.
33623369
dependencies:
33633370
ms "2.0.0"
33643371

3372+
debug@=3.1.0:
3373+
version "3.1.0"
3374+
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
3375+
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
3376+
dependencies:
3377+
ms "2.0.0"
3378+
33653379
debug@^3.0.0, debug@^3.1.1, debug@^3.2.5, debug@^3.2.6:
33663380
version "3.2.6"
33673381
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
@@ -4409,6 +4423,13 @@ flush-write-stream@^1.0.0:
44094423
inherits "^2.0.3"
44104424
readable-stream "^2.3.6"
44114425

4426+
follow-redirects@1.5.10:
4427+
version "1.5.10"
4428+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
4429+
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
4430+
dependencies:
4431+
debug "=3.1.0"
4432+
44124433
follow-redirects@^1.0.0:
44134434
version "1.9.0"
44144435
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.9.0.tgz#8d5bcdc65b7108fe1508649c79c12d732dcedb4f"

0 commit comments

Comments
 (0)