tanstack-cacher 1.6.0 → 1.6.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.
Files changed (3) hide show
  1. package/index.d.ts +20 -2
  2. package/index.js +85 -0
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ interface PaginationConfig {
8
8
  totalPagesPath?: string;
9
9
  currentPagePath?: string;
10
10
  pageSizePath?: string;
11
+ numberOfElementsPath?: string;
11
12
  }
12
13
  interface CacheConfig<TData, TItem> {
13
14
  queryClient?: QueryClient;
@@ -24,6 +25,7 @@ interface CacheHandlers<TItem> {
24
25
  onDelete: (itemOrId: TItem | string | number, matcher?: (item: TItem) => boolean) => void;
25
26
  }
26
27
  type InsertPosition = 'start' | 'end';
28
+ type ResolvedCacheConfig<TData, TItem> = Required<Pick<CacheConfig<TData, TItem>, 'queryClient' | 'queryKey' | 'itemsPath'>> & Pick<CacheConfig<TData, TItem>, 'keyExtractor' | 'pagination' | 'initialData' | 'isPaginated'>;
27
29
 
28
30
  declare class QueryCacheManager<TData, TItem> {
29
31
  private config;
@@ -47,7 +49,7 @@ declare class QueryCacheManager<TData, TItem> {
47
49
  refetch(key?: string | string[]): void;
48
50
  hasQuery(key?: string): boolean;
49
51
  removeQuery(key?: string): void;
50
- getConfig(): typeof this.config;
52
+ getConfig(): ResolvedCacheConfig<TData, TItem>;
51
53
  createHandlers(): CacheHandlers<TItem>;
52
54
  }
53
55
 
@@ -97,6 +99,22 @@ declare const useQueryCacheManagers: <T extends Record<string, QueryCacheManager
97
99
  options?: Partial<Omit<CacheConfig<any, any>, "queryClient">>;
98
100
  }; }) => T;
99
101
 
