chore: add deployment config
This commit is contained in:
@@ -1,9 +1,27 @@
|
||||
// src/api/category.ts
|
||||
import { http } from './http';
|
||||
import { ApiResponse, Category } from '@/types';
|
||||
import { ApiResponse, Category, CategoryCreateRequest, CategoryUpdateRequest } from '@/types';
|
||||
|
||||
// 카테고리 트리 구조 조회 (GET /api/categories)
|
||||
export const getCategories = async () => {
|
||||
const response = await http.get<ApiResponse<Category[]>>('/api/categories');
|
||||
return response.data.data; // ApiResponse로 감싸져 있으므로 .data.data 반환
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
// 카테고리 생성 (POST /api/admin/categories)
|
||||
export const createCategory = async (data: CategoryCreateRequest) => {
|
||||
const response = await http.post<ApiResponse<Category>>('/api/admin/categories', data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 카테고리 수정/이동 (PUT /api/admin/categories/{id})
|
||||
// 백엔드 반환값이 ApiResponse<Nothing> 이므로 data는 null입니다.
|
||||
export const updateCategory = async (id: number, data: CategoryUpdateRequest) => {
|
||||
const response = await http.put<ApiResponse<null>>(`/api/admin/categories/${id}`, data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 카테고리 삭제 (DELETE /api/admin/categories/{id})
|
||||
export const deleteCategory = async (id: number) => {
|
||||
const response = await http.delete<ApiResponse<null>>(`/api/admin/categories/${id}`);
|
||||
return response.data;
|
||||
};
|
||||
39
src/api/comments.ts
Normal file
39
src/api/comments.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { http } from './http';
|
||||
import { ApiResponse, Comment, CommentSaveRequest, CommentDeleteRequest } from '@/types';
|
||||
|
||||
// 1. 댓글 목록 조회
|
||||
export const getComments = async (postSlug: string) => {
|
||||
const response = await http.get<ApiResponse<Comment[]>>('/api/comments', {
|
||||
params: { postSlug },
|
||||
});
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
// 2. 댓글 작성
|
||||
export const createComment = async (data: CommentSaveRequest) => {
|
||||
const response = await http.post<ApiResponse<null>>('/api/comments', data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 3. 댓글 삭제
|
||||
export const deleteComment = async (id: number, password?: string) => {
|
||||
// Axios의 delete 메서드에서 body를 보내려면 data 속성을 사용해야 합니다.
|
||||
const config = password ? { data: { guestPassword: password } as CommentDeleteRequest } : undefined;
|
||||
|
||||
const response = await http.delete<ApiResponse<null>>(`/api/comments/${id}`, config);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 4. 관리자 댓글 목록 조회 (대시보드용)
|
||||
export const getAdminComments = async (page = 0, size = 20) => {
|
||||
const response = await http.get<ApiResponse<any>>('/api/admin/comments', {
|
||||
params: { page, size },
|
||||
});
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
// 5. 관리자 댓글 강제 삭제
|
||||
export const deleteAdminComment = async (id: number) => {
|
||||
const response = await http.delete<ApiResponse<null>>(`/api/admin/comments/${id}`);
|
||||
return response.data;
|
||||
};
|
||||
120
src/api/http.ts
120
src/api/http.ts
@@ -1,44 +1,118 @@
|
||||
import axios from 'axios';
|
||||
import { useAuthStore } from '@/store/authStore';
|
||||
|
||||
// 🛠️ 환경 변수 처리 (배포 환경 대응)
|
||||
const BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080';
|
||||
|
||||
export const http = axios.create({
|
||||
baseURL: 'http://localhost:8080', // 백엔드 주소 확인
|
||||
baseURL: BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
withCredentials: true, // 쿠키 사용 시 필요
|
||||
withCredentials: true, // 쿠키(RefreshToken) 전송을 위해 필수
|
||||
});
|
||||
|
||||
// 🟢 요청 인터셉터 추가 (범인 검거 현장)
|
||||
// 1. 요청 인터셉터: 헤더에 AccessToken 주입
|
||||
http.interceptors.request.use(
|
||||
(config) => {
|
||||
// 1. 로컬 스토리지에서 zustand가 저장한 데이터 꺼내기
|
||||
const storage = localStorage.getItem('auth-storage');
|
||||
|
||||
if (storage) {
|
||||
// Zustand는 { state: { ... }, version: 0 } 형태로 저장함
|
||||
const parsedStorage = JSON.parse(storage);
|
||||
const token = parsedStorage.state?.accessToken;
|
||||
|
||||
// 2. 토큰이 있다면 헤더에 심어주기
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
const { accessToken } = useAuthStore.getState();
|
||||
if (accessToken) {
|
||||
config.headers['Authorization'] = `Bearer ${accessToken}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
(error) => Promise.reject(error)
|
||||
);
|
||||
|
||||
// 응답 인터셉터 (에러 처리용, 선택 사항)
|
||||
// --- 토큰 갱신 관련 변수 ---
|
||||
let isRefreshing = false;
|
||||
let failedQueue: any[] = [];
|
||||
|
||||
// 실패한 요청들을 큐에 담아두었다가 토큰 갱신 후 재시도하는 함수
|
||||
const processQueue = (error: any, token: string | null = null) => {
|
||||
failedQueue.forEach((prom) => {
|
||||
if (error) {
|
||||
prom.reject(error);
|
||||
} else {
|
||||
prom.resolve(token);
|
||||
}
|
||||
});
|
||||
failedQueue = [];
|
||||
};
|
||||
|
||||
// 2. 응답 인터셉터: 401 또는 403 발생 시 토큰 갱신
|
||||
http.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
// 토큰 만료 시 로그아웃 처리 등을 여기서 할 수 있음
|
||||
console.error('인증 실패: 토큰이 만료되었거나 유효하지 않습니다.');
|
||||
async (error) => {
|
||||
const originalRequest = error.config;
|
||||
const status = error.response?.status; // 응답 상태 코드 확인
|
||||
|
||||
// 401(Unauthorized) 또는 403(Forbidden) 에러이고, 아직 재시도하지 않은 요청인 경우
|
||||
if ((status === 401 || status === 403) && !originalRequest._retry) {
|
||||
// 이미 갱신 중이라면 큐에 넣고 대기
|
||||
if (isRefreshing) {
|
||||
return new Promise((resolve, reject) => {
|
||||
failedQueue.push({ resolve, reject });
|
||||
})
|
||||
.then((token) => {
|
||||
// 대기하던 요청들도 새 토큰으로 헤더 교체 후 재시도
|
||||
if (originalRequest.headers) {
|
||||
originalRequest.headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
return http(originalRequest);
|
||||
})
|
||||
.catch((err) => Promise.reject(err));
|
||||
}
|
||||
|
||||
originalRequest._retry = true; // 재시도 플래그 설정 (무한 루프 방지)
|
||||
isRefreshing = true;
|
||||
|
||||
try {
|
||||
const { refreshToken, login, logout } = useAuthStore.getState();
|
||||
|
||||
// RefreshToken이 없으면 갱신 시도 없이 바로 로그아웃
|
||||
if (!refreshToken) {
|
||||
throw new Error('No refresh token available');
|
||||
}
|
||||
|
||||
// 🛠️ 토큰 갱신 요청
|
||||
// 중요: refresh 요청도 쿠키/CORS 처리를 위해 withCredentials: true 추가
|
||||
const { data } = await axios.post(
|
||||
`${BASE_URL}/api/auth/refresh`,
|
||||
{ refreshToken },
|
||||
{ withCredentials: true }
|
||||
);
|
||||
|
||||
// 새 토큰 저장
|
||||
const newAccessToken = data.data.accessToken;
|
||||
const newRefreshToken = data.data.refreshToken || refreshToken;
|
||||
|
||||
login(newAccessToken, newRefreshToken); // 스토어 업데이트
|
||||
|
||||
// 큐에 대기 중이던 요청들 처리 (새 토큰 전달)
|
||||
processQueue(null, newAccessToken);
|
||||
|
||||
// 실패했던 원래 요청 재시도 (헤더 안전하게 교체)
|
||||
if (originalRequest.headers) {
|
||||
originalRequest.headers['Authorization'] = `Bearer ${newAccessToken}`;
|
||||
}
|
||||
return http(originalRequest);
|
||||
|
||||
} catch (refreshError) {
|
||||
// 갱신 실패 시 로그아웃 및 큐 정리
|
||||
processQueue(refreshError, null);
|
||||
useAuthStore.getState().logout();
|
||||
|
||||
// 브라우저 환경에서만 로그인 페이지로 이동
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
return Promise.reject(refreshError);
|
||||
} finally {
|
||||
isRefreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
@@ -1,18 +1,13 @@
|
||||
import { http } from './http';
|
||||
import { ApiResponse } from '@/types';
|
||||
|
||||
// 이미지 업로드 (POST /api/admin/images)
|
||||
export const uploadImage = async (file: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append('image', file);
|
||||
|
||||
// 👇 헤더에 Content-Type을 'multipart/form-data'로 명시하거나,
|
||||
// 아예 지워서(undefined) 브라우저가 알아서 boundary를 붙이게 해야 합니다.
|
||||
// 가장 안전한 방법은 'Content-Type': 'multipart/form-data'를 명시하는 것입니다.
|
||||
|
||||
const response = await http.post<ApiResponse<string>>('/api/admin/images', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data', // 👈 여기! 이거 추가하면 해결됩니다.
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
|
||||
@@ -1,36 +1,53 @@
|
||||
// src/api/posts.ts
|
||||
import { http } from './http';
|
||||
import { ApiResponse, PostListResponse } from '@/types'; // ApiResponse 타입 추가
|
||||
import { Post } from '@/types';
|
||||
import { ApiResponse, PostListResponse, Post } from '@/types';
|
||||
|
||||
export const getPosts = async (page = 0, size = 10, categoryId?: number, search?: string) => {
|
||||
const params: any = { page, size };
|
||||
|
||||
if (categoryId) params.categoryId = categoryId;
|
||||
if (search) params.search = search;
|
||||
|
||||
// 1. 응답 타입을 ApiResponse<PostListResponse>로 변경
|
||||
const response = await http.get<ApiResponse<PostListResponse>>('/api/posts', { params });
|
||||
|
||||
// 2. response.data는 { code, message, data: {...} } 형태입니다.
|
||||
// 우리가 필요한 건 그 안의 data(실제 게시글 목록)이므로 .data를 한번 더 접근합니다.
|
||||
return response.data.data;
|
||||
// 1. 게시글 목록 조회 (검색, 카테고리, 태그 필터링 지원)
|
||||
export const getPosts = async (params?: {
|
||||
page?: number;
|
||||
size?: number;
|
||||
keyword?: string;
|
||||
category?: string;
|
||||
tag?: string;
|
||||
}) => {
|
||||
const response = await http.get<ApiResponse<PostListResponse>>('/api/posts', {
|
||||
params: {
|
||||
...params,
|
||||
sort: 'createdAt,desc',
|
||||
}
|
||||
});
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
export const getPostBySlug = async (slug: string) => {
|
||||
// 2. 카테고리별 게시글 조회
|
||||
export const getPostsByCategory = async (categoryName: string, page = 0, size = 10) => {
|
||||
return getPosts({
|
||||
page,
|
||||
size,
|
||||
category: categoryName
|
||||
});
|
||||
};
|
||||
|
||||
// 3. 게시글 상세 조회
|
||||
export const getPost = async (slug: string) => {
|
||||
const response = await http.get<ApiResponse<Post>>(`/api/posts/${slug}`);
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
export interface CreatePostRequest {
|
||||
title: string;
|
||||
content: string;
|
||||
categoryId: number;
|
||||
}
|
||||
// 4. 게시글 작성 (추가됨)
|
||||
// PostSaveRequest 타입에 맞춰 데이터를 보냅니다.
|
||||
export const createPost = async (data: any) => {
|
||||
const response = await http.post<ApiResponse<Post>>('/api/admin/posts', data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 게시글 생성
|
||||
export const createPost = async (data: CreatePostRequest) => {
|
||||
// 👇 여기를 수정했습니다! (/api/posts -> /api/admin/posts)
|
||||
const response = await http.post<ApiResponse<any>>('/api/admin/posts', data);
|
||||
// 5. 게시글 수정 (추가됨)
|
||||
export const updatePost = async (id: number, data: any) => {
|
||||
const response = await http.put<ApiResponse<Post>>(`/api/admin/posts/${id}`, data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 6. 게시글 삭제 (추가됨)
|
||||
export const deletePost = async (id: number) => {
|
||||
const response = await http.delete<ApiResponse<null>>(`/api/admin/posts/${id}`);
|
||||
return response.data;
|
||||
};
|
||||
14
src/api/profile.ts
Normal file
14
src/api/profile.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { http } from './http';
|
||||
import { ApiResponse, Profile, ProfileUpdateRequest } from '@/types';
|
||||
|
||||
// 블로그 프로필 정보 조회 (GET /api/profile)
|
||||
export const getProfile = async () => {
|
||||
const response = await http.get<ApiResponse<Profile>>('/api/profile');
|
||||
return response.data.data;
|
||||
};
|
||||
|
||||
// 프로필 수정 (PUT /api/admin/profile) - 관리자 전용
|
||||
export const updateProfile = async (data: ProfileUpdateRequest) => {
|
||||
const response = await http.put<ApiResponse<Profile>>('/api/admin/profile', data);
|
||||
return response.data.data;
|
||||
};
|
||||
Reference in New Issue
Block a user