@kasuvia/sdk 0.0.2 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/index.d.mts +430 -0
- package/dist/hooks/index.d.ts +430 -0
- package/dist/hooks/index.js +3073 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +2962 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/index-CxNLI4nw.d.ts +786 -0
- package/dist/index-CzVMyUs4.d.mts +492 -0
- package/dist/index-CzVMyUs4.d.ts +492 -0
- package/dist/index-Dowl5224.d.mts +786 -0
- package/dist/index.d.mts +48 -1739
- package/dist/index.d.ts +48 -1739
- package/dist/index.js +1544 -2359
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1527 -2258
- package/dist/index.mjs.map +1 -1
- package/dist/sdk-context-BuarJbeb.d.ts +75 -0
- package/dist/sdk-context-Db8kf1Tv.d.mts +75 -0
- package/dist/services/index.d.mts +2 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +2369 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/index.mjs +2252 -0
- package/dist/services/index.mjs.map +1 -0
- package/package.json +11 -2
- package/src/config/index.ts +10 -91
- package/src/constants.ts +13 -0
- package/src/context/sdk-context.tsx +70 -0
- package/src/graphql/collections.graphql.ts +2 -10
- package/src/graphql/products.graphql.ts +215 -65
- package/src/graphql/wishlist.graphql.ts +7 -6
- package/src/hooks/index.ts +2 -0
- package/src/hooks/use-auth.ts +82 -158
- package/src/hooks/use-bespoke-availability.ts +25 -8
- package/src/hooks/use-cart.ts +222 -119
- package/src/hooks/use-cms.ts +179 -86
- package/src/hooks/use-collections.ts +52 -15
- package/src/hooks/use-consultation-stream.ts +4 -3
- package/src/hooks/use-consultations.ts +120 -51
- package/src/hooks/use-countries.ts +35 -13
- package/src/hooks/use-currencies.ts +26 -13
- package/src/hooks/use-google-translate.ts +22 -8
- package/src/hooks/use-languages.ts +40 -20
- package/src/hooks/use-mobile.ts +23 -0
- package/src/hooks/use-order-stream.ts +9 -5
- package/src/hooks/use-orders.ts +99 -44
- package/src/hooks/use-payment.ts +108 -40
- package/src/hooks/use-products.ts +251 -53
- package/src/hooks/use-service-booking.ts +152 -90
- package/src/hooks/use-states.ts +35 -15
- package/src/hooks/use-wishlist.ts +92 -91
- package/src/index.ts +26 -30
- package/src/services/auth.service.ts +209 -167
- package/src/services/bespoke.service.ts +12 -4
- package/src/services/cart.service.ts +172 -70
- package/src/services/cms.service.ts +70 -35
- package/src/services/collection.service.ts +22 -5
- package/src/services/config.service.ts +34 -14
- package/src/services/consultation.service.ts +72 -18
- package/src/services/logistics.service.ts +35 -11
- package/src/services/notifications.service.ts +27 -12
- package/src/services/order.service.ts +74 -15
- package/src/services/payment.service.ts +86 -31
- package/src/services/product.service.ts +279 -39
- package/src/services/service-booking.service.ts +98 -26
- package/src/services/wishlist.service.ts +69 -25
- package/src/types/auth/index.ts +6 -4
- package/src/types/business/index.ts +7 -2
- package/src/types/payments/index.ts +3 -3
- package/src/types/services/index.ts +2 -5
- package/src/types/store/index.ts +38 -3
- package/src/utils/error-handler.ts +10 -5
- package/src/utils/storage.ts +48 -26
- package/src/utils/validators.ts +85 -0
- package/src/client/authClient.ts +0 -135
- package/src/client/graphqlClient.ts +0 -53
- package/src/client/httpClient.ts +0 -298
|
@@ -29,11 +29,10 @@ export const GET_STOREFRONT_PRODUCTS = gql`
|
|
|
29
29
|
is_bespoke
|
|
30
30
|
base_price
|
|
31
31
|
currency
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
32
|
+
sale_price
|
|
33
|
+
sale_start
|
|
34
|
+
sale_end
|
|
35
|
+
images
|
|
37
36
|
tags
|
|
38
37
|
custom_fields {
|
|
39
38
|
label
|
|
@@ -58,11 +57,7 @@ export const GET_STOREFRONT_PRODUCTS = gql`
|
|
|
58
57
|
sku
|
|
59
58
|
attributes
|
|
60
59
|
price
|
|
61
|
-
images
|
|
62
|
-
id
|
|
63
|
-
url
|
|
64
|
-
alt
|
|
65
|
-
}
|
|
60
|
+
images
|
|
66
61
|
weight_kg
|
|
67
62
|
is_active
|
|
68
63
|
inventory {
|
|
@@ -156,11 +151,7 @@ export const GET_STOREFRONT_PRODUCT_BY_SLUG = gql`
|
|
|
156
151
|
sku
|
|
157
152
|
attributes
|
|
158
153
|
price
|
|
159
|
-
images
|
|
160
|
-
id
|
|
161
|
-
url
|
|
162
|
-
alt
|
|
163
|
-
}
|
|
154
|
+
images
|
|
164
155
|
weight_kg
|
|
165
156
|
is_active
|
|
166
157
|
inventory {
|
|
@@ -189,11 +180,7 @@ export const GET_STOREFRONT_PRODUCT_BY_SLUG = gql`
|
|
|
189
180
|
description
|
|
190
181
|
collection_type
|
|
191
182
|
rules
|
|
192
|
-
image
|
|
193
|
-
id
|
|
194
|
-
url
|
|
195
|
-
alt
|
|
196
|
-
}
|
|
183
|
+
image
|
|
197
184
|
status
|
|
198
185
|
sort_order
|
|
199
186
|
created_at
|
|
@@ -221,11 +208,10 @@ export const GET_STOREFRONT_LATEST = gql`
|
|
|
221
208
|
is_bespoke
|
|
222
209
|
base_price
|
|
223
210
|
currency
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
}
|
|
211
|
+
sale_price
|
|
212
|
+
sale_start
|
|
213
|
+
sale_end
|
|
214
|
+
images
|
|
229
215
|
tags
|
|
230
216
|
custom_fields {
|
|
231
217
|
label
|
|
@@ -250,11 +236,7 @@ export const GET_STOREFRONT_LATEST = gql`
|
|
|
250
236
|
sku
|
|
251
237
|
attributes
|
|
252
238
|
price
|
|
253
|
-
images
|
|
254
|
-
id
|
|
255
|
-
url
|
|
256
|
-
alt
|
|
257
|
-
}
|
|
239
|
+
images
|
|
258
240
|
weight_kg
|
|
259
241
|
is_active
|
|
260
242
|
inventory {
|
|
@@ -320,11 +302,10 @@ export const GET_STOREFRONT_POPULAR = gql`
|
|
|
320
302
|
is_bespoke
|
|
321
303
|
base_price
|
|
322
304
|
currency
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
305
|
+
sale_price
|
|
306
|
+
sale_start
|
|
307
|
+
sale_end
|
|
308
|
+
images
|
|
328
309
|
tags
|
|
329
310
|
custom_fields {
|
|
330
311
|
label
|
|
@@ -349,11 +330,7 @@ export const GET_STOREFRONT_POPULAR = gql`
|
|
|
349
330
|
sku
|
|
350
331
|
attributes
|
|
351
332
|
price
|
|
352
|
-
images
|
|
353
|
-
id
|
|
354
|
-
url
|
|
355
|
-
alt
|
|
356
|
-
}
|
|
333
|
+
images
|
|
357
334
|
weight_kg
|
|
358
335
|
is_active
|
|
359
336
|
inventory {
|
|
@@ -419,11 +396,10 @@ export const GET_STOREFRONT_FEATURED = gql`
|
|
|
419
396
|
is_bespoke
|
|
420
397
|
base_price
|
|
421
398
|
currency
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
}
|
|
399
|
+
sale_price
|
|
400
|
+
sale_start
|
|
401
|
+
sale_end
|
|
402
|
+
images
|
|
427
403
|
tags
|
|
428
404
|
custom_fields {
|
|
429
405
|
label
|
|
@@ -448,11 +424,7 @@ export const GET_STOREFRONT_FEATURED = gql`
|
|
|
448
424
|
sku
|
|
449
425
|
attributes
|
|
450
426
|
price
|
|
451
|
-
images
|
|
452
|
-
id
|
|
453
|
-
url
|
|
454
|
-
alt
|
|
455
|
-
}
|
|
427
|
+
images
|
|
456
428
|
weight_kg
|
|
457
429
|
is_active
|
|
458
430
|
inventory {
|
|
@@ -518,11 +490,104 @@ export const GET_STOREFRONT_NEW = gql`
|
|
|
518
490
|
is_bespoke
|
|
519
491
|
base_price
|
|
520
492
|
currency
|
|
521
|
-
|
|
493
|
+
sale_price
|
|
494
|
+
sale_start
|
|
495
|
+
sale_end
|
|
496
|
+
images
|
|
497
|
+
tags
|
|
498
|
+
custom_fields {
|
|
499
|
+
label
|
|
500
|
+
unit
|
|
501
|
+
required
|
|
502
|
+
}
|
|
503
|
+
is_published
|
|
504
|
+
is_featured
|
|
505
|
+
is_popular
|
|
506
|
+
is_new
|
|
507
|
+
is_temporary
|
|
508
|
+
digital_file_key
|
|
509
|
+
download_limit
|
|
510
|
+
download_expires_at
|
|
511
|
+
weight_kg
|
|
512
|
+
length_cm
|
|
513
|
+
width_cm
|
|
514
|
+
height_cm
|
|
515
|
+
variants {
|
|
522
516
|
id
|
|
523
|
-
|
|
524
|
-
|
|
517
|
+
product_id
|
|
518
|
+
sku
|
|
519
|
+
attributes
|
|
520
|
+
price
|
|
521
|
+
images
|
|
522
|
+
weight_kg
|
|
523
|
+
is_active
|
|
524
|
+
inventory {
|
|
525
|
+
id
|
|
526
|
+
product_id
|
|
527
|
+
variant_id
|
|
528
|
+
quantity
|
|
529
|
+
reserved
|
|
530
|
+
available
|
|
531
|
+
is_in_stock
|
|
532
|
+
is_low_stock
|
|
533
|
+
reorder_point
|
|
534
|
+
reorder_quantity
|
|
535
|
+
track_quantity
|
|
536
|
+
allow_backorder
|
|
537
|
+
updated_at
|
|
538
|
+
}
|
|
539
|
+
created_at
|
|
540
|
+
updated_at
|
|
541
|
+
}
|
|
542
|
+
collections {
|
|
543
|
+
id
|
|
544
|
+
business_id
|
|
545
|
+
name
|
|
546
|
+
slug
|
|
547
|
+
description
|
|
548
|
+
collection_type
|
|
549
|
+
rules
|
|
550
|
+
image {
|
|
551
|
+
id
|
|
552
|
+
url
|
|
553
|
+
alt
|
|
554
|
+
}
|
|
555
|
+
status
|
|
556
|
+
sort_order
|
|
557
|
+
created_at
|
|
558
|
+
updated_at
|
|
525
559
|
}
|
|
560
|
+
created_at
|
|
561
|
+
updated_at
|
|
562
|
+
}
|
|
563
|
+
total
|
|
564
|
+
page
|
|
565
|
+
page_size
|
|
566
|
+
total_pages
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
`
|
|
570
|
+
|
|
571
|
+
export const GET_STOREFRONT_ON_SALE = gql`
|
|
572
|
+
query GetStorefrontOnSale($page: Int = 1, $pageSize: Int = 4) {
|
|
573
|
+
storefront_on_sale(page: $page, pageSize: $pageSize) {
|
|
574
|
+
items {
|
|
575
|
+
id
|
|
576
|
+
business_id
|
|
577
|
+
name
|
|
578
|
+
slug
|
|
579
|
+
description
|
|
580
|
+
brand
|
|
581
|
+
product_type
|
|
582
|
+
requires_shipping
|
|
583
|
+
is_consumable
|
|
584
|
+
is_bespoke
|
|
585
|
+
base_price
|
|
586
|
+
currency
|
|
587
|
+
sale_price
|
|
588
|
+
sale_start
|
|
589
|
+
sale_end
|
|
590
|
+
images
|
|
526
591
|
tags
|
|
527
592
|
custom_fields {
|
|
528
593
|
label
|
|
@@ -547,11 +612,101 @@ export const GET_STOREFRONT_NEW = gql`
|
|
|
547
612
|
sku
|
|
548
613
|
attributes
|
|
549
614
|
price
|
|
550
|
-
images
|
|
615
|
+
images
|
|
616
|
+
weight_kg
|
|
617
|
+
is_active
|
|
618
|
+
inventory {
|
|
619
|
+
id
|
|
620
|
+
product_id
|
|
621
|
+
variant_id
|
|
622
|
+
quantity
|
|
623
|
+
reserved
|
|
624
|
+
available
|
|
625
|
+
is_in_stock
|
|
626
|
+
is_low_stock
|
|
627
|
+
reorder_point
|
|
628
|
+
reorder_quantity
|
|
629
|
+
track_quantity
|
|
630
|
+
allow_backorder
|
|
631
|
+
updated_at
|
|
632
|
+
}
|
|
633
|
+
created_at
|
|
634
|
+
updated_at
|
|
635
|
+
}
|
|
636
|
+
collections {
|
|
637
|
+
id
|
|
638
|
+
business_id
|
|
639
|
+
name
|
|
640
|
+
slug
|
|
641
|
+
description
|
|
642
|
+
collection_type
|
|
643
|
+
rules
|
|
644
|
+
image {
|
|
551
645
|
id
|
|
552
646
|
url
|
|
553
647
|
alt
|
|
554
648
|
}
|
|
649
|
+
status
|
|
650
|
+
sort_order
|
|
651
|
+
created_at
|
|
652
|
+
updated_at
|
|
653
|
+
}
|
|
654
|
+
created_at
|
|
655
|
+
updated_at
|
|
656
|
+
}
|
|
657
|
+
total
|
|
658
|
+
page
|
|
659
|
+
page_size
|
|
660
|
+
total_pages
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
`
|
|
664
|
+
|
|
665
|
+
export const GET_STOREFRONT_LOW_STOCK = gql`
|
|
666
|
+
query GetStorefrontLowStock($page: Int = 1, $pageSize: Int = 4) {
|
|
667
|
+
storefront_low_stock(page: $page, pageSize: $pageSize) {
|
|
668
|
+
items {
|
|
669
|
+
id
|
|
670
|
+
business_id
|
|
671
|
+
name
|
|
672
|
+
slug
|
|
673
|
+
description
|
|
674
|
+
brand
|
|
675
|
+
product_type
|
|
676
|
+
requires_shipping
|
|
677
|
+
is_consumable
|
|
678
|
+
is_bespoke
|
|
679
|
+
base_price
|
|
680
|
+
currency
|
|
681
|
+
sale_price
|
|
682
|
+
sale_start
|
|
683
|
+
sale_end
|
|
684
|
+
images
|
|
685
|
+
tags
|
|
686
|
+
custom_fields {
|
|
687
|
+
label
|
|
688
|
+
unit
|
|
689
|
+
required
|
|
690
|
+
}
|
|
691
|
+
is_published
|
|
692
|
+
is_featured
|
|
693
|
+
is_popular
|
|
694
|
+
is_new
|
|
695
|
+
is_temporary
|
|
696
|
+
digital_file_key
|
|
697
|
+
download_limit
|
|
698
|
+
download_expires_at
|
|
699
|
+
weight_kg
|
|
700
|
+
length_cm
|
|
701
|
+
width_cm
|
|
702
|
+
height_cm
|
|
703
|
+
variants {
|
|
704
|
+
id
|
|
705
|
+
product_id
|
|
706
|
+
sku
|
|
707
|
+
attributes
|
|
708
|
+
price
|
|
709
|
+
images
|
|
555
710
|
weight_kg
|
|
556
711
|
is_active
|
|
557
712
|
inventory {
|
|
@@ -625,11 +780,10 @@ export const GET_STOREFRONT_RECOMMENDED = gql`
|
|
|
625
780
|
is_bespoke
|
|
626
781
|
base_price
|
|
627
782
|
currency
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
}
|
|
783
|
+
sale_price
|
|
784
|
+
sale_start
|
|
785
|
+
sale_end
|
|
786
|
+
images
|
|
633
787
|
tags
|
|
634
788
|
custom_fields {
|
|
635
789
|
label
|
|
@@ -654,11 +808,7 @@ export const GET_STOREFRONT_RECOMMENDED = gql`
|
|
|
654
808
|
sku
|
|
655
809
|
attributes
|
|
656
810
|
price
|
|
657
|
-
images
|
|
658
|
-
id
|
|
659
|
-
url
|
|
660
|
-
alt
|
|
661
|
-
}
|
|
811
|
+
images
|
|
662
812
|
weight_kg
|
|
663
813
|
is_active
|
|
664
814
|
inventory {
|
|
@@ -707,4 +857,4 @@ export const GET_STOREFRONT_RECOMMENDED = gql`
|
|
|
707
857
|
}
|
|
708
858
|
}
|
|
709
859
|
`
|
|
710
|
-
|
|
860
|
+
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { gql } from 'graphql-request'
|
|
5
5
|
|
|
6
|
-
export const
|
|
7
|
-
query
|
|
8
|
-
|
|
6
|
+
export const GET_STOREFRONT_WISHLIST = gql`
|
|
7
|
+
query GetStorefrontWishlist($page: Int = 1, $pageSize: Int = 20) {
|
|
8
|
+
storefront_wishlist(page: $page, pageSize: $pageSize) {
|
|
9
9
|
items {
|
|
10
10
|
id
|
|
11
11
|
product_id
|
|
@@ -14,6 +14,7 @@ export const GET_WISHLIST = gql`
|
|
|
14
14
|
product_name
|
|
15
15
|
product_slug
|
|
16
16
|
product_price
|
|
17
|
+
currency
|
|
17
18
|
product_image
|
|
18
19
|
}
|
|
19
20
|
total
|
|
@@ -24,9 +25,9 @@ export const GET_WISHLIST = gql`
|
|
|
24
25
|
}
|
|
25
26
|
`
|
|
26
27
|
|
|
27
|
-
export const
|
|
28
|
-
query
|
|
29
|
-
|
|
28
|
+
export const CHECK_IN_STOREFRONT_WISHLIST = gql`
|
|
29
|
+
query CheckInStorefrontWishlist($productId: String!) {
|
|
30
|
+
check_in_storefront_wishlist(productId: $productId) {
|
|
30
31
|
in_wishlist
|
|
31
32
|
}
|
|
32
33
|
}
|
package/src/hooks/index.ts
CHANGED
|
@@ -20,7 +20,9 @@ export * from './use-consultations'
|
|
|
20
20
|
export * from './use-consultation-stream'
|
|
21
21
|
export * from './use-order-stream'
|
|
22
22
|
export * from './use-is-mounted'
|
|
23
|
+
export * from './use-mobile'
|
|
23
24
|
export * from './use-google-translate'
|
|
24
25
|
export * from './use-payment'
|
|
25
26
|
export * from './use-cms'
|
|
27
|
+
export * from '../context/sdk-context'
|
|
26
28
|
|
package/src/hooks/use-auth.ts
CHANGED
|
@@ -1,182 +1,106 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Auth
|
|
2
|
+
* Auth Hooks
|
|
3
3
|
*
|
|
4
|
-
*
|
|
4
|
+
* Stateless React Query hooks for authentication operations.
|
|
5
|
+
* The storefront should provide its own AuthProvider context that manages
|
|
6
|
+
* tokens and passes config to these hooks.
|
|
5
7
|
*/
|
|
6
|
-
import React, { createContext, useContext, useState, useEffect, useCallback, type ReactNode } from 'react'
|
|
7
8
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
8
|
-
import { getAuthUser, loginCustomer, registerCustomer, updateProfile, logout as logoutService
|
|
9
|
-
import { tokenStore } from '../client/authClient'
|
|
10
|
-
import { resetGraphQLClient } from '../client/graphqlClient'
|
|
9
|
+
import { getAuthUser, loginCustomer, registerCustomer, updateProfile, logout as logoutService } from '../services/auth.service'
|
|
11
10
|
import { logger } from '../utils/logger'
|
|
12
11
|
import type {
|
|
13
12
|
CustomerLoginPayload,
|
|
14
13
|
CustomerRegisterPayload,
|
|
15
14
|
UpdateProfilePayload,
|
|
16
15
|
CustomerUser,
|
|
17
|
-
AuthContextValue,
|
|
18
16
|
} from '../types/auth'
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export const AuthCtx = React.createContext ? React.createContext<AuthContextValue | null>(null) : null
|
|
23
|
-
|
|
24
|
-
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
25
|
-
const [customer, setCustomer] = useState<CustomerUser | null>(null)
|
|
26
|
-
const [isLoading, setIsLoading] = useState(true)
|
|
27
|
-
const [isModalOpen, setIsModalOpen] = useState(false)
|
|
28
|
-
|
|
29
|
-
// Rehydrate session from stored refresh token on app boot
|
|
30
|
-
useEffect(() => {
|
|
31
|
-
let voided = false
|
|
32
|
-
;(async () => {
|
|
33
|
-
setIsLoading(true)
|
|
34
|
-
try {
|
|
35
|
-
const result = await silentRefresh()
|
|
36
|
-
if (!voided && result) {
|
|
37
|
-
setCustomer(result.user)
|
|
38
|
-
resetGraphQLClient()
|
|
39
|
-
}
|
|
40
|
-
} catch (error: unknown) {
|
|
41
|
-
logger.error('[AuthProvider] Session restore failed:', error)
|
|
42
|
-
if (!voided) setCustomer(null)
|
|
43
|
-
} finally {
|
|
44
|
-
if (!voided) setIsLoading(false)
|
|
45
|
-
}
|
|
46
|
-
})()
|
|
47
|
-
return () => {
|
|
48
|
-
voided = true
|
|
49
|
-
}
|
|
50
|
-
}, [])
|
|
51
|
-
|
|
52
|
-
const openModal = useCallback(() => setIsModalOpen(true), [])
|
|
53
|
-
const closeModal = useCallback(() => {
|
|
54
|
-
setIsModalOpen(false)
|
|
55
|
-
if (typeof window !== 'undefined' && (window.location.hash === '#login' || window.location.hash === '#signup')) {
|
|
56
|
-
window.history.replaceState(null, '', window.location.pathname + window.location.search)
|
|
57
|
-
}
|
|
58
|
-
}, [])
|
|
59
|
-
|
|
60
|
-
// Listen for #login and #signup hashes to open modal automatically
|
|
61
|
-
useEffect(() => {
|
|
62
|
-
const handleHash = () => {
|
|
63
|
-
if (typeof window === 'undefined') return
|
|
64
|
-
if (window.location.hash === '#login' || window.location.hash === '#signup') {
|
|
65
|
-
if (!isLoading && customer) {
|
|
66
|
-
window.history.replaceState(null, '', window.location.pathname + window.location.search)
|
|
67
|
-
} else if (!isLoading && !customer) {
|
|
68
|
-
setIsModalOpen(true)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
handleHash()
|
|
73
|
-
window.addEventListener('hashchange', handleHash)
|
|
74
|
-
return () => window.removeEventListener('hashchange', handleHash)
|
|
75
|
-
}, [customer, isLoading])
|
|
76
|
-
|
|
77
|
-
// Reset GraphQL client when customer state changes to ensure auth headers are included
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
resetGraphQLClient()
|
|
80
|
-
}, [customer])
|
|
81
|
-
|
|
82
|
-
const handleLogout = useCallback(async () => {
|
|
83
|
-
await logoutService()
|
|
84
|
-
setCustomer(null)
|
|
85
|
-
setIsModalOpen(false)
|
|
86
|
-
resetGraphQLClient()
|
|
87
|
-
}, [])
|
|
88
|
-
|
|
89
|
-
const value: AuthContextValue = {
|
|
90
|
-
customer,
|
|
91
|
-
isLoading,
|
|
92
|
-
isModalOpen,
|
|
93
|
-
openModal,
|
|
94
|
-
closeModal,
|
|
95
|
-
setCustomer,
|
|
96
|
-
logout: handleLogout,
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (!AuthCtx) return children
|
|
100
|
-
return React.createElement(AuthCtx.Provider, { value }, children)
|
|
101
|
-
}
|
|
17
|
+
import type { SdkConfig } from '../config'
|
|
18
|
+
import { useSdkContext, resolveHookArgs } from '../context/sdk-context'
|
|
102
19
|
|
|
103
20
|
// ── Hooks ────────────────────────────────────────────────────────────────────
|
|
104
21
|
|
|
105
|
-
export function useAuthUser() {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
onSuccess: () => {
|
|
119
|
-
queryClient.invalidateQueries({ queryKey: ['auth-user'] })
|
|
120
|
-
queryClient.invalidateQueries({ queryKey: ['cart'] })
|
|
121
|
-
},
|
|
122
|
-
})
|
|
22
|
+
export function useAuthUser(config?: SdkConfig, accessToken?: string) {
|
|
23
|
+
const ctx = useSdkContext()
|
|
24
|
+
try {
|
|
25
|
+
const { config: cfg, accessToken: token } = resolveHookArgs(config, accessToken ?? null, ctx)
|
|
26
|
+
return useQuery({
|
|
27
|
+
queryKey: ['auth-user'],
|
|
28
|
+
queryFn: () => getAuthUser(cfg, token ?? ''),
|
|
29
|
+
enabled: !!token,
|
|
30
|
+
})
|
|
31
|
+
} catch (error) {
|
|
32
|
+
logger.warn('[useAuthUser] Called outside QueryClientProvider, returning empty user')
|
|
33
|
+
return { data: undefined, isLoading: false, error: null, isFetching: false, refetch: () => Promise.resolve({ data: undefined }) } as any
|
|
34
|
+
}
|
|
123
35
|
}
|
|
124
36
|
|
|
125
|
-
export function
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
37
|
+
export function useLogin(config?: SdkConfig) {
|
|
38
|
+
const ctx = useSdkContext()
|
|
39
|
+
try {
|
|
40
|
+
const { config: cfg } = resolveHookArgs(config, undefined, ctx)
|
|
41
|
+
const queryClient = useQueryClient()
|
|
42
|
+
return useMutation({
|
|
43
|
+
mutationFn: (payload: Omit<CustomerLoginPayload, 'tenant_id'>) => loginCustomer(cfg, payload),
|
|
44
|
+
onSuccess: () => {
|
|
45
|
+
queryClient.invalidateQueries({ queryKey: ['auth-user'] })
|
|
46
|
+
queryClient.invalidateQueries({ queryKey: ['cart'] })
|
|
47
|
+
},
|
|
48
|
+
})
|
|
49
|
+
} catch (error) {
|
|
50
|
+
logger.warn('[useLogin] Called outside QueryClientProvider, returning no-op mutation')
|
|
51
|
+
return { mutate: () => {}, mutateAsync: () => Promise.resolve(undefined) } as any
|
|
52
|
+
}
|
|
135
53
|
}
|
|
136
54
|
|
|
137
|
-
export function
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
55
|
+
export function useRegister(config?: SdkConfig) {
|
|
56
|
+
const ctx = useSdkContext()
|
|
57
|
+
try {
|
|
58
|
+
const { config: cfg } = resolveHookArgs(config, undefined, ctx)
|
|
59
|
+
const queryClient = useQueryClient()
|
|
60
|
+
return useMutation({
|
|
61
|
+
mutationFn: (payload: Omit<CustomerRegisterPayload, 'tenant_id'>) => registerCustomer(cfg, payload),
|
|
62
|
+
onSuccess: () => {
|
|
63
|
+
queryClient.invalidateQueries({ queryKey: ['auth-user'] })
|
|
64
|
+
queryClient.invalidateQueries({ queryKey: ['cart'] })
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
} catch (error) {
|
|
68
|
+
logger.warn('[useRegister] Called outside QueryClientProvider, returning no-op mutation')
|
|
69
|
+
return { mutate: () => {}, mutateAsync: () => Promise.resolve(undefined) } as any
|
|
70
|
+
}
|
|
146
71
|
}
|
|
147
72
|
|
|
148
|
-
export function
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
73
|
+
export function useUpdateProfile(config?: SdkConfig, accessToken?: string | null) {
|
|
74
|
+
const ctx = useSdkContext()
|
|
75
|
+
try {
|
|
76
|
+
const { config: cfg, accessToken: token } = resolveHookArgs(config, accessToken, ctx)
|
|
77
|
+
const queryClient = useQueryClient()
|
|
78
|
+
return useMutation({
|
|
79
|
+
mutationFn: (payload: UpdateProfilePayload) =>
|
|
80
|
+
token ? updateProfile(cfg, token, payload) : Promise.reject('No access token'),
|
|
81
|
+
onSuccess: () => {
|
|
82
|
+
queryClient.invalidateQueries({ queryKey: ['auth-user'] })
|
|
83
|
+
},
|
|
84
|
+
})
|
|
85
|
+
} catch (error) {
|
|
86
|
+
logger.warn('[useUpdateProfile] Called outside QueryClientProvider, returning no-op mutation')
|
|
87
|
+
return { mutate: () => {}, mutateAsync: () => Promise.resolve(undefined) } as any
|
|
88
|
+
}
|
|
157
89
|
}
|
|
158
90
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
isLoading: authUserQuery.isLoading,
|
|
174
|
-
isModalOpen: false,
|
|
175
|
-
openModal: () => {},
|
|
176
|
-
closeModal: () => {},
|
|
177
|
-
setCustomer: () => {},
|
|
178
|
-
logout: async () => {
|
|
179
|
-
await logoutMutation.mutateAsync()
|
|
180
|
-
},
|
|
91
|
+
export function useLogout(config?: SdkConfig, refreshToken?: string) {
|
|
92
|
+
const ctx = useSdkContext()
|
|
93
|
+
try {
|
|
94
|
+
const { config: cfg } = resolveHookArgs(config, undefined, ctx)
|
|
95
|
+
const queryClient = useQueryClient()
|
|
96
|
+
return useMutation({
|
|
97
|
+
mutationFn: () => logoutService(cfg, refreshToken),
|
|
98
|
+
onSuccess: () => {
|
|
99
|
+
queryClient.clear()
|
|
100
|
+
},
|
|
101
|
+
})
|
|
102
|
+
} catch (error) {
|
|
103
|
+
logger.warn('[useLogout] Called outside QueryClientProvider, returning no-op mutation')
|
|
104
|
+
return { mutate: () => {}, mutateAsync: () => Promise.resolve(undefined) } as any
|
|
181
105
|
}
|
|
182
106
|
}
|