Skip to content

Commit 2044716

Browse files
author
Le Tuan Kiet
committed
[RTT-03] Implement axios
1 parent 8116da1 commit 2044716

File tree

8 files changed

+94
-2
lines changed

8 files changed

+94
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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<any, { q: string; appid: string } | { a: string }>(
8+
// API.WEATHER
9+
// ),
10+
};

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable import/no-anonymous-default-export */
2+
export enum API_STATUS {
3+
HTTP_404_NOT_FOUND = 400,
4+
}
5+
6+
//TODO: you need delete when integrate API
7+
export default {
8+
HELLO: "hello",
9+
WEATHER: "weather",
10+
};

src/utils/service/api.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import axios, { AxiosError } 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 = ({ response }: AxiosError) => {
12+
if (
13+
!response?.status ||
14+
[API_STATUS.HTTP_404_NOT_FOUND].includes(response?.status)
15+
) {
16+
console.log({ response });
17+
}
18+
};
19+
20+
class RestService<ApiResponse, ApiRequestParam = any> {
21+
constructor(protected resource: string) {}
22+
23+
index(params?: ApiRequestParam) {
24+
return new Promise((resolve, reject) => {
25+
axios
26+
.get<ApiResponse>(linkApi(this.resource), {
27+
params,
28+
})
29+
.then(resolve)
30+
.catch((error: AxiosError) => {
31+
handleError(error);
32+
});
33+
});
34+
}
35+
}
36+
37+
export default RestService;

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)