API 재활용1

This commit is contained in:
wallace
2025-11-28 14:26:54 +09:00
parent 0963cfdf5b
commit c91dd4a30f
7 changed files with 586 additions and 438 deletions

View File

@@ -1,3 +1,5 @@
import apiService from "@/app/lib/apiService";
export type Course = {
id: string;
courseName: string;
@@ -10,45 +12,13 @@ export type Course = {
// 과목 리스트 조회 API
export async function getCourses(): Promise<Course[]> {
try {
const token = typeof window !== 'undefined'
? (localStorage.getItem('token') || document.cookie
.split('; ')
.find(row => row.startsWith('token='))
?.split('=')[1])
: null;
const response = await apiService.getSubjects();
const data = response.data;
const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL
? process.env.NEXT_PUBLIC_API_BASE_URL
: 'https://hrdi.coconutmeet.net';
const apiUrl = `${baseUrl}/subjects`;
const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
});
if (!response.ok) {
// 404 에러는 리소스가 없는 것이므로 빈 배열 반환
if (response.status === 404) {
console.warn('⚠️ [getCourses] 과목 리스트를 찾을 수 없습니다 (404)');
return [];
}
const errorText = await response.text();
console.error('❌ [getCourses] API 에러 응답:', errorText);
throw new Error(`과목 리스트 조회 실패 (${response.status})`);
}
const data = await response.json();
// 디버깅: API 응답 구조 확인
console.log('🔍 [getCourses] API 원본 응답:', data);
console.log('🔍 [getCourses] 응답 타입:', Array.isArray(data) ? '배열' : typeof data);
// API 응답이 배열이 아닌 경우 처리 (예: { items: [...] } 형태)
let coursesArray: any[] = [];
if (Array.isArray(data)) {