Skip to content

Commit 37f59f1

Browse files
committed
fix: api호출 부분 수정
1 parent dc47041 commit 37f59f1

File tree

6 files changed

+24
-43
lines changed

6 files changed

+24
-43
lines changed

src/components/ui/DeploymentDetailModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ const DeploymentDetailModal: React.FC<Props> = ({
5050
useEffect(() => {
5151
const fetchDetail = async () => {
5252
try {
53-
const res = await apiFetch(
53+
const data = await apiFetch(
5454
`/api/projects/${projectId}/terraform/deployments/${deploymentId}`
5555
);
56-
setDetail(res.data);
56+
setDetail(data);
5757
} catch (err) {
5858
toast.error("배포 상세 정보를 불러오지 못했습니다.");
5959
} finally {

src/components/ui/MainHeader.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useProjectStore } from "../../stores";
44
import { useBlockStore } from "../../stores/blockStore";
55
import { useAuth } from "../../stores/authStore";
66
import { useNavigate } from "react-router-dom";
7+
import { apiFetch } from "../../utils/apiClients";
78

89
const MainHeader: React.FC<{
910
onSaveProject: () => void;
@@ -37,22 +38,18 @@ const MainHeader: React.FC<{
3738
setProjectName(trimmed);
3839

3940
try {
40-
const response = await fetch(`/api/projects/${projectId}`, {
41+
// apiFetch 사용으로 토큰 갱신 자동 처리
42+
await apiFetch(`/api/projects/${projectId}`, {
4143
method: "PUT",
4244
headers: {
4345
"Content-Type": "application/json",
44-
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
4546
},
4647
body: JSON.stringify({
4748
name: trimmed,
4849
description,
4950
}),
5051
});
5152

52-
if (!response.ok) {
53-
throw new Error(`(${response.status}) 서버 응답 오류`);
54-
}
55-
5653
// 필요 시 서버 응답에서 name을 다시 읽어 동기화 가능
5754
} catch (err: any) {
5855
alert("❌ 제목 저장 실패: " + err.message);

src/components/ui/ProjectDeploymentCard.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ const ProjectDeploymentCard: React.FC<Props> = ({
7979
useEffect(() => {
8080
const fetchDeployments = async () => {
8181
try {
82-
const res = await apiFetch(
82+
const data = await apiFetch(
8383
`/api/projects/${projectId}/terraform/deployments?size=1`
8484
);
85-
setDeployments(res.data?.deployments || []);
85+
setDeployments(data?.deployments || []);
8686
} catch (err) {
8787
toast.error(
8888
`프로젝트 ${projectName}의 배포 이력을 불러오지 못했습니다.`,

src/hooks/useDeployment.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export const useDeployment = (projectId: string | undefined) => {
6161
}
6262
);
6363

64-
const isValid = validateRes?.data?.valid;
64+
// apiFetch가 이미 unwrapped data를 반환
65+
const isValid = validateRes?.valid;
6566

6667
if (!isValid) {
6768
setLoadingStatus(null);
@@ -82,8 +83,9 @@ export const useDeployment = (projectId: string | undefined) => {
8283
}
8384
);
8485

85-
const status = applyRes?.data?.status;
86-
const message = applyRes?.data?.message;
86+
// apiFetch가 이미 unwrapped data를 반환
87+
const status = applyRes?.status;
88+
const message = applyRes?.message;
8789

8890
if (status === 'SUCCESS' || status === 'PENDING') {
8991
toast.success(`🚀 배포 요청 완료: ${message}`);

src/pages/ProjectEditorPage.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,8 @@ terraform {
14661466
}
14671467
);
14681468

1469-
const isValid = validateRes?.data?.valid;
1469+
// apiFetch가 이미 unwrapped data를 반환
1470+
const isValid = validateRes?.valid;
14701471

14711472
if (!isValid) {
14721473
setLoadingStatus(null); // ✅ 중단 시 로딩 종료
@@ -1487,8 +1488,9 @@ terraform {
14871488
}
14881489
);
14891490

1490-
const status = applyRes?.data?.status;
1491-
const message = applyRes?.data?.message;
1491+
// apiFetch가 이미 unwrapped data를 반환
1492+
const status = applyRes?.status;
1493+
const message = applyRes?.message;
14921494

14931495
if (status === "SUCCESS" || status === "PENDING") {
14941496
toast.success(`🚀 배포 요청 완료: ${message}`);

src/services/projectService.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
// src/services/projectService.ts
22
import { useProjectStore } from "../stores/projectStore";
3-
import { TokenStorage } from "./authService";
4-
import type { ApiResponse } from "../types/auth";
5-
6-
const API_BASE_URL =
7-
import.meta.env.VITE_API_BASE_URL || "http://localhost:8080";
3+
import { apiFetch } from "../utils/apiClients";
84

95
export async function createProject(name: string, description: string) {
10-
const accessToken = TokenStorage.getAccessToken();
11-
12-
const response = await fetch(`${API_BASE_URL}/api/projects`, {
6+
// apiFetch는 이미 토큰 갱신 및 에러 처리를 포함하고 있음
7+
// 응답: {success: true, data: {id, name, description, ...}} -> apiFetch가 data 부분만 반환
8+
const project = await apiFetch("/api/projects", {
139
method: "POST",
1410
headers: {
1511
"Content-Type": "application/json",
16-
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
1712
},
1813
body: JSON.stringify({ name, description }),
19-
credentials: "include",
2014
});
2115

22-
const data: ApiResponse = await response.json();
23-
24-
if (!data.success || !data.data) {
25-
const errorMsg = data.error?.message || "프로젝트 생성 실패";
26-
const fields = data.error?.fields;
27-
28-
// 필드별 에러가 있으면 사용자에게 친절하게 표시
29-
if (fields) {
30-
const fieldErrors = Object.values(fields).join(", ");
31-
throw new Error(fieldErrors || errorMsg);
32-
}
33-
34-
throw new Error(errorMsg);
35-
}
36-
37-
useProjectStore.getState().loadProject(data.data);
38-
return data.data;
16+
// project는 이미 unwrapped된 {id, name, description, createdAt, updatedAt}
17+
useProjectStore.getState().loadProject(project);
18+
return project;
3919
}

0 commit comments

Comments
 (0)