@spoosh/plugin-retry 0.4.0 → 0.6.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 +12 -4
- package/dist/index.d.mts +18 -24
- package/dist/index.d.ts +18 -24
- package/dist/index.js +4 -4
- package/dist/index.mjs +4 -4
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -21,10 +21,14 @@ const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
|
21
21
|
]);
|
|
22
22
|
|
|
23
23
|
// Per-query override
|
|
24
|
-
useRead((api) => api("posts").GET(), {
|
|
24
|
+
useRead((api) => api("posts").GET(), {
|
|
25
|
+
retry: { retries: 5, delay: 2000 },
|
|
26
|
+
});
|
|
25
27
|
|
|
26
28
|
// Disable retries for a specific request
|
|
27
|
-
useRead((api) => api("posts").GET(), {
|
|
29
|
+
useRead((api) => api("posts").GET(), {
|
|
30
|
+
retry: { retries: false },
|
|
31
|
+
});
|
|
28
32
|
```
|
|
29
33
|
|
|
30
34
|
## Retry Behavior
|
|
@@ -49,7 +53,9 @@ retryPlugin({
|
|
|
49
53
|
|
|
50
54
|
// Per-request override
|
|
51
55
|
useRead((api) => api("posts").GET(), {
|
|
52
|
-
|
|
56
|
+
retry: {
|
|
57
|
+
shouldRetry: ({ status }) => status === 429,
|
|
58
|
+
},
|
|
53
59
|
});
|
|
54
60
|
```
|
|
55
61
|
|
|
@@ -67,10 +73,12 @@ useRead((api) => api("posts").GET(), {
|
|
|
67
73
|
|
|
68
74
|
### Per-Request Options
|
|
69
75
|
|
|
76
|
+
Pass options via the `retry` object:
|
|
77
|
+
|
|
70
78
|
| Option | Type | Description |
|
|
71
79
|
| ------------- | --------------------- | ---------------------------------------- |
|
|
72
80
|
| `retries` | `number \| false` | Override retry attempts for this request |
|
|
73
|
-
| `
|
|
81
|
+
| `delay` | `number` | Override retry delay for this request |
|
|
74
82
|
| `shouldRetry` | `ShouldRetryCallback` | Override retry logic for this request |
|
|
75
83
|
|
|
76
84
|
### ShouldRetryContext
|
package/dist/index.d.mts
CHANGED
|
@@ -22,43 +22,38 @@ interface ShouldRetryContext {
|
|
|
22
22
|
type ShouldRetryCallback = (context: ShouldRetryContext) => boolean;
|
|
23
23
|
/** Status codes that are retried by default: 408, 429, 500, 502, 503, 504 */
|
|
24
24
|
declare const DEFAULT_RETRY_STATUS_CODES: readonly [408, 429, 500, 502, 503, 504];
|
|
25
|
-
interface
|
|
26
|
-
/** Number of retry attempts. Set to `false` to disable retries.
|
|
25
|
+
interface RetryConfig {
|
|
26
|
+
/** Number of retry attempts. Set to `false` to disable retries. */
|
|
27
27
|
retries?: number | false;
|
|
28
|
-
/** Delay between retries in milliseconds.
|
|
29
|
-
|
|
28
|
+
/** Delay between retries in milliseconds. */
|
|
29
|
+
delay?: number;
|
|
30
30
|
/**
|
|
31
31
|
* Custom callback to determine if a request should be retried.
|
|
32
32
|
* Network errors are always retried regardless of this callback.
|
|
33
|
-
* Defaults to retrying on status codes: 408, 429, 500, 502, 503, 504.
|
|
34
33
|
*/
|
|
35
34
|
shouldRetry?: ShouldRetryCallback;
|
|
36
35
|
}
|
|
37
|
-
interface
|
|
38
|
-
/** Number of retry attempts. Set to `false` to disable retries.
|
|
36
|
+
interface RetryPluginConfig {
|
|
37
|
+
/** Number of retry attempts. Set to `false` to disable retries. Defaults to 3. */
|
|
39
38
|
retries?: number | false;
|
|
40
|
-
/** Delay between retries in milliseconds.
|
|
39
|
+
/** Delay between retries in milliseconds. Defaults to 1000. */
|
|
41
40
|
retryDelay?: number;
|
|
42
41
|
/**
|
|
43
42
|
* Custom callback to determine if a request should be retried.
|
|
44
43
|
* Network errors are always retried regardless of this callback.
|
|
45
|
-
*
|
|
44
|
+
* Defaults to retrying on status codes: 408, 429, 500, 502, 503, 504.
|
|
46
45
|
*/
|
|
47
46
|
shouldRetry?: ShouldRetryCallback;
|
|
48
47
|
}
|
|
48
|
+
interface RetryReadOptions {
|
|
49
|
+
/** Retry configuration. Overrides plugin defaults. */
|
|
50
|
+
retry?: RetryConfig;
|
|
51
|
+
}
|
|
49
52
|
interface RetryWriteOptions {
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
/** Delay between retries in milliseconds. Overrides plugin default. */
|
|
53
|
-
retryDelay?: number;
|
|
54
|
-
/**
|
|
55
|
-
* Custom callback to determine if a request should be retried.
|
|
56
|
-
* Network errors are always retried regardless of this callback.
|
|
57
|
-
* Overrides plugin default.
|
|
58
|
-
*/
|
|
59
|
-
shouldRetry?: ShouldRetryCallback;
|
|
53
|
+
/** Retry configuration. Overrides plugin defaults. */
|
|
54
|
+
retry?: RetryConfig;
|
|
60
55
|
}
|
|
61
|
-
type
|
|
56
|
+
type RetryPagesOptions = RetryReadOptions;
|
|
62
57
|
type RetryQueueOptions = RetryReadOptions;
|
|
63
58
|
type RetryReadResult = object;
|
|
64
59
|
type RetryWriteResult = object;
|
|
@@ -85,19 +80,18 @@ type RetryQueueResult = object;
|
|
|
85
80
|
*
|
|
86
81
|
* // Per-query override
|
|
87
82
|
* useRead((api) => api("posts").GET(), {
|
|
88
|
-
* retries: 5,
|
|
89
|
-
* retryDelay: 2000,
|
|
83
|
+
* retry: { retries: 5, delay: 2000 },
|
|
90
84
|
* });
|
|
91
85
|
* ```
|
|
92
86
|
*/
|
|
93
87
|
declare function retryPlugin(config?: RetryPluginConfig): SpooshPlugin<{
|
|
94
88
|
readOptions: RetryReadOptions;
|
|
95
89
|
writeOptions: RetryWriteOptions;
|
|
96
|
-
|
|
90
|
+
pagesOptions: RetryPagesOptions;
|
|
97
91
|
queueOptions: RetryQueueOptions;
|
|
98
92
|
readResult: RetryReadResult;
|
|
99
93
|
writeResult: RetryWriteResult;
|
|
100
94
|
queueResult: RetryQueueResult;
|
|
101
95
|
}>;
|
|
102
96
|
|
|
103
|
-
export { DEFAULT_RETRY_STATUS_CODES, type
|
|
97
|
+
export { DEFAULT_RETRY_STATUS_CODES, type RetryConfig, type RetryPagesOptions, type RetryPluginConfig, type RetryQueueOptions, type RetryQueueResult, type RetryReadOptions, type RetryReadResult, type RetryWriteOptions, type RetryWriteResult, type ShouldRetryCallback, type ShouldRetryContext, retryPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -22,43 +22,38 @@ interface ShouldRetryContext {
|
|
|
22
22
|
type ShouldRetryCallback = (context: ShouldRetryContext) => boolean;
|
|
23
23
|
/** Status codes that are retried by default: 408, 429, 500, 502, 503, 504 */
|
|
24
24
|
declare const DEFAULT_RETRY_STATUS_CODES: readonly [408, 429, 500, 502, 503, 504];
|
|
25
|
-
interface
|
|
26
|
-
/** Number of retry attempts. Set to `false` to disable retries.
|
|
25
|
+
interface RetryConfig {
|
|
26
|
+
/** Number of retry attempts. Set to `false` to disable retries. */
|
|
27
27
|
retries?: number | false;
|
|
28
|
-
/** Delay between retries in milliseconds.
|
|
29
|
-
|
|
28
|
+
/** Delay between retries in milliseconds. */
|
|
29
|
+
delay?: number;
|
|
30
30
|
/**
|
|
31
31
|
* Custom callback to determine if a request should be retried.
|
|
32
32
|
* Network errors are always retried regardless of this callback.
|
|
33
|
-
* Defaults to retrying on status codes: 408, 429, 500, 502, 503, 504.
|
|
34
33
|
*/
|
|
35
34
|
shouldRetry?: ShouldRetryCallback;
|
|
36
35
|
}
|
|
37
|
-
interface
|
|
38
|
-
/** Number of retry attempts. Set to `false` to disable retries.
|
|
36
|
+
interface RetryPluginConfig {
|
|
37
|
+
/** Number of retry attempts. Set to `false` to disable retries. Defaults to 3. */
|
|
39
38
|
retries?: number | false;
|
|
40
|
-
/** Delay between retries in milliseconds.
|
|
39
|
+
/** Delay between retries in milliseconds. Defaults to 1000. */
|
|
41
40
|
retryDelay?: number;
|
|
42
41
|
/**
|
|
43
42
|
* Custom callback to determine if a request should be retried.
|
|
44
43
|
* Network errors are always retried regardless of this callback.
|
|
45
|
-
*
|
|
44
|
+
* Defaults to retrying on status codes: 408, 429, 500, 502, 503, 504.
|
|
46
45
|
*/
|
|
47
46
|
shouldRetry?: ShouldRetryCallback;
|
|
48
47
|
}
|
|
48
|
+
interface RetryReadOptions {
|
|
49
|
+
/** Retry configuration. Overrides plugin defaults. */
|
|
50
|
+
retry?: RetryConfig;
|
|
51
|
+
}
|
|
49
52
|
interface RetryWriteOptions {
|
|
50
|
-
/**
|
|
51
|
-
|
|
52
|
-
/** Delay between retries in milliseconds. Overrides plugin default. */
|
|
53
|
-
retryDelay?: number;
|
|
54
|
-
/**
|
|
55
|
-
* Custom callback to determine if a request should be retried.
|
|
56
|
-
* Network errors are always retried regardless of this callback.
|
|
57
|
-
* Overrides plugin default.
|
|
58
|
-
*/
|
|
59
|
-
shouldRetry?: ShouldRetryCallback;
|
|
53
|
+
/** Retry configuration. Overrides plugin defaults. */
|
|
54
|
+
retry?: RetryConfig;
|
|
60
55
|
}
|
|
61
|
-
type
|
|
56
|
+
type RetryPagesOptions = RetryReadOptions;
|
|
62
57
|
type RetryQueueOptions = RetryReadOptions;
|
|
63
58
|
type RetryReadResult = object;
|
|
64
59
|
type RetryWriteResult = object;
|
|
@@ -85,19 +80,18 @@ type RetryQueueResult = object;
|
|
|
85
80
|
*
|
|
86
81
|
* // Per-query override
|
|
87
82
|
* useRead((api) => api("posts").GET(), {
|
|
88
|
-
* retries: 5,
|
|
89
|
-
* retryDelay: 2000,
|
|
83
|
+
* retry: { retries: 5, delay: 2000 },
|
|
90
84
|
* });
|
|
91
85
|
* ```
|
|
92
86
|
*/
|
|
93
87
|
declare function retryPlugin(config?: RetryPluginConfig): SpooshPlugin<{
|
|
94
88
|
readOptions: RetryReadOptions;
|
|
95
89
|
writeOptions: RetryWriteOptions;
|
|
96
|
-
|
|
90
|
+
pagesOptions: RetryPagesOptions;
|
|
97
91
|
queueOptions: RetryQueueOptions;
|
|
98
92
|
readResult: RetryReadResult;
|
|
99
93
|
writeResult: RetryWriteResult;
|
|
100
94
|
queueResult: RetryQueueResult;
|
|
101
95
|
}>;
|
|
102
96
|
|
|
103
|
-
export { DEFAULT_RETRY_STATUS_CODES, type
|
|
97
|
+
export { DEFAULT_RETRY_STATUS_CODES, type RetryConfig, type RetryPagesOptions, type RetryPluginConfig, type RetryQueueOptions, type RetryQueueResult, type RetryReadOptions, type RetryReadResult, type RetryWriteOptions, type RetryWriteResult, type ShouldRetryCallback, type ShouldRetryContext, retryPlugin };
|
package/dist/index.js
CHANGED
|
@@ -51,14 +51,14 @@ function retryPlugin(config = {}) {
|
|
|
51
51
|
} = config;
|
|
52
52
|
return {
|
|
53
53
|
name: PLUGIN_NAME,
|
|
54
|
-
operations: ["read", "write", "
|
|
54
|
+
operations: ["read", "write", "pages", "queue"],
|
|
55
55
|
priority: 200,
|
|
56
56
|
middleware: async (context, next) => {
|
|
57
57
|
const t = context.tracer?.(PLUGIN_NAME);
|
|
58
58
|
const pluginOptions = context.pluginOptions;
|
|
59
|
-
const retriesConfig = pluginOptions?.retries ?? defaultRetries;
|
|
60
|
-
const retryDelayConfig = pluginOptions?.
|
|
61
|
-
const shouldRetryFn = pluginOptions?.shouldRetry ?? defaultShouldRetryFn;
|
|
59
|
+
const retriesConfig = pluginOptions?.retry?.retries ?? defaultRetries;
|
|
60
|
+
const retryDelayConfig = pluginOptions?.retry?.delay ?? defaultRetryDelay;
|
|
61
|
+
const shouldRetryFn = pluginOptions?.retry?.shouldRetry ?? defaultShouldRetryFn;
|
|
62
62
|
const maxRetries = retriesConfig === false ? 0 : retriesConfig;
|
|
63
63
|
if (!maxRetries || maxRetries < 0) {
|
|
64
64
|
t?.skip("Disabled");
|
package/dist/index.mjs
CHANGED
|
@@ -24,14 +24,14 @@ function retryPlugin(config = {}) {
|
|
|
24
24
|
} = config;
|
|
25
25
|
return {
|
|
26
26
|
name: PLUGIN_NAME,
|
|
27
|
-
operations: ["read", "write", "
|
|
27
|
+
operations: ["read", "write", "pages", "queue"],
|
|
28
28
|
priority: 200,
|
|
29
29
|
middleware: async (context, next) => {
|
|
30
30
|
const t = context.tracer?.(PLUGIN_NAME);
|
|
31
31
|
const pluginOptions = context.pluginOptions;
|
|
32
|
-
const retriesConfig = pluginOptions?.retries ?? defaultRetries;
|
|
33
|
-
const retryDelayConfig = pluginOptions?.
|
|
34
|
-
const shouldRetryFn = pluginOptions?.shouldRetry ?? defaultShouldRetryFn;
|
|
32
|
+
const retriesConfig = pluginOptions?.retry?.retries ?? defaultRetries;
|
|
33
|
+
const retryDelayConfig = pluginOptions?.retry?.delay ?? defaultRetryDelay;
|
|
34
|
+
const shouldRetryFn = pluginOptions?.retry?.shouldRetry ?? defaultShouldRetryFn;
|
|
35
35
|
const maxRetries = retriesConfig === false ? 0 : retriesConfig;
|
|
36
36
|
if (!maxRetries || maxRetries < 0) {
|
|
37
37
|
t?.skip("Disabled");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-retry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Automatic retry plugin for Spoosh with configurable attempts and delay",
|
|
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.15.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.
|
|
40
|
-
"@spoosh/test-utils": "0.
|
|
39
|
+
"@spoosh/core": "0.15.0",
|
|
40
|
+
"@spoosh/test-utils": "0.3.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"dev": "tsup --watch",
|