Files
sam-react-prod/src/components/board/DynamicBoard/actions.ts
유병철 55e0791e16 refactor(WEB): Server Action 공통화 및 보안 강화
- executeServerAction 공통 유틸 도입으로 actions.ts 대폭 간소화 (50+개 파일)
- sanitize 유틸 추가 (XSS 방지)
- middleware CSP 헤더 추가 및 Open Redirect 방지
- 프록시 라우트 로깅 개발환경 한정으로 변경
- 프로덕션 불필요 console.log 제거

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-09 16:14:06 +09:00

117 lines
4.1 KiB
TypeScript

'use server';
import { executeServerAction, type ActionResult } from '@/lib/api/execute-server-action';
import type {
PostApiData,
PostPaginationResponse,
PostFilters,
CommentApiData,
CommentsApiResponse,
} from '@/components/customer-center/shared/types';
const API_URL = process.env.NEXT_PUBLIC_API_URL;
// ===== 게시글 API =====
export async function getDynamicBoardPosts(
boardCode: string, filters?: PostFilters
): Promise<ActionResult<PostPaginationResponse>> {
const params = new URLSearchParams();
if (filters?.search) params.append('search', filters.search);
if (filters?.is_notice !== undefined) params.append('is_notice', String(filters.is_notice));
if (filters?.status) params.append('status', filters.status);
if (filters?.per_page) params.append('per_page', String(filters.per_page));
if (filters?.page) params.append('page', String(filters.page));
const queryString = params.toString();
return executeServerAction<PostPaginationResponse>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts${queryString ? `?${queryString}` : ''}`,
errorMessage: '게시글 목록 조회에 실패했습니다.',
});
}
export async function getDynamicBoardPost(
boardCode: string, postId: number | string
): Promise<ActionResult<PostApiData>> {
return executeServerAction<PostApiData>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}`,
errorMessage: '게시글을 찾을 수 없습니다.',
});
}
export async function createDynamicBoardPost(
boardCode: string,
data: { title: string; content: string; is_secret?: boolean; is_notice?: boolean; custom_fields?: Record<string, string> }
): Promise<ActionResult<PostApiData>> {
return executeServerAction<PostApiData>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts`,
method: 'POST',
body: data,
errorMessage: '게시글 등록에 실패했습니다.',
});
}
export async function updateDynamicBoardPost(
boardCode: string, postId: number | string,
data: { title?: string; content?: string; is_secret?: boolean; is_notice?: boolean; custom_fields?: Record<string, string> }
): Promise<ActionResult<PostApiData>> {
return executeServerAction<PostApiData>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}`,
method: 'PUT',
body: data,
errorMessage: '게시글 수정에 실패했습니다.',
});
}
export async function deleteDynamicBoardPost(
boardCode: string, postId: number | string
): Promise<ActionResult> {
return executeServerAction({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}`,
method: 'DELETE',
errorMessage: '게시글 삭제에 실패했습니다.',
});
}
// ===== 댓글 API =====
export async function getDynamicBoardComments(
boardCode: string, postId: number | string
): Promise<ActionResult<CommentsApiResponse>> {
return executeServerAction<CommentsApiResponse>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}/comments`,
errorMessage: '댓글 목록 조회에 실패했습니다.',
});
}
export async function createDynamicBoardComment(
boardCode: string, postId: number | string, content: string
): Promise<ActionResult<CommentApiData>> {
return executeServerAction<CommentApiData>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}/comments`,
method: 'POST',
body: { content },
errorMessage: '댓글 등록에 실패했습니다.',
});
}
export async function updateDynamicBoardComment(
boardCode: string, postId: number | string, commentId: number | string, content: string
): Promise<ActionResult<CommentApiData>> {
return executeServerAction<CommentApiData>({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}/comments/${commentId}`,
method: 'PUT',
body: { content },
errorMessage: '댓글 수정에 실패했습니다.',
});
}
export async function deleteDynamicBoardComment(
boardCode: string, postId: number | string, commentId: number | string
): Promise<ActionResult> {
return executeServerAction({
url: `${API_URL}/api/v1/boards/${boardCode}/posts/${postId}/comments/${commentId}`,
method: 'DELETE',
errorMessage: '댓글 삭제에 실패했습니다.',
});
}