.
This commit is contained in:
21
src/api/auth.ts
Normal file
21
src/api/auth.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// src/api/auth.ts
|
||||
import { http } from './http';
|
||||
import { ApiResponse, LoginRequest, LoginResponse, SignupRequest, VerifyRequest } from '@/types';
|
||||
|
||||
// 1. 회원가입 (이메일 발송)
|
||||
export const signup = async (data: SignupRequest) => {
|
||||
const response = await http.post<ApiResponse<null>>('/api/auth/signup', data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 2. 이메일 인증 코드 확인
|
||||
export const verifyEmail = async (data: VerifyRequest) => {
|
||||
const response = await http.post<ApiResponse<null>>('/api/auth/verify', data);
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// 3. 로그인 (토큰 발급)
|
||||
export const login = async (data: LoginRequest) => {
|
||||
const response = await http.post<ApiResponse<LoginResponse>>('/api/auth/login', data);
|
||||
return response.data;
|
||||
};
|
||||
9
src/api/category.ts
Normal file
9
src/api/category.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/api/category.ts
|
||||
import { http } from './http';
|
||||
import { ApiResponse, Category } 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 반환
|
||||
};
|
||||
44
src/api/http.ts
Normal file
44
src/api/http.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import axios from 'axios';
|
||||
|
||||
export const http = axios.create({
|
||||
baseURL: 'http://localhost:8080', // 백엔드 주소 확인
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
withCredentials: true, // 쿠키 사용 시 필요
|
||||
});
|
||||
|
||||
// 🟢 요청 인터셉터 추가 (범인 검거 현장)
|
||||
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}`;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// 응답 인터셉터 (에러 처리용, 선택 사항)
|
||||
http.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response && error.response.status === 401) {
|
||||
// 토큰 만료 시 로그아웃 처리 등을 여기서 할 수 있음
|
||||
console.error('인증 실패: 토큰이 만료되었거나 유효하지 않습니다.');
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
19
src/api/image.ts
Normal file
19
src/api/image.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
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', // 👈 여기! 이거 추가하면 해결됩니다.
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
};
|
||||
36
src/api/posts.ts
Normal file
36
src/api/posts.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// src/api/posts.ts
|
||||
import { http } from './http';
|
||||
import { ApiResponse, PostListResponse } from '@/types'; // ApiResponse 타입 추가
|
||||
import { 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;
|
||||
};
|
||||
|
||||
export const getPostBySlug = 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;
|
||||
}
|
||||
|
||||
// 게시글 생성
|
||||
export const createPost = async (data: CreatePostRequest) => {
|
||||
// 👇 여기를 수정했습니다! (/api/posts -> /api/admin/posts)
|
||||
const response = await http.post<ApiResponse<any>>('/api/admin/posts', data);
|
||||
return response.data;
|
||||
};
|
||||
Reference in New Issue
Block a user