102
+ interface UsePaginatedCacheActionsConfig<TData, TItem> {
103
+ cacher: QueryCacheManager<TData, TItem>;
104
+ defaultPage?: number;
105
+ refetchThreshold?: number;
106
+ onNavigateToPage?: (page: number) => void;
107
+ onClearSearch?: () => void;
108
+ onRefetch?: () => void;
109
+ }
110
+ declare const usePaginatedCacheActions: <TData, TItem extends {
111
+ id: string | number;
112
+ }>({ cacher, defaultPage, refetchThreshold, onNavigateToPage, onClearSearch, onRefetch, }: UsePaginatedCacheActionsConfig<TData, TItem>) => {
113
+ add: (item: TItem) => void;
114
+ update: (item: TItem) => void;
115
+ remove: (id: string | number) => void;
116
+ };
117
+
100
118
  interface CacheProviderProps {
101
119
  children: ReactNode;
102
120
  config: CacheContextType;
@@ -108,4 +126,4 @@ type CacheOptions<TData = any, TItem = any> = Omit<CacheConfig<TData, TItem>, 'q
108
126
  declare const resetCacheManager: (queryKey: QueryKey) => void;
109
127
  declare const resetAllCacheManagers: () => void;
110
128
 
111
- export { type CacheConfig, type CacheHandlers, type CacheManagerConstructor, type CacheOptions, CacheProvider, type CustomMutationOptions, type InsertPosition, type PaginationConfig, QueryCacheManager, cacheManagerFactory, resetAllCacheManagers, resetCacheManager, useCacherContext, useCacherMutation, useQueryCacheManagers };
129
+ export { type CacheConfig, type CacheHandlers, type CacheManagerConstructor, type CacheOptions, CacheProvider, type CustomMutationOptions, type InsertPosition, type PaginationConfig, QueryCacheManager, type UsePaginatedCacheActionsConfig, cacheManagerFactory, resetAllCacheManagers, resetCacheManager, useCacherContext, useCacherMutation, usePaginatedCacheActions, useQueryCacheManagers };
package/index.js CHANGED
@@ -272,6 +272,10 @@ var QueryCacheManager = class {
272
272
  try {
273
273
  this.config.queryClient.setQueryData(this.config.queryKey, (oldData) => {
274
274
  if (!oldData) return oldData;
275
+ if (!updater) {
276
+ this.invalidate();
277
+ return oldData;
278
+ }
275
279
  const current = getAtPath(oldData, path, []);
276
280
  const safeCurrent = Array.isArray(current) ? current : [];
277
281
  let updatedItems;
@@ -493,6 +497,86 @@ var useQueryCacheManagers = (configs) => {
493
497
  });
494
498
  return managers;
495
499
  };
500
+
501
+ // src/hooks/usePaginatedCacheActions.ts
502
+ var DEFAULT_REFETCH_THRESHOLD = 5;
503
+ var usePaginatedCacheActions = ({
504
+ cacher,
505
+ defaultPage = 0,
506
+ refetchThreshold = DEFAULT_REFETCH_THRESHOLD,
507
+ onNavigateToPage,
508
+ onClearSearch,
509
+ onRefetch
510
+ }) => {
511
+ const config = cacher.getConfig();
512
+ const { queryClient, queryKey, itemsPath, pagination } = config;
513
+ const currentPagePath = pagination?.currentPagePath ?? "page";
514
+ const pageSizePath = pagination?.pageSizePath ?? "size";
515
+ const totalElementsPath = pagination?.totalElementsPath ?? "totalElements";
516
+ const totalPagesPath = pagination?.totalPagesPath ?? "totalPages";
517
+ const numberOfElementsPath = pagination?.numberOfElementsPath ?? "numberOfElements";
518
+ const add = (item) => {
519
+ onClearSearch?.();
520
+ onNavigateToPage?.(1);
521
+ queryClient.setQueriesData({ queryKey }, (old) => {
522
+ if (!old) return old;
523
+ const currentPage = getAtPath(old, currentPagePath, 1);
524
+ if (currentPage !== defaultPage) return old;
525
+ const items = getAtPath(old, itemsPath, []);
526
+ const totalElements = getAtPath(old, totalElementsPath, 0);
527
+ const pageSize = getAtPath(old, pageSizePath, 10);
528
+ const newTotal = totalElements + 1;
529
+ const newItems = [item, ...items];
530
+ let result = { ...old };
531
+ result = setAtPath(result, itemsPath, newItems);
532
+ result = setAtPath(result, totalElementsPath, newTotal);
533
+ result = setAtPath(result, totalPagesPath, Math.ceil(newTotal / pageSize));
534
+ result = setAtPath(result, numberOfElementsPath, newItems.length);
535
+ return result;
536
+ });
537
+ };
538
+ const update = (item) => {
539
+ queryClient.setQueriesData({ queryKey }, (old) => {
540
+ if (!old) return old;
541
+ const items = getAtPath(old, itemsPath, []);
542
+ const index = items.findIndex((i) => i.id === item.id);
543
+ if (index === -1) return old;
544
+ const newItems = [...items];
545
+ newItems[index] = item;
546
+ return setAtPath({ ...old }, itemsPath, newItems);
547
+ });
548
+ };
549
+ const remove = (id) => {
550
+ let shouldRefetch = false;
551
+ queryClient.setQueriesData({ queryKey }, (old) => {
552
+ if (!old) return old;
553
+ const items = getAtPath(old, itemsPath, []);
554
+ const index = items.findIndex((i) => i.id === id);
555
+ if (index === -1) return old;
556
+ const newItems = items.filter((i) => i.id !== id);
557
+ const currentPage = getAtPath(old, currentPagePath, 1);
558
+ const totalElements = getAtPath(old, totalElementsPath, 0);
559
+ const pageSize = getAtPath(old, pageSizePath, 10);
560
+ const totalPages = getAtPath(old, totalPagesPath, 1);
561
+ const newTotal = Math.max(0, totalElements - 1);
562
+ const newTotalPages = Math.ceil(newTotal / pageSize);
563
+ if (newItems.length === 0 && currentPage > 1) {
564
+ onNavigateToPage?.(currentPage - 1);
565
+ } else if (newItems.length > 0 && newItems.length <= refetchThreshold && totalPages > currentPage) {
566
+ shouldRefetch = true;
567
+ }
568
+ let result = { ...old };
569
+ result = setAtPath(result, itemsPath, newItems);
570
+ result = setAtPath(result, totalElementsPath, newTotal);
571
+ result = setAtPath(result, totalPagesPath, newTotalPages);
572
+ result = setAtPath(result, numberOfElementsPath, newItems.length);
573
+ return result;
574
+ });
575
+ if (shouldRefetch) onRefetch?.();
576
+ };
577
+ return { add, update, remove };
578
+ };
579
+ var usePaginatedCacheActions_default = usePaginatedCacheActions;
496
580
  var CacheProvider = ({ config, children }) => {
497
581
  const queryClient = reactQuery.useQueryClient();
498
582
  React.useEffect(() => {
@@ -535,6 +619,7 @@ exports.resetAllCacheManagers = resetAllCacheManagers;
535
619
  exports.resetCacheManager = resetCacheManager;
536
620
  exports.useCacherContext = useCacherContext;
537
621
  exports.useCacherMutation = useCacherMutation;
622
+ exports.usePaginatedCacheActions = usePaginatedCacheActions_default;
538
623
  exports.useQueryCacheManagers = useQueryCacheManagers;
539
624
  Object.keys(reactQuery).forEach(function (k) {
540
625
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-cacher",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "A lightweight cache management utility for TanStack Query that simplifies adding, updating, deleting, and synchronizing cached data",
5
5
  "license": "MIT",
6
6
  "repository": {