Skip to content

Commit dd31848

Browse files
committed
feat(axios): configured axios
1 parent 52ca240 commit dd31848

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@mui/material": "^5.5.0",
1111
"@redux-saga/core": "^1.1.3",
1212
"@reduxjs/toolkit": "^1.8.0",
13+
"axios": "^0.26.1",
1314
"history": "^5.3.0",
1415
"i18next": "^21.6.14",
1516
"react": "^17.0.2",

src/features/posts/api/index.ts

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { Env } from 'config/Env'
33
import { Post } from 'features/posts/types'
4+
import makeApi from "libs/core/configureAxios";
45

5-
const POSTS_BASE_URL = `${Env.API_BASE_URL}/posts`
6+
const api = makeApi(`${Env.API_BASE_URL}`);
67

7-
export const getPosts = async (): Promise<Post[]> => fetch(POSTS_BASE_URL).then(res => res.json())
8+
const POSTS_BASE_URL = `/posts`
89

9-
export const createPost = async (post: Post): Promise<Post> =>
10-
fetch(POSTS_BASE_URL, {
11-
method: 'POST',
12-
headers: {
13-
'Content-Type': 'application/json',
14-
},
15-
body: JSON.stringify(post),
16-
}).then(res => res.json())
10+
export const getPosts = (): Promise<Post[]> => api.get(POSTS_BASE_URL)
1711

18-
export const updatePost = async (post: Post): Promise<Post> =>
19-
fetch(`${POSTS_BASE_URL}/${post.id}`, {
20-
method: 'PUT',
21-
headers: {
22-
'Content-Type': 'application/json',
23-
},
24-
body: JSON.stringify(post),
25-
}).then(res => res.json())
12+
export const createPost = (post: Post): Promise<Post> => api.post(POSTS_BASE_URL, post);
2613

27-
export const deletePost = async (post: Post): Promise<Post> =>
28-
fetch(`${POSTS_BASE_URL}/${post.id}`, {
29-
method: 'DELETE',
30-
}).then(() => post)
14+
export const updatePost = (post: Post): Promise<Post> => api.put(`${POSTS_BASE_URL}/${post.id}`, post)
15+
16+
export const deletePost = (post: Post): Promise<Post> => api.delete(`${POSTS_BASE_URL}/${post.id}`, { data: post})

src/libs/core/configureAxios.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import axios from 'axios'
2+
3+
4+
export default function makeApi(baseURL: string) {
5+
const api = axios.create({
6+
baseURL,
7+
})
8+
9+
api.defaults.headers.post['Content-Type'] = "application/json";
10+
api.defaults.headers.put['Content-Type'] = "application/json";
11+
api.defaults.headers.delete['Content-Type'] = "application/json";
12+
13+
api.interceptors.request.use(
14+
(config) => {
15+
16+
if(localStorage.getItem('authToken')) {
17+
config.headers = {...config.headers, Authorization: `Bearer ${localStorage.getItem('authToken')}`}
18+
}
19+
20+
return config;
21+
},
22+
(error) => Promise.reject(error)
23+
)
24+
25+
api.interceptors.response.use(
26+
(response) => response.data, // return data object
27+
(error) => Promise.reject(error)
28+
)
29+
return api
30+
}

yarn.lock

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5078,6 +5078,13 @@ axios@^0.21.1:
50785078
dependencies:
50795079
follow-redirects "^1.14.0"
50805080

5081+
axios@^0.26.1:
5082+
version "0.26.1"
5083+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9"
5084+
integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==
5085+
dependencies:
5086+
follow-redirects "^1.14.8"
5087+
50815088
axobject-query@^2.2.0:
50825089
version "2.2.0"
50835090
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -8553,7 +8560,7 @@ flush-write-stream@^1.0.0:
85538560
inherits "^2.0.3"
85548561
readable-stream "^2.3.6"
85558562

8556-
follow-redirects@^1.0.0, follow-redirects@^1.14.0:
8563+
follow-redirects@^1.0.0, follow-redirects@^1.14.0, follow-redirects@^1.14.8:
85578564
version "1.14.9"
85588565
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
85598566
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

0 commit comments

Comments
 (0)