Skip to content

Commit 42a5fbe

Browse files
Le Tuan Kietletuankiet212
authored andcommitted
[RTT-03] Implement axios
1 parent 8116da1 commit 42a5fbe

File tree

10 files changed

+143
-2
lines changed

10 files changed

+143
-2
lines changed

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
REACT_APP_API_ENDPOINT=https://api.openweathermap.org/data/2.5/
2+
REACT_APP_KEY = 623949ba087cb249c44a770ad1a293fe

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
"@types/react": "^18.0.0",
1515
"@types/react-dom": "^18.0.0",
1616
"@types/uuid": "^8.3.4",
17+
"axios": "^0.27.2",
1718
"react": "^18.2.0",
1819
"react-dom": "^18.2.0",
1920
"react-scripts": "5.0.1",
2021
"typescript": "^4.4.2",
22+
"url-join": "^5.0.0",
2123
"uuid": "^9.0.0",
2224
"web-vitals": "^2.1.0"
2325
},

src/App.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import './App.css';
22

3-
function App() {
3+
function App() {
4+
5+
46
return (
57
<div className="App">
68
<header className="App-header">

src/api/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable import/no-anonymous-default-export */
2+
import API from '../utils/constants/api';
3+
import RestService from '../utils/service/api';
4+
5+
export default {
6+
// EXAMPLE: Your need delete it
7+
// weather: new RestService<ApiResponse, FormSearchByName | FormSearchByLatLong>(API.WEATHER),
8+
};

src/react-app-env.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
/// <reference types="react-scripts" />
2+
declare namespace NodeJS {
3+
interface ProcessEnv {
4+
//types of envs
5+
NODE_ENV: "development" | "production" | "test";
6+
PUBLIC_URL: string;
7+
}
8+
}

src/utils/constants/api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export enum API_STATUS {
2+
HTTP_404_NOT_FOUND = 400,
3+
HTTP_500_SERVER = 500,
4+
}
5+
6+
//TODO: you need delete when integrate API
7+
// export default {
8+
// WEATHER: 'weather',
9+
// };

src/utils/reducer/api-reducer.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
export const INITIAL_STATE: InfoApi = {
3+
isLoading: false,
4+
isError: false,
5+
data: null,
6+
errorMsg: '',
7+
};
8+
9+
export enum ACTION_TYPE {
10+
FETCH_START = 'START',
11+
FETCH_SUCCESS = 'SUCCESS',
12+
FETCH_FAILED = 'FAILED',
13+
}
14+
15+
export const apiReducer = (state: InfoApi, action: ActionReducer) => {
16+
switch (action.type) {
17+
case ACTION_TYPE.FETCH_START:
18+
return {
19+
...state,
20+
isLoading: true,
21+
isError: false,
22+
};
23+
case ACTION_TYPE.FETCH_SUCCESS:
24+
return {
25+
isLoading: false,
26+
isError: false,
27+
data: action.payload,
28+
};
29+
30+
case ACTION_TYPE.FETCH_FAILED:
31+
return {
32+
...state,
33+
isLoading: false,
34+
isError: true,
35+
errorMsg: action.errorMsg,
36+
};
37+
38+
default:
39+
return INITIAL_STATE;
40+
}
41+
};

src/utils/service/api.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import axios, { AxiosError, AxiosResponse } from 'axios';
2+
import urlJoin from 'url-join';
3+
import { API_STATUS } from '../constants/api';
4+
5+
const baseURL = process.env.REACT_APP_API_ENDPOINT || '';
6+
7+
const linkApi = (resource: string) => {
8+
return urlJoin(baseURL, resource);
9+
};
10+
11+
const handleError = (error: AxiosError) => {
12+
if (!error.response?.status || [API_STATUS.HTTP_500_SERVER].includes(error.response?.status)) {
13+
console.log(error.response);
14+
} else {
15+
return error.response.data;
16+
}
17+
};
18+
19+
class RestService<T, R = any> {
20+
constructor(protected resource: string) {}
21+
22+
index(params?: R) {
23+
return new Promise<AxiosResponse<T>>((resolve, rejects) => {
24+
axios
25+
.get<T>(linkApi(this.resource), {
26+
params,
27+
})
28+
.then(resolve)
29+
.catch((error: AxiosError) => {
30+
rejects(handleError(error));
31+
});
32+
});
33+
}
34+
}
35+
36+
export default RestService;

src/utils/types/api.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
interface InfoApi {
2+
isLoading: boolean;
3+
data?: any;
4+
isError: boolean;
5+
errorMsg?: string;
6+
}
7+
8+
interface ActionReducer {
9+
type: string;
10+
payload?: ApiResponse;
11+
errorMsg?: string;
12+
}

yarn.lock

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2862,6 +2862,14 @@ axe-core@^4.4.3:
28622862
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.4.3.tgz#11c74d23d5013c0fa5d183796729bc3482bd2f6f"
28632863
integrity sha512-32+ub6kkdhhWick/UjvEwRchgoetXqTK14INLqbGm5U2TzBkBNF3nQtLYm8ovxSkQWArjEQvftCKryjZaATu3w==
28642864

2865+
axios@^0.27.2:
2866+
version "0.27.2"
2867+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
2868+
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
2869+
dependencies:
2870+
follow-redirects "^1.14.9"
2871+
form-data "^4.0.0"
2872+
28652873
axobject-query@^2.2.0:
28662874
version "2.2.0"
28672875
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -4637,7 +4645,7 @@ flatted@^3.1.0:
46374645
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
46384646
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==
46394647

4640-
follow-redirects@^1.0.0:
4648+
follow-redirects@^1.0.0, follow-redirects@^1.14.9:
46414649
version "1.15.2"
46424650
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
46434651
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
@@ -4670,6 +4678,15 @@ form-data@^3.0.0:
46704678
combined-stream "^1.0.8"
46714679
mime-types "^2.1.12"
46724680

4681+
form-data@^4.0.0:
4682+
version "4.0.0"
4683+
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
4684+
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
4685+
dependencies:
4686+
asynckit "^0.4.0"
4687+
combined-stream "^1.0.8"
4688+
mime-types "^2.1.12"
4689+
46734690
forwarded@0.2.0:
46744691
version "0.2.0"
46754692
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@@ -8765,6 +8782,11 @@ uri-js@^4.2.2:
87658782
dependencies:
87668783
punycode "^2.1.0"
87678784

8785+
url-join@^5.0.0:
8786+
version "5.0.0"
8787+
resolved "https://registry.yarnpkg.com/url-join/-/url-join-5.0.0.tgz#c2f1e5cbd95fa91082a93b58a1f42fecb4bdbcf1"
8788+
integrity sha512-n2huDr9h9yzd6exQVnH/jU5mr+Pfx08LRXXZhkLLetAMESRj+anQsTAh940iMrIetKAmry9coFuZQ2jY8/p3WA==
8789+
87688790
url-parse@^1.5.3:
87698791
version "1.5.10"
87708792
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.10.tgz#9d3c2f736c1d75dd3bd2be507dcc111f1e2ea9c1"

0 commit comments

Comments
 (0)