Skip to content

Commit d9fc18a

Browse files
committed
fix: hook 대신 API 함수에서 에러를 처리하도록 수정
1 parent 5270b6e commit d9fc18a

File tree

2 files changed

+39
-55
lines changed

2 files changed

+39
-55
lines changed

packages/shop/src/apis/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,31 @@ namespace ShopAPIs {
4444

4545
/**
4646
* 로그아웃합니다.
47-
* @throws 401 - 로그아웃이 성공할 시에도 항상 401 에러가 발생합니다.
4847
*/
49-
export const signOut = (client: ShopAPIClient) => () =>
50-
client.delete<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
48+
export const signOut = (client: ShopAPIClient) => async () => {
49+
try {
50+
await client.delete<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
51+
} catch (error) {
52+
}
53+
return Promise.resolve({});
54+
}
5155

5256
/**
5357
* 로그인 정보를 조회합니다.
5458
* @returns 로그인 정보
55-
* @throws 401 - 로그인 정보가 없습니다.
5659
*/
57-
export const retrieveUserInfo = (client: ShopAPIClient) => () =>
58-
client.get<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
60+
export const retrieveUserInfo = (client: ShopAPIClient) => async () => {
61+
try {
62+
const response = await client.get<ShopSchemas.UserSignedInStatus>("authn/social/browser/v1/auth/session");
63+
if (response.meta.is_authenticated) {
64+
return response;
65+
} else {
66+
throw new Error("User is not authenticated");
67+
}
68+
} catch (error) {
69+
}
70+
return Promise.resolve(null);
71+
}
5972

6073
/**
6174
* 노출 중인 모든 상품의 목록을 가져옵니다.

packages/shop/src/hooks/index.ts

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,23 @@ import ShopContext from '../contexts';
88
import ShopSchemas from "../schemas";
99

1010
const QUERY_KEYS = {
11-
USER: ["query", "user"],
12-
PRODUCT_LIST: ["query", "products"],
13-
CART_INFO: ["query", "cart"],
14-
ORDER_LIST: ["query", "orders"],
11+
BASE: ["query", "shop"],
12+
USER: ["query", "shop", "user"],
13+
PRODUCT_LIST: ["query", "shop", "products"],
14+
CART_INFO: ["query", "shop", "cart"],
15+
ORDER_LIST: ["query", "shop", "orders"],
1516
};
1617

1718
const MUTATION_KEYS = {
18-
USER_SIGN_IN_EMAIL: ["mutation", "user", "sign_in", "email"],
19-
USER_SIGN_IN_SNS: ["mutation", "user", "sign_in", "sns"],
20-
USER_SIGN_OUT: ["mutation", "user", "sign_out"],
21-
CART_ITEM_APPEND: ["mutation", "cart", "item", "append"],
22-
CART_ITEM_REMOVE: ["mutation", "cart", "item", "remove"],
23-
CART_ORDER_START: ["mutation", "cart_order", "start"],
24-
ONE_ITEM_ORDER_START: ["mutation", "one_item_order", "start"],
25-
ALL_ORDER_REFUND: ["mutation", "all_order_refund"],
26-
ONE_ITEM_REFUND: ["mutation", "one_item_refund"],
19+
USER_SIGN_IN_EMAIL: ["mutation", "shop", "user", "sign_in", "email"],
20+
USER_SIGN_IN_SNS: ["mutation", "shop", "user", "sign_in", "sns"],
21+
USER_SIGN_OUT: ["mutation", "shop", "user", "sign_out"],
22+
CART_ITEM_APPEND: ["mutation", "shop", "cart", "item", "append"],
23+
CART_ITEM_REMOVE: ["mutation", "shop", "cart", "item", "remove"],
24+
CART_ORDER_START: ["mutation", "shop", "cart_order", "start"],
25+
ONE_ITEM_ORDER_START: ["mutation", "shop", "one_item_order", "start"],
26+
ALL_ORDER_REFUND: ["mutation", "shop", "all_order_refund"],
27+
ONE_ITEM_REFUND: ["mutation", "shop", "one_item_refund"],
2728
};
2829

2930
namespace ShopHooks {
@@ -43,59 +44,29 @@ namespace ShopHooks {
4344
export const useUserStatus = () =>
4445
useSuspenseQuery({
4546
queryKey: QUERY_KEYS.USER,
46-
queryFn: async () => {
47-
try {
48-
const userInfo = await clientDecorator(ShopAPIs.retrieveUserInfo)();
49-
return userInfo.meta.is_authenticated === true ? userInfo : null;
50-
} catch (e) {
51-
return null;
52-
}
53-
},
47+
queryFn: clientDecorator(ShopAPIs.retrieveUserInfo),
48+
retry: 3,
5449
});
5550

5651
export const useSignInWithEmailMutation = () =>
5752
useMutation({
5853
mutationKey: MUTATION_KEYS.USER_SIGN_IN_EMAIL,
5954
mutationFn: clientDecorator(ShopAPIs.signInWithEmail),
60-
meta: {
61-
invalidates: [
62-
QUERY_KEYS.USER,
63-
QUERY_KEYS.CART_INFO,
64-
QUERY_KEYS.ORDER_LIST,
65-
],
66-
},
55+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
6756
});
6857

6958
export const useSignInWithSNSMutation = () =>
7059
useMutation({
7160
mutationKey: MUTATION_KEYS.USER_SIGN_IN_SNS,
7261
mutationFn: clientDecorator(ShopAPIs.signInWithSNS),
73-
meta: {
74-
invalidates: [
75-
QUERY_KEYS.USER,
76-
QUERY_KEYS.CART_INFO,
77-
QUERY_KEYS.ORDER_LIST,
78-
],
79-
},
62+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
8063
});
8164

8265
export const useSignOutMutation = () =>
8366
useMutation({
8467
mutationKey: MUTATION_KEYS.USER_SIGN_OUT,
85-
mutationFn: async () => {
86-
try {
87-
return await clientDecorator(ShopAPIs.signOut)();
88-
} catch (e) {
89-
return null;
90-
}
91-
},
92-
meta: {
93-
invalidates: [
94-
QUERY_KEYS.USER,
95-
QUERY_KEYS.CART_INFO,
96-
QUERY_KEYS.ORDER_LIST,
97-
],
98-
},
68+
mutationFn: clientDecorator(ShopAPIs.signOut),
69+
meta: { invalidates: [ QUERY_KEYS.BASE ] },
9970
});
10071

10172
export const useProducts = (qs?: ShopSchemas.ProductListQueryParams) =>

0 commit comments

Comments
 (0)