@spoosh/plugin-optimistic 0.8.0 → 0.8.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/dist/index.js CHANGED
@@ -32,54 +32,28 @@ var import_plugin_invalidation = require("@spoosh/plugin-invalidation");
32
32
  function isParameterSegment(segment) {
33
33
  return segment.startsWith(":");
34
34
  }
35
- function hasPatternParams(path) {
36
- return path.split("/").some(isParameterSegment);
37
- }
38
- function pathMatchesPattern(actualPath, pattern) {
39
- const actualSegments = actualPath.split("/").filter(Boolean);
35
+ function pathMatchesPattern(resolvedPath, pattern) {
36
+ const resolvedSegments = resolvedPath.split("/").filter(Boolean);
40
37
  const patternSegments = pattern.split("/").filter(Boolean);
41
- if (actualSegments.length !== patternSegments.length) {
42
- return { matches: false, params: {}, paramMapping: {} };
38
+ if (resolvedSegments.length !== patternSegments.length) {
39
+ return { matches: false, params: {} };
43
40
  }
44
41
  const params = {};
45
- const paramMapping = {};
46
42
  for (let i = 0; i < patternSegments.length; i++) {
47
43
  const patternSeg = patternSegments[i];
48
- const actualSeg = actualSegments[i];
44
+ const resolvedSeg = resolvedSegments[i];
49
45
  if (isParameterSegment(patternSeg)) {
50
- const targetParamName = patternSeg.slice(1);
51
- if (isParameterSegment(actualSeg)) {
52
- const actualParamName = actualSeg.slice(1);
53
- paramMapping[targetParamName] = actualParamName;
54
- continue;
55
- }
56
- params[targetParamName] = actualSeg;
57
- } else if (isParameterSegment(actualSeg)) {
58
- continue;
59
- } else if (patternSeg !== actualSeg) {
60
- return { matches: false, params: {}, paramMapping: {} };
46
+ const paramName = patternSeg.slice(1);
47
+ params[paramName] = resolvedSeg;
48
+ } else if (patternSeg !== resolvedSeg) {
49
+ return { matches: false, params: {} };
61
50
  }
62
51
  }
63
- return { matches: true, params, paramMapping };
52
+ return { matches: true, params };
64
53
  }
65
54
 
66
55
  // src/utils/cache-key.ts
67
56
  var import_core = require("@spoosh/core");
68
- function extractPathFromKey(key) {
69
- try {
70
- const parsed = JSON.parse(key);
71
- const path = parsed.path;
72
- if (typeof path === "string") {
73
- return path;
74
- }
75
- if (Array.isArray(path)) {
76
- return path.join("/");
77
- }
78
- return null;
79
- } catch {
80
- return null;
81
- }
82
- }
83
57
  function formatCacheKeyForTrace(key) {
84
58
  const resolvedPath = (0, import_core.generateSelfTagFromKey)(key);
85
59
  if (!resolvedPath) return "unknown";
@@ -122,21 +96,9 @@ function extractOptionsFromKey(key) {
122
96
  return null;
123
97
  }
124
98
  }
125
- function mapParamsToTargetNames(actualParams, paramMapping) {
126
- if (!actualParams) return {};
127
- const result = {};
128
- for (const [targetName, actualName] of Object.entries(paramMapping)) {
129
- if (actualName in actualParams) {
130
- result[targetName] = String(actualParams[actualName]);
131
- }
132
- }
133
- for (const [key, value] of Object.entries(actualParams)) {
134
- if (!Object.values(paramMapping).includes(key)) {
135
- result[key] = String(value);
136
- }
137
- }
138
- return result;
139
- }
99
+
100
+ // src/plugin.ts
101
+ var import_core3 = require("@spoosh/core");
140
102
 
141
103
  // src/builder/index.ts
142
104
  function createBuilder(state, isConfirmed = false) {
@@ -193,27 +155,13 @@ function resolveOptimisticTargets(context) {
193
155
  function getMatchingEntries(stateManager, targetPath) {
194
156
  const results = [];
195
157
  const allEntries = stateManager.getAllCacheEntries();
196
- if (hasPatternParams(targetPath)) {
197
- for (const { key, entry } of allEntries) {
198
- if (!key.includes(`"method":"GET"`)) continue;
199
- const actualPath = extractPathFromKey(key);
200
- if (!actualPath) continue;
201
- const { matches, params, paramMapping } = pathMatchesPattern(
202
- actualPath,
203
- targetPath
204
- );
205
- if (matches) {
206
- results.push({ key, entry, extractedParams: params, paramMapping });
207
- }
208
- }
209
- } else {
210
- for (const { key, entry } of allEntries) {
211
- if (!key.includes(`"method":"GET"`)) continue;
212
- const actualPath = extractPathFromKey(key);
213
- if (!actualPath) continue;
214
- if (actualPath === targetPath) {
215
- results.push({ key, entry, extractedParams: {}, paramMapping: {} });
216
- }
158
+ for (const { key, entry } of allEntries) {
159
+ if (!key.includes(`"method":"GET"`)) continue;
160
+ const resolvedPath = (0, import_core3.generateSelfTagFromKey)(key);
161
+ if (!resolvedPath) continue;
162
+ const { matches, params } = pathMatchesPattern(resolvedPath, targetPath);
163
+ if (matches) {
164
+ results.push({ key, entry, extractedParams: params });
217
165
  }
218
166
  }
219
167
  return results;
@@ -225,18 +173,14 @@ function applyUpdate(stateManager, target, updater, response, t) {
225
173
  t?.skip(`Skipped ${target.path} (no cache entry)`);
226
174
  return [];
227
175
  }
228
- for (const { key, entry, extractedParams, paramMapping } of matchingEntries) {
176
+ for (const { key, entry, extractedParams } of matchingEntries) {
229
177
  if (target.filter) {
230
178
  const options = extractOptionsFromKey(key) ?? {};
231
- const mappedParams = mapParamsToTargetNames(
232
- options.params,
233
- paramMapping
234
- );
235
179
  const mergedOptions = {
236
180
  ...options,
237
181
  params: {
238
- ...extractedParams,
239
- ...mappedParams
182
+ ...options.params,
183
+ ...extractedParams
240
184
  }
241
185
  };
242
186
  try {
package/dist/index.mjs CHANGED
@@ -8,54 +8,28 @@ import "@spoosh/plugin-invalidation";
8
8
  function isParameterSegment(segment) {
9
9
  return segment.startsWith(":");
10
10
  }
11
- function hasPatternParams(path) {
12
- return path.split("/").some(isParameterSegment);
13
- }
14
- function pathMatchesPattern(actualPath, pattern) {
15
- const actualSegments = actualPath.split("/").filter(Boolean);
11
+ function pathMatchesPattern(resolvedPath, pattern) {
12
+ const resolvedSegments = resolvedPath.split("/").filter(Boolean);
16
13
  const patternSegments = pattern.split("/").filter(Boolean);
17
- if (actualSegments.length !== patternSegments.length) {
18
- return { matches: false, params: {}, paramMapping: {} };
14
+ if (resolvedSegments.length !== patternSegments.length) {
15
+ return { matches: false, params: {} };
19
16
  }
20
17
  const params = {};
21
- const paramMapping = {};
22
18
  for (let i = 0; i < patternSegments.length; i++) {
23
19
  const patternSeg = patternSegments[i];
24
- const actualSeg = actualSegments[i];
20
+ const resolvedSeg = resolvedSegments[i];
25
21
  if (isParameterSegment(patternSeg)) {
26
- const targetParamName = patternSeg.slice(1);
27
- if (isParameterSegment(actualSeg)) {
28
- const actualParamName = actualSeg.slice(1);
29
- paramMapping[targetParamName] = actualParamName;
30
- continue;
31
- }
32
- params[targetParamName] = actualSeg;
33
- } else if (isParameterSegment(actualSeg)) {
34
- continue;
35
- } else if (patternSeg !== actualSeg) {
36
- return { matches: false, params: {}, paramMapping: {} };
22
+ const paramName = patternSeg.slice(1);
23
+ params[paramName] = resolvedSeg;
24
+ } else if (patternSeg !== resolvedSeg) {
25
+ return { matches: false, params: {} };
37
26
  }
38
27
  }
39
- return { matches: true, params, paramMapping };
28
+ return { matches: true, params };
40
29
  }
41
30
 
42
31
  // src/utils/cache-key.ts
43
32
  import { generateSelfTagFromKey } from "@spoosh/core";
44
- function extractPathFromKey(key) {
45
- try {
46
- const parsed = JSON.parse(key);
47
- const path = parsed.path;
48
- if (typeof path === "string") {
49
- return path;
50
- }
51
- if (Array.isArray(path)) {
52
- return path.join("/");
53
- }
54
- return null;
55
- } catch {
56
- return null;
57
- }
58
- }
59
33
  function formatCacheKeyForTrace(key) {
60
34
  const resolvedPath = generateSelfTagFromKey(key);
61
35
  if (!resolvedPath) return "unknown";
@@ -98,21 +72,9 @@ function extractOptionsFromKey(key) {
98
72
  return null;
99
73
  }
100
74
  }
101
- function mapParamsToTargetNames(actualParams, paramMapping) {
102
- if (!actualParams) return {};
103
- const result = {};
104
- for (const [targetName, actualName] of Object.entries(paramMapping)) {
105
- if (actualName in actualParams) {
106
- result[targetName] = String(actualParams[actualName]);
107
- }
108
- }
109
- for (const [key, value] of Object.entries(actualParams)) {
110
- if (!Object.values(paramMapping).includes(key)) {
111
- result[key] = String(value);
112
- }
113
- }
114
- return result;
115
- }
75
+
76
+ // src/plugin.ts
77
+ import { generateSelfTagFromKey as generateSelfTagFromKey2 } from "@spoosh/core";
116
78
 
117
79
  // src/builder/index.ts
118
80
  function createBuilder(state, isConfirmed = false) {
@@ -169,27 +131,13 @@ function resolveOptimisticTargets(context) {
169
131
  function getMatchingEntries(stateManager, targetPath) {
170
132
  const results = [];
171
133
  const allEntries = stateManager.getAllCacheEntries();
172
- if (hasPatternParams(targetPath)) {
173
- for (const { key, entry } of allEntries) {
174
- if (!key.includes(`"method":"GET"`)) continue;
175
- const actualPath = extractPathFromKey(key);
176
- if (!actualPath) continue;
177
- const { matches, params, paramMapping } = pathMatchesPattern(
178
- actualPath,
179
- targetPath
180
- );
181
- if (matches) {
182
- results.push({ key, entry, extractedParams: params, paramMapping });
183
- }
184
- }
185
- } else {
186
- for (const { key, entry } of allEntries) {
187
- if (!key.includes(`"method":"GET"`)) continue;
188
- const actualPath = extractPathFromKey(key);
189
- if (!actualPath) continue;
190
- if (actualPath === targetPath) {
191
- results.push({ key, entry, extractedParams: {}, paramMapping: {} });
192
- }
134
+ for (const { key, entry } of allEntries) {
135
+ if (!key.includes(`"method":"GET"`)) continue;
136
+ const resolvedPath = generateSelfTagFromKey2(key);
137
+ if (!resolvedPath) continue;
138
+ const { matches, params } = pathMatchesPattern(resolvedPath, targetPath);
139
+ if (matches) {
140
+ results.push({ key, entry, extractedParams: params });
193
141
  }
194
142
  }
195
143
  return results;
@@ -201,18 +149,14 @@ function applyUpdate(stateManager, target, updater, response, t) {
201
149
  t?.skip(`Skipped ${target.path} (no cache entry)`);
202
150
  return [];
203
151
  }
204
- for (const { key, entry, extractedParams, paramMapping } of matchingEntries) {
152
+ for (const { key, entry, extractedParams } of matchingEntries) {
205
153
  if (target.filter) {
206
154
  const options = extractOptionsFromKey(key) ?? {};
207
- const mappedParams = mapParamsToTargetNames(
208
- options.params,
209
- paramMapping
210
- );
211
155
  const mergedOptions = {
212
156
  ...options,
213
157
  params: {
214
- ...extractedParams,
215
- ...mappedParams
158
+ ...options.params,
159
+ ...extractedParams
216
160
  }
217
161
  };
218
162
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spoosh/plugin-optimistic",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Optimistic updates plugin for Spoosh - instant UI updates with automatic rollback",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "@spoosh/plugin-invalidation": ">=0.7.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@spoosh/core": "0.17.0",
40
+ "@spoosh/core": "0.17.1",
41
41
  "@spoosh/plugin-invalidation": "0.10.0",
42
42
  "@spoosh/test-utils": "0.3.0"
43
43
  },