기간 필터링 추가
This commit is contained in:
@@ -2,28 +2,28 @@
|
||||
* 대시보드 API 클라이언트
|
||||
*/
|
||||
|
||||
import { DashboardElement } from '@/components/admin/dashboard/types';
|
||||
import { DashboardElement } from "@/components/admin/dashboard/types";
|
||||
|
||||
// API 기본 설정
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api';
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001/api";
|
||||
|
||||
// 토큰 가져오기 (실제 인증 시스템에 맞게 수정)
|
||||
function getAuthToken(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
return localStorage.getItem('authToken') || sessionStorage.getItem('authToken');
|
||||
if (typeof window === "undefined") return null;
|
||||
return localStorage.getItem("authToken") || sessionStorage.getItem("authToken");
|
||||
}
|
||||
|
||||
// API 요청 헬퍼
|
||||
async function apiRequest<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
endpoint: string,
|
||||
options: RequestInit = {},
|
||||
): Promise<{ success: boolean; data?: T; message?: string; pagination?: any }> {
|
||||
const token = getAuthToken();
|
||||
|
||||
|
||||
const config: RequestInit = {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(token && { 'Authorization': `Bearer ${token}` }),
|
||||
"Content-Type": "application/json",
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
...options.headers,
|
||||
},
|
||||
...options,
|
||||
@@ -31,32 +31,32 @@ async function apiRequest<T>(
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE_URL}${endpoint}`, config);
|
||||
|
||||
|
||||
// 응답이 JSON이 아닐 수도 있으므로 안전하게 처리
|
||||
let result;
|
||||
try {
|
||||
result = await response.json();
|
||||
} catch (jsonError) {
|
||||
console.error('JSON Parse Error:', jsonError);
|
||||
console.error("JSON Parse Error:", jsonError);
|
||||
throw new Error(`서버 응답을 파싱할 수 없습니다. Status: ${response.status}`);
|
||||
}
|
||||
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('API Error Response:', {
|
||||
console.error("API Error Response:", {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
result
|
||||
result,
|
||||
});
|
||||
throw new Error(result.message || `HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
} catch (error: any) {
|
||||
console.error('API Request Error:', {
|
||||
console.error("API Request Error:", {
|
||||
endpoint,
|
||||
error: error?.message || error,
|
||||
errorObj: error,
|
||||
config
|
||||
config,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
@@ -99,154 +99,153 @@ export interface DashboardListQuery {
|
||||
|
||||
// 대시보드 API 함수들
|
||||
export const dashboardApi = {
|
||||
|
||||
/**
|
||||
* 대시보드 생성
|
||||
*/
|
||||
async createDashboard(data: CreateDashboardRequest): Promise<Dashboard> {
|
||||
const result = await apiRequest<Dashboard>('/dashboards', {
|
||||
method: 'POST',
|
||||
const result = await apiRequest<Dashboard>("/dashboards", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || '대시보드 생성에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 생성에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return result.data;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 대시보드 목록 조회
|
||||
*/
|
||||
async getDashboards(query: DashboardListQuery = {}) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (query.page) params.append('page', query.page.toString());
|
||||
if (query.limit) params.append('limit', query.limit.toString());
|
||||
if (query.search) params.append('search', query.search);
|
||||
if (query.category) params.append('category', query.category);
|
||||
if (typeof query.isPublic === 'boolean') params.append('isPublic', query.isPublic.toString());
|
||||
|
||||
|
||||
if (query.page) params.append("page", query.page.toString());
|
||||
if (query.limit) params.append("limit", query.limit.toString());
|
||||
if (query.search) params.append("search", query.search);
|
||||
if (query.category) params.append("category", query.category);
|
||||
if (typeof query.isPublic === "boolean") params.append("isPublic", query.isPublic.toString());
|
||||
|
||||
const queryString = params.toString();
|
||||
const endpoint = `/dashboards${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
const endpoint = `/dashboards${queryString ? `?${queryString}` : ""}`;
|
||||
|
||||
const result = await apiRequest<Dashboard[]>(endpoint);
|
||||
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || '대시보드 목록 조회에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 목록 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
dashboards: result.data || [],
|
||||
pagination: result.pagination
|
||||
pagination: result.pagination,
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 내 대시보드 목록 조회
|
||||
*/
|
||||
async getMyDashboards(query: DashboardListQuery = {}) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (query.page) params.append('page', query.page.toString());
|
||||
if (query.limit) params.append('limit', query.limit.toString());
|
||||
if (query.search) params.append('search', query.search);
|
||||
if (query.category) params.append('category', query.category);
|
||||
|
||||
|
||||
if (query.page) params.append("page", query.page.toString());
|
||||
if (query.limit) params.append("limit", query.limit.toString());
|
||||
if (query.search) params.append("search", query.search);
|
||||
if (query.category) params.append("category", query.category);
|
||||
|
||||
const queryString = params.toString();
|
||||
const endpoint = `/dashboards/my${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
const endpoint = `/dashboards/my${queryString ? `?${queryString}` : ""}`;
|
||||
|
||||
const result = await apiRequest<Dashboard[]>(endpoint);
|
||||
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || '내 대시보드 목록 조회에 실패했습니다.');
|
||||
throw new Error(result.message || "내 대시보드 목록 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
dashboards: result.data || [],
|
||||
pagination: result.pagination
|
||||
pagination: result.pagination,
|
||||
};
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 대시보드 상세 조회
|
||||
*/
|
||||
async getDashboard(id: string): Promise<Dashboard> {
|
||||
const result = await apiRequest<Dashboard>(`/dashboards/${id}`);
|
||||
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || '대시보드 조회에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return result.data;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 공개 대시보드 조회 (인증 불필요)
|
||||
*/
|
||||
async getPublicDashboard(id: string): Promise<Dashboard> {
|
||||
const result = await apiRequest<Dashboard>(`/dashboards/public/${id}`);
|
||||
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || '대시보드 조회에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return result.data;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 대시보드 수정
|
||||
*/
|
||||
async updateDashboard(id: string, data: Partial<CreateDashboardRequest>): Promise<Dashboard> {
|
||||
const result = await apiRequest<Dashboard>(`/dashboards/${id}`, {
|
||||
method: 'PUT',
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || '대시보드 수정에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 수정에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return result.data;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 대시보드 삭제
|
||||
*/
|
||||
async deleteDashboard(id: string): Promise<void> {
|
||||
const result = await apiRequest(`/dashboards/${id}`, {
|
||||
method: 'DELETE',
|
||||
method: "DELETE",
|
||||
});
|
||||
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || '대시보드 삭제에 실패했습니다.');
|
||||
throw new Error(result.message || "대시보드 삭제에 실패했습니다.");
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* 공개 대시보드 목록 조회 (인증 불필요)
|
||||
*/
|
||||
async getPublicDashboards(query: DashboardListQuery = {}) {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (query.page) params.append('page', query.page.toString());
|
||||
if (query.limit) params.append('limit', query.limit.toString());
|
||||
if (query.search) params.append('search', query.search);
|
||||
if (query.category) params.append('category', query.category);
|
||||
|
||||
|
||||
if (query.page) params.append("page", query.page.toString());
|
||||
if (query.limit) params.append("limit", query.limit.toString());
|
||||
if (query.search) params.append("search", query.search);
|
||||
if (query.category) params.append("category", query.category);
|
||||
|
||||
const queryString = params.toString();
|
||||
const endpoint = `/dashboards/public${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
const endpoint = `/dashboards/public${queryString ? `?${queryString}` : ""}`;
|
||||
|
||||
const result = await apiRequest<Dashboard[]>(endpoint);
|
||||
|
||||
|
||||
if (!result.success) {
|
||||
throw new Error(result.message || '공개 대시보드 목록 조회에 실패했습니다.');
|
||||
throw new Error(result.message || "공개 대시보드 목록 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
dashboards: result.data || [],
|
||||
pagination: result.pagination
|
||||
pagination: result.pagination,
|
||||
};
|
||||
},
|
||||
|
||||
@@ -254,17 +253,41 @@ export const dashboardApi = {
|
||||
* 쿼리 실행 (차트 데이터 조회)
|
||||
*/
|
||||
async executeQuery(query: string): Promise<{ columns: string[]; rows: any[]; rowCount: number }> {
|
||||
const result = await apiRequest<{ columns: string[]; rows: any[]; rowCount: number }>('/dashboards/execute-query', {
|
||||
method: 'POST',
|
||||
const result = await apiRequest<{ columns: string[]; rows: any[]; rowCount: number }>("/dashboards/execute-query", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ query }),
|
||||
});
|
||||
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || '쿼리 실행에 실패했습니다.');
|
||||
throw new Error(result.message || "쿼리 실행에 실패했습니다.");
|
||||
}
|
||||
|
||||
|
||||
return result.data;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 테이블 스키마 조회 (날짜 컬럼 감지용)
|
||||
*/
|
||||
async getTableSchema(tableName: string): Promise<{
|
||||
tableName: string;
|
||||
columns: Array<{ name: string; type: string; udtName: string }>;
|
||||
dateColumns: string[];
|
||||
}> {
|
||||
const result = await apiRequest<{
|
||||
tableName: string;
|
||||
columns: Array<{ name: string; type: string; udtName: string }>;
|
||||
dateColumns: string[];
|
||||
}>("/dashboards/table-schema", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ tableName }),
|
||||
});
|
||||
|
||||
if (!result.success || !result.data) {
|
||||
throw new Error(result.message || "테이블 스키마 조회에 실패했습니다.");
|
||||
}
|
||||
|
||||
return result.data;
|
||||
},
|
||||
};
|
||||
|
||||
// 에러 처리 유틸리티
|
||||
@@ -272,10 +295,10 @@ export function handleApiError(error: any): string {
|
||||
if (error.message) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
if (typeof error === 'string') {
|
||||
|
||||
if (typeof error === "string") {
|
||||
return error;
|
||||
}
|
||||
|
||||
return '알 수 없는 오류가 발생했습니다.';
|
||||
|
||||
return "알 수 없는 오류가 발생했습니다.";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user