@spoosh/plugin-invalidation 0.4.0 → 0.5.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 CHANGED
@@ -112,14 +112,14 @@ await trigger({
112
112
 
113
113
  ### Plugin Config
114
114
 
115
- | Option | Type | Default | Description |
116
- | ------------- | --------------------------- | ------- | ---------------------------------------------------- |
115
+ | Option | Type | Default | Description |
116
+ | ------------- | --------------------------- | ------- | --------------------------------------------------- |
117
117
  | `defaultMode` | `"all" \| "self" \| "none"` | `"all"` | Default invalidation mode when option not specified |
118
118
 
119
119
  ### Per-Request Options
120
120
 
121
- | Option | Type | Description |
122
- | ------------ | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
121
+ | Option | Type | Description |
122
+ | ------------ | --------------------------------------- | ------------------------------------------------------------------------------------------------------- |
123
123
  | `invalidate` | `"all" \| "self" \| "none" \| string[]` | Mode only (string), tags only (array), or mode + tags (array with 'all'/'self' keyword at any position) |
124
124
 
125
125
  ### Invalidation Modes
@@ -151,6 +151,6 @@ socket.on("data-changed", (tags) => {
151
151
  });
152
152
  ```
153
153
 
154
- | Method | Description |
155
- | ------------ | ------------------------------------------- |
156
- | `invalidate` | Manually invalidate cache entries by tags |
154
+ | Method | Description |
155
+ | ------------ | ----------------------------------------- |
156
+ | `invalidate` | Manually invalidate cache entries by tags |
package/dist/index.js CHANGED
@@ -25,27 +25,39 @@ __export(src_exports, {
25
25
  module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/plugin.ts
28
+ var import_core = require("@spoosh/core");
28
29
  var INVALIDATION_DEFAULT_KEY = "invalidation:defaultMode";
29
- function resolveModeTags(context, mode) {
30
+ function resolveModeTags(context, mode, stripTagPrefix) {
30
31
  switch (mode) {
31
32
  case "all":
33
+ if (stripTagPrefix) {
34
+ return context.tags.map((tag) => {
35
+ const segments = tag.split("/").filter(Boolean);
36
+ return (0, import_core.stripPrefixFromPath)(segments, stripTagPrefix).join("/");
37
+ });
38
+ }
32
39
  return context.tags;
33
- case "self":
34
- return [context.path.join("/")];
40
+ case "self": {
41
+ const selfPath = context.path.join("/");
42
+ if (stripTagPrefix) {
43
+ return [(0, import_core.stripPrefixFromPath)(context.path, stripTagPrefix).join("/")];
44
+ }
45
+ return [selfPath];
46
+ }
35
47
  case "none":
36
48
  return [];
37
49
  }
38
50
  }
39
- function resolveInvalidateTags(context, defaultMode) {
51
+ function resolveInvalidateTags(context, defaultMode, stripTagPrefix) {
40
52
  const pluginOptions = context.pluginOptions;
41
53
  const invalidateOption = pluginOptions?.invalidate;
42
54
  if (!invalidateOption) {
43
55
  const overrideDefault = context.metadata.get(INVALIDATION_DEFAULT_KEY);
44
56
  const effectiveDefault = overrideDefault ?? defaultMode;
45
- return resolveModeTags(context, effectiveDefault);
57
+ return resolveModeTags(context, effectiveDefault, stripTagPrefix);
46
58
  }
47
59
  if (typeof invalidateOption === "string") {
48
- return resolveModeTags(context, invalidateOption);
60
+ return resolveModeTags(context, invalidateOption, stripTagPrefix);
49
61
  }
50
62
  if (Array.isArray(invalidateOption)) {
51
63
  const tags = [];
@@ -54,10 +66,15 @@ function resolveInvalidateTags(context, defaultMode) {
54
66
  if (item === "all" || item === "self") {
55
67
  mode = item;
56
68
  } else if (typeof item === "string") {
57
- tags.push(item);
69
+ if (stripTagPrefix) {
70
+ const segments = item.split("/").filter(Boolean);
71
+ tags.push((0, import_core.stripPrefixFromPath)(segments, stripTagPrefix).join("/"));
72
+ } else {
73
+ tags.push(item);
74
+ }
58
75
  }
59
76
  }
60
- tags.push(...resolveModeTags(context, mode));
77
+ tags.push(...resolveModeTags(context, mode, stripTagPrefix));
61
78
  return [...new Set(tags)];
62
79
  }
63
80
  return [];
@@ -74,9 +91,13 @@ function invalidationPlugin(config = {}) {
74
91
  }
75
92
  };
76
93
  },
77
- onResponse(context, response) {
94
+ afterResponse(context, response) {
78
95
  if (!response.error) {
79
- const tags = resolveInvalidateTags(context, defaultMode);
96
+ const tags = resolveInvalidateTags(
97
+ context,
98
+ defaultMode,
99
+ context.stripTagPrefix
100
+ );
80
101
  if (tags.length > 0) {
81
102
  context.stateManager.markStale(tags);
82
103
  context.eventEmitter.emit("invalidate", tags);
package/dist/index.mjs CHANGED
@@ -1,25 +1,39 @@
1
1
  // src/plugin.ts
2
+ import {
3
+ stripPrefixFromPath
4
+ } from "@spoosh/core";
2
5
  var INVALIDATION_DEFAULT_KEY = "invalidation:defaultMode";
3
- function resolveModeTags(context, mode) {
6
+ function resolveModeTags(context, mode, stripTagPrefix) {
4
7
  switch (mode) {
5
8
  case "all":
9
+ if (stripTagPrefix) {
10
+ return context.tags.map((tag) => {
11
+ const segments = tag.split("/").filter(Boolean);
12
+ return stripPrefixFromPath(segments, stripTagPrefix).join("/");
13
+ });
14
+ }
6
15
  return context.tags;
7
- case "self":
8
- return [context.path.join("/")];
16
+ case "self": {
17
+ const selfPath = context.path.join("/");
18
+ if (stripTagPrefix) {
19
+ return [stripPrefixFromPath(context.path, stripTagPrefix).join("/")];
20
+ }
21
+ return [selfPath];
22
+ }
9
23
  case "none":
10
24
  return [];
11
25
  }
12
26
  }
13
- function resolveInvalidateTags(context, defaultMode) {
27
+ function resolveInvalidateTags(context, defaultMode, stripTagPrefix) {
14
28
  const pluginOptions = context.pluginOptions;
15
29
  const invalidateOption = pluginOptions?.invalidate;
16
30
  if (!invalidateOption) {
17
31
  const overrideDefault = context.metadata.get(INVALIDATION_DEFAULT_KEY);
18
32
  const effectiveDefault = overrideDefault ?? defaultMode;
19
- return resolveModeTags(context, effectiveDefault);
33
+ return resolveModeTags(context, effectiveDefault, stripTagPrefix);
20
34
  }
21
35
  if (typeof invalidateOption === "string") {
22
- return resolveModeTags(context, invalidateOption);
36
+ return resolveModeTags(context, invalidateOption, stripTagPrefix);
23
37
  }
24
38
  if (Array.isArray(invalidateOption)) {
25
39
  const tags = [];
@@ -28,10 +42,15 @@ function resolveInvalidateTags(context, defaultMode) {
28
42
  if (item === "all" || item === "self") {
29
43
  mode = item;
30
44
  } else if (typeof item === "string") {
31
- tags.push(item);
45
+ if (stripTagPrefix) {
46
+ const segments = item.split("/").filter(Boolean);
47
+ tags.push(stripPrefixFromPath(segments, stripTagPrefix).join("/"));
48
+ } else {
49
+ tags.push(item);
50
+ }
32
51
  }
33
52
  }
34
- tags.push(...resolveModeTags(context, mode));
53
+ tags.push(...resolveModeTags(context, mode, stripTagPrefix));
35
54
  return [...new Set(tags)];
36
55
  }
37
56
  return [];
@@ -48,9 +67,13 @@ function invalidationPlugin(config = {}) {
48
67
  }
49
68
  };
50
69
  },
51
- onResponse(context, response) {
70
+ afterResponse(context, response) {
52
71
  if (!response.error) {
53
- const tags = resolveInvalidateTags(context, defaultMode);
72
+ const tags = resolveInvalidateTags(
73
+ context,
74
+ defaultMode,
75
+ context.stripTagPrefix
76
+ );
54
77
  if (tags.length > 0) {
55
78
  context.stateManager.markStale(tags);
56
79
  context.eventEmitter.emit("invalidate", tags);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-invalidation",
3
- "version": "0.4.0",
3
+ "version": "0.5.1",
4
4
  "description": "Cache invalidation plugin for Spoosh - auto-invalidates after mutations",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -33,10 +33,10 @@
33
33
  }
34
34
  },
35
35
  "peerDependencies": {
36
- "@spoosh/core": ">=0.6.0"
36
+ "@spoosh/core": ">=0.9.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@spoosh/core": "0.6.0",
39
+ "@spoosh/core": "0.9.1",
40
40
  "@spoosh/test-utils": "0.1.5"
41
41
  },
42
42
  "scripts": {