Skip to content

Commit

Permalink
refactor: 구조분해 할당 적용으로 코드 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
Creative-Lee committed Jun 9, 2023
1 parent a3aad30 commit 70ac1ce
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/apis/cart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ const cartProductsParser = (data: any): CartProducts => {
};

export const getCartProducts = async (): Promise<CartProducts> => {
const fetchedData = await api.get<ServerCartProduct[]>(URL);
const cartProducts = fetchedData.data;
const { data: cartProducts } = await api.get<ServerCartProduct[]>(URL);

return cartProductsParser(cartProducts);
};

export const postCartProducts = async (productId: Product['id']): Promise<number> => {
const fetchedData = await api.post(URL, { productId });
const { headers } = await api.post(URL, { productId });

const location = fetchedData.headers.get('Location');
const location = headers.get('Location');
if (!location) {
throw new Error(`장바구니 상품 추가 요청 성공시 반환되는 location이 없습니다.`);
}
Expand All @@ -57,8 +56,7 @@ export const getCartPriceInfo = async (checkedCartProductIds: number[]): Promise
const params = new URLSearchParams();
checkedCartProductIds.map((id) => params.append('item', `${id}`));

const fetchedData = await api.get<CartPriceInfo>(`${URL}/price?${params.toString()}`);
const cartPriceInfo = fetchedData.data;
const { data: cartPriceInfo } = await api.get<CartPriceInfo>(`${URL}/price?${params.toString()}`);

return cartPriceInfo;
};
10 changes: 4 additions & 6 deletions src/apis/orders/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ import { Order } from 'types/order';
const URL = '/orders';

export const getOrderList = async () => {
const fetchedData = await api.get<Order[]>(URL);
const orderList = fetchedData.data;
const { data: orderList } = await api.get<Order[]>(URL);

return orderList;
};

export const getOrder = async (orderId: number) => {
const fetchedData = await api.get<Order>(`${URL}/${orderId}`);
const order = fetchedData.data;
const { data: order } = await api.get<Order>(`${URL}/${orderId}`);

return order;
};

export const postOrder = async (cartProductIds: number[], pointCost: number) => {
const fetchedData = await api.post(URL, { cartItemIds: cartProductIds, point: pointCost });
const location = fetchedData.headers.get('Location');
const { headers } = await api.post(URL, { cartItemIds: cartProductIds, point: pointCost });
const location = headers.get('Location');
if (!location) throw new Error(`상품 주문 요청 성공시 반환되는 location이 없습니다.`);

const orderId = location.replace('/orders/', '');
Expand Down
3 changes: 1 addition & 2 deletions src/apis/points/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ type ServerUserOwnPoints = {
};

export const getUserOwnPoints = async () => {
const fetchedData = await api.get<ServerUserOwnPoints>(URL);
const userOwnPoints = fetchedData.data;
const { data: userOwnPoints } = await api.get<ServerUserOwnPoints>(URL);

return userOwnPoints.point;
};
3 changes: 1 addition & 2 deletions src/apis/products/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { Product } from 'types/product';
const URL = '/products';

export const getProducts = async (): Promise<Product[]> => {
const data = await api.get<Product[]>(URL);
const products = data.data;
const { data: products } = await api.get<Product[]>(URL);

return products;
};

0 comments on commit 70ac1ce

Please sign in to comment.