Skip to content

Commit

Permalink
feat: 서버 API 및 fetchAPI 래퍼 추가 pagers-org#20
Browse files Browse the repository at this point in the history
  • Loading branch information
xianeml committed Jan 31, 2022
1 parent f09e812 commit d2009be
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/xianeml/src/api/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import request from '../utils/request.js';
import { Tparams } from '../types/api.js';

/* 카테고리별 메뉴리스트 조회 */
export const getMenus = (category: string) => {
return request({
url: `/api/category/${category}/menu`,
method: 'GET',
});
};

/* 메뉴 생성 */
export const createMenu = ({ category, name }: Tparams) => {
return request({
url: `/api/category/${category}/menu`,
method: 'POST',
data: { name },
});
};

/* 메뉴 이름 수정 */
export const modifyMenu = ({ category, menuId, name }: Tparams) => {
return request({
url: `/api/category/${category}/menu/${menuId}`,
method: 'PUT',
data: { name },
});
};

/* 메뉴 품절 처리 */
export const soleOutMenu = ({ category, menuId }: Tparams) => {
return request({
url: `/api/category/${category}/menu/${menuId}`,
method: 'PUT',
});
};

/* 메뉴 삭제 */
export const deleteMenu = ({ category, menuId }: Tparams) => {
return request({
url: `/api/category/${category}/menu/${menuId}`,
method: 'DELETE',
});
};
5 changes: 5 additions & 0 deletions packages/xianeml/src/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Tparams = {
category: string;
menuId?: string;
name?: string;
};
1 change: 1 addition & 0 deletions packages/xianeml/src/utils/constants/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SERVER_URL = 'http://localhost:3000' as const;
26 changes: 26 additions & 0 deletions packages/xianeml/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SERVER_URL } from '../utils/constants/env.js';

type Tconfig = {
url: string;
method: string;
data?: { name?: string };
};

export default async (config: Tconfig) => {
const { url, method, data } = config;

const requestUrl = SERVER_URL + url;

const response = await fetch(requestUrl, {
method,
body: JSON.stringify(data),
});
console.log('요청정보? >>>> ', response);
const resData = await response.json();

if (response.status === 200) {
return resData;
} else {
throw new Error('서버요청 에러!!');
}
};

0 comments on commit d2009be

Please sign in to comment.