@spoosh/plugin-cache 0.3.4 → 0.3.5
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 +7 -3
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install @spoosh/plugin-cache
|
|
|
16
16
|
import { Spoosh } from "@spoosh/core";
|
|
17
17
|
import { cachePlugin } from "@spoosh/plugin-cache";
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
20
20
|
cachePlugin({ staleTime: 5000 }),
|
|
21
21
|
]);
|
|
22
22
|
|
|
@@ -24,6 +24,10 @@ const client = new Spoosh<ApiSchema, Error>("/api").use([
|
|
|
24
24
|
useRead((api) => api("posts").GET(), { staleTime: 10000 });
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
+
## How It Works
|
|
28
|
+
|
|
29
|
+
The cache plugin runs with **priority -10**, meaning it executes early in the middleware chain to check the cache before other plugins (like retry, debug) run. This ensures maximum efficiency by short-circuiting requests when cached data is available.
|
|
30
|
+
|
|
27
31
|
## Options
|
|
28
32
|
|
|
29
33
|
### Plugin Config
|
|
@@ -61,9 +65,9 @@ await trigger({ clearCache: true, invalidate: "*" });
|
|
|
61
65
|
The plugin exposes a `clearCache` function for manually clearing all cached data:
|
|
62
66
|
|
|
63
67
|
```typescript
|
|
64
|
-
import {
|
|
68
|
+
import { create } from "@spoosh/react";
|
|
65
69
|
|
|
66
|
-
const { useRead, clearCache } =
|
|
70
|
+
const { useRead, clearCache } = create(spoosh);
|
|
67
71
|
|
|
68
72
|
// Clear all cached data only (no refetch)
|
|
69
73
|
function handleLogout() {
|
package/dist/index.d.mts
CHANGED
|
@@ -33,6 +33,9 @@ type CacheWriteResult = object;
|
|
|
33
33
|
* Returns cached data immediately if available and not stale,
|
|
34
34
|
* avoiding unnecessary network requests.
|
|
35
35
|
*
|
|
36
|
+
* This plugin runs with priority -10, meaning it executes early in the middleware chain
|
|
37
|
+
* to check the cache before other plugins (like retry, debug) run.
|
|
38
|
+
*
|
|
36
39
|
* @param config - Plugin configuration
|
|
37
40
|
*
|
|
38
41
|
* @see {@link https://spoosh.dev/docs/react/plugins/cache | Cache Plugin Documentation}
|
|
@@ -41,10 +44,10 @@ type CacheWriteResult = object;
|
|
|
41
44
|
* ```ts
|
|
42
45
|
* import { Spoosh } from "@spoosh/core";
|
|
43
46
|
*
|
|
44
|
-
* const
|
|
47
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
45
48
|
* .use([
|
|
46
|
-
* // ... other plugins
|
|
47
49
|
* cachePlugin({ staleTime: 5000 }),
|
|
50
|
+
* // ... other plugins
|
|
48
51
|
* ]);
|
|
49
52
|
*
|
|
50
53
|
* // Per-query override
|
package/dist/index.d.ts
CHANGED
|
@@ -33,6 +33,9 @@ type CacheWriteResult = object;
|
|
|
33
33
|
* Returns cached data immediately if available and not stale,
|
|
34
34
|
* avoiding unnecessary network requests.
|
|
35
35
|
*
|
|
36
|
+
* This plugin runs with priority -10, meaning it executes early in the middleware chain
|
|
37
|
+
* to check the cache before other plugins (like retry, debug) run.
|
|
38
|
+
*
|
|
36
39
|
* @param config - Plugin configuration
|
|
37
40
|
*
|
|
38
41
|
* @see {@link https://spoosh.dev/docs/react/plugins/cache | Cache Plugin Documentation}
|
|
@@ -41,10 +44,10 @@ type CacheWriteResult = object;
|
|
|
41
44
|
* ```ts
|
|
42
45
|
* import { Spoosh } from "@spoosh/core";
|
|
43
46
|
*
|
|
44
|
-
* const
|
|
47
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
45
48
|
* .use([
|
|
46
|
-
* // ... other plugins
|
|
47
49
|
* cachePlugin({ staleTime: 5000 }),
|
|
50
|
+
* // ... other plugins
|
|
48
51
|
* ]);
|
|
49
52
|
*
|
|
50
53
|
* // Per-query override
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ function cachePlugin(config = {}) {
|
|
|
30
30
|
return {
|
|
31
31
|
name: "spoosh:cache",
|
|
32
32
|
operations: ["read", "infiniteRead", "write"],
|
|
33
|
+
priority: -10,
|
|
33
34
|
middleware: async (context, next) => {
|
|
34
35
|
if (!context.forceRefetch) {
|
|
35
36
|
const cached = context.stateManager.getCache(context.queryKey);
|
package/dist/index.mjs
CHANGED
|
@@ -4,6 +4,7 @@ function cachePlugin(config = {}) {
|
|
|
4
4
|
return {
|
|
5
5
|
name: "spoosh:cache",
|
|
6
6
|
operations: ["read", "infiniteRead", "write"],
|
|
7
|
+
priority: -10,
|
|
7
8
|
middleware: async (context, next) => {
|
|
8
9
|
if (!context.forceRefetch) {
|
|
9
10
|
const cached = context.stateManager.getCache(context.queryKey);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-cache",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.5",
|
|
4
4
|
"description": "Response caching plugin for Spoosh with configurable stale time",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.12.
|
|
36
|
+
"@spoosh/core": ">=0.12.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/
|
|
40
|
-
"@spoosh/
|
|
39
|
+
"@spoosh/test-utils": "0.1.8",
|
|
40
|
+
"@spoosh/core": "0.12.1"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|