@spoosh/plugin-deduplication 0.1.0-beta.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Spoosh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @spoosh/plugin-deduplication
2
+
3
+ Request deduplication plugin for Spoosh - prevents duplicate in-flight requests.
4
+
5
+ **[Documentation](https://spoosh.dev/docs/plugins/deduplication)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @spoosh/plugin-deduplication
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { deduplicationPlugin } from "@spoosh/plugin-deduplication";
17
+
18
+ // Default: dedupe reads, not writes
19
+ const plugins = [deduplicationPlugin()];
20
+
21
+ // Enable deduplication for writes too
22
+ // Extreme caution: may cause unintended side effects
23
+ // **Avoid using it unless you fully understand the implications**
24
+ const plugins = [deduplicationPlugin({ write: "in-flight" })];
25
+
26
+ // Per-request override
27
+ useRead((api) => api.posts.$get(), { dedupe: false });
28
+ ```
29
+
30
+ ## Options
31
+
32
+ ### Plugin Config
33
+
34
+ | Option | Type | Default | Description |
35
+ | ------- | ---------------------- | ------------- | ----------------------------- |
36
+ | `read` | `"in-flight" \| false` | `"in-flight"` | Deduplication mode for reads |
37
+ | `write` | `"in-flight" \| false` | `false` | Deduplication mode for writes |
38
+
39
+ ### Per-Request Options
40
+
41
+ | Option | Type | Description |
42
+ | -------- | ---------------------- | --------------------------------------- |
43
+ | `dedupe` | `"in-flight" \| false` | Override deduplication for this request |
44
+
45
+ ### Modes
46
+
47
+ - `"in-flight"` - Reuse existing in-flight request promise if one exists
48
+ - `false` - Always make a new request
@@ -0,0 +1,63 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type DedupeMode = "in-flight" | false;
4
+ type DeduplicationConfig = {
5
+ /** Deduplication mode for read operations. @default "in-flight" */
6
+ read?: DedupeMode;
7
+ /** Deduplication mode for write operations. @default false */
8
+ write?: DedupeMode;
9
+ };
10
+ type DeduplicationReadOptions = {
11
+ /** Override deduplication for this request. */
12
+ dedupe?: DedupeMode;
13
+ };
14
+ type DeduplicationWriteOptions = {
15
+ /** Override deduplication for this request. */
16
+ dedupe?: DedupeMode;
17
+ };
18
+ type DeduplicationInfiniteReadOptions = {
19
+ /** Override deduplication for this request. */
20
+ dedupe?: DedupeMode;
21
+ };
22
+ type DeduplicationReadResult = object;
23
+ type DeduplicationWriteResult = object;
24
+ declare module "@spoosh/core" {
25
+ interface PluginExportsRegistry {
26
+ "spoosh:deduplication": {
27
+ getConfig: () => {
28
+ read: DedupeMode;
29
+ write: DedupeMode;
30
+ };
31
+ };
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Prevents duplicate in-flight requests for the same query.
37
+ *
38
+ * When multiple components request the same data simultaneously,
39
+ * only one network request is made and all callers share the result.
40
+ *
41
+ * @param config - Plugin configuration
42
+ *
43
+ * @see {@link https://spoosh.dev/docs/plugins/deduplication | Deduplication Plugin Documentation}
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const plugins = [deduplicationPlugin({ read: "in-flight" })];
48
+ *
49
+ * // Per-query override
50
+ * useRead((api) => api.posts.$get(), {
51
+ * dedupe: false, // Disable deduplication for this query
52
+ * });
53
+ * ```
54
+ */
55
+ declare function deduplicationPlugin(config?: DeduplicationConfig): SpooshPlugin<{
56
+ readOptions: DeduplicationReadOptions;
57
+ writeOptions: DeduplicationWriteOptions;
58
+ infiniteReadOptions: DeduplicationInfiniteReadOptions;
59
+ readResult: DeduplicationReadResult;
60
+ writeResult: DeduplicationWriteResult;
61
+ }>;
62
+
63
+ export { type DedupeMode, type DeduplicationConfig, type DeduplicationInfiniteReadOptions, type DeduplicationReadOptions, type DeduplicationReadResult, type DeduplicationWriteOptions, type DeduplicationWriteResult, deduplicationPlugin };
@@ -0,0 +1,63 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ type DedupeMode = "in-flight" | false;
4
+ type DeduplicationConfig = {
5
+ /** Deduplication mode for read operations. @default "in-flight" */
6
+ read?: DedupeMode;
7
+ /** Deduplication mode for write operations. @default false */
8
+ write?: DedupeMode;
9
+ };
10
+ type DeduplicationReadOptions = {
11
+ /** Override deduplication for this request. */
12
+ dedupe?: DedupeMode;
13
+ };
14
+ type DeduplicationWriteOptions = {
15
+ /** Override deduplication for this request. */
16
+ dedupe?: DedupeMode;
17
+ };
18
+ type DeduplicationInfiniteReadOptions = {
19
+ /** Override deduplication for this request. */
20
+ dedupe?: DedupeMode;
21
+ };
22
+ type DeduplicationReadResult = object;
23
+ type DeduplicationWriteResult = object;
24
+ declare module "@spoosh/core" {
25
+ interface PluginExportsRegistry {
26
+ "spoosh:deduplication": {
27
+ getConfig: () => {
28
+ read: DedupeMode;
29
+ write: DedupeMode;
30
+ };
31
+ };
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Prevents duplicate in-flight requests for the same query.
37
+ *
38
+ * When multiple components request the same data simultaneously,
39
+ * only one network request is made and all callers share the result.
40
+ *
41
+ * @param config - Plugin configuration
42
+ *
43
+ * @see {@link https://spoosh.dev/docs/plugins/deduplication | Deduplication Plugin Documentation}
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const plugins = [deduplicationPlugin({ read: "in-flight" })];
48
+ *
49
+ * // Per-query override
50
+ * useRead((api) => api.posts.$get(), {
51
+ * dedupe: false, // Disable deduplication for this query
52
+ * });
53
+ * ```
54
+ */
55
+ declare function deduplicationPlugin(config?: DeduplicationConfig): SpooshPlugin<{
56
+ readOptions: DeduplicationReadOptions;
57
+ writeOptions: DeduplicationWriteOptions;
58
+ infiniteReadOptions: DeduplicationInfiniteReadOptions;
59
+ readResult: DeduplicationReadResult;
60
+ writeResult: DeduplicationWriteResult;
61
+ }>;
62
+
63
+ export { type DedupeMode, type DeduplicationConfig, type DeduplicationInfiniteReadOptions, type DeduplicationReadOptions, type DeduplicationReadResult, type DeduplicationWriteOptions, type DeduplicationWriteResult, deduplicationPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ deduplicationPlugin: () => deduplicationPlugin
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/plugin.ts
28
+ function deduplicationPlugin(config) {
29
+ const resolvedConfig = {
30
+ read: config?.read ?? "in-flight",
31
+ write: config?.write ?? false
32
+ };
33
+ return {
34
+ name: "spoosh:deduplication",
35
+ operations: ["read", "infiniteRead", "write"],
36
+ middleware: async (context, next) => {
37
+ const defaultMode = context.operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
38
+ const requestOverride = context.pluginOptions?.dedupe;
39
+ const dedupeMode = requestOverride ?? defaultMode;
40
+ if (dedupeMode === "in-flight") {
41
+ const existingPromise = context.stateManager.getPendingPromise(
42
+ context.queryKey
43
+ );
44
+ if (existingPromise) {
45
+ return existingPromise;
46
+ }
47
+ }
48
+ return next();
49
+ },
50
+ exports: () => ({
51
+ getConfig: () => resolvedConfig
52
+ })
53
+ };
54
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,31 @@
1
+ // src/plugin.ts
2
+ function deduplicationPlugin(config) {
3
+ const resolvedConfig = {
4
+ read: config?.read ?? "in-flight",
5
+ write: config?.write ?? false
6
+ };
7
+ return {
8
+ name: "spoosh:deduplication",
9
+ operations: ["read", "infiniteRead", "write"],
10
+ middleware: async (context, next) => {
11
+ const defaultMode = context.operationType === "write" ? resolvedConfig.write : resolvedConfig.read;
12
+ const requestOverride = context.pluginOptions?.dedupe;
13
+ const dedupeMode = requestOverride ?? defaultMode;
14
+ if (dedupeMode === "in-flight") {
15
+ const existingPromise = context.stateManager.getPendingPromise(
16
+ context.queryKey
17
+ );
18
+ if (existingPromise) {
19
+ return existingPromise;
20
+ }
21
+ }
22
+ return next();
23
+ },
24
+ exports: () => ({
25
+ getConfig: () => resolvedConfig
26
+ })
27
+ };
28
+ }
29
+ export {
30
+ deduplicationPlugin
31
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@spoosh/plugin-deduplication",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Request deduplication plugin for Spoosh - prevents duplicate in-flight requests",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/plugin-deduplication"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev/docs/plugins/deduplication",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "spoosh",
20
+ "plugin",
21
+ "deduplication",
22
+ "api-client",
23
+ "dedupe"
24
+ ],
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.mjs",
32
+ "require": "./dist/index.js"
33
+ }
34
+ },
35
+ "peerDependencies": {
36
+ "@spoosh/core": ">=0.1.0"
37
+ },
38
+ "devDependencies": {
39
+ "@spoosh/core": "0.1.0-beta.0",
40
+ "@spoosh/test-utils": "0.1.0-beta.0"
41
+ },
42
+ "scripts": {
43
+ "dev": "tsup --watch",
44
+ "build": "tsup",
45
+ "typecheck": "tsc --noEmit",
46
+ "lint": "eslint src --max-warnings 0",
47
+ "format": "prettier --write 'src/**/*.ts'"
48
+ }
49
+ }