-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[구은모] sprint4 #39
base: basic-구은모
Are you sure you want to change the base?
The head ref may contain hidden characters: "basic-\uAD6C\uC740\uBAA8-sprint4"
[구은모] sprint4 #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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", { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기는 위에 처럼 return fetch 만 해줘도 될것 같습니다 |
||
method: "POST", | ||
body: JSON.stringify({ title, content, image }), | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}) | ||
.then((res) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에서도 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 method에서 오타입니다. |
||
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("데이터를 불러오는데 실패했습니다."); | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* | ||
*getProductList : page, pageSize, keyword부분 | ||
*/ | ||
|
||
export async function getProductList(page, pageSize, keyword) { | ||
try { | ||
const res = await fetch( | ||
`https://sprint-mission-api.vercel.app/products?page=${page}&pageSize=${pageSize}&keyword=${keyword}` | ||
); | ||
|
||
if (!res.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다."); | ||
} | ||
|
||
const data = await res.json(); | ||
return data; | ||
} catch (error) { | ||
console.log("Error!", error); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*getProduct : GET메서드 | ||
*/ | ||
|
||
export async function getProduct(id) { | ||
try { | ||
const res = await fetch( | ||
`https://sprint-mission-api.vercel.app/products/${id}` | ||
); | ||
|
||
if (!res.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다."); | ||
} | ||
|
||
const data = await res.json(); | ||
return data; | ||
} catch (error) { | ||
console.log("Error!!", error); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*createProduct POST메서드 : name, description, price, tags, images부분 | ||
*/ | ||
|
||
export async function createProduct(name, description, price, tags, images) { | ||
try { | ||
const res = await fetch("https://sprint-mission-api.vercel.app/products", { | ||
method: "POST", | ||
body: JSON.stringify({ name, description, price, tags, images }), | ||
headers: { | ||
"content-type": "applocation/json", | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다."); | ||
} | ||
|
||
const data = await res.json(); | ||
return data; | ||
} catch (error) { | ||
console.log("Error!!!", error); | ||
} | ||
} | ||
|
||
// PATCH메서드 | ||
export async function patchProduct() { | ||
try { | ||
const res = await fetch("https://sprint-mission-api.vercel.app/products", { | ||
method: "PATCH", | ||
body: JSON.stringify(data), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pathProduct 함수 파라미터에 data가 빠져있어서 여기서 참조를 할수 없습니다ㅠ |
||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다."); | ||
} | ||
|
||
const data = await res.json(); | ||
return data; | ||
} catch (error) { | ||
console.log("Error!!!", error); | ||
} | ||
} | ||
// DELETE메서드 | ||
export async function deleteProduct() { | ||
try { | ||
const res = await fetch("https://sprint-mission-api.vercel.app/products", { | ||
method: "DELETE", | ||
body: JSON.stringify(), | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error("데이터를 불러오는데 실패했습니다."); | ||
} | ||
|
||
const data = await res.json(); | ||
return data; | ||
} catch (error) { | ||
console.log("Error!!!", error); | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://sprint-mission-api.vercel.app 이부분까지는 따로 변수로 빼서 사용하는게 가독성도 좋고, 만약 host가 변경되었을때 일일이 수정안해줘도 되어서 좋습니다 ㅎㅎ