@behio/storefront-sdk 0.2.0 → 0.3.0

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/react.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  BehioStorefront
3
- } from "./chunk-EFFPOXWL.mjs";
3
+ } from "./chunk-45FTJKSW.mjs";
4
4
 
5
5
  // src/react/provider.tsx
6
6
  import { useRef, useEffect, useMemo } from "react";
@@ -720,13 +720,77 @@ function useAddresses(options) {
720
720
  };
721
721
  }
722
722
 
723
+ // src/react/hooks/use-address-autocomplete.ts
724
+ import { useState as useState3, useEffect as useEffect3, useRef as useRef2, useCallback as useCallback6 } from "react";
725
+ import { useQuery as useQuery12 } from "@tanstack/react-query";
726
+ var AC_KEY = ["behio", "address-autocomplete"];
727
+ function useAddressAutocomplete(options) {
728
+ const { country, debounce = 300, minChars = 3, enabled = true } = options;
729
+ const { client } = useBehio();
730
+ const [query, setQuery] = useState3("");
731
+ const [debouncedQuery, setDebouncedQuery] = useState3("");
732
+ const [selected, setSelected] = useState3(null);
733
+ const [isSelecting, setIsSelecting] = useState3(false);
734
+ const timerRef = useRef2(void 0);
735
+ useEffect3(() => {
736
+ if (timerRef.current) clearTimeout(timerRef.current);
737
+ if (query.length < minChars) {
738
+ setDebouncedQuery("");
739
+ return;
740
+ }
741
+ timerRef.current = setTimeout(() => {
742
+ setDebouncedQuery(query);
743
+ }, debounce);
744
+ return () => {
745
+ if (timerRef.current) clearTimeout(timerRef.current);
746
+ };
747
+ }, [query, debounce, minChars]);
748
+ const { data, isLoading } = useQuery12({
749
+ queryKey: [...AC_KEY, debouncedQuery, country],
750
+ queryFn: () => client.addresses.autocomplete(debouncedQuery, country),
751
+ enabled: enabled && debouncedQuery.length >= minChars,
752
+ staleTime: 6e4
753
+ // Cache suggestions for 1 min
754
+ });
755
+ const select = useCallback6(
756
+ async (placeId) => {
757
+ setIsSelecting(true);
758
+ try {
759
+ const detail = await client.addresses.getDetail(placeId);
760
+ setSelected(detail);
761
+ setQuery(detail.formattedAddress);
762
+ setDebouncedQuery("");
763
+ return detail;
764
+ } finally {
765
+ setIsSelecting(false);
766
+ }
767
+ },
768
+ [client]
769
+ );
770
+ const clear = useCallback6(() => {
771
+ setQuery("");
772
+ setDebouncedQuery("");
773
+ setSelected(null);
774
+ }, []);
775
+ return {
776
+ query,
777
+ setQuery,
778
+ suggestions: data?.suggestions ?? [],
779
+ isLoading: isLoading && debouncedQuery.length >= minChars,
780
+ select,
781
+ selected,
782
+ isSelecting,
783
+ clear
784
+ };
785
+ }
786
+
723
787
  // src/react/hooks/use-orders.ts
724
- import { useCallback as useCallback6, useMemo as useMemo3, useState as useState3 } from "react";
788
+ import { useCallback as useCallback7, useMemo as useMemo3, useState as useState4 } from "react";
725
789
  import { useInfiniteQuery as useInfiniteQuery2 } from "@tanstack/react-query";
