File tree Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Expand file tree Collapse file tree 3 files changed +85
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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 > ;
You can’t perform that action at this time.
0 commit comments