Skip to content

Commit 1eae942

Browse files
committed
feat: Post 공용 fetch API 추가
1 parent 2f82de3 commit 1eae942

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

apps/web/entities/post/model/api.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { httpClient } from '@/shared/api/http-client';
2+
import { CreatePostDTO, Post, UpdatePostDTO } from '@/shared/types/post-types';
3+
4+
// 게시글 생성 (POST /posts)
5+
export const createPost = async (postData: CreatePostDTO): Promise<Post> => {
6+
return httpClient<Post>('/posts', {
7+
method: 'POST',
8+
body: JSON.stringify(postData),
9+
});
10+
};
11+
12+
// 모든 게시글 조회 (GET /posts)
13+
export const getPosts = async (): Promise<Post[]> => {
14+
return httpClient<Post[]>('/posts');
15+
};
16+
17+
// 특정 게시글 조회 (GET /posts/{id})
18+
export const getPostById = async (postId: string): Promise<Post> => {
19+
return httpClient<Post>(`/posts/${postId}`);
20+
};
21+
22+
// 게시글 수정 (PUT /posts/{id})
23+
export const updatePost = async (
24+
postId: string,
25+
updatedData: UpdatePostDTO
26+
): Promise<Post> => {
27+
return httpClient<Post>(`/posts/${postId}`, {
28+
method: 'PUT',
29+
body: JSON.stringify(updatedData),
30+
});
31+
};
32+
33+
// 게시글 삭제 (DELETE /posts/{id})
34+
export const deletePost = async (
35+
postId: string
36+
): Promise<{ message: string }> => {
37+
return httpClient<{ message: string }>(`/posts/${postId}`, {
38+
method: 'DELETE',
39+
});
40+
};

apps/web/shared/api/http-client.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const BASE_URL = 'http://localhost:4000/api';
2+
3+
export const httpClient = async <T>(
4+
endpoint: string,
5+
options?: RequestInit
6+
): Promise<T> => {
7+
const response = await fetch(`${BASE_URL}${endpoint}`, {
8+
headers: {
9+
'Content-Type': 'application/json',
10+
},
11+
...options,
12+
});
13+
14+
if (!response.ok) {
15+
throw new Error(`API 요청 실패: ${response.status}`);
16+
}
17+
18+
return response.json();
19+
};

apps/web/shared/types/post-types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export interface Post {
2+
id: string;
3+
title: string;
4+
content: string;
5+
author: string;
6+
createdAt: string;
7+
updatedAt?: string;
8+
}
9+
10+
export type CreatePostDTO = Pick<Post, 'title' | 'content' | 'author'>;
11+
export type UpdatePostDTO = Partial<CreatePostDTO>;
12+
13+
interface CursorPaginationResponse<T> {
14+
data: T[];
15+
nextCursor: string | null;
16+
hasMore: boolean;
17+
}
18+
interface OffsetPaginationResponse<T> {
19+
data: T[];
20+
currentPage: number;
21+
totalPages: number;
22+
hasMore: boolean;
23+
}
24+
25+
export type GetPostsCursor = CursorPaginationResponse<Post>;
26+
export type GetPostsOffset = OffsetPaginationResponse<Post>;

0 commit comments

Comments
 (0)