-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: 배포 환경에서 AxiosInterceptor 설정 #219
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "balance-talk", | ||
"name": "pick-o", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,83 @@ | ||
import store from '@/store'; | ||
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios'; | ||
import { AXIOS, END_POINT } from '../constants/api'; | ||
import { HTTPError } from './HttpError'; | ||
|
||
export interface AxiosErrorResponse { | ||
status: number; | ||
httpStatus?: string; | ||
message?: string; | ||
} | ||
|
||
export const axiosInstance = axios.create({ | ||
// baseURL: process.env.API_URL, | ||
// baseURL: '/api', | ||
baseURL: process.env.MSW ? process.env.API_URL : '/api', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
withCredentials: true, | ||
timeout: AXIOS.TIMEOUT, | ||
}); | ||
|
||
// request interceptor (before request) | ||
axiosInstance.interceptors.request.use( | ||
(config: InternalAxiosRequestConfig) => { | ||
if (config.headers.Authorization) return config; | ||
|
||
const { accessToken } = store.getState().token; | ||
const newConfig = { ...config }; | ||
|
||
if (newConfig.url === END_POINT.FILE_UPLOAD) { | ||
newConfig.headers['Content-Type'] = 'multipart/form-data'; | ||
} | ||
if (accessToken) { | ||
newConfig.headers.Authorization = `Bearer ${accessToken}`; | ||
} | ||
|
||
// console.log('요청 전 config', newConfig); | ||
return newConfig; | ||
}, | ||
(error: AxiosError<AxiosErrorResponse>) => { | ||
// console.log('요청 전 config 에러'); | ||
|
||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
// response interceptor (after request) | ||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
// console.log('요청 후 response'); | ||
return response; | ||
}, | ||
(error: AxiosError<AxiosErrorResponse>) => { | ||
// console.log('요청 후 response 에러'); | ||
const originalRequest = error.config; | ||
if (!error.response || !originalRequest) throw error; | ||
|
||
const { data, status } = error.response; | ||
// const refreshToken = localStorage.getItem('rtk'); | ||
|
||
// if (refreshToken) { | ||
// if (status === HTTP_STATUS_CODE.UNAUTHORIZED) { | ||
// const accessToken = getRefreshToken(); | ||
// console.log('new accessToken: ', accessToken); | ||
// localStorage.setItem('accessToken', accessToken); | ||
// store.dispatch({ type: 'token/setAccessToken', payload: accessToken }); | ||
// originalRequest.headers.Authorization = `Bearer ${accessToken}`; | ||
// return axiosInstance(originalRequest); | ||
// console.log('토큰 재발급'); | ||
// } | ||
// if (status === HTTP_STATUS_CODE.BAD_REQUEST) { | ||
// localStorage.removeItem('accessToken'); | ||
// localStorage.removeItem('rtk'); | ||
// window.location.href = '/'; | ||
// } | ||
// } | ||
throw new HTTPError(status, data.httpStatus, data.message); | ||
}, | ||
); | ||
import store from '@/store'; | ||
import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios'; | ||
import { AXIOS, END_POINT } from '../constants/api'; | ||
import { HTTPError } from './HttpError'; | ||
|
||
export interface AxiosErrorResponse { | ||
status: number; | ||
httpStatus?: string; | ||
message?: string; | ||
} | ||
|
||
const baseURL = | ||
process.env.NODE_ENV === 'production' ? process.env.API_URL : '/api'; | ||
|
||
export const axiosInstance = axios.create({ | ||
// baseURL: process.env.API_URL, | ||
// baseURL: '/api', | ||
baseURL, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
withCredentials: true, | ||
timeout: AXIOS.TIMEOUT, | ||
}); | ||
|
||
// request interceptor (before request) | ||
axiosInstance.interceptors.request.use( | ||
(config: InternalAxiosRequestConfig) => { | ||
if (config.headers.Authorization) return config; | ||
|
||
const { accessToken } = store.getState().token; | ||
const newConfig = { ...config }; | ||
|
||
if (newConfig.url === END_POINT.FILE_UPLOAD) { | ||
newConfig.headers['Content-Type'] = 'multipart/form-data'; | ||
} | ||
if (accessToken) { | ||
newConfig.headers.Authorization = `Bearer ${accessToken}`; | ||
} | ||
|
||
// console.log('요청 전 config', newConfig); | ||
return newConfig; | ||
}, | ||
(error: AxiosError<AxiosErrorResponse>) => { | ||
// console.log('요청 전 config 에러'); | ||
|
||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
// response interceptor (after request) | ||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
// console.log('요청 후 response'); | ||
return response; | ||
}, | ||
(error: AxiosError<AxiosErrorResponse>) => { | ||
// console.log('요청 후 response 에러'); | ||
const originalRequest = error.config; | ||
if (!error.response || !originalRequest) throw error; | ||
|
||
const { data, status } = error.response; | ||
// const refreshToken = localStorage.getItem('rtk'); | ||
|
||
// if (refreshToken) { | ||
// if (status === HTTP_STATUS_CODE.UNAUTHORIZED) { | ||
// const accessToken = getRefreshToken(); | ||
// console.log('new accessToken: ', accessToken); | ||
// localStorage.setItem('accessToken', accessToken); | ||
// store.dispatch({ type: 'token/setAccessToken', payload: accessToken }); | ||
// originalRequest.headers.Authorization = `Bearer ${accessToken}`; | ||
// return axiosInstance(originalRequest); | ||
// console.log('토큰 재발급'); | ||
// } | ||
// if (status === HTTP_STATUS_CODE.BAD_REQUEST) { | ||
// localStorage.removeItem('accessToken'); | ||
// localStorage.removeItem('rtk'); | ||
// window.location.href = '/'; | ||
// } | ||
// } | ||
throw new HTTPError(status, data.httpStatus, data.message); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codebase verification
환경 변수 타입 정의와 검증 로직이 필요합니다
코드베이스 분석 결과,
process.env.API_URL
이 여러 곳에서 사용되고 있으나 타입 정의와 유효성 검증이 누락되어 있습니다:src/api/interceptor.ts
에서 production 환경일 때API_URL
사용webpack.config.js
의 프록시 설정다음 작업이 필요합니다:
env.d.ts
파일에API_URL
환경 변수 타입 선언 추가API_URL
값 존재 여부 검증 로직 구현🔗 Analysis chain
환경 변수 타입 안전성 및 유효성 검증 필요
process.env.API_URL
이 production 환경에서 undefined일 경우 문제가 발생할 수 있습니다. 다음 사항들을 고려해주세요:🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 48
Script:
Length of output: 3583