Skip to content
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

Open
wants to merge 9 commits into
base: basic-조형민
Choose a base branch
from
80 changes: 80 additions & 0 deletions ArticleService.js
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/",
Copy link
Collaborator

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 요런식으로요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵! 명심하겠습니다. 안 그래도 다음 미션할 때 base url에 / 했다가..계속 에러가 나서 한참을 헤맸었습니다 ㅠㅠ

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로 구현 연습 */
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;

// }
5 changes: 5 additions & 0 deletions PrintError.js
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}`
);
}
51 changes: 51 additions & 0 deletions ProductService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import axios from "axios";

const instance = axios.create({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axios.create로 인스턴스까지 만드실줄 아신다면, 저는 axios 관련 js 를 만들어서 ArticleSerice, ProductService에서 같은 인스턴스로 빼볼듯 합니다! 중복된 코드 제거는 언제나 좋습니다 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 });
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
* 랜딩 페이지 레이아웃/링크
* 로그인 페이지 레이아웃/링크
* 회원 가입 페이지 레이아웃/링크
2. **스프린트 미션 3**
* 비밀번호 보이기/숨기기 버튼 기능 구현
* 로그인/회원가입 입력값 유효성 체크
* 로그인/회원가입 버튼 활성화
* 사용자 데이터 확인 후 에러메시지 팝업(모달) 또는 페이지 이동
* 반응형 디자인(PC, 태블릿, 모바일)
3. **스프린트 미션 4**
* API를 이용한 비동기 처리 함수 구현
48 changes: 0 additions & 48 deletions css/reset.css

This file was deleted.

Loading