@spoosh/plugin-cache 0.2.2 → 0.3.1
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/README.md +29 -5
- package/dist/index.d.mts +11 -4
- package/dist/index.d.ts +11 -4
- package/dist/index.js +14 -3
- package/dist/index.mjs +14 -3
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Response caching plugin for Spoosh with configurable stale time.
|
|
4
4
|
|
|
5
|
-
**[Documentation](https://spoosh.dev/docs/plugins/cache)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
5
|
+
**[Documentation](https://spoosh.dev/docs/react/plugins/cache)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
@@ -38,6 +38,24 @@ useRead((api) => api("posts").GET(), { staleTime: 10000 });
|
|
|
38
38
|
| ----------- | -------- | ------------------------------------ |
|
|
39
39
|
| `staleTime` | `number` | Override stale time for this request |
|
|
40
40
|
|
|
41
|
+
### Write Options
|
|
42
|
+
|
|
43
|
+
Clear cache after a mutation completes successfully:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
const { trigger } = useWrite((api) => api("auth/logout").POST);
|
|
47
|
+
|
|
48
|
+
// Clear cache after logout
|
|
49
|
+
await trigger({ clearCache: true });
|
|
50
|
+
|
|
51
|
+
// Clear cache + trigger all queries to refetch
|
|
52
|
+
await trigger({ clearCache: true, invalidate: "*" });
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
| Option | Type | Description |
|
|
56
|
+
| ------------ | --------- | --------------------------------------------- |
|
|
57
|
+
| `clearCache` | `boolean` | Clear all cached data after mutation succeeds |
|
|
58
|
+
|
|
41
59
|
## Instance API
|
|
42
60
|
|
|
43
61
|
The plugin exposes a `clearCache` function for manually clearing all cached data:
|
|
@@ -47,12 +65,18 @@ import { createReactSpoosh } from "@spoosh/react";
|
|
|
47
65
|
|
|
48
66
|
const { useRead, clearCache } = createReactSpoosh(client);
|
|
49
67
|
|
|
50
|
-
// Clear all cached data (
|
|
68
|
+
// Clear all cached data only (no refetch)
|
|
51
69
|
function handleLogout() {
|
|
52
70
|
clearCache();
|
|
53
71
|
}
|
|
72
|
+
|
|
73
|
+
// Clear cache and trigger all queries to refetch
|
|
74
|
+
function handleUserSwitch() {
|
|
75
|
+
clearCache({ refetchAll: true });
|
|
76
|
+
}
|
|
54
77
|
```
|
|
55
78
|
|
|
56
|
-
| Method
|
|
57
|
-
|
|
|
58
|
-
| `clearCache`
|
|
79
|
+
| Method | Description |
|
|
80
|
+
| ---------------------------------- | ------------------------------------------------- |
|
|
81
|
+
| `clearCache()` | Clears all cached data without triggering refetch |
|
|
82
|
+
| `clearCache({ refetchAll: true })` | Clears cache and triggers all queries to refetch |
|
package/dist/index.d.mts
CHANGED
|
@@ -4,15 +4,22 @@ interface CachePluginConfig {
|
|
|
4
4
|
/** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
|
|
5
5
|
staleTime?: number;
|
|
6
6
|
}
|
|
7
|
+
interface ClearCacheOptions {
|
|
8
|
+
/** Whether to trigger all queries to refetch after clearing. Defaults to false. */
|
|
9
|
+
refetchAll?: boolean;
|
|
10
|
+
}
|
|
7
11
|
interface CacheInstanceApi {
|
|
8
12
|
/** Clear all cached data. Useful for logout or user switching scenarios. */
|
|
9
|
-
clearCache: () => void;
|
|
13
|
+
clearCache: (options?: ClearCacheOptions) => void;
|
|
10
14
|
}
|
|
11
15
|
interface CacheReadOptions {
|
|
12
16
|
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
13
17
|
staleTime?: number;
|
|
14
18
|
}
|
|
15
|
-
|
|
19
|
+
interface CacheWriteOptions {
|
|
20
|
+
/** Clear all cached data after mutation completes successfully. */
|
|
21
|
+
clearCache?: boolean;
|
|
22
|
+
}
|
|
16
23
|
interface CacheInfiniteReadOptions {
|
|
17
24
|
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
18
25
|
staleTime?: number;
|
|
@@ -28,7 +35,7 @@ type CacheWriteResult = object;
|
|
|
28
35
|
*
|
|
29
36
|
* @param config - Plugin configuration
|
|
30
37
|
*
|
|
31
|
-
* @see {@link https://spoosh.dev/docs/plugins/cache | Cache Plugin Documentation}
|
|
38
|
+
* @see {@link https://spoosh.dev/docs/react/plugins/cache | Cache Plugin Documentation}
|
|
32
39
|
*
|
|
33
40
|
* @example
|
|
34
41
|
* ```ts
|
|
@@ -55,4 +62,4 @@ declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
|
|
|
55
62
|
instanceApi: CacheInstanceApi;
|
|
56
63
|
}>;
|
|
57
64
|
|
|
58
|
-
export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
|
|
65
|
+
export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, type ClearCacheOptions, cachePlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,15 +4,22 @@ interface CachePluginConfig {
|
|
|
4
4
|
/** Default stale time in milliseconds. Data older than this is considered stale. Defaults to 0. */
|
|
5
5
|
staleTime?: number;
|
|
6
6
|
}
|
|
7
|
+
interface ClearCacheOptions {
|
|
8
|
+
/** Whether to trigger all queries to refetch after clearing. Defaults to false. */
|
|
9
|
+
refetchAll?: boolean;
|
|
10
|
+
}
|
|
7
11
|
interface CacheInstanceApi {
|
|
8
12
|
/** Clear all cached data. Useful for logout or user switching scenarios. */
|
|
9
|
-
clearCache: () => void;
|
|
13
|
+
clearCache: (options?: ClearCacheOptions) => void;
|
|
10
14
|
}
|
|
11
15
|
interface CacheReadOptions {
|
|
12
16
|
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
13
17
|
staleTime?: number;
|
|
14
18
|
}
|
|
15
|
-
|
|
19
|
+
interface CacheWriteOptions {
|
|
20
|
+
/** Clear all cached data after mutation completes successfully. */
|
|
21
|
+
clearCache?: boolean;
|
|
22
|
+
}
|
|
16
23
|
interface CacheInfiniteReadOptions {
|
|
17
24
|
/** Time in milliseconds before cached data is considered stale. Overrides plugin default. */
|
|
18
25
|
staleTime?: number;
|
|
@@ -28,7 +35,7 @@ type CacheWriteResult = object;
|
|
|
28
35
|
*
|
|
29
36
|
* @param config - Plugin configuration
|
|
30
37
|
*
|
|
31
|
-
* @see {@link https://spoosh.dev/docs/plugins/cache | Cache Plugin Documentation}
|
|
38
|
+
* @see {@link https://spoosh.dev/docs/react/plugins/cache | Cache Plugin Documentation}
|
|
32
39
|
*
|
|
33
40
|
* @example
|
|
34
41
|
* ```ts
|
|
@@ -55,4 +62,4 @@ declare function cachePlugin(config?: CachePluginConfig): SpooshPlugin<{
|
|
|
55
62
|
instanceApi: CacheInstanceApi;
|
|
56
63
|
}>;
|
|
57
64
|
|
|
58
|
-
export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, cachePlugin };
|
|
65
|
+
export { type CacheInfiniteReadOptions, type CacheInstanceApi, type CachePluginConfig, type CacheReadOptions, type CacheReadResult, type CacheWriteOptions, type CacheWriteResult, type ClearCacheOptions, cachePlugin };
|
package/dist/index.js
CHANGED
|
@@ -29,7 +29,7 @@ function cachePlugin(config = {}) {
|
|
|
29
29
|
const { staleTime: defaultStaleTime = 0 } = config;
|
|
30
30
|
return {
|
|
31
31
|
name: "spoosh:cache",
|
|
32
|
-
operations: ["read", "infiniteRead"],
|
|
32
|
+
operations: ["read", "infiniteRead", "write"],
|
|
33
33
|
middleware: async (context, next) => {
|
|
34
34
|
if (!context.forceRefetch) {
|
|
35
35
|
const cached = context.stateManager.getCache(context.queryKey);
|
|
@@ -44,10 +44,21 @@ function cachePlugin(config = {}) {
|
|
|
44
44
|
}
|
|
45
45
|
return await next();
|
|
46
46
|
},
|
|
47
|
+
afterResponse(context, response) {
|
|
48
|
+
if (!response.error) {
|
|
49
|
+
const pluginOptions = context.pluginOptions;
|
|
50
|
+
if (pluginOptions?.clearCache) {
|
|
51
|
+
context.stateManager.clear();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
47
55
|
instanceApi(context) {
|
|
48
|
-
const { stateManager } = context;
|
|
49
|
-
const clearCache = () => {
|
|
56
|
+
const { stateManager, eventEmitter } = context;
|
|
57
|
+
const clearCache = (options) => {
|
|
50
58
|
stateManager.clear();
|
|
59
|
+
if (options?.refetchAll) {
|
|
60
|
+
eventEmitter.emit("refetchAll", void 0);
|
|
61
|
+
}
|
|
51
62
|
};
|
|
52
63
|
return { clearCache };
|
|
53
64
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -3,7 +3,7 @@ function cachePlugin(config = {}) {
|
|
|
3
3
|
const { staleTime: defaultStaleTime = 0 } = config;
|
|
4
4
|
return {
|
|
5
5
|
name: "spoosh:cache",
|
|
6
|
-
operations: ["read", "infiniteRead"],
|
|
6
|
+
operations: ["read", "infiniteRead", "write"],
|
|
7
7
|
middleware: async (context, next) => {
|
|
8
8
|
if (!context.forceRefetch) {
|
|
9
9
|
const cached = context.stateManager.getCache(context.queryKey);
|
|
@@ -18,10 +18,21 @@ function cachePlugin(config = {}) {
|
|
|
18
18
|
}
|
|
19
19
|
return await next();
|
|
20
20
|
},
|
|
21
|
+
afterResponse(context, response) {
|
|
22
|
+
if (!response.error) {
|
|
23
|
+
const pluginOptions = context.pluginOptions;
|
|
24
|
+
if (pluginOptions?.clearCache) {
|
|
25
|
+
context.stateManager.clear();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
},
|
|
21
29
|
instanceApi(context) {
|
|
22
|
-
const { stateManager } = context;
|
|
23
|
-
const clearCache = () => {
|
|
30
|
+
const { stateManager, eventEmitter } = context;
|
|
31
|
+
const clearCache = (options) => {
|
|
24
32
|
stateManager.clear();
|
|
33
|
+
if (options?.refetchAll) {
|
|
34
|
+
eventEmitter.emit("refetchAll", void 0);
|
|
35
|
+
}
|
|
25
36
|
};
|
|
26
37
|
return { clearCache };
|
|
27
38
|
}
|
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-cache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Response caching plugin for Spoosh with configurable stale time",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/
|
|
8
|
+
"url": "git+https://github.com/spooshdev/spoosh.git",
|
|
9
9
|
"directory": "packages/plugin-cache"
|
|
10
10
|
},
|
|
11
11
|
"bugs": {
|
|
12
|
-
"url": "https://github.com/
|
|
12
|
+
"url": "https://github.com/spooshdev/spoosh/issues"
|
|
13
13
|
},
|
|
14
|
-
"homepage": "https://spoosh.dev/react/
|
|
14
|
+
"homepage": "https://spoosh.dev/docs/react/plugins/cache",
|
|
15
15
|
"publishConfig": {
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"@spoosh/core": ">=0.8.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/
|
|
40
|
-
"@spoosh/
|
|
39
|
+
"@spoosh/test-utils": "0.1.5",
|
|
40
|
+
"@spoosh/core": "0.10.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|