@spoosh/plugin-invalidation 0.5.7 → 0.7.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/README.md +6 -6
- package/dist/index.d.mts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +13 -1
- package/dist/index.mjs +13 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ When a mutation succeeds, related queries are automatically invalidated:
|
|
|
30
30
|
|
|
31
31
|
```typescript
|
|
32
32
|
// Creating a post at users/123/posts invalidates:
|
|
33
|
-
const { trigger } = useWrite((api) => api("users/:id/posts").POST);
|
|
33
|
+
const { trigger } = useWrite((api) => api("users/:id/posts").POST());
|
|
34
34
|
await trigger({ params: { id: 123 }, body: { title: "New Post" } });
|
|
35
35
|
|
|
36
36
|
// ✓ Invalidates: "users", "users/123", "users/123/posts"
|
|
@@ -43,9 +43,9 @@ await trigger({ params: { id: 123 }, body: { title: "New Post" } });
|
|
|
43
43
|
import { Spoosh } from "@spoosh/core";
|
|
44
44
|
import { invalidationPlugin } from "@spoosh/plugin-invalidation";
|
|
45
45
|
|
|
46
|
-
const
|
|
46
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([invalidationPlugin()]);
|
|
47
47
|
|
|
48
|
-
const { trigger } = useWrite((api) => api("posts").POST);
|
|
48
|
+
const { trigger } = useWrite((api) => api("posts").POST());
|
|
49
49
|
await trigger({ body: { title: "New Post" } });
|
|
50
50
|
```
|
|
51
51
|
|
|
@@ -172,9 +172,9 @@ await trigger({ invalidate: "*" });
|
|
|
172
172
|
The plugin exposes `invalidate` for manually triggering cache invalidation outside of mutations:
|
|
173
173
|
|
|
174
174
|
```typescript
|
|
175
|
-
import {
|
|
175
|
+
import { create } from "@spoosh/react";
|
|
176
176
|
|
|
177
|
-
const { useRead, invalidate } =
|
|
177
|
+
const { useRead, invalidate } = create(spoosh);
|
|
178
178
|
|
|
179
179
|
// Invalidate with string array
|
|
180
180
|
invalidate(["users", "posts"]);
|
|
@@ -205,7 +205,7 @@ socket.on("full-sync", () => {
|
|
|
205
205
|
For scenarios like logout or user switching, combine `invalidate: "*"` with `clearCache` from `@spoosh/plugin-cache`:
|
|
206
206
|
|
|
207
207
|
```typescript
|
|
208
|
-
const { trigger } = useWrite((api) => api("auth/logout").POST);
|
|
208
|
+
const { trigger } = useWrite((api) => api("auth/logout").POST());
|
|
209
209
|
|
|
210
210
|
// Clear cache + trigger all queries to refetch
|
|
211
211
|
await trigger({
|
package/dist/index.d.mts
CHANGED
|
@@ -23,7 +23,8 @@ interface InvalidationPluginConfig {
|
|
|
23
23
|
*/
|
|
24
24
|
defaultMode?: InvalidationMode;
|
|
25
25
|
}
|
|
26
|
-
|
|
26
|
+
type InvalidationWriteOptions = object;
|
|
27
|
+
interface InvalidationWriteTriggerOptions<TSchema = unknown> {
|
|
27
28
|
/** Unified invalidation configuration */
|
|
28
29
|
invalidate?: InvalidateOption<TSchema>;
|
|
29
30
|
}
|
|
@@ -73,7 +74,7 @@ declare module "@spoosh/core" {
|
|
|
73
74
|
* ```ts
|
|
74
75
|
* import { Spoosh } from "@spoosh/core";
|
|
75
76
|
*
|
|
76
|
-
* const
|
|
77
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
77
78
|
* .use([
|
|
78
79
|
* invalidationPlugin({ defaultMode: "all" }),
|
|
79
80
|
* ]);
|
|
@@ -103,10 +104,11 @@ declare module "@spoosh/core" {
|
|
|
103
104
|
declare function invalidationPlugin(config?: InvalidationPluginConfig): SpooshPlugin<{
|
|
104
105
|
readOptions: InvalidationReadOptions;
|
|
105
106
|
writeOptions: InvalidationWriteOptions;
|
|
107
|
+
writeTriggerOptions: InvalidationWriteTriggerOptions;
|
|
106
108
|
infiniteReadOptions: InvalidationInfiniteReadOptions;
|
|
107
109
|
readResult: InvalidationReadResult;
|
|
108
110
|
writeResult: InvalidationWriteResult;
|
|
109
111
|
instanceApi: InvalidationInstanceApi;
|
|
110
112
|
}>;
|
|
111
113
|
|
|
112
|
-
export { type InvalidateOption, type InvalidationInfiniteReadOptions, type InvalidationMode, type InvalidationPluginConfig, type InvalidationPluginExports, type InvalidationReadOptions, type InvalidationReadResult, type InvalidationWriteOptions, type InvalidationWriteResult, invalidationPlugin };
|
|
114
|
+
export { type InvalidateOption, type InvalidationInfiniteReadOptions, type InvalidationMode, type InvalidationPluginConfig, type InvalidationPluginExports, type InvalidationReadOptions, type InvalidationReadResult, type InvalidationWriteOptions, type InvalidationWriteResult, type InvalidationWriteTriggerOptions, invalidationPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -23,7 +23,8 @@ interface InvalidationPluginConfig {
|
|
|
23
23
|
*/
|
|
24
24
|
defaultMode?: InvalidationMode;
|
|
25
25
|
}
|
|
26
|
-
|
|
26
|
+
type InvalidationWriteOptions = object;
|
|
27
|
+
interface InvalidationWriteTriggerOptions<TSchema = unknown> {
|
|
27
28
|
/** Unified invalidation configuration */
|
|
28
29
|
invalidate?: InvalidateOption<TSchema>;
|
|
29
30
|
}
|
|
@@ -73,7 +74,7 @@ declare module "@spoosh/core" {
|
|
|
73
74
|
* ```ts
|
|
74
75
|
* import { Spoosh } from "@spoosh/core";
|
|
75
76
|
*
|
|
76
|
-
* const
|
|
77
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
77
78
|
* .use([
|
|
78
79
|
* invalidationPlugin({ defaultMode: "all" }),
|
|
79
80
|
* ]);
|
|
@@ -103,10 +104,11 @@ declare module "@spoosh/core" {
|
|
|
103
104
|
declare function invalidationPlugin(config?: InvalidationPluginConfig): SpooshPlugin<{
|
|
104
105
|
readOptions: InvalidationReadOptions;
|
|
105
106
|
writeOptions: InvalidationWriteOptions;
|
|
107
|
+
writeTriggerOptions: InvalidationWriteTriggerOptions;
|
|
106
108
|
infiniteReadOptions: InvalidationInfiniteReadOptions;
|
|
107
109
|
readResult: InvalidationReadResult;
|
|
108
110
|
writeResult: InvalidationWriteResult;
|
|
109
111
|
instanceApi: InvalidationInstanceApi;
|
|
110
112
|
}>;
|
|
111
113
|
|
|
112
|
-
export { type InvalidateOption, type InvalidationInfiniteReadOptions, type InvalidationMode, type InvalidationPluginConfig, type InvalidationPluginExports, type InvalidationReadOptions, type InvalidationReadResult, type InvalidationWriteOptions, type InvalidationWriteResult, invalidationPlugin };
|
|
114
|
+
export { type InvalidateOption, type InvalidationInfiniteReadOptions, type InvalidationMode, type InvalidationPluginConfig, type InvalidationPluginExports, type InvalidationReadOptions, type InvalidationReadResult, type InvalidationWriteOptions, type InvalidationWriteResult, type InvalidationWriteTriggerOptions, invalidationPlugin };
|
package/dist/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/plugin.ts
|
|
28
|
+
var PLUGIN_NAME = "spoosh:invalidation";
|
|
28
29
|
var INVALIDATION_DEFAULT_KEY = "invalidation:defaultMode";
|
|
29
30
|
function resolveModeTags(context, mode) {
|
|
30
31
|
switch (mode) {
|
|
@@ -68,7 +69,7 @@ function resolveInvalidateTags(context, defaultMode) {
|
|
|
68
69
|
function invalidationPlugin(config = {}) {
|
|
69
70
|
const { defaultMode = "all" } = config;
|
|
70
71
|
return {
|
|
71
|
-
name:
|
|
72
|
+
name: PLUGIN_NAME,
|
|
72
73
|
operations: ["write"],
|
|
73
74
|
exports(context) {
|
|
74
75
|
return {
|
|
@@ -78,27 +79,38 @@ function invalidationPlugin(config = {}) {
|
|
|
78
79
|
};
|
|
79
80
|
},
|
|
80
81
|
afterResponse(context, response) {
|
|
82
|
+
const t = context.tracer?.(PLUGIN_NAME);
|
|
81
83
|
if (!response.error) {
|
|
82
84
|
const tags = resolveInvalidateTags(context, defaultMode);
|
|
83
85
|
if (tags.includes("*")) {
|
|
86
|
+
t?.log("Refetch all", { color: "warning" });
|
|
84
87
|
context.eventEmitter.emit("refetchAll", void 0);
|
|
85
88
|
return;
|
|
86
89
|
}
|
|
87
90
|
if (tags.length > 0) {
|
|
91
|
+
t?.log("Invalidated tags", {
|
|
92
|
+
color: "info",
|
|
93
|
+
info: [{ label: "Tags", value: tags }]
|
|
94
|
+
});
|
|
88
95
|
context.stateManager.markStale(tags);
|
|
89
96
|
context.eventEmitter.emit("invalidate", tags);
|
|
97
|
+
} else {
|
|
98
|
+
t?.skip("No tags to invalidate", { color: "muted" });
|
|
90
99
|
}
|
|
91
100
|
}
|
|
92
101
|
},
|
|
93
102
|
instanceApi(context) {
|
|
94
103
|
const { stateManager, eventEmitter } = context;
|
|
104
|
+
const et = context.eventTracer?.(PLUGIN_NAME);
|
|
95
105
|
const invalidate = (input) => {
|
|
96
106
|
const tags = Array.isArray(input) ? input : [input];
|
|
97
107
|
if (tags.includes("*")) {
|
|
108
|
+
et?.emit("Refetch all (manual)", { color: "warning" });
|
|
98
109
|
eventEmitter.emit("refetchAll", void 0);
|
|
99
110
|
return;
|
|
100
111
|
}
|
|
101
112
|
if (tags.length > 0) {
|
|
113
|
+
et?.emit(`Invalidated: ${tags.join(", ")}`, { color: "info" });
|
|
102
114
|
stateManager.markStale(tags);
|
|
103
115
|
eventEmitter.emit("invalidate", tags);
|
|
104
116
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/plugin.ts
|
|
2
|
+
var PLUGIN_NAME = "spoosh:invalidation";
|
|
2
3
|
var INVALIDATION_DEFAULT_KEY = "invalidation:defaultMode";
|
|
3
4
|
function resolveModeTags(context, mode) {
|
|
4
5
|
switch (mode) {
|
|
@@ -42,7 +43,7 @@ function resolveInvalidateTags(context, defaultMode) {
|
|
|
42
43
|
function invalidationPlugin(config = {}) {
|
|
43
44
|
const { defaultMode = "all" } = config;
|
|
44
45
|
return {
|
|
45
|
-
name:
|
|
46
|
+
name: PLUGIN_NAME,
|
|
46
47
|
operations: ["write"],
|
|
47
48
|
exports(context) {
|
|
48
49
|
return {
|
|
@@ -52,27 +53,38 @@ function invalidationPlugin(config = {}) {
|
|
|
52
53
|
};
|
|
53
54
|
},
|
|
54
55
|
afterResponse(context, response) {
|
|
56
|
+
const t = context.tracer?.(PLUGIN_NAME);
|
|
55
57
|
if (!response.error) {
|
|
56
58
|
const tags = resolveInvalidateTags(context, defaultMode);
|
|
57
59
|
if (tags.includes("*")) {
|
|
60
|
+
t?.log("Refetch all", { color: "warning" });
|
|
58
61
|
context.eventEmitter.emit("refetchAll", void 0);
|
|
59
62
|
return;
|
|
60
63
|
}
|
|
61
64
|
if (tags.length > 0) {
|
|
65
|
+
t?.log("Invalidated tags", {
|
|
66
|
+
color: "info",
|
|
67
|
+
info: [{ label: "Tags", value: tags }]
|
|
68
|
+
});
|
|
62
69
|
context.stateManager.markStale(tags);
|
|
63
70
|
context.eventEmitter.emit("invalidate", tags);
|
|
71
|
+
} else {
|
|
72
|
+
t?.skip("No tags to invalidate", { color: "muted" });
|
|
64
73
|
}
|
|
65
74
|
}
|
|
66
75
|
},
|
|
67
76
|
instanceApi(context) {
|
|
68
77
|
const { stateManager, eventEmitter } = context;
|
|
78
|
+
const et = context.eventTracer?.(PLUGIN_NAME);
|
|
69
79
|
const invalidate = (input) => {
|
|
70
80
|
const tags = Array.isArray(input) ? input : [input];
|
|
71
81
|
if (tags.includes("*")) {
|
|
82
|
+
et?.emit("Refetch all (manual)", { color: "warning" });
|
|
72
83
|
eventEmitter.emit("refetchAll", void 0);
|
|
73
84
|
return;
|
|
74
85
|
}
|
|
75
86
|
if (tags.length > 0) {
|
|
87
|
+
et?.emit(`Invalidated: ${tags.join(", ")}`, { color: "info" });
|
|
76
88
|
stateManager.markStale(tags);
|
|
77
89
|
eventEmitter.emit("invalidate", tags);
|
|
78
90
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-invalidation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Cache invalidation plugin for Spoosh - auto-invalidates after mutations",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.
|
|
36
|
+
"@spoosh/core": ">=0.13.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.
|
|
39
|
+
"@spoosh/core": "0.13.1",
|
|
40
|
+
"@spoosh/test-utils": "0.2.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|