-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathArticleService.js
125 lines (114 loc) · 3 KB
/
ArticleService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
*
*getArticleList : page, pageSize, keyword부분
*/
export async function getProductList(page, pageSize, keyword) {
const res = await fetch(
`https://sprint-mission-api.vercel.app/products?page=${page}&pageSize=${pageSize}&keyword=${keyword}`
)
.then((response) => response.json())
.catch((error) => {
console.log("Error!", error);
throw new Error("데이터를 불러오는데 실패했습니다.");
});
if (!res.ok) {
throw new Error("데이터를 불러오는데 실패했습니다.");
}
const data = await res.json();
return data;
}
/**
*
*getArticle : 그냥 GET메서드
*/
export async function getArticle(id) {
return fetch(`https://sprint-mission-api.vercel.app/articles/${id}`)
.then((res) => {
if (!res.ok) {
throw new Error("데이터를 불러오는데 실패했습니다.");
}
return res.json();
})
.catch((error) => {
console.log("Error!", error);
throw new Error("데이터를 불러오는데 실패했습니다.");
});
}
/**
*
*createArticle : title, content, image
*/
export async function createArticle(title, content, image) {
const res = await fetch("https://sprint-mission-api.vercel.app/articles", {
method: "POST",
body: JSON.stringify({ title, content, image }),
headers: {
"content-type": "application/json",
},
})
.then((res) => {
if (!res.ok) {
throw new Error("데이터를 불러오는데 실패했습니다.");
}
return res.json();
})
.then((data) => {
return data;
})
.catch((error) => {
console.log("Error!", error);
throw new Error("데이터를 불러오는데 실패했습니다.");
});
}
/**
*
*patchArticle : 그냥 PATCH메서드
*/
export async function patchArticle() {
const res = await fetch("https://sprint-mission-api.vercel.app/articles", {
method: "PATCH",
body: JSON.stringify({ title, content, image }),
headers: {
"content-type": "applocation/json",
},
})
.then((res) => {
if (!res.ok) {
throw new Error("데이터를 불러오는데 실패했습니다.");
}
return res.json();
})
.then((data) => {
return data;
})
.catch((error) => {
console.log("Error!", error);
throw new Error("데이터를 불러오는데 실패했습니다.");
});
}
/**
*
*deleteArticle : 그냥 DELETE메서드
*/
export async function deleteArticle() {
const res = await fetch("https://sprint-mission-api.vercel.app/articles", {
method: "DELET",
body: JSON.stringify({ title, content, image }),
headers: {
"content-type": "applocation/json",
},
})
.then((res) => {
if (!res.ok) {
throw new Error("데이터를 불러오는데 실패했습니다.");
}
return res.json();
})
.then((data) => {
return data;
})
.catch((error) => {
console.log("Error!", error);
throw new Error("데이터를 불러오는데 실패했습니다.");
});
}