@spoosh/plugin-retry 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 +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +65 -7
- package/dist/index.mjs +65 -7
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install @spoosh/plugin-retry
|
|
|
16
16
|
import { Spoosh } from "@spoosh/core";
|
|
17
17
|
import { retryPlugin } from "@spoosh/plugin-retry";
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const spoosh = new Spoosh<ApiSchema, Error>("/api").use([
|
|
20
20
|
retryPlugin({ retries: 3, retryDelay: 1000 }),
|
|
21
21
|
]);
|
|
22
22
|
|
package/dist/index.d.mts
CHANGED
|
@@ -35,7 +35,7 @@ type RetryWriteResult = object;
|
|
|
35
35
|
* ```ts
|
|
36
36
|
* import { Spoosh } from "@spoosh/core";
|
|
37
37
|
*
|
|
38
|
-
* const
|
|
38
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
39
39
|
* .use([
|
|
40
40
|
* // ... other plugins
|
|
41
41
|
* retryPlugin({ retries: 3, retryDelay: 1000 }),
|
package/dist/index.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ type RetryWriteResult = object;
|
|
|
35
35
|
* ```ts
|
|
36
36
|
* import { Spoosh } from "@spoosh/core";
|
|
37
37
|
*
|
|
38
|
-
* const
|
|
38
|
+
* const spoosh = new Spoosh<ApiSchema, Error>("/api")
|
|
39
39
|
* .use([
|
|
40
40
|
* // ... other plugins
|
|
41
41
|
* retryPlugin({ retries: 3, retryDelay: 1000 }),
|
package/dist/index.js
CHANGED
|
@@ -25,6 +25,42 @@ __export(src_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
27
|
// src/plugin.ts
|
|
28
|
+
var import_core = require("@spoosh/core");
|
|
29
|
+
|
|
30
|
+
// src/cloneObject.ts
|
|
31
|
+
function clone(value, seen = /* @__PURE__ */ new WeakMap()) {
|
|
32
|
+
if (value === void 0 || value === null || typeof value !== "object") {
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
if (seen.has(value)) {
|
|
36
|
+
return seen.get(value);
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(value)) {
|
|
39
|
+
const arr = [];
|
|
40
|
+
seen.set(value, arr);
|
|
41
|
+
return value.map((v) => clone(v, seen));
|
|
42
|
+
}
|
|
43
|
+
if (value instanceof Date) {
|
|
44
|
+
return new Date(value.getTime());
|
|
45
|
+
}
|
|
46
|
+
if (value instanceof RegExp) {
|
|
47
|
+
return new RegExp(value.source, value.flags);
|
|
48
|
+
}
|
|
49
|
+
if (value.constructor !== Object) {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
const obj = {};
|
|
53
|
+
seen.set(value, obj);
|
|
54
|
+
for (const key in value) {
|
|
55
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
56
|
+
obj[key] = clone(value[key], seen);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return obj;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// src/plugin.ts
|
|
63
|
+
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
28
64
|
function retryPlugin(config = {}) {
|
|
29
65
|
const { retries: defaultRetries = 3, retryDelay: defaultRetryDelay = 1e3 } = config;
|
|
30
66
|
return {
|
|
@@ -32,14 +68,36 @@ function retryPlugin(config = {}) {
|
|
|
32
68
|
operations: ["read", "write", "infiniteRead"],
|
|
33
69
|
middleware: async (context, next) => {
|
|
34
70
|
const pluginOptions = context.pluginOptions;
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
71
|
+
const retriesConfig = pluginOptions?.retries ?? defaultRetries;
|
|
72
|
+
const retryDelayConfig = pluginOptions?.retryDelay ?? defaultRetryDelay;
|
|
73
|
+
const maxRetries = retriesConfig === false ? 0 : retriesConfig;
|
|
74
|
+
if (!maxRetries || maxRetries < 0) {
|
|
75
|
+
return next();
|
|
76
|
+
}
|
|
77
|
+
const originalRequest = {
|
|
78
|
+
headers: clone(context.request.headers),
|
|
79
|
+
params: clone(context.request.params),
|
|
80
|
+
body: clone(context.request.body)
|
|
41
81
|
};
|
|
42
|
-
|
|
82
|
+
let res;
|
|
83
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
84
|
+
if (attempt > 0) {
|
|
85
|
+
context.request.headers = clone(originalRequest.headers);
|
|
86
|
+
context.request.params = clone(originalRequest.params);
|
|
87
|
+
context.request.body = clone(originalRequest.body);
|
|
88
|
+
}
|
|
89
|
+
res = await next();
|
|
90
|
+
if ((0, import_core.isAbortError)(res.error)) {
|
|
91
|
+
return res;
|
|
92
|
+
}
|
|
93
|
+
if ((0, import_core.isNetworkError)(res.error) && attempt < maxRetries) {
|
|
94
|
+
const delayMs = retryDelayConfig * Math.pow(2, attempt);
|
|
95
|
+
await delay(delayMs);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
return res;
|
|
99
|
+
}
|
|
100
|
+
return res;
|
|
43
101
|
}
|
|
44
102
|
};
|
|
45
103
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,40 @@
|
|
|
1
1
|
// src/plugin.ts
|
|
2
|
+
import { isNetworkError, isAbortError } from "@spoosh/core";
|
|
3
|
+
|
|
4
|
+
// src/cloneObject.ts
|
|
5
|
+
function clone(value, seen = /* @__PURE__ */ new WeakMap()) {
|
|
6
|
+
if (value === void 0 || value === null || typeof value !== "object") {
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
if (seen.has(value)) {
|
|
10
|
+
return seen.get(value);
|
|
11
|
+
}
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
const arr = [];
|
|
14
|
+
seen.set(value, arr);
|
|
15
|
+
return value.map((v) => clone(v, seen));
|
|
16
|
+
}
|
|
17
|
+
if (value instanceof Date) {
|
|
18
|
+
return new Date(value.getTime());
|
|
19
|
+
}
|
|
20
|
+
if (value instanceof RegExp) {
|
|
21
|
+
return new RegExp(value.source, value.flags);
|
|
22
|
+
}
|
|
23
|
+
if (value.constructor !== Object) {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
const obj = {};
|
|
27
|
+
seen.set(value, obj);
|
|
28
|
+
for (const key in value) {
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
30
|
+
obj[key] = clone(value[key], seen);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return obj;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/plugin.ts
|
|
37
|
+
var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
2
38
|
function retryPlugin(config = {}) {
|
|
3
39
|
const { retries: defaultRetries = 3, retryDelay: defaultRetryDelay = 1e3 } = config;
|
|
4
40
|
return {
|
|
@@ -6,14 +42,36 @@ function retryPlugin(config = {}) {
|
|
|
6
42
|
operations: ["read", "write", "infiniteRead"],
|
|
7
43
|
middleware: async (context, next) => {
|
|
8
44
|
const pluginOptions = context.pluginOptions;
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
45
|
+
const retriesConfig = pluginOptions?.retries ?? defaultRetries;
|
|
46
|
+
const retryDelayConfig = pluginOptions?.retryDelay ?? defaultRetryDelay;
|
|
47
|
+
const maxRetries = retriesConfig === false ? 0 : retriesConfig;
|
|
48
|
+
if (!maxRetries || maxRetries < 0) {
|
|
49
|
+
return next();
|
|
50
|
+
}
|
|
51
|
+
const originalRequest = {
|
|
52
|
+
headers: clone(context.request.headers),
|
|
53
|
+
params: clone(context.request.params),
|
|
54
|
+
body: clone(context.request.body)
|
|
15
55
|
};
|
|
16
|
-
|
|
56
|
+
let res;
|
|
57
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
58
|
+
if (attempt > 0) {
|
|
59
|
+
context.request.headers = clone(originalRequest.headers);
|
|
60
|
+
context.request.params = clone(originalRequest.params);
|
|
61
|
+
context.request.body = clone(originalRequest.body);
|
|
62
|
+
}
|
|
63
|
+
res = await next();
|
|
64
|
+
if (isAbortError(res.error)) {
|
|
65
|
+
return res;
|
|
66
|
+
}
|
|
67
|
+
if (isNetworkError(res.error) && attempt < maxRetries) {
|
|
68
|
+
const delayMs = retryDelayConfig * Math.pow(2, attempt);
|
|
69
|
+
await delay(delayMs);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
return res;
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
17
75
|
}
|
|
18
76
|
};
|
|
19
77
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spoosh/plugin-retry",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Automatic retry plugin for Spoosh with configurable attempts and delay",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@spoosh/core": ">=0.12.
|
|
36
|
+
"@spoosh/core": ">=0.12.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@spoosh/core": "0.12.
|
|
39
|
+
"@spoosh/core": "0.12.1",
|
|
40
40
|
"@spoosh/test-utils": "0.1.8"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|