@simpleapps-com/augur-hooks 0.2.0 → 0.2.2

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/index.d.cts CHANGED
@@ -1,10 +1,11 @@
1
- import { G as GetItemCategoryApiOptions, P as PageData, S as SearchSuggestionsResponse, a as SearchSuggestion, I as InfiniteScrollPage, A as AugurCallbacks, C as CacheConfig, b as AugurApiClient, c as AugurAuthContext } from './joomla-content-list-CDF5-OJ3.cjs';
2
- export { d as AugurHooksProvider, e as CacheProvider, f as CacheTierConfig, g as getCartPricingQueryOptions, h as getCategoryItemsInfiniteKey, i as getCategoryItemsInfiniteOptions, j as getInvMastDocKey, k as getInvMastDocOptions, l as getInvMastKey, m as getInvMastOptions, n as getInvMastStockKey, o as getInvMastStockOptions, p as getItemAttributesKey, q as getItemAttributesOptions, r as getItemCategoryKey, s as getItemCategoryOptions, t as getItemDetailsKey, u as getItemDetailsOptions, v as getItemPriceKey, w as getItemPriceOptions, x as getItemSearchInfiniteKey, y as getItemSearchInfiniteOptions, z as getJoomlaContentKey, B as getJoomlaContentListKey, D as getJoomlaContentListOptions, E as getJoomlaContentOptions, F as getProductCategoryKey, H as getProductCategoryOptions, J as getProductSearchKey, K as getProductSearchOptions, L as getSearchSuggestionsKey, M as getSearchSuggestionsOptions, N as useAugurApi, O as useAugurAuth, Q as useAugurCache } from './joomla-content-list-CDF5-OJ3.cjs';
1
+ import { G as GetItemCategoryApiOptions, P as PageData, S as SearchSuggestionsResponse, a as SearchSuggestion, I as InfiniteScrollPage, A as AugurCallbacks, C as CacheConfig, b as AugurApiClient, c as AugurAuthContext, d as PersistenceConfig } from './joomla-content-list-DGPtwJ99.cjs';
2
+ export { e as AugurHooksProvider, f as CacheProvider, g as CacheTierConfig, h as getCartPricingQueryOptions, i as getCategoryItemsInfiniteKey, j as getCategoryItemsInfiniteOptions, k as getInvMastDocKey, l as getInvMastDocOptions, m as getInvMastKey, n as getInvMastOptions, o as getInvMastStockKey, p as getInvMastStockOptions, q as getItemAttributesKey, r as getItemAttributesOptions, s as getItemCategoryKey, t as getItemCategoryOptions, u as getItemDetailsKey, v as getItemDetailsOptions, w as getItemPriceKey, x as getItemPriceOptions, y as getItemSearchInfiniteKey, z as getItemSearchInfiniteOptions, B as getJoomlaContentKey, D as getJoomlaContentListKey, E as getJoomlaContentListOptions, F as getJoomlaContentOptions, H as getProductCategoryKey, J as getProductCategoryOptions, K as getProductSearchKey, L as getProductSearchOptions, M as getSearchSuggestionsKey, N as getSearchSuggestionsOptions, O as useAugurApi, Q as useAugurAuth, R as useAugurCache } from './joomla-content-list-DGPtwJ99.cjs';
3
3
  import * as zustand from 'zustand';
4
4
  import { TCartLine, TItemsFilters, TPriceData, TInvMastDoc, TCategory, TItemDetails, TAttribute, TJoomlaContent, TJoomlaContentFilters } from '@simpleapps-com/augur-utils';
5
5
  import * as _tanstack_react_query from '@tanstack/react-query';
6
6
  import { QueryKey } from '@tanstack/react-query';
7
7
  import { ReactNode, createElement } from 'react';
8
+ import * as _tanstack_react_query_persist_client from '@tanstack/react-query-persist-client';
8
9
  import 'react/jsx-runtime';
9
10
 
