tanstack-cacher 1.5.0 → 1.6.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/index.d.ts +4 -1
- package/index.js +22 -0
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ interface PaginationConfig {
|
|
|
10
10
|
pageSizePath?: string;
|
|
11
11
|
}
|
|
12
12
|
interface CacheConfig<TData, TItem> {
|
|
13
|
-
queryClient
|
|
13
|
+
queryClient?: QueryClient;
|
|
14
14
|
queryKey: QueryKey;
|
|
15
15
|
itemsPath?: string;
|
|
16
16
|
pagination?: PaginationConfig;
|
|
@@ -36,6 +36,9 @@ declare class QueryCacheManager<TData, TItem> {
|
|
|
36
36
|
update(updatedItem: Partial<TItem>, matcher?: (item: TItem) => boolean): void;
|
|
37
37
|
updateWithCustomLogic(updater: (oldData: any) => any): void;
|
|
38
38
|
delete(itemOrId: TItem | string | number, matcher?: (item: TItem) => boolean): void;
|
|
39
|
+
updateArrayAtPath<TItem = any>(path: string, updater: TItem | TItem[] | ((items: TItem[]) => TItem[]), options?: {
|
|
40
|
+
position?: InsertPosition;
|
|
41
|
+
}): void;
|
|
39
42
|
replace(newData: TData): void;
|
|
40
43
|
clear(): void;
|
|
41
44
|
getItemsFromCache(): TItem[];
|
package/index.js
CHANGED
|
@@ -268,6 +268,28 @@ var QueryCacheManager = class {
|
|
|
268
268
|
this.invalidate();
|
|
269
269
|
}
|
|
270
270
|
}
|
|
271
|
+
updateArrayAtPath(path, updater, options) {
|
|
272
|
+
try {
|
|
273
|
+
this.config.queryClient.setQueryData(this.config.queryKey, (oldData) => {
|
|
274
|
+
if (!oldData) return oldData;
|
|
275
|
+
const current = getAtPath(oldData, path, []);
|
|
276
|
+
const safeCurrent = Array.isArray(current) ? current : [];
|
|
277
|
+
let updatedItems;
|
|
278
|
+
if (typeof updater === "function") {
|
|
279
|
+
updatedItems = updater(safeCurrent);
|
|
280
|
+
} else if (Array.isArray(updater)) {
|
|
281
|
+
updatedItems = updater;
|
|
282
|
+
} else {
|
|
283
|
+
const position = options?.position ?? "start";
|
|
284
|
+
updatedItems = position === "start" ? [updater, ...safeCurrent] : [...safeCurrent, updater];
|
|
285
|
+
}
|
|
286
|
+
return setAtPath(oldData, path, updatedItems);
|
|
287
|
+
});
|
|
288
|
+
} catch (error) {
|
|
289
|
+
console.error("[QueryCacheManager] updateArrayAtPath failed:", error);
|
|
290
|
+
this.invalidate();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
271
293
|
/**
|
|
272
294
|
* Replace full data
|
|
273
295
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tanstack-cacher",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
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": {
|
|
@@ -81,4 +81,4 @@
|
|
|
81
81
|
"tsup": "^8.3.5",
|
|
82
82
|
"typescript": "^5.7.2"
|
|
83
83
|
}
|
|
84
|
-
}
|
|
84
|
+
}
|