-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from always97/fe-api-exception
[FE] feat#233#210 API λΆλ¦¬ λ° ν¨μ ꡬν & μλΉμ€ μ 체 μμΈμν© λ¦¬μ€νΈ 체ν¬
- Loading branch information
Showing
30 changed files
with
601 additions
and
22 deletions.
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
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.