@spoosh/plugin-retry 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,43 @@
1
+ # @spoosh/plugin-retry
2
+
3
+ Automatic retry plugin for Spoosh with configurable attempts and delay.
4
+
5
+ **[Documentation](https://spoosh.dev/docs/plugins/retry)** · **Requirements:** TypeScript >= 5.0 · **Peer Dependencies:** `@spoosh/core`
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @spoosh/plugin-retry
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { retryPlugin } from "@spoosh/plugin-retry";
17
+
18
+ const plugins = [
19
+ retryPlugin({ retries: 3, retryDelay: 1000 }), // 3 retries with 1 second delay
20
+ ];
21
+
22
+ // Per-query override
23
+ useRead((api) => api.posts.$get(), { retries: 5, retryDelay: 2000 });
24
+
25
+ // Disable retries for a specific request
26
+ useRead((api) => api.posts.$get(), { retries: false });
27
+ ```
28
+
29
+ ## Options
30
+
31
+ ### Plugin Config
32
+
33
+ | Option | Type | Default | Description |
34
+ | ------------ | ----------------- | ------- | ------------------------------------------------------------ |
35
+ | `retries` | `number \| false` | `3` | Number of retry attempts. Set to `false` to disable retries. |
36
+ | `retryDelay` | `number` | `1000` | Delay between retries in milliseconds |
37
+
38
+ ### Per-Request Options
39
+
40
+ | Option | Type | Description |
41
+ | ------------ | ----------------- | ---------------------------------------- |
42
+ | `retries` | `number \| false` | Override retry attempts for this request |
43
+ | `retryDelay` | `number` | Override retry delay for this request |
@@ -0,0 +1,53 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ interface RetryPluginConfig {
4
+ /** Number of retry attempts. Set to `false` to disable retries. Defaults to 3. */
5
+ retries?: number | false;
6
+ /** Delay between retries in milliseconds. Defaults to 1000. */
7
+ retryDelay?: number;
8
+ }
9
+ interface RetryReadOptions {
10
+ /** Number of retry attempts. Set to `false` to disable retries. Overrides plugin default. */
11
+ retries?: number | false;
12
+ /** Delay between retries in milliseconds. Overrides plugin default. */
13
+ retryDelay?: number;
14
+ }
15
+ interface RetryWriteOptions {
16
+ /** Number of retry attempts. Set to `false` to disable retries. Overrides plugin default. */
17
+ retries?: number | false;
18
+ /** Delay between retries in milliseconds. Overrides plugin default. */
19
+ retryDelay?: number;
20
+ }
21
+ type RetryInfiniteReadOptions = RetryReadOptions;
22
+ type RetryReadResult = object;
23
+ type RetryWriteResult = object;
24
+
25
+ /**
26
+ * Enables automatic retry for failed requests.
27
+ *
28
+ * Retries failed requests with configurable attempt count and delay.
29
+ *
30
+ * @param config - Plugin configuration
31
+ *
32
+ * @see {@link https://spoosh.dev/docs/plugins/retry | Retry Plugin Documentation}
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const plugins = [retryPlugin({ retries: 3, retryDelay: 1000 })];
37
+ *
38
+ * // Per-query override
39
+ * useRead((api) => api.posts.$get(), {
40
+ * retries: 5,
41
+ * retryDelay: 2000,
42
+ * });
43
+ * ```
44
+ */
45
+ declare function retryPlugin(config?: RetryPluginConfig): SpooshPlugin<{
46
+ readOptions: RetryReadOptions;
47
+ writeOptions: RetryWriteOptions;
48
+ infiniteReadOptions: RetryInfiniteReadOptions;
49
+ readResult: RetryReadResult;
50
+ writeResult: RetryWriteResult;
51
+ }>;
52
+
53
+ export { type RetryInfiniteReadOptions, type RetryPluginConfig, type RetryReadOptions, type RetryReadResult, type RetryWriteOptions, type RetryWriteResult, retryPlugin };
@@ -0,0 +1,53 @@
1
+ import { SpooshPlugin } from '@spoosh/core';
2
+
3
+ interface RetryPluginConfig {
4
+ /** Number of retry attempts. Set to `false` to disable retries. Defaults to 3. */
5
+ retries?: number | false;
6
+ /** Delay between retries in milliseconds. Defaults to 1000. */
7
+ retryDelay?: number;
8
+ }
9
+ interface RetryReadOptions {
10
+ /** Number of retry attempts. Set to `false` to disable retries. Overrides plugin default. */
11
+ retries?: number | false;
12
+ /** Delay between retries in milliseconds. Overrides plugin default. */
13
+ retryDelay?: number;
14
+ }
15
+ interface RetryWriteOptions {
16
+ /** Number of retry attempts. Set to `false` to disable retries. Overrides plugin default. */
17
+ retries?: number | false;
18
+ /** Delay between retries in milliseconds. Overrides plugin default. */
19
+ retryDelay?: number;
20
+ }
21
+ type RetryInfiniteReadOptions = RetryReadOptions;
22
+ type RetryReadResult = object;
23
+ type RetryWriteResult = object;
24
+
25
+ /**
26
+ * Enables automatic retry for failed requests.
27
+ *
28
+ * Retries failed requests with configurable attempt count and delay.
29
+ *
30
+ * @param config - Plugin configuration
31
+ *
32
+ * @see {@link https://spoosh.dev/docs/plugins/retry | Retry Plugin Documentation}
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const plugins = [retryPlugin({ retries: 3, retryDelay: 1000 })];
37
+ *
38
+ * // Per-query override
39
+ * useRead((api) => api.posts.$get(), {
40
+ * retries: 5,
41
+ * retryDelay: 2000,
42
+ * });
43
+ * ```
44
+ */
45
+ declare function retryPlugin(config?: RetryPluginConfig): SpooshPlugin<{
46
+ readOptions: RetryReadOptions;
47
+ writeOptions: RetryWriteOptions;
48
+ infiniteReadOptions: RetryInfiniteReadOptions;
49
+ readResult: RetryReadResult;
50
+ writeResult: RetryWriteResult;
51
+ }>;
52
+
53
+ export { type RetryInfiniteReadOptions, type RetryPluginConfig, type RetryReadOptions, type RetryReadResult, type RetryWriteOptions, type RetryWriteResult, retryPlugin };
package/dist/index.js ADDED
@@ -0,0 +1,45 @@
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
+ retryPlugin: () => retryPlugin
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/plugin.ts
28
+ function retryPlugin(config = {}) {
29
+ const { retries: defaultRetries = 3, retryDelay: defaultRetryDelay = 1e3 } = config;
30
+ return {
31
+ name: "spoosh:retry",
32
+ operations: ["read", "write", "infiniteRead"],
33
+ middleware: async (context, next) => {
34
+ const pluginOptions = context.pluginOptions;
35
+ const retries = pluginOptions?.retries ?? defaultRetries;
36
+ const retryDelay = pluginOptions?.retryDelay ?? defaultRetryDelay;
37
+ context.requestOptions = {
38
+ ...context.requestOptions,
39
+ retries,
40
+ retryDelay
41
+ };
42
+ return next();
43
+ }
44
+ };
45
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,22 @@
1
+ // src/plugin.ts
2
+ function retryPlugin(config = {}) {
3
+ const { retries: defaultRetries = 3, retryDelay: defaultRetryDelay = 1e3 } = config;
4
+ return {
5
+ name: "spoosh:retry",
6
+ operations: ["read", "write", "infiniteRead"],
7
+ middleware: async (context, next) => {
8
+ const pluginOptions = context.pluginOptions;
9
+ const retries = pluginOptions?.retries ?? defaultRetries;
10
+ const retryDelay = pluginOptions?.retryDelay ?? defaultRetryDelay;
11
+ context.requestOptions = {
12
+ ...context.requestOptions,
13
+ retries,
14
+ retryDelay
15
+ };
16
+ return next();
17
+ }
18
+ };
19
+ }
20
+ export {
21
+ retryPlugin
22
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@spoosh/plugin-retry",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Automatic retry plugin for Spoosh with configurable attempts and delay",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/nxnom/spoosh.git",
9
+ "directory": "packages/plugin-retry"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/nxnom/spoosh/issues"
13
+ },
14
+ "homepage": "https://spoosh.dev/docs/plugins/retry",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "keywords": [
19
+ "spoosh",
20
+ "plugin",
21
+ "retry",
22
+ "resilience",
23
+ "api-client"
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
+ }