@spoosh/plugin-invalidation 0.5.7 → 0.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/README.md CHANGED
@@ -43,7 +43,7 @@ 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 client = new Spoosh<ApiSchema, Error>("/api").use([invalidationPlugin()]);
46
+ const spoosh = new Spoosh<ApiSchema, Error>("/api").use([invalidationPlugin()]);
47
47
 
48
48
  const { trigger } = useWrite((api) => api("posts").POST);
49
49
  await trigger({ body: { title: "New Post" } });
@@ -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 { createReactSpoosh } from "@spoosh/react";
175
+ import { create } from "@spoosh/react";
176
176
 
177
- const { useRead, invalidate } = createReactSpoosh(client);
177
+ const { useRead, invalidate } = create(spoosh);
178
178
 
179
179
  // Invalidate with string array
180
180
  invalidate(["users", "posts"]);
package/dist/index.d.mts CHANGED
@@ -73,7 +73,7 @@ declare module "@spoosh/core" {
73
73
  * ```ts
74
74
  * import { Spoosh } from "@spoosh/core";
75
75
  *
76
- * const client = new Spoosh<ApiSchema, Error>("/api")
76
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
77
77
  * .use([
78
78
  * invalidationPlugin({ defaultMode: "all" }),
79
79
  * ]);
package/dist/index.d.ts CHANGED
@@ -73,7 +73,7 @@ declare module "@spoosh/core" {
73
73
  * ```ts
74
74
  * import { Spoosh } from "@spoosh/core";
75
75
  *
76
- * const client = new Spoosh<ApiSchema, Error>("/api")
76
+ * const spoosh = new Spoosh<ApiSchema, Error>("/api")
77
77
  * .use([
78
78
  * invalidationPlugin({ defaultMode: "all" }),
79
79
  * ]);
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: "spoosh:invalidation",
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: "spoosh:invalidation",
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.5.7",
3
+ "version": "0.6.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.12.0"
36
+ "@spoosh/core": ">=0.13.0"
37
37
  },
38
38
  "devDependencies": {
39
- "@spoosh/core": "0.12.0",
40
- "@spoosh/test-utils": "0.1.8"
39
+ "@spoosh/core": "0.13.0",
40
+ "@spoosh/test-utils": "0.2.0"
41
41
  },
42
42
  "scripts": {
43
43
  "dev": "tsup --watch",