Skip to content

Commit

Permalink
Merge pull request #234 from always97/fe-api-exception
Browse files Browse the repository at this point in the history
[FE] feat#233#210 API 뢄리 및 ν•¨μˆ˜ κ΅¬ν˜„ & μ„œλΉ„μŠ€ 전체 μ˜ˆμ™Έμƒν™© 리슀트 체크
  • Loading branch information
always97 authored Nov 21, 2024
2 parents ef7dcd5 + 3d369b3 commit f1c7bdf
Show file tree
Hide file tree
Showing 30 changed files with 601 additions and 22 deletions.
91 changes: 91 additions & 0 deletions FE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion FE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@emotion/styled": "^11.13.0",
"@mui/material": "^6.1.6",
"@tanstack/react-query": "^5.59.19",
"axios": "^1.7.7",
"framer-motion": "^11.11.11",
"lottie-react": "^2.4.0",
"lottie-web": "^5.12.2",
Expand Down Expand Up @@ -49,4 +50,4 @@
"public"
]
}
}
}
52 changes: 52 additions & 0 deletions FE/src/api/rest/authApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios from 'axios';
import axiosInstance from './instance';

type LoginResponse = {
result: string;
};

export async function login(email: string, password: string): Promise<LoginResponse | null> {
try {
const payload = { email, password };

const response = await axiosInstance.post<LoginResponse>('/api/auth/login', payload);

console.log('Login Successful:', response.data);
return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Login Error:', error.response?.data || error.message);
} else {
console.error('Unexpected Error:', error);
}

return null;
}
}

type SignupPayload = {
email: string;
password: string;
nickname: string;
};

type SignupResponse = {
result: string;
};

export async function signUp(data: SignupPayload): Promise<SignupResponse | null> {
try {
const response = await axiosInstance.post<SignupResponse>('/api/auth/signup', data);
console.log('Signup Successful:', response.data);

return response.data;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Signup Error:', error.response?.data || error.message);
} else {
console.error('Unexpected Error:', error);
}

return null;
}
}
53 changes: 53 additions & 0 deletions FE/src/api/rest/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import axios from 'axios';

const BASE_URL = 'test';

const axiosInstance = axios.create({
baseURL: BASE_URL,
timeout: 10000,
headers: {
'Content-Type': 'application/json'
}
});

axiosInstance.interceptors.request.use(
(config) => {
const token = localStorage.getItem('accessToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);

// axiosInstance.interceptors.response.use(
// (response) => {
// if (response.status === 404) {
// console.log('404 νŽ˜μ΄μ§€λ‘œ 이동');
// }

// return response;
// },
// async (error) => {
// if (error.response?.status === 401) {
// // isTokenExpired() - 토큰 만료 μ—¬λΆ€λ₯Ό ν™•μΈν•˜λŠ” ν•¨μˆ˜
// // tokenRefresh() - 토큰을 κ°±μ‹ ν•΄μ£ΌλŠ” ν•¨μˆ˜
// if (isTokenExpired()) await tokenRefresh();

// const accessToken = getToken();

// error.config.headers = {
// 'Content-Type': 'application/json',
// Authorization: `Bearer ${accessToken}`
// };

// // μ€‘λ‹¨λœ μš”μ²­μ„(μ—λŸ¬λ‚œ μš”μ²­)을 토큰 κ°±μ‹  ν›„ μž¬μš”μ²­
// const response = await axios.request(error.config);
// return response;
// }
// return Promise.reject(error);
// }
// );

export default axiosInstance;
82 changes: 82 additions & 0 deletions FE/src/api/rest/quizApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import axiosInstance from './instance';
import {
CreateQuizSetPayload,
CreateQuizSetResponse,
QuizSet,
QuizSetDetailResponse,
QuizSetListResponse
} from './quizTypes';

// ν€΄μ¦ˆμ…‹ λͺ©λ‘ 쑰회 (검색, νŽ˜μ΄μ§• κ³ λ €)
export async function getQuizSetList(
category: string,
cursor: string,
take: number,
search: string
): Promise<QuizSetListResponse | null> {
try {
const params = { category, cursor, take, search };
const response = await axiosInstance.get<QuizSetListResponse>('/api/quizset', { params });

return response.data;
} catch (error) {
console.error('Error fetching quiz set list:', error);
return null;
}
}

// ν€΄μ¦ˆμ…‹ ν•˜λ‚˜ 쑰회
export async function getQuizSetDetail(id: string): Promise<QuizSetDetailResponse | null> {
try {
const response = await axiosInstance.get<QuizSetDetailResponse>(`/api/quizset/${id}`);
return response.data;
} catch (error) {
console.error('Error fetching quiz set detail:', error);
return null;
}
}
// μ œμž‘ν•œ ν€΄μ¦ˆμ…‹ λͺ©λ‘ 쑰회 (둜그인 된 μ‚¬μš©μž)
export async function getMyQuizSets(): Promise<QuizSet[] | null> {
try {
const response = await axiosInstance.get<QuizSet[]>('/api/quizset/private');
return response.data;
} catch (error) {
console.error('Error fetching private quiz sets:', error);
return null;
}
}

// ν€΄μ¦ˆμ…‹ 생성
export async function createQuizSet(
data: CreateQuizSetPayload
): Promise<CreateQuizSetResponse | null> {
try {
const response = await axiosInstance.post<CreateQuizSetResponse>('/api/quizset', data);
return response.data;
} catch (error) {
console.error('Error creating quiz set:', error);
return null;
}
}
// ν€΄μ¦ˆμ…‹ μˆ˜μ •
export async function updateQuizSet(
id: string,
data: CreateQuizSetPayload
): Promise<CreateQuizSetResponse | null> {
try {
const response = await axiosInstance.patch<CreateQuizSetResponse>(`/api/quizset/${id}`, data);
return response.data;
} catch (error) {
console.error('Error updating quiz set:', error);
return null;
}
}
// ν€΄μ¦ˆμ…‹ μ‚­μ œ
export async function deleteQuizSet(id: string): Promise<void> {
try {
await axiosInstance.delete(`/api/quizset/${id}`);
console.log(`Quiz set ${id} deleted successfully`);
} catch (error) {
console.error('Error deleting quiz set:', error);
}
}
Loading

0 comments on commit f1c7bdf

Please sign in to comment.