@spoosh/plugin-deduplication 0.1.9 → 0.2.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 +2 -2
- package/dist/index.d.mts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +13 -2
- package/dist/index.mjs +13 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -16,11 +16,11 @@ npm install @spoosh/plugin-deduplication
|
|
|
16
16
|
import { Spoosh } from "@spoosh/core";
|
|
17
17
|
import { deduplicationPlugin } from "@spoosh/plugin-deduplication";
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
20
20
|
deduplicationPlugin(),
|
|
21
21
|
]);
|
|
22
22
|
|
|
23
|
-
const
|
|
23
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
24
24
|
deduplicationPlugin({ write: "in-flight" }),
|
|
25
25
|
]);
|
|
26
26
|
|
package/dist/index.d.mts
CHANGED
|
@@ -28,6 +28,9 @@ declare module "@spoosh/core" {
|
|
|
28
28
|
read: DedupeMode;
|
|
29
29
|
write: DedupeMode;
|
|
30
30
|
};
|
|
31
|
+
isDedupeEnabled: (operationType: string, pluginOptions?: {
|
|
32
|
+
dedupe?: DedupeMode;
|
|
33
|
+
}) => boolean;
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
36
|
}
|
|
@@ -46,7 +49,7 @@ declare module "@spoosh/core" {
|
|
|
46
49
|
* ```ts
|
|
47
50
|
* import { Spoosh } from "@spoosh/core";
|
|
48
51
|
*
|
|
49
|
-
* const
|
|
52
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
50
53
|
* .use([
|
|
51
54
|
* // ... other plugins
|
|
52
55
|
* deduplicationPlugin({ read: "in-flight" }),
|
package/dist/index.d.ts
CHANGED
|
@@ -28,6 +28,9 @@ declare module "@spoosh/core" {
|
|
|
28
28
|
read: DedupeMode;
|
|
29
29
|
write: DedupeMode;
|
|
30
30
|
};
|
|
31
|
+
isDedupeEnabled: (operationType: string, pluginOptions?: {
|
|
32
|
+
dedupe?: DedupeMode;
|
|
33
|
+
}) => boolean;
|
|
31
34
|
};
|
|
32
35
|
}
|
|
33
36
|
}
|
|
@@ -46,7 +49,7 @@ declare module "@spoosh/core" {
|
|
|
46
49
|
* ```ts
|
|
47
50
|
* import { Spoosh } from "@spoosh/core";
|
|
48
51
|
*
|
|
49
|
-
* const
|
|
52
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
50
53
|
* .use([
|
|
51
54
|
* // ... other plugins
|
|
52
55
|
* deduplicationPlugin({ read: "in-flight" }),
|
package/dist/index.js
CHANGED
|
@@ -25,15 +25,17 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/plugin.ts
|
|
28
|
+
var PLUGIN_NAME = "spoosh:deduplication";
|
|
28
29
|
function deduplicationPlugin(config) {
|
|
29
30
|
const resolvedConfig = {
|
|
30
31
|
read: config?.read ?? "in-flight",
|
|
31
32
|
write: config?.write ?? false
|
|
32
33
|
};
|
|
33
34
|
return {
|
|
34
|
-
name:
|
|
35
|
+
name: PLUGIN_NAME,
|
|
35
36
|
operations: ["read", "infiniteRead", "write"],
|
|
36
37
|
middleware: async (context, next) => {
|
|
38
|
+
const et = context.eventTracer?.(PLUGIN_NAME);
|
|
37
39
|
const defaultMode = context.operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
|
|
38
40
|
const requestOverride = context.pluginOptions?.dedupe;
|
|
39
41
|
const dedupeMode = requestOverride ?? defaultMode;
|
|
@@ -42,13 +44,22 @@ function deduplicationPlugin(config) {
|
|
|
42
44
|
context.queryKey
|
|
43
45
|
);
|
|
44
46
|
if (existingPromise) {
|
|
47
|
+
et?.emit("Deduplicated (in-flight)", {
|
|
48
|
+
color: "success",
|
|
49
|
+
queryKey: context.queryKey
|
|
50
|
+
});
|
|
45
51
|
return existingPromise;
|
|
46
52
|
}
|
|
47
53
|
}
|
|
48
54
|
return next();
|
|
49
55
|
},
|
|
50
56
|
exports: () => ({
|
|
51
|
-
getConfig: () => resolvedConfig
|
|
57
|
+
getConfig: () => resolvedConfig,
|
|
58
|
+
isDedupeEnabled: (operationType, pluginOptions) => {
|
|
59
|
+
const defaultMode = operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
|
|
60
|
+
const dedupeMode = pluginOptions?.dedupe ?? defaultMode;
|
|
61
|
+
return dedupeMode === "in-flight";
|
|
62
|
+
}
|
|
52
63
|
})
|
|
53
64
|
};
|
|
54
65
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// src/plugin.ts
|
|
2
|
+
var PLUGIN_NAME = "spoosh:deduplication";
|
|
2
3
|
function deduplicationPlugin(config) {
|
|
3
4
|
const resolvedConfig = {
|
|
4
5
|
read: config?.read ?? "in-flight",
|
|
5
6
|
write: config?.write ?? false
|
|
6
7
|
};
|
|
7
8
|
return {
|
|
8
|
-
name:
|
|
9
|
+
name: PLUGIN_NAME,
|
|
9
10
|
operations: ["read", "infiniteRead", "write"],
|
|
10
11
|
middleware: async (context, next) => {
|
|
12
|
+
const et = context.eventTracer?.(PLUGIN_NAME);
|
|
11
13
|
const defaultMode = context.operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
|
|
12
14
|
const requestOverride = context.pluginOptions?.dedupe;
|
|
13
15
|
const dedupeMode = requestOverride ?? defaultMode;
|
|
@@ -16,13 +18,22 @@ function deduplicationPlugin(config) {
|
|
|
16
18
|
context.queryKey
|
|
17
19
|
);
|
|
18
20
|
if (existingPromise) {
|
|
21
|
+
et?.emit("Deduplicated (in-flight)", {
|
|
22
|
+
color: "success",
|
|
23
|
+
queryKey: context.queryKey
|
|
24
|
+
});
|
|
19
25
|
return existingPromise;
|
|
20
26
|
}
|
|
21
27
|
}
|
|
22
28
|
return next();
|
|
23
29
|
},
|
|
24
30
|
exports: () => ({
|
|
25
|
-
getConfig: () => resolvedConfig
|
|
31
|
+
getConfig: () => resolvedConfig,
|
|
32
|
+
isDedupeEnabled: (operationType, pluginOptions) => {
|
|
33
|
+
const defaultMode = operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
|
|
34
|
+
const dedupeMode = pluginOptions?.dedupe ?? defaultMode;
|
|
35
|
+
return dedupeMode === "in-flight";
|
|
36
|
+
}
|
|
26
37
|
})
|
|
27
38
|
};
|
|
28
39
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-deduplication",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Request deduplication plugin for Spoosh - prevents duplicate in-flight requests",
|
|
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.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.
|
|
39
|
+
"@spoosh/core": "0.13.0",
|
|
40
|
+
"@spoosh/test-utils": "0.2.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|