-
Notifications
You must be signed in to change notification settings - Fork 0
[init/#110] api 기본 세팅 #111
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
Open
Hwanggyuun
wants to merge
8
commits into
develop
Choose a base branch
from
init/#110/api-setting
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f32552
[init] api 기본 세팅
Hwanggyuun 4404713
[fix] build error
Hwanggyuun d27f15e
fix: QueryClient 인스턴스 중복 생성 제거
Hwanggyuun 8db9af8
fix: 항상 truthly 오류 수정
Hwanggyuun 6d95d4f
fix: 응답 래퍼 타입이 백엔드/스웨거 스키마와 불일치
Hwanggyuun 1c47be3
[fix] build error 해결
Hwanggyuun 5cf6b35
[fix] body 오류 수정
Hwanggyuun b5e5b1c
[fix] 쿠키 포함 설정 추가
Hwanggyuun 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
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Empty file.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { ROUTES } from '@router/constant/Routes'; | ||
| import axios from 'axios'; | ||
|
|
||
| const axiosInstance = axios.create({ | ||
| baseURL: import.meta.env.VITE_API_BASE_URL, | ||
| timeout: 10000, | ||
| headers: { | ||
| Accept: 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| axiosInstance.interceptors.request.use( | ||
| config => { | ||
| const token = localStorage.getItem('accessToken'); | ||
| if (token) { | ||
| config.headers.Authorization = `Bearer ${token}`; | ||
| } | ||
| return config; | ||
| }, | ||
| error => { | ||
| return Promise.reject(error); | ||
| } | ||
| ); | ||
|
|
||
| axiosInstance.interceptors.response.use( | ||
| response => { | ||
| return response; | ||
| }, | ||
| error => { | ||
| //후에 이 조건문에 리이슈 로직 추가 | ||
| if (error.response?.status === 401) { | ||
| localStorage.removeItem('accessToken'); | ||
| window.location.href = ROUTES.HOME; | ||
| } | ||
| return Promise.reject(error); | ||
| } | ||
| ); | ||
|
|
||
| export default axiosInstance; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { QueryCache, QueryClient } from '@tanstack/react-query'; | ||
|
|
||
| export const queryClient = new QueryClient({ | ||
| queryCache: new QueryCache({ | ||
| onError: error => { | ||
| if (import.meta.env.DEV) console.error('[React Query Error]', error); | ||
| }, | ||
| }), | ||
| defaultOptions: { | ||
| queries: { | ||
| retry: 1, | ||
| staleTime: 1000 * 60 * 2, | ||
| gcTime: 1000 * 60 * 5, | ||
| }, | ||
| }, | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { isAxiosError, type AxiosRequestConfig } from 'axios'; | ||
| import axiosInstance from '@/api/axiosInstance'; | ||
| import type { BaseResponse } from '@/api/types'; | ||
|
|
||
| export const HTTPMethod = { | ||
| GET: 'GET', | ||
| POST: 'POST', | ||
| PUT: 'PUT', | ||
| DELETE: 'DELETE', | ||
| PATCH: 'PATCH', | ||
| } as const; | ||
|
|
||
| export type HTTPMethodType = (typeof HTTPMethod)[keyof typeof HTTPMethod]; | ||
|
|
||
| export interface RequestConfig { | ||
| method: HTTPMethodType; | ||
| url: string; | ||
| query?: Record<string, string | number | boolean>; | ||
| body?: unknown | FormData; | ||
| headers?: Record<string, string>; | ||
| withCredentials?: boolean; | ||
| } | ||
|
|
||
| export const request = async <T>(config: RequestConfig): Promise<T> => { | ||
| const { method, url, query, body, headers, withCredentials } = config; | ||
|
|
||
| const requestConfig: AxiosRequestConfig = { | ||
| method, | ||
| url, | ||
| params: query, | ||
| data: body, | ||
| withCredentials, | ||
| }; | ||
|
|
||
| if (headers) { | ||
| requestConfig.headers = headers; | ||
| } else if (body && !(body instanceof FormData)) { | ||
| requestConfig.headers = { 'Content-Type': 'application/json' }; | ||
| } | ||
|
|
||
| try { | ||
| const response = await axiosInstance.request<BaseResponse<T>>(requestConfig); | ||
| return response.data.data; | ||
| } catch (error: unknown) { | ||
Hwanggyuun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!isAxiosError(error)) { | ||
| console.error(`[실패] ${url} : 네트워크 오류`); | ||
| throw error; | ||
| } | ||
|
|
||
| if (error.response) { | ||
| const { status, data } = error.response; | ||
| const message = data?.message; | ||
|
|
||
| const displayMessage = status + ' ' + message; | ||
|
|
||
| if (import.meta.env.DEV) { | ||
| console.error(`[실패] ${url} : ${displayMessage}`); | ||
| } | ||
| } else { | ||
| if (import.meta.env.DEV) { | ||
| console.error(`[실패] ${url} : 서버에 연결할 수 없습니다.`); | ||
| } | ||
| } | ||
| throw error; | ||
| } | ||
| }; | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export interface BaseResponse<T> { | ||
| success?: boolean; | ||
| code?: number; | ||
| message?: string; | ||
| data: T; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.