726
790
  function useOrders(options) {
727
791
  const { client } = useBehio();
728
792
  const { page: initialPage, limit, enabled } = options ?? {};
729
- const [page, setPage] = useState3(initialPage ?? 1);
793
+ const [page, setPage] = useState4(initialPage ?? 1);
730
794
  const infinite = useInfiniteQuery2({
731
795
  queryKey: ["behio", "orders", limit, page],
732
796
  queryFn: ({ pageParam }) => client.orders.list({ limit, page: pageParam }),
@@ -740,13 +804,13 @@ function useOrders(options) {
740
804
  [infinite.data]
741
805
  );
742
806
  const lastPage = infinite.data?.pages[infinite.data.pages.length - 1];
743
- const loadMore = useCallback6(() => {
807
+ const loadMore = useCallback7(() => {
744
808
  if (infinite.hasNextPage && !infinite.isFetchingNextPage) {
745
809
  return infinite.fetchNextPage();
746
810
  }
747
811
  return Promise.resolve();
748
812
  }, [infinite]);
749
- const goToPage = useCallback6((newPage) => {
813
+ const goToPage = useCallback7((newPage) => {
750
814
  setPage(newPage);
751
815
  }, []);
752
816
  return {
@@ -772,16 +836,16 @@ function useOrders(options) {
772
836
  }
773
837
 
774
838
  // src/react/hooks/use-order.ts
775
- import { useCallback as useCallback7 } from "react";
776
- import { useQuery as useQuery12, useMutation as useMutation5, useQueryClient as useQueryClient6 } from "@tanstack/react-query";
839
+ import { useCallback as useCallback8 } from "react";
840
+ import { useQuery as useQuery13, useMutation as useMutation5, useQueryClient as useQueryClient7 } from "@tanstack/react-query";
777
841
  function useOrder(orderNumber, options) {
778
842
  const { client } = useBehio();
779
- const queryClient = useQueryClient6();
843
+ const queryClient = useQueryClient7();
780
844
  const {
781
845
  data,
782
846
  isLoading,
783
847
  error
784
- } = useQuery12({
848
+ } = useQuery13({
785
849
  queryKey: ["behio", "order", orderNumber],
786
850
  queryFn: () => client.orders.get(orderNumber),
787
851
  enabled: options?.enabled !== false && !!orderNumber && !!client.getAccessToken()
@@ -793,7 +857,7 @@ function useOrder(orderNumber, options) {
793
857
  queryClient.invalidateQueries({ queryKey: ["behio", "orders"] });
794
858
  }
795
859
  });
796
- const cancel = useCallback7(
860
+ const cancel = useCallback8(
797
861
  () => cancelMutation.mutateAsync(),
798
862
  [cancelMutation]
799
863
  );
@@ -807,12 +871,12 @@ function useOrder(orderNumber, options) {
807
871
  }
808
872
 
809
873
  // src/react/hooks/use-checkout.ts
810
- import { useState as useState4, useCallback as useCallback8 } from "react";
811
- import { useMutation as useMutation6, useQueryClient as useQueryClient7 } from "@tanstack/react-query";
874
+ import { useState as useState5, useCallback as useCallback9 } from "react";
875
+ import { useMutation as useMutation6, useQueryClient as useQueryClient8 } from "@tanstack/react-query";
812
876
  function useCheckout() {
813
877
  const { client, storage } = useBehio();
814
- const queryClient = useQueryClient7();
815
- const [order, setOrder] = useState4(null);
878
+ const queryClient = useQueryClient8();
879
+ const [order, setOrder] = useState5(null);
816
880
  const mutation = useMutation6({
817
881
  mutationFn: (input) => client.checkout.createOrder(input),
818
882
  onSuccess: (result) => {
@@ -822,11 +886,11 @@ function useCheckout() {
822
886
  queryClient.invalidateQueries({ queryKey: ["behio", "orders"] });
823
887
  }
824
888
  });
825
- const createOrder = useCallback8(
889
+ const createOrder = useCallback9(
826
890
  (input) => mutation.mutateAsync(input),
827
891
  [mutation]
828
892
  );
829
- const reset = useCallback8(() => {
893
+ const reset = useCallback9(() => {
830
894
  setOrder(null);
831
895
  mutation.reset();
832
896
  }, [mutation]);
@@ -840,10 +904,10 @@ function useCheckout() {
840
904
  }
841
905
 
842
906
  // src/react/hooks/use-pages.ts
843
- import { useQuery as useQuery13 } from "@tanstack/react-query";
907
+ import { useQuery as useQuery14 } from "@tanstack/react-query";
844
908
  function usePages(locale, options) {
845
909
  const { client } = useBehio();
846
- return useQuery13({
910
+ return useQuery14({
847
911
  queryKey: ["behio", "pages", locale],
848
912
  queryFn: async () => {
849
913
  const result = await client.pages.list(locale);
@@ -854,7 +918,7 @@ function usePages(locale, options) {
854
918
  }
855
919
  function usePage(slug, locale, options) {
856
920
  const { client } = useBehio();
857
- return useQuery13({
921
+ return useQuery14({
858
922
  queryKey: ["behio", "page", slug, locale],
859
923
  queryFn: () => client.pages.get(slug, locale),
860
924
  enabled: options?.enabled !== false && !!slug
@@ -862,10 +926,10 @@ function usePage(slug, locale, options) {
862
926
  }
863
927
 
864
928
  // src/react/hooks/use-shop-info.ts
865
- import { useQuery as useQuery14 } from "@tanstack/react-query";
929
+ import { useQuery as useQuery15 } from "@tanstack/react-query";
866
930
  function useShopInfo(options) {
867
931
  const { client } = useBehio();
868
- return useQuery14({
932
+ return useQuery15({
869
933
  queryKey: ["behio", "shop-info"],
870
934
  queryFn: () => client.getShopInfo(),
871
935
  enabled: options?.enabled !== false
@@ -873,11 +937,11 @@ function useShopInfo(options) {
873
937
  }
874
938
 
875
939
  // src/react/hooks/use-shop-seo.ts
876
- import { useQuery as useQuery15 } from "@tanstack/react-query";
940
+ import { useQuery as useQuery16 } from "@tanstack/react-query";
877
941
  function useShopSeo(options) {
878
942
  const { client } = useBehio();
879
943
  const { locale, initialData, enabled = true } = options ?? {};
880
- return useQuery15({
944
+ return useQuery16({
881
945
  queryKey: ["behio", "shop-seo", locale ?? "_default"],
882
946
  queryFn: () => client.getShopSeo(locale),
883
947
  initialData,
@@ -886,10 +950,10 @@ function useShopSeo(options) {
886
950
  }
887
951
 
888
952
  // src/react/hooks/use-bundles.ts
889
- import { useQuery as useQuery16 } from "@tanstack/react-query";
953
+ import { useQuery as useQuery17 } from "@tanstack/react-query";
890
954
  function useBundles(options) {
891
955
  const { client } = useBehio();
892
- return useQuery16({
956
+ return useQuery17({
893
957
  queryKey: ["behio", "bundles"],
894
958
  queryFn: () => client.catalog.getBundles(),
895
959
  enabled: options?.enabled ?? true,
@@ -898,7 +962,7 @@ function useBundles(options) {
898
962
  }
899
963
  function useBundle(slug, options) {
900
964
  const { client } = useBehio();
901
- return useQuery16({
965
+ return useQuery17({
902
966
  queryKey: ["behio", "bundle", slug],
903
967
  queryFn: () => client.catalog.getBundle(slug),
904
968
  enabled: Boolean(slug) && (options?.enabled ?? true),
@@ -907,10 +971,10 @@ function useBundle(slug, options) {
907
971
  }
908
972
 
909
973
  // src/react/hooks/use-cross-sell.ts
910
- import { useQuery as useQuery17 } from "@tanstack/react-query";
974
+ import { useQuery as useQuery18 } from "@tanstack/react-query";
911
975
  function useCrossSell(productSlug, options) {
912
976
  const { client } = useBehio();
913
- return useQuery17({
977
+ return useQuery18({
914
978
  queryKey: ["behio", "cross-sell", productSlug],
915
979
  queryFn: () => client.catalog.getCrossSell(productSlug),
916
980
  enabled: Boolean(productSlug) && (options?.enabled ?? true),
@@ -919,10 +983,10 @@ function useCrossSell(productSlug, options) {
919
983
  }
920
984
 
921
985
  // src/react/hooks/use-product-promotions.ts
922
- import { useQuery as useQuery18 } from "@tanstack/react-query";
986
+ import { useQuery as useQuery19 } from "@tanstack/react-query";
923
987
  function useProductPromotions(productSlug, options) {
924
988
  const { client } = useBehio();
925
- return useQuery18({
989
+ return useQuery19({
926
990
  queryKey: ["behio", "product-promotions", productSlug],
927
991
  queryFn: () => client.catalog.getProductPromotions(productSlug),
928
992
  enabled: Boolean(productSlug) && (options?.enabled ?? true),
@@ -931,11 +995,11 @@ function useProductPromotions(productSlug, options) {
931
995
  }
932
996
 
933
997
  // src/react/hooks/use-gift-card.ts
934
- import { useQuery as useQuery19 } from "@tanstack/react-query";
998
+ import { useQuery as useQuery20 } from "@tanstack/react-query";
935
999
  function useGiftCardBalance(code, options) {
936
1000
  const { client } = useBehio();
937
1001
  const trimmed = code?.trim();
938
- return useQuery19({
1002
+ return useQuery20({
939
1003
  queryKey: ["behio", "gift-card-balance", trimmed],
940
1004
  queryFn: () => client.catalog.checkGiftCard(trimmed),
941
1005
  enabled: Boolean(trimmed && trimmed.length >= 6) && (options?.enabled ?? true)
@@ -943,11 +1007,11 @@ function useGiftCardBalance(code, options) {
943
1007
  }
944
1008
 
945
1009
  // src/react/hooks/use-wishlist.ts
946
- import { useQuery as useQuery20, useMutation as useMutation7, useQueryClient as useQueryClient8 } from "@tanstack/react-query";
1010
+ import { useQuery as useQuery21, useMutation as useMutation7, useQueryClient as useQueryClient9 } from "@tanstack/react-query";
947
1011
  function useWishlist(options) {
948
1012
  const { client } = useBehio();
949
- const qc = useQueryClient8();
950
- const query = useQuery20({
1013
+ const qc = useQueryClient9();
1014
+ const query = useQuery21({
951
1015
  queryKey: ["behio", "wishlist"],
952
1016
  queryFn: () => client.wishlist.get(),
953
1017
  enabled: options?.enabled ?? true
@@ -970,7 +1034,7 @@ function useWishlist(options) {
970
1034
  }
971
1035
  function useIsInWishlist(productId) {
972
1036
  const { client } = useBehio();
973
- return useQuery20({
1037
+ return useQuery21({
974
1038
  queryKey: ["behio", "wishlist-check", productId],
975
1039
  queryFn: () => client.wishlist.isInWishlist(productId),
976
1040
  enabled: Boolean(productId)
@@ -978,10 +1042,10 @@ function useIsInWishlist(productId) {
978
1042
  }
979
1043
 
980
1044
  // src/react/hooks/use-reviews.ts
981
- import { useQuery as useQuery21, useMutation as useMutation8, useQueryClient as useQueryClient9 } from "@tanstack/react-query";
1045
+ import { useQuery as useQuery22, useMutation as useMutation8, useQueryClient as useQueryClient10 } from "@tanstack/react-query";
982
1046
  function useProductReviews(productId, options) {
983
1047
  const { client } = useBehio();
984
- return useQuery21({
1048
+ return useQuery22({
985
1049
  queryKey: ["behio", "reviews", productId, options?.page ?? 1],
986
1050
  queryFn: () => client.reviews.getProductReviews(productId, options?.page, options?.limit),
987
1051
  enabled: Boolean(productId) && (options?.enabled ?? true)
@@ -989,7 +1053,7 @@ function useProductReviews(productId, options) {
989
1053
  }
990
1054
  function useSubmitReview() {
991
1055
  const { client } = useBehio();
992
- const qc = useQueryClient9();
1056
+ const qc = useQueryClient10();
993
1057
  return useMutation8({
994
1058
  mutationFn: (input) => client.reviews.submit(input),
995
1059
  onSuccess: (_, input) => qc.invalidateQueries({ queryKey: ["behio", "reviews", input.productId] })
@@ -997,7 +1061,7 @@ function useSubmitReview() {
997
1061
  }
998
1062
 
999
1063
  // src/react/hooks/use-returns.ts
1000
- import { useQuery as useQuery22, useMutation as useMutation9 } from "@tanstack/react-query";
1064
+ import { useQuery as useQuery23, useMutation as useMutation9 } from "@tanstack/react-query";
1001
1065
  function useSubmitReturn() {
1002
1066
  const { client } = useBehio();
1003
1067
  return useMutation9({
@@ -1006,7 +1070,7 @@ function useSubmitReturn() {
1006
1070
  }
1007
1071
  function useReturnStatus(returnId, email) {
1008
1072
  const { client } = useBehio();
1009
- return useQuery22({
1073
+ return useQuery23({
1010
1074
  queryKey: ["behio", "return-status", returnId],
1011
1075
  queryFn: () => client.returns.getStatus(returnId, email),
1012
1076
  enabled: Boolean(returnId && email)
@@ -1014,11 +1078,11 @@ function useReturnStatus(returnId, email) {
1014
1078
  }
1015
1079
 
1016
1080
  // src/react/hooks/use-consent.ts
1017
- import { useQuery as useQuery23, useMutation as useMutation10, useQueryClient as useQueryClient10 } from "@tanstack/react-query";
1081
+ import { useQuery as useQuery24, useMutation as useMutation10, useQueryClient as useQueryClient11 } from "@tanstack/react-query";
1018
1082
  function useCookieConsent(visitorId) {
1019
1083
  const { client } = useBehio();
1020
- const qc = useQueryClient10();
1021
- const query = useQuery23({
1084
+ const qc = useQueryClient11();
1085
+ const query = useQuery24({
1022
1086
  queryKey: ["behio", "consent", visitorId],
1023
1087
  queryFn: () => client.consent.get(visitorId),
1024
1088
  enabled: Boolean(visitorId)
@@ -1039,7 +1103,7 @@ function useCookieConsent(visitorId) {
1039
1103
  }
1040
1104
 
1041
1105
  // src/react/hooks/use-quotes.ts
1042
- import { useMutation as useMutation11, useQuery as useQuery24 } from "@tanstack/react-query";
1106
+ import { useMutation as useMutation11, useQuery as useQuery25 } from "@tanstack/react-query";
1043
1107
  function useSubmitQuote() {
1044
1108
  const { client } = useBehio();
1045
1109
  return useMutation11({
@@ -1048,7 +1112,7 @@ function useSubmitQuote() {
1048
1112
  }
1049
1113
  function useQuoteStatus(quoteId) {
1050
1114
  const { client } = useBehio();
1051
- return useQuery24({
1115
+ return useQuery25({
1052
1116
  queryKey: ["behio", "quote-status", quoteId],
1053
1117
  queryFn: () => client.quotes.getStatus(quoteId),
1054
1118
  enabled: Boolean(quoteId)
@@ -1082,6 +1146,7 @@ export {
1082
1146
  formatPrice,
1083
1147
  localStorageAdapter,
1084
1148
  memoryStorage,
1149
+ useAddressAutocomplete,
1085
1150
  useAddresses,
1086
1151
  useAuth,
1087
1152
  useBehio,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behio/storefront-sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "TypeScript SDK for Behio Headless E-Shop — core client + React hooks",
5
5
  "author": "Behio",
6
6
  "license": "MIT",