-
Notifications
You must be signed in to change notification settings - Fork 15
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 #30
base: basic-조형민
Are you sure you want to change the base?
The head ref may contain hidden characters: "basic-\uC870\uD615\uBBFC-sprint4"
[조형민] sprint4 #30
Changes from all commits
be45b97
3d4b393
863203a
e9a3091
a7bab21
5de8f72
1cba686
24710f0
8870879
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import axios from "axios"; | ||
import { PrintError } from "./PrintError"; | ||
|
||
const instance = axios.create({ | ||
baseURL: "https://sprint-mission-api.vercel.app/", | ||
timeout: 3000, | ||
}); | ||
|
||
export function getArticleList(params = {}) { | ||
instance | ||
.get("/articles", { params }) | ||
.then((res) => console.log(res.data)) | ||
.catch((e) => PrintError(e)); | ||
} | ||
|
||
export function getArticle(id) { | ||
instance | ||
.get(`/articles/${id}`) | ||
.then((res) => console.log(res.data)) | ||
.catch((e) => PrintError(e)); | ||
} | ||
|
||
export function createArticle(article) { | ||
instance | ||
.post("/articles", article) | ||
.then((res) => console.log(res.data)) | ||
.catch((e) => PrintError(e)); | ||
} | ||
|
||
export function patchArticle(id, article) { | ||
instance | ||
.patch(`/articles/${id}`, article) | ||
.then((res) => console.log(res.data)) | ||
.catch((e) => PrintError(e)); | ||
} | ||
|
||
export function deleteArticle(id) { | ||
instance | ||
.delete(`/articles/${id}`) | ||
.then((res) => console.log(res.data)) | ||
.catch((e) => PrintError(e)); | ||
} | ||
|
||
/* fetch로 구현 연습 */ | ||
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. 오.. fetch로도 따로 구현하시고, axios로 작업하셧군요! |
||
// export async function getArticleList(params = {}) { | ||
// const url = new URL('https://sprint-mission-api.vercel.app/articles'); | ||
// Object.keys(params).forEach(element => { | ||
// url.searchParams.append(element, params[element]); | ||
// }); | ||
|
||
// const res = await fetch(url); | ||
// const data = await res.json(); | ||
// return data; | ||
|
||
// } | ||
|
||
// export async function getArticle(id) { | ||
// const res = await fetch(`https://sprint-mission-api.vercel.app/articles/${id}`); | ||
// const data = await res.json(); | ||
// return data; | ||
// } | ||
|
||
// export function getArticle(id) { | ||
// fetch(`https://sprint-mission-api.vercel.app/articles/${id}`) | ||
// .then((response) => response.json()) | ||
// .then((data) => { console.log(data); }); | ||
// } | ||
|
||
// export async function createArticle(article) { | ||
// const res = fetch('https://sprint-mission-api.vercel.app/articles', { | ||
// method: 'POST', | ||
// body: JSON.stringify(article), | ||
// headers: { | ||
// 'Content-Type': 'application/json' | ||
// }, | ||
// }); | ||
// const data = (await res).json(); | ||
// return data; | ||
|
||
// } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export function PrintError(e) { | ||
console.log( | ||
`status: ${e.response.status}\nmessage: ${e.response.data.message}` | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import axios from "axios"; | ||
|
||
const instance = axios.create({ | ||
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. axios.create로 인스턴스까지 만드실줄 아신다면, 저는 axios 관련 js 를 만들어서 ArticleSerice, ProductService에서 같은 인스턴스로 빼볼듯 합니다! 중복된 코드 제거는 언제나 좋습니다 ㅎㅎ 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. 당장은 무슨 뜻인지 잘 모르겠지만 잘 생각해보겠습니닷! 😳 |
||
baseURL: "https://sprint-mission-api.vercel.app/", | ||
timeout: 3000, | ||
}); | ||
|
||
export async function getProductList(params = {}) { | ||
try { | ||
const res = await instance.get("/products", { params }); | ||
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. 오 여기는 async await 을 하셨군요 try catch로 잘 처리 된것 같습니다! |
||
return res.data; | ||
} catch (e) { | ||
PrintError(e); | ||
} | ||
} | ||
|
||
export async function getProduct(id) { | ||
try { | ||
const res = await instance.get(`/products/${id}`); | ||
return res.data; | ||
} catch (e) { | ||
PrintError(e); | ||
} | ||
} | ||
|
||
export async function createProduct(product) { | ||
try { | ||
const res = await instance.post("/products", product); | ||
return res.data; | ||
} catch (e) { | ||
PrintError(e); | ||
} | ||
} | ||
|
||
export async function patchProduct(id, product) { | ||
try { | ||
const res = await instance.patch(`/products/${id}`, product); | ||
return res.data; | ||
} catch (e) { | ||
PrintError(e); | ||
} | ||
} | ||
|
||
export async function deleteProduct(id) { | ||
try { | ||
const res = await instance.delete(`/products/${id}`); | ||
return res.data; | ||
} catch (e) { | ||
PrintError(e); | ||
} | ||
} |
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.
axios 로 작성하셨다니 좋습니다~ 다만 baseUrl 에 마지막에 슬래시는 없어도 될것 같습니다!
아래쪽에 인스턴스에서 get 등을 호출할때 "/articles" 이런식으로 호출되다보니
https://sprint-mission-api.vercel.app//articles 이런식으로 들어갑니다. 동작은 하지만 그래도 슬래시 하나 빼주는게 더 좋을것 같아요!
baseURL: https://sprint-mission-api.vercel.app
요런식으로요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.
넵! 명심하겠습니다. 안 그래도 다음 미션할 때 base url에 / 했다가..계속 에러가 나서 한참을 헤맸었습니다 ㅠㅠ