1+ const BASE_URL = `${ process . env . NEXT_PUBLIC_BASE_URL } /items` ;
2+
13export async function getData ( ) {
2- const BASE_URL = `${ process . env . NEXT_PUBLIC_BASE_URL } /items` ;
34 try {
45 const response = await fetch ( BASE_URL ) ;
56 if ( ! response . ok ) {
@@ -12,3 +13,44 @@ export async function getData() {
1213 console . log ( "에러 발생" , error ) ;
1314 }
1415}
16+
17+ export async function postData ( todoData : string ) {
18+ try {
19+ const response = await fetch ( BASE_URL , {
20+ method : "POST" ,
21+ headers : {
22+ "Content-Type" : "application/json" ,
23+ } ,
24+ body : JSON . stringify ( { name : todoData } ) ,
25+ } ) ;
26+ if ( ! response . ok ) {
27+ throw new Error ( "서버 요청 실패" + response . status ) ;
28+ }
29+ const data = response . json ( ) ;
30+ return data ;
31+ } catch ( error ) {
32+ console . log ( "에러 발생" , error ) ;
33+ }
34+ }
35+
36+ export async function patchData (
37+ itemId : number ,
38+ updateData : Partial < { name : string ; isCompleted : boolean } >
39+ ) {
40+ try {
41+ const response = await fetch ( `${ BASE_URL } /${ itemId } ` , {
42+ method : "PATCH" ,
43+ headers : {
44+ "Content-Type" : "application/json" ,
45+ } ,
46+ body : JSON . stringify ( updateData ) ,
47+ } ) ;
48+ if ( ! response . ok ) {
49+ throw new Error ( "서버 요청 실패" + response . status ) ;
50+ }
51+ const data = response . json ( ) ;
52+ return data ;
53+ } catch ( error ) {
54+ console . log ( "에러 발생" , error ) ;
55+ }
56+ }
0 commit comments