10
11
  interface CartState {
@@ -526,4 +527,55 @@ interface SiteHooks {
526
527
  */
527
528
  declare function createSiteHooks(config?: CreateSiteHooksConfig): SiteHooks;
528
529
 
529
- export { AugurApiClient, AugurAuthContext, AugurCallbacks, CacheConfig, type CartActionCallbacks, type CartInitCallbacks, type CartPriceData, type CartPricingResult, type CartSessionInfo, type CreateSiteHooksConfig, GetItemCategoryApiOptions, InfiniteScrollPage, PageData, SearchSuggestion, SearchSuggestionsResponse, createSiteHooks, useCartActions, useCartHdrUid, useCartInitialization, useCartItemCount, useCartLines, useCartPricing, useCartStore, useCategoryItemsInfinite, useClearCart, useDebounce, useFormatPrice, useInvMast, useInvMastDoc, useInvMastStock, useItemAttributes, useItemCategory, useItemDetails, useItemFiltersStore, useItemPrice, useItemSearchInfinite, useJoomlaContent, useJoomlaContentList, usePaginationPrefetch, useProductCategory, useProductSearch, useSearchSuggestions, useSetCartHdrUid, useSetCartLines };
530
+ /**
531
+ * Returns a fully configured `persistOptions` object for use with
532
+ * `PersistQueryClientProvider` from `@tanstack/react-query-persist-client`.
533
+ *
534
+ * Handles:
535
+ * - SSR-safe persister creation (no-op on server)
536
+ * - Map → Record serialization for localStorage compatibility
537
+ * - Selective persistence (only successful queries matching persistableKeys)
538
+ * - Per-key-prefix LRU eviction (via `limits`)
539
+ * - Throttled writes to localStorage
540
+ * - Hydration safety (maxAge: 0 until client mount)
541
+ * - Storage monitoring with automatic eviction when over threshold
542
+ * - Dev mode auto-buster (invalidates cache on each restart)
543
+ *
544
+ * @example
545
+ * ```tsx
546
+ * import { useAugurPersistence } from "@simpleapps-com/augur-hooks";
547
+ *
548
+ * function Providers({ children }) {
549
+ * const [queryClient] = useState(() => new QueryClient({ ... }));
550
+ * const persistOptions = useAugurPersistence({
551
+ * key: "my-site-cache",
552
+ * buster: "1.0.0",
553
+ * persistableKeys: ["price", "categories", "invMast"],
554
+ * limits: { invMast: 100 },
555
+ * });
556
+ *
557
+ * return (
558
+ * <PersistQueryClientProvider client={queryClient} persistOptions={persistOptions}>
559
+ * <augur.Provider api={api} auth={auth}>
560
+ * {children}
561
+ * </augur.Provider>
562
+ * </PersistQueryClientProvider>
563
+ * );
564
+ * }
565
+ * ```
566
+ */
567
+ declare function useAugurPersistence(config: PersistenceConfig): {
568
+ persister: _tanstack_react_query_persist_client.Persister;
569
+ maxAge: number;
570
+ buster: string;
571
+ dehydrateOptions: {
572
+ shouldDehydrateQuery: (query: {
573
+ queryKey: readonly unknown[];
574
+ state: {
575
+ status: string;
576
+ };
577
+ }) => boolean;
578
+ };
579
+ };
580
+
581
+ export { AugurApiClient, AugurAuthContext, AugurCallbacks, CacheConfig, type CartActionCallbacks, type CartInitCallbacks, type CartPriceData, type CartPricingResult, type CartSessionInfo, type CreateSiteHooksConfig, GetItemCategoryApiOptions, InfiniteScrollPage, PageData, PersistenceConfig, SearchSuggestion, SearchSuggestionsResponse, createSiteHooks, useAugurPersistence, useCartActions, useCartHdrUid, useCartInitialization, useCartItemCount, useCartLines, useCartPricing, useCartStore, useCategoryItemsInfinite, useClearCart, useDebounce, useFormatPrice, useInvMast, useInvMastDoc, useInvMastStock, useItemAttributes, useItemCategory, useItemDetails, useItemFiltersStore, useItemPrice, useItemSearchInfinite, useJoomlaContent, useJoomlaContentList, usePaginationPrefetch, useProductCategory, useProductSearch, useSearchSuggestions, useSetCartHdrUid, useSetCartLines };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { G as GetItemCategoryApiOptions, P as PageData, S as SearchSuggestionsResponse, a as SearchSuggestion, I as InfiniteScrollPage, A as AugurCallbacks, C as CacheConfig, b as AugurApiClient, c as AugurAuthContext } from './joomla-content-list-CDF5-OJ3.js';
2
- export { d as AugurHooksProvider, e as CacheProvider, f as CacheTierConfig, g as getCartPricingQueryOptions, h as getCategoryItemsInfiniteKey, i as getCategoryItemsInfiniteOptions, j as getInvMastDocKey, k as getInvMastDocOptions, l as getInvMastKey, m as getInvMastOptions, n as getInvMastStockKey, o as getInvMastStockOptions, p as getItemAttributesKey, q as getItemAttributesOptions, r as getItemCategoryKey, s as getItemCategoryOptions, t as getItemDetailsKey, u as getItemDetailsOptions, v as getItemPriceKey, w as getItemPriceOptions, x as getItemSearchInfiniteKey, y as getItemSearchInfiniteOptions, z as getJoomlaContentKey, B as getJoomlaContentListKey, D as getJoomlaContentListOptions, E as getJoomlaContentOptions, F as getProductCategoryKey, H as getProductCategoryOptions, J as getProductSearchKey, K as getProductSearchOptions, L as getSearchSuggestionsKey, M as getSearchSuggestionsOptions, N as useAugurApi, O as useAugurAuth, Q as useAugurCache } from './joomla-content-list-CDF5-OJ3.js';
1
+ import { G as GetItemCategoryApiOptions, P as PageData, S as SearchSuggestionsResponse, a as SearchSuggestion, I as InfiniteScrollPage, A as AugurCallbacks, C as CacheConfig, b as AugurApiClient, c as AugurAuthContext, d as PersistenceConfig } from './joomla-content-list-DGPtwJ99.js';
2
+ export { e as AugurHooksProvider, f as CacheProvider, g as CacheTierConfig, h as getCartPricingQueryOptions, i as getCategoryItemsInfiniteKey, j as getCategoryItemsInfiniteOptions, k as getInvMastDocKey, l as getInvMastDocOptions, m as getInvMastKey, n as getInvMastOptions, o as getInvMastStockKey, p as getInvMastStockOptions, q as getItemAttributesKey, r as getItemAttributesOptions, s as getItemCategoryKey, t as getItemCategoryOptions, u as getItemDetailsKey, v as getItemDetailsOptions, w as getItemPriceKey, x as getItemPriceOptions, y as getItemSearchInfiniteKey, z as getItemSearchInfiniteOptions, B as getJoomlaContentKey, D as getJoomlaContentListKey, E as getJoomlaContentListOptions, F as getJoomlaContentOptions, H as getProductCategoryKey, J as getProductCategoryOptions, K as getProductSearchKey, L as getProductSearchOptions, M as getSearchSuggestionsKey, N as getSearchSuggestionsOptions, O as useAugurApi, Q as useAugurAuth, R as useAugurCache } from './joomla-content-list-DGPtwJ99.js';
3
3
  import * as zustand from 'zustand';
4
4
  import { TCartLine, TItemsFilters, TPriceData, TInvMastDoc, TCategory, TItemDetails, TAttribute, TJoomlaContent, TJoomlaContentFilters } from '@simpleapps-com/augur-utils';
5
5
  import * as _tanstack_react_query from '@tanstack/react-query';
6
6
  import { QueryKey } from '@tanstack/react-query';
7
7
  import { ReactNode, createElement } from 'react';
8
+ import * as _tanstack_react_query_persist_client from '@tanstack/react-query-persist-client';
8
9
  import 'react/jsx-runtime';
9
10
 
10
11
  interface CartState {
@@ -526,4 +527,55 @@ interface SiteHooks {
526
527
  */
527
528
  declare function createSiteHooks(config?: CreateSiteHooksConfig): SiteHooks;
528
529
 
529
- export { AugurApiClient, AugurAuthContext, AugurCallbacks, CacheConfig, type CartActionCallbacks, type CartInitCallbacks, type CartPriceData, type CartPricingResult, type CartSessionInfo, type CreateSiteHooksConfig, GetItemCategoryApiOptions, InfiniteScrollPage, PageData, SearchSuggestion, SearchSuggestionsResponse, createSiteHooks, useCartActions, useCartHdrUid, useCartInitialization, useCartItemCount, useCartLines, useCartPricing, useCartStore, useCategoryItemsInfinite, useClearCart, useDebounce, useFormatPrice, useInvMast, useInvMastDoc, useInvMastStock, useItemAttributes, useItemCategory, useItemDetails, useItemFiltersStore, useItemPrice, useItemSearchInfinite, useJoomlaContent, useJoomlaContentList, usePaginationPrefetch, useProductCategory, useProductSearch, useSearchSuggestions, useSetCartHdrUid, useSetCartLines };
530
+ /**
531
+ * Returns a fully configured `persistOptions` object for use with
532
+ * `PersistQueryClientProvider` from `@tanstack/react-query-persist-client`.
533
+ *
534
+ * Handles:
535
+ * - SSR-safe persister creation (no-op on server)
536
+ * - Map → Record serialization for localStorage compatibility
537
+ * - Selective persistence (only successful queries matching persistableKeys)
538
+ * - Per-key-prefix LRU eviction (via `limits`)
539
+ * - Throttled writes to localStorage
540
+ * - Hydration safety (maxAge: 0 until client mount)
541
+ * - Storage monitoring with automatic eviction when over threshold
542
+ * - Dev mode auto-buster (invalidates cache on each restart)
543
+ *
544
+ * @example
545
+ * ```tsx
546
+ * import { useAugurPersistence } from "@simpleapps-com/augur-hooks";
547
+ *
548
+ * function Providers({ children }) {
549
+ * const [queryClient] = useState(() => new QueryClient({ ... }));
550
+ * const persistOptions = useAugurPersistence({
551
+ * key: "my-site-cache",
552
+ * buster: "1.0.0",
553
+ * persistableKeys: ["price", "categories", "invMast"],
554
+ * limits: { invMast: 100 },
555
+ * });
556
+ *
557
+ * return (
558
+ * <PersistQueryClientProvider client={queryClient} persistOptions={persistOptions}>
559
+ * <augur.Provider api={api} auth={auth}>
560
+ * {children}
561
+ * </augur.Provider>
562
+ * </PersistQueryClientProvider>
563
+ * );
564
+ * }
565
+ * ```
566
+ */
567
+ declare function useAugurPersistence(config: PersistenceConfig): {
568
+ persister: _tanstack_react_query_persist_client.Persister;
569
+ maxAge: number;
570
+ buster: string;
571
+ dehydrateOptions: {
572
+ shouldDehydrateQuery: (query: {
573
+ queryKey: readonly unknown[];
574
+ state: {
575
+ status: string;
576
+ };
577
+ }) => boolean;
578
+ };
579
+ };
580
+
581
+ export { AugurApiClient, AugurAuthContext, AugurCallbacks, CacheConfig, type CartActionCallbacks, type CartInitCallbacks, type CartPriceData, type CartPricingResult, type CartSessionInfo, type CreateSiteHooksConfig, GetItemCategoryApiOptions, InfiniteScrollPage, PageData, PersistenceConfig, SearchSuggestion, SearchSuggestionsResponse, createSiteHooks, useAugurPersistence, useCartActions, useCartHdrUid, useCartInitialization, useCartItemCount, useCartLines, useCartPricing, useCartStore, useCategoryItemsInfinite, useClearCart, useDebounce, useFormatPrice, useInvMast, useInvMastDoc, useInvMastStock, useItemAttributes, useItemCategory, useItemDetails, useItemFiltersStore, useItemPrice, useItemSearchInfinite, useJoomlaContent, useJoomlaContentList, usePaginationPrefetch, useProductCategory, useProductSearch, useSearchSuggestions, useSetCartHdrUid, useSetCartLines };
package/dist/index.js CHANGED
@@ -935,6 +935,168 @@ function createSiteHooks(config = {}) {
935
935
  useFormatPrice
936
936
  };
937
937
  }
938
+
939
+ // src/persistence/use-persistence.ts
940
+ import { useState as useState2, useEffect as useEffect4, useMemo as useMemo3 } from "react";
941
+
942
+ // src/persistence/serializer.ts
943
+ function serialize(data) {
944
+ return JSON.stringify(data, (_key, value) => {
945
+ if (value instanceof Map) return Object.fromEntries(value);
946
+ return value;
947
+ });
948
+ }
949
+ function deserialize(data) {
950
+ return JSON.parse(data);
951
+ }
952
+
953
+ // src/persistence/apply-limits.ts
954
+ function applyLimits(data, limits) {
955
+ const queries = data.clientState?.queries;
956
+ if (!queries || Object.keys(limits).length === 0) return data;
957
+ const entries = queries;
958
+ const unlimited = [];
959
+ const groups = /* @__PURE__ */ new Map();
960
+ for (const q of entries) {
961
+ const prefix = Array.isArray(q.queryKey) ? q.queryKey[0] : void 0;
962
+ if (typeof prefix === "string" && prefix in limits) {
963
+ const group = groups.get(prefix) ?? [];
964
+ group.push(q);
965
+ groups.set(prefix, group);
966
+ } else {
967
+ unlimited.push(q);
968
+ }
969
+ }
970
+ const kept = [];
971
+ for (const [prefix, group] of groups) {
972
+ const limit = limits[prefix];
973
+ const sorted = [...group].sort(
974
+ (a, b) => (b.state.dataUpdatedAt ?? 0) - (a.state.dataUpdatedAt ?? 0)
975
+ );
976
+ kept.push(...sorted.slice(0, limit));
977
+ }
978
+ return {
979
+ ...data,
980
+ clientState: {
981
+ ...data.clientState,
982
+ queries: [...unlimited, ...kept]
983
+ }
984
+ };
985
+ }
986
+
987
+ // src/persistence/create-persister.ts
988
+ var noopPersister = {
989
+ persistClient: async () => {
990
+ },
991
+ restoreClient: async () => void 0,
992
+ removeClient: async () => {
993
+ }
994
+ };
995
+ function createPersister(config) {
996
+ if (typeof window === "undefined") return noopPersister;
997
+ const throttleMs = config.throttleMs ?? 1e3;
998
+ const limits = config.limits ?? {};
999
+ let timer;
1000
+ let pending;
1001
+ function flush() {
1002
+ if (!pending) return;
1003
+ try {
1004
+ const limited = applyLimits(pending, limits);
1005
+ window.localStorage.setItem(config.key, serialize(limited));
1006
+ } catch {
1007
+ }
1008
+ pending = void 0;
1009
+ }
1010
+ return {
1011
+ persistClient(client) {
1012
+ pending = client;
1013
+ if (!timer) {
1014
+ timer = setTimeout(() => {
1015
+ timer = void 0;
1016
+ flush();
1017
+ }, throttleMs);
1018
+ }
1019
+ },
1020
+ restoreClient() {
1021
+ try {
1022
+ const raw = window.localStorage.getItem(config.key);
1023
+ if (!raw) return void 0;
1024
+ return deserialize(raw);
1025
+ } catch {
1026
+ return void 0;
1027
+ }
1028
+ },
1029
+ removeClient() {
1030
+ window.localStorage.removeItem(config.key);
1031
+ }
1032
+ };
1033
+ }
1034
+
1035
+ // src/persistence/use-storage-monitor.ts
1036
+ import { useEffect as useEffect3 } from "react";
1037
+ var CHECK_INTERVAL_MS = 5 * 60 * 1e3;
1038
+ function useStorageMonitor(config) {
1039
+ const { key, limits = {}, maxStorageMB = 3 } = config;
1040
+ const limitKeys = Object.keys(limits);
1041
+ useEffect3(() => {
1042
+ if (typeof window === "undefined" || limitKeys.length === 0) return;
1043
+ function evict() {
1044
+ const storage = window.localStorage;
1045
+ let totalSize = 0;
1046
+ for (const k of Object.keys(storage)) {
1047
+ const v = storage.getItem(k);
1048
+ if (v) totalSize += k.length + v.length;
1049
+ }
1050
+ const usedMB = totalSize * 2 / 1024 / 1024;
1051
+ if (usedMB <= maxStorageMB) return;
1052
+ try {
1053
+ const raw = storage.getItem(key);
1054
+ if (!raw) return;
1055
+ const parsed = JSON.parse(raw);
1056
+ if (!parsed.clientState?.queries) return;
1057
+ parsed.clientState.queries = parsed.clientState.queries.filter(
1058
+ (q) => {
1059
+ if (!Array.isArray(q.queryKey)) return true;
1060
+ const prefix = q.queryKey[0];
1061
+ return typeof prefix !== "string" || !limitKeys.includes(prefix);
1062
+ }
1063
+ );
1064
+ storage.setItem(key, JSON.stringify(parsed));
1065
+ } catch {
1066
+ storage.removeItem(key);
1067
+ }
1068
+ }
1069
+ evict();
1070
+ const id = setInterval(evict, CHECK_INTERVAL_MS);
1071
+ return () => clearInterval(id);
1072
+ }, [key, maxStorageMB, limitKeys.length]);
1073
+ }
1074
+
1075
+ // src/persistence/use-persistence.ts
1076
+ function useAugurPersistence(config) {
1077
+ const [persister] = useState2(() => createPersister(config));
1078
+ const [isMounted, setIsMounted] = useState2(false);
1079
+ useEffect4(() => {
1080
+ setIsMounted(true);
1081
+ }, []);
1082
+ useStorageMonitor(config);
1083
+ const isDev = typeof process !== "undefined" && process.env?.NODE_ENV === "development";
1084
+ const { persistableKeys } = config;
1085
+ const dehydrateOptions = useMemo3(() => ({
1086
+ shouldDehydrateQuery: (query) => {
1087
+ const key = query.queryKey;
1088
+ if (!Array.isArray(key) || key.length === 0) return false;
1089
+ if (query.state.status !== "success") return false;
1090
+ return persistableKeys.includes(key[0]);
1091
+ }
1092
+ }), [persistableKeys]);
1093
+ return {
1094
+ persister,
1095
+ maxAge: isMounted ? config.maxAge ?? 24 * 60 * 60 * 1e3 : 0,
1096
+ buster: isDev ? String(Date.now()) : config.buster ?? "",
1097
+ dehydrateOptions
1098
+ };
1099
+ }
938
1100
  export {
939
1101
  AugurHooksProvider,
940
1102
  createSiteHooks,
@@ -970,6 +1132,7 @@ export {
970
1132
  useAugurApi,
971
1133
  useAugurAuth,
972
1134
  useAugurCache,
1135
+ useAugurPersistence,
973
1136
  useCartActions,
974
1137
  useCartHdrUid,
975
1138
  useCartInitialization,