pricelees ef903cf267 [#22] 프론트엔드 React 전환 및 인증 API 수정 (#23)
<!-- 제목 양식 -->
<!-- [이슈번호] 작업 요약 (예시: [#10] Gitea 템플릿 생성) -->

## 📝 관련 이슈 및 PR

**PR과 관련된 이슈 번호**
- #22

##  작업 내용
<!-- 어떤 작업을 했는지 알려주세요! -->
- 기존 Thymeleaf 기반의 프론트엔드 코드를 React + Typescript 기반으로 마이그레이션
- 프론트엔드 분리에 따른 인증 API 수정 및 회원가입 API 추가

## 🧪 테스트
<!-- 어떤 테스트를 생각했고 진행했는지 알려주세요! -->
- 새로 추가된 API, 변경된 API 테스트 반영

## 📚 참고 자료 및 기타
<!-- 참고한 자료, 또는 논의할 사항이 있다면 알려주세요! -->
프론트엔드 코드는 Gemini CLI가 구현하였고, API 관련 코드(ee21782ef9, frontend/src/api/**) 만 직접 구성

Reviewed-on: #23
Co-authored-by: pricelees <priceelees@gmail.com>
Co-committed-by: pricelees <priceelees@gmail.com>
2025-07-27 03:39:20 +00:00

82 lines
2.4 KiB
TypeScript

import axios, { type AxiosError, type AxiosRequestConfig, type Method } from 'axios';
const apiClient = axios.create({
baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:8080',
timeout: 10000,
});
export const isLoginRequiredError = (error: any): boolean => {
if (!axios.isAxiosError(error) || !error.response) {
return false;
}
const LOGIN_REQUIRED_ERROR_CODE = ['A001', 'A002', 'A003', 'A006'];
const code = error.response?.data?.code;
return code && LOGIN_REQUIRED_ERROR_CODE.includes(code);
}
async function request<T>(
method: Method,
endpoint: string,
data: object = {},
isRequiredAuth: boolean = false
): Promise<T> {
const config: AxiosRequestConfig = {
method,
url: endpoint,
headers: {
'Content-Type': 'application/json'
},
};
if (isRequiredAuth) {
const accessToken = localStorage.getItem('accessToken');
if (accessToken) {
if (!config.headers) {
config.headers = {};
}
config.headers['Authorization'] = `Bearer ${accessToken}`;
}
}
if (method.toUpperCase() !== 'GET') {
config.data = data;
}
try {
const response = await apiClient.request(config);
return response.data.data;
} catch (error: unknown) {
const axiosError = error as AxiosError<{ code: string, message: string }>;
console.error('API 요청 실패:', axiosError);
throw axiosError;
}
}
async function get<T>(endpoint: string, isRequiredAuth: boolean = false): Promise<T> {
return request<T>('GET', endpoint, {}, isRequiredAuth);
}
async function post<T>(endpoint: string, data: object = {}, isRequiredAuth: boolean = false): Promise<T> {
return request<T>('POST', endpoint, data, isRequiredAuth);
}
async function put<T>(endpoint: string, data: object = {}, isRequiredAuth: boolean = false): Promise<T> {
return request<T>('PUT', endpoint, data, isRequiredAuth);
}
async function patch<T>(endpoint: string, data: object = {}, isRequiredAuth: boolean = false): Promise<T> {
return request<T>('PATCH', endpoint, data, isRequiredAuth);
}
async function del<T>(endpoint: string, isRequiredAuth: boolean = false): Promise<T> {
return request<T>('DELETE', endpoint, {}, isRequiredAuth);
}
export default {
get,
post,
put,
patch,
del
};