Skip to content

Commit 20b0216

Browse files
authored
Merge pull request #155 from kakao-tech-campus-3rd-step3/feat/dev-cors-proxy#154
[FEAT] Vite ํ”„๋ก์‹œ ๋„์ž…์„ ํ†ตํ•œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ฐœ์„  ๋ฐ API ํ˜ธ์ถœ ๋ฆฌํŒฉํ„ฐ๋ง (#154)
2 parents e3ea7fd + bdb0b46 commit 20b0216

File tree

9 files changed

+104
-88
lines changed

9 files changed

+104
-88
lines changed
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { Comment } from '@/pages/admin/ApplicationDetail/types/comments';
22

33
export const fetchComments = async (applicationId: number): Promise<Comment[]> => {
4-
const response = await fetch(
5-
import.meta.env.VITE_API_BASE_URL + `/applications/${applicationId}/comments`,
6-
);
4+
const url = `/api/applications/${applicationId}/comments`;
5+
const response = await fetch(url);
6+
7+
if (!response.ok) throw new Error('Failed to fetch comments');
78
return await response.json();
89
};
910

@@ -12,26 +13,26 @@ export const createComment = async (
1213
content: string,
1314
rating: number,
1415
): Promise<Comment> => {
15-
const response = await fetch(
16-
import.meta.env.VITE_API_BASE_URL + `/applications/${applicationId}/comments`,
17-
{
18-
method: 'POST',
19-
headers: {
20-
'Content-Type': 'application/json',
21-
},
22-
body: JSON.stringify({ content, rating }),
16+
const url = `/api/applications/${applicationId}/comments`;
17+
const response = await fetch(url, {
18+
method: 'POST',
19+
headers: {
20+
'Content-Type': 'application/json',
2321
},
24-
);
22+
body: JSON.stringify({ content, rating }),
23+
});
24+
25+
if (!response.ok) throw new Error('Failed to create comment');
2526
return await response.json();
2627
};
2728

2829
export const deleteComment = async (applicationId: number, commentId: number): Promise<void> => {
29-
await fetch(
30-
import.meta.env.VITE_API_BASE_URL + `/applications/${applicationId}/comments/${commentId}`,
31-
{
32-
method: 'DELETE',
33-
},
34-
);
30+
const url = `/api/applications/${applicationId}/comments/${commentId}`;
31+
const response = await fetch(url, {
32+
method: 'DELETE',
33+
});
34+
35+
if (!response.ok) throw new Error('Failed to delete comment');
3536
};
3637

3738
export const updateComment = async (
@@ -40,15 +41,15 @@ export const updateComment = async (
4041
content: string,
4142
rating: number,
4243
): Promise<Comment> => {
43-
const response = await fetch(
44-
import.meta.env.VITE_API_BASE_URL + `/applications/${applicationId}/comments/${commentId}`,
45-
{
46-
method: 'PUT',
47-
headers: {
48-
'Content-Type': 'application/json',
49-
},
50-
body: JSON.stringify({ content, rating }),
44+
const url = `/api/applications/${applicationId}/comments/${commentId}`;
45+
const response = await fetch(url, {
46+
method: 'PATCH',
47+
headers: {
48+
'Content-Type': 'application/json',
5149
},
52-
);
50+
body: JSON.stringify({ content, rating }),
51+
});
52+
53+
if (!response.ok) throw new Error('Failed to update comment');
5354
return await response.json();
5455
};

โ€Žsrc/pages/admin/ApplicationDetail/api/detailApplication.tsโ€Ž

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@ export const fetchDetailApplication = async (
44
clubId: number,
55
applicantId: number,
66
): Promise<DetailApplication> => {
7-
const response = await fetch(
8-
import.meta.env.VITE_API_BASE_URL + `/clubs/${clubId}/applicants/${applicantId}/application`,
9-
);
7+
const url = `/api/clubs/${clubId}/applicants/${applicantId}/application`;
8+
const response = await fetch(url);
9+
10+
if (!response.ok) throw new Error('์ง€์›์„œ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค');
1011
return await response.json();
1112
};
1213

1314
export const updateApplicationStatus = async (
1415
applicationId: number,
1516
status: DetailApplication['status'],
16-
): Promise<void> => {
17-
await fetch(import.meta.env.VITE_API_BASE_URL + `/applications/${applicationId}`, {
17+
): Promise<unknown> => {
18+
const url = `/api/applications/${applicationId}`;
19+
const response = await fetch(url, {
1820
method: 'PATCH',
1921
headers: {
2022
'Content-Type': 'application/json',
2123
},
2224
body: JSON.stringify({ status }),
2325
});
26+
27+
if (!response.ok) throw new Error('์ง€์›์„œ ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค');
28+
return await response.json();
2429
};
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import type { ClubDetailEdit } from '../types/clubDetailEdit';
22

3-
const BASE_URL = import.meta.env.VITE_API_BASE_URL;
4-
53
export const fetchClubDetailEdit = async (clubId: string | number): Promise<ClubDetailEdit> => {
6-
const res = await fetch(`${BASE_URL}/clubs/${clubId}`);
7-
if (!res.ok) {
4+
const url = `/api/clubs/${clubId}`;
5+
const response = await fetch(url);
6+
7+
if (!response.ok) {
88
throw new Error('๋™์•„๋ฆฌ ์ƒ์„ธ ์ˆ˜์ • ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
99
}
10-
return res.json() as Promise<ClubDetailEdit>;
10+
return response.json() as Promise<ClubDetailEdit>;
1111
};
12-
1312
export const updateClubDetailEdit = async (
1413
clubId: string | number,
1514
updatedData: Partial<ClubDetailEdit>,
1615
): Promise<ClubDetailEdit> => {
17-
const res = await fetch(`${BASE_URL}/clubs/${clubId}`, {
16+
const url = `/api/clubs/${clubId}`;
17+
const response = await fetch(url, {
1818
method: 'POST',
1919
headers: {
2020
'Content-Type': 'application/json',
2121
},
2222
body: JSON.stringify(updatedData),
2323
});
2424

25-
if (!res.ok) {
25+
if (!response.ok) {
2626
throw new Error('๋™์•„๋ฆฌ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•˜๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
2727
}
2828

29-
return res.json() as Promise<ClubDetailEdit>;
29+
return response.json() as Promise<ClubDetailEdit>;
3030
};
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
import type {
2-
ApplicantData,
3-
ApplicationFilterOption,
4-
} from '@/pages/admin/Dashboard/types/dashboard';
1+
import type { ApplicantData } from '@/pages/admin/Dashboard/types/dashboard';
52

6-
export const fetchApplicants = async (
7-
clubId: number,
8-
status?: ApplicationFilterOption,
9-
): Promise<ApplicantData[]> => {
10-
const url = new URL(
11-
import.meta.env.VITE_API_BASE_URL + `/clubs/${clubId}/applicants`,
12-
window.location.origin,
13-
);
14-
if (status && status !== 'ALL') {
15-
url.searchParams.set('status', status);
3+
export const fetchApplicants = async (clubId: number): Promise<ApplicantData[]> => {
4+
const url = `/api/clubs/${clubId}/dashboard/applicants`;
5+
const response = await fetch(url);
6+
7+
if (!response.ok) {
8+
const errorText = await response.text();
9+
throw new Error(`HTTP ${response.status}: ${errorText}`);
1610
}
1711

18-
const response = await fetch(url.toString());
1912
return await response.json();
2013
};

โ€Žsrc/pages/admin/Dashboard/hooks/useApplicants.tsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const useApplicants = (
1212
): UseApiQueryResult<ApplicantData[]> => {
1313
const { data, isLoading, error } = useQuery({
1414
queryKey: ['applicants', clubId, status],
15-
queryFn: () => fetchApplicants(clubId, status),
15+
queryFn: () => fetchApplicants(clubId),
1616
staleTime: 1000 * 60 * 2,
1717
});
1818

โ€Žsrc/pages/user/Apply/api/apply.tsโ€Ž

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import type {
66
} from '@/pages/user/Apply/type/apply.ts';
77

88
export const fetchApplicationForm = async (Id: number): Promise<ApplicationForm> => {
9-
const response = await fetch(import.meta.env.VITE_API_BASE_URL + `/clubs/${Id}/apply`);
9+
const url = `/api/clubs/${Id}/apply`;
10+
const response = await fetch(url);
11+
12+
if (!response.ok) throw new Error('์ง€์›์„œ ์–‘์‹์„ ๊ฐ€์ ธ์˜ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค');
1013
return await response.json();
1114
};
1215

@@ -17,16 +20,16 @@ export const postApplicationForm = async (
1720
): Promise<ApplicationFormRequest> => {
1821
const applicationDto = applicationFormDto(formData, questionArray);
1922

20-
const response = await fetch(
21-
import.meta.env.VITE_API_BASE_URL + `/clubs/${clubId}/apply-submit`,
22-
{
23-
method: 'POST',
24-
headers: {
25-
'Content-Type': 'application/json',
26-
},
27-
body: JSON.stringify(applicationDto),
23+
const url = `/api/clubs/${clubId}/apply-submit`;
24+
const response = await fetch(url, {
25+
method: 'POST',
26+
headers: {
27+
'Content-Type': 'application/json',
2828
},
29-
);
29+
body: JSON.stringify(applicationDto),
30+
});
31+
32+
if (!response.ok) throw new Error('์ง€์›์„œ ์–‘์‹์„ ์ œ์ถœํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค');
3033
return await response.json();
3134
};
3235

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import type { ClubDetail } from '../types/clubDetail';
22

3-
const BASE_URL = import.meta.env.VITE_API_BASE_URL;
4-
53
export const fetchClubDetail = async (clubId: string | number): Promise<ClubDetail> => {
6-
const res = await fetch(`${BASE_URL}/clubs/${clubId}`);
7-
if (!res.ok) throw new Error('๋™์•„๋ฆฌ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
8-
return res.json() as Promise<ClubDetail>;
4+
const url = `/api/clubs/${clubId}`;
5+
const response = await fetch(url);
6+
7+
if (!response.ok) {
8+
throw new Error('๋™์•„๋ฆฌ ์ƒ์„ธ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.');
9+
}
10+
return response.json() as Promise<ClubDetail>;
911
};

โ€Žsrc/pages/user/Main/api/club.tsโ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export type ClubResponse = {
66
};
77

88
export async function getClubsByCategory(filter: ClubCategoryEng): Promise<ClubResponse> {
9-
const response = await fetch(
10-
import.meta.env.VITE_API_BASE_URL + `/clubs/search/category?category=${filter}`,
11-
);
9+
const url = `/api/clubs?category=${filter}`;
10+
const response = await fetch(url);
11+
1212
return await response.json();
1313
}

โ€Žvite.config.tsโ€Ž

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import react from '@vitejs/plugin-react';
2-
import { defineConfig } from 'vite';
2+
import { defineConfig, loadEnv, type ConfigEnv } from 'vite';
33

44
// https://vite.dev/config/
5-
export default defineConfig({
6-
plugins: [
7-
react({
8-
babel: {
9-
plugins: ['@emotion/babel-plugin'],
5+
export default ({ mode }: ConfigEnv) => {
6+
const env = loadEnv(mode, process.cwd(), '');
7+
return defineConfig({
8+
plugins: [
9+
react({
10+
babel: {
11+
plugins: ['@emotion/babel-plugin'],
12+
},
13+
}),
14+
],
15+
resolve: {
16+
alias: {
17+
'@': '/src',
1018
},
11-
}),
12-
],
13-
resolve: {
14-
alias: {
15-
'@': '/src',
1619
},
17-
},
18-
});
20+
server: {
21+
proxy: {
22+
'/api': {
23+
target: env.VITE_API_BASE_URL,
24+
changeOrigin: true,
25+
secure: false,
26+
},
27+
},
28+
},
29+
});
30+
};

0 commit comments

Comments
ย (0)