@@ -8,22 +8,23 @@ import ShopContext from '../contexts';
88import ShopSchemas from "../schemas" ;
99
1010const 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
1718const 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
2930namespace ShopHooks {
@@ -35,133 +36,103 @@ namespace ShopHooks {
3536 return context ;
3637 }
3738
38- const clientDecorator = < T = CallableFunction > ( func : ( client : ShopAPIClient ) => T ) : T => {
39+ export const useShopClient = ( ) => {
3940 const { shopApiDomain, shopApiCSRFCookieName, shopApiTimeout } = useShopContext ( ) ;
40- return func ( new ShopAPIClient ( shopApiDomain , shopApiCSRFCookieName , shopApiTimeout ) ) ;
41+ return new ShopAPIClient ( shopApiDomain , shopApiCSRFCookieName , shopApiTimeout ) ;
4142 }
4243
43- export const useUserStatus = ( ) =>
44+ export const useUserStatus = ( client : ShopAPIClient ) =>
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 : ShopAPIs . retrieveUserInfo ( client ) ,
48+ retry : 3 ,
5449 } ) ;
5550
56- export const useSignInWithEmailMutation = ( ) =>
51+ export const useSignInWithEmailMutation = ( client : ShopAPIClient ) =>
5752 useMutation ( {
5853 mutationKey : MUTATION_KEYS . USER_SIGN_IN_EMAIL ,
59- mutationFn : clientDecorator ( ShopAPIs . signInWithEmail ) ,
60- meta : {
61- invalidates : [
62- QUERY_KEYS . USER ,
63- QUERY_KEYS . CART_INFO ,
64- QUERY_KEYS . ORDER_LIST ,
65- ] ,
66- } ,
54+ mutationFn : ShopAPIs . signInWithEmail ( client ) ,
55+ meta : { invalidates : [ QUERY_KEYS . BASE ] } ,
6756 } ) ;
6857
69- export const useSignInWithSNSMutation = ( ) =>
58+ export const useSignInWithSNSMutation = ( client : ShopAPIClient ) =>
7059 useMutation ( {
7160 mutationKey : MUTATION_KEYS . USER_SIGN_IN_SNS ,
72- mutationFn : clientDecorator ( ShopAPIs . signInWithSNS ) ,
73- meta : {
74- invalidates : [
75- QUERY_KEYS . USER ,
76- QUERY_KEYS . CART_INFO ,
77- QUERY_KEYS . ORDER_LIST ,
78- ] ,
79- } ,
61+ mutationFn : ShopAPIs . signInWithSNS ( client ) ,
62+ meta : { invalidates : [ QUERY_KEYS . BASE ] } ,
8063 } ) ;
8164
82- export const useSignOutMutation = ( ) =>
65+ export const useSignOutMutation = ( client : ShopAPIClient ) =>
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 : ShopAPIs . signOut ( client ) ,
69+ meta : { invalidates : [ QUERY_KEYS . BASE ] } ,
9970 } ) ;
10071
101- export const useProducts = ( qs ?: ShopSchemas . ProductListQueryParams ) =>
72+ export const useProducts = ( client : ShopAPIClient , qs ?: ShopSchemas . ProductListQueryParams ) =>
10273 useSuspenseQuery ( {
10374 queryKey : QUERY_KEYS . PRODUCT_LIST ,
104- queryFn : ( ) => clientDecorator ( ShopAPIs . listProducts ) ( qs ) ,
75+ queryFn : ( ) => ShopAPIs . listProducts ( client ) ( qs ) ,
10576 } ) ;
10677
107- export const useCart = ( ) =>
78+ export const useCart = ( client : ShopAPIClient ) =>
10879 useSuspenseQuery ( {
10980 queryKey : QUERY_KEYS . CART_INFO ,
110- queryFn : clientDecorator ( ShopAPIs . retrieveCart ) ,
81+ queryFn : ShopAPIs . retrieveCart ( client ) ,
11182 } ) ;
11283
113- export const useAddItemToCartMutation = ( ) =>
84+ export const useAddItemToCartMutation = ( client : ShopAPIClient ) =>
11485 useMutation ( {
11586 mutationKey : MUTATION_KEYS . CART_ITEM_APPEND ,
116- mutationFn : clientDecorator ( ShopAPIs . appendItemToCart ) ,
87+ mutationFn : ShopAPIs . appendItemToCart ( client ) ,
11788 meta : { invalidates : [ QUERY_KEYS . CART_INFO ] } ,
11889 } ) ;
11990
120- export const useRemoveItemFromCartMutation = ( ) =>
91+ export const useRemoveItemFromCartMutation = ( client : ShopAPIClient ) =>
12192 useMutation ( {
12293 mutationKey : MUTATION_KEYS . CART_ITEM_REMOVE ,
123- mutationFn : clientDecorator ( ShopAPIs . removeItemFromCart ) ,
94+ mutationFn : ShopAPIs . removeItemFromCart ( client ) ,
12495 meta : { invalidates : [ QUERY_KEYS . CART_INFO ] } ,
12596 } ) ;
12697
127- export const usePrepareOneItemOrderMutation = ( ) =>
98+ export const usePrepareOneItemOrderMutation = ( client : ShopAPIClient ) =>
12899 useMutation ( {
129100 mutationKey : MUTATION_KEYS . ONE_ITEM_ORDER_START ,
130- mutationFn : clientDecorator ( ShopAPIs . prepareOneItemOrder ) ,
101+ mutationFn : ShopAPIs . prepareOneItemOrder ( client ) ,
131102 meta : { invalidates : [ QUERY_KEYS . CART_INFO , QUERY_KEYS . ORDER_LIST ] } ,
132103 } ) ;
133104
134- export const usePrepareCartOrderMutation = ( ) =>
105+ export const usePrepareCartOrderMutation = ( client : ShopAPIClient ) =>
135106 useMutation ( {
136107 mutationKey : MUTATION_KEYS . CART_ORDER_START ,
137- mutationFn : clientDecorator ( ShopAPIs . prepareCartOrder ) ,
108+ mutationFn : ShopAPIs . prepareCartOrder ( client ) ,
138109 meta : { invalidates : [ QUERY_KEYS . CART_INFO , QUERY_KEYS . ORDER_LIST ] } ,
139110 } ) ;
140111
141- export const useOrders = ( ) =>
112+ export const useOrders = ( client : ShopAPIClient ) =>
142113 useSuspenseQuery ( {
143114 queryKey : QUERY_KEYS . ORDER_LIST ,
144- queryFn : clientDecorator ( ShopAPIs . listOrders ) ,
115+ queryFn : ShopAPIs . listOrders ( client ) ,
145116 } ) ;
146117
147- export const useOneItemRefundMutation = ( ) =>
118+ export const useOneItemRefundMutation = ( client : ShopAPIClient ) =>
148119 useMutation ( {
149120 mutationKey : MUTATION_KEYS . ONE_ITEM_REFUND ,
150- mutationFn : clientDecorator ( ShopAPIs . refundOneItemFromOrder ) ,
121+ mutationFn : ShopAPIs . refundOneItemFromOrder ( client ) ,
151122 meta : { invalidates : [ QUERY_KEYS . ORDER_LIST ] } ,
152123 } ) ;
153124
154- export const useOrderRefundMutation = ( ) =>
125+ export const useOrderRefundMutation = ( client : ShopAPIClient ) =>
155126 useMutation ( {
156127 mutationKey : MUTATION_KEYS . ALL_ORDER_REFUND ,
157- mutationFn : clientDecorator ( ShopAPIs . refundAllItemsInOrder ) ,
128+ mutationFn : ShopAPIs . refundAllItemsInOrder ( client ) ,
158129 meta : { invalidates : [ QUERY_KEYS . ORDER_LIST ] } ,
159130 } ) ;
160131
161- export const useOptionsOfOneItemInOrderPatchMutation = ( ) =>
132+ export const useOptionsOfOneItemInOrderPatchMutation = ( client : ShopAPIClient ) =>
162133 useMutation ( {
163134 mutationKey : MUTATION_KEYS . CART_ITEM_APPEND ,
164- mutationFn : clientDecorator ( ShopAPIs . patchOrderOptions ) ,
135+ mutationFn : ShopAPIs . patchOrderOptions ( client ) ,
165136 meta : { invalidates : [ QUERY_KEYS . ORDER_LIST ] } ,
166137 } ) ;
167138}
0 commit comments