Skip to content

Commit 0427897

Browse files
authored
Merge pull request #32 from TaskFlow-CLAP/CLAP-151
Clap-151 axios 공통함수 선언
2 parents cf46ee2 + bfdc184 commit 0427897

File tree

7 files changed

+96
-104
lines changed

7 files changed

+96
-104
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dist
1313
dist-ssr
1414
coverage
1515
*.local
16+
.env
1617

1718
/cypress/videos/
1819
/cypress/screenshots/
@@ -27,4 +28,5 @@ coverage
2728
*.sln
2829
*.sw?
2930

31+
3032
*.tsbuildinfo

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
},
1515
"dependencies": {
1616
"axios": "^1.7.9",
17+
"js-cookie": "^3.0.5",
1718
"pinia": "^2.3.0",
1819
"vue": "^3.5.13",
1920
"vue-router": "^4.5.0"
2021
},
2122
"devDependencies": {
2223
"@tsconfig/node22": "^22.0.0",
24+
"@types/js-cookie": "^3.0.6",
2325
"@types/node": "^22.10.2",
2426
"@typescript-eslint/eslint-plugin": "^8.20.0",
2527
"@typescript-eslint/parser": "^8.20.0",

src/api/test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import axiosInstance from '../utils/axios'
2+
3+
export const getNotifications = async () => {
4+
const response = await axiosInstance.get('/api/notifications?page=0&size=5')
5+
return response.data
6+
}
7+
8+
export const postLogin = async (nickname: string, password: string) => {
9+
const response = await axiosInstance.post('/api/auths/login', { nickname, password })
10+
return response.data
11+
}
12+
13+
export const patchReadNotification = async (notificationId: number) => {
14+
const response = await axiosInstance.patch(`/api/notification/${notificationId}`)
15+
return response.data
16+
}

src/router/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ const router = createRouter({
3232
name: 'MyRequest',
3333
component: MyRequestView
3434
},
35-
{
36-
path: '/icon',
37-
name: 'icon',
38-
component: () => import('../views/IconView.vue')
39-
},
4035
{
4136
path: '/task-request',
4237
name: 'task-request',

src/utils/axios.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import axios, { type InternalAxiosRequestConfig } from 'axios'
2+
import Cookies from 'js-cookie'
3+
const baseURL = import.meta.env.VITE_API_BASE_URL
4+
5+
const axiosInstance = axios.create({
6+
baseURL: baseURL,
7+
withCredentials: true,
8+
headers: {
9+
'Content-Type': 'application/json',
10+
Accept: 'application/json'
11+
}
12+
})
13+
14+
axiosInstance.interceptors.request.use(
15+
(config: InternalAxiosRequestConfig) => {
16+
if (typeof window !== 'undefined') {
17+
const token = Cookies.get('accessToken')
18+
if (token) {
19+
config.headers.Authorization = `Bearer ${token}`
20+
}
21+
}
22+
return config
23+
},
24+
error => Promise.reject(error)
25+
)
26+
27+
axiosInstance.interceptors.response.use(
28+
response => response,
29+
error => {
30+
if (axios.isCancel(error)) {
31+
console.log('요청이 취소되었습니다:', error.message)
32+
} else {
33+
if (error.response) {
34+
switch (error.response.status) {
35+
case 401:
36+
console.error('인증 오류: 다시 로그인하세요.')
37+
break
38+
case 403:
39+
console.error('권한 오류: 접근 권한이 없습니다.')
40+
break
41+
case 404:
42+
console.error('요청한 자원을 찾을 수 없습니다.')
43+
break
44+
case 500:
45+
console.error('서버 오류: 잠시 후 다시 시도하세요.')
46+
break
47+
default:
48+
console.error(`에러 발생: ${error.response.status}`)
49+
}
50+
} else {
51+
console.error('네트워크 오류: 서버에 연결할 수 없습니다.')
52+
}
53+
}
54+
return Promise.reject(error)
55+
}
56+
)
57+
58+
export default axiosInstance

src/views/IconView.vue

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)