@sebspark/retry 0.1.0 → 0.1.2
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/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +16 -21
- package/dist/index.mjs +13 -18
- package/package.json +6 -5
package/dist/index.d.mts
CHANGED
|
@@ -25,10 +25,10 @@ type HttpError = {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
declare const retryCondition: {
|
|
28
|
-
always: (_error:
|
|
28
|
+
always: (_error: unknown) => boolean;
|
|
29
29
|
httpErrors: (...codes: ErrorType[]) => (error: HttpError) => boolean;
|
|
30
|
-
serverErrors: (error:
|
|
30
|
+
serverErrors: (error: HttpError) => boolean;
|
|
31
31
|
custom: (func: (error: any) => boolean) => (error: any) => boolean;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
export { RetryConditionFunction, RetrySettings, interval, retry, retryCondition };
|
|
34
|
+
export { type RetryConditionFunction, type RetrySettings, interval, retry, retryCondition };
|
package/dist/index.d.ts
CHANGED
|
@@ -25,10 +25,10 @@ type HttpError = {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
declare const retryCondition: {
|
|
28
|
-
always: (_error:
|
|
28
|
+
always: (_error: unknown) => boolean;
|
|
29
29
|
httpErrors: (...codes: ErrorType[]) => (error: HttpError) => boolean;
|
|
30
|
-
serverErrors: (error:
|
|
30
|
+
serverErrors: (error: HttpError) => boolean;
|
|
31
31
|
custom: (func: (error: any) => boolean) => (error: any) => boolean;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
-
export { RetryConditionFunction, RetrySettings, interval, retry, retryCondition };
|
|
34
|
+
export { type RetryConditionFunction, type RetrySettings, interval, retry, retryCondition };
|
package/dist/index.js
CHANGED
|
@@ -18,13 +18,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
23
|
interval: () => interval,
|
|
24
24
|
retry: () => retry,
|
|
25
25
|
retryCondition: () => retryCondition
|
|
26
26
|
});
|
|
27
|
-
module.exports = __toCommonJS(
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
29
|
// src/retry.ts
|
|
30
30
|
var defaultSettings = {
|
|
@@ -32,8 +32,7 @@ var defaultSettings = {
|
|
|
32
32
|
};
|
|
33
33
|
var mergeSettings = (...settings) => {
|
|
34
34
|
const s = settings.filter((s2) => s2);
|
|
35
|
-
if (!s.length)
|
|
36
|
-
return void 0;
|
|
35
|
+
if (!s.length) return void 0;
|
|
37
36
|
const merged = Object.assign(defaultSettings, ...s);
|
|
38
37
|
return merged;
|
|
39
38
|
};
|
|
@@ -45,13 +44,12 @@ var retry = async (func, ...settings) => {
|
|
|
45
44
|
const response = await func();
|
|
46
45
|
return response;
|
|
47
46
|
} catch (error) {
|
|
48
|
-
if (mergedSettings &&
|
|
49
|
-
|
|
50
|
-
await wait(mergedSettings.interval(retries));
|
|
51
|
-
return makeCall(retries);
|
|
52
|
-
} else {
|
|
53
|
-
throw error;
|
|
47
|
+
if (mergedSettings && // biome-ignore lint/style/noNonNullAssertion: Will be set by defaultSettings
|
|
48
|
+
mergedSettings.retryCondition(error) && retries < mergedSettings.maxRetries) {
|
|
49
|
+
await wait(mergedSettings.interval(retries + 1));
|
|
50
|
+
return makeCall(retries + 1);
|
|
54
51
|
}
|
|
52
|
+
throw error;
|
|
55
53
|
}
|
|
56
54
|
};
|
|
57
55
|
return makeCall(0);
|
|
@@ -59,7 +57,7 @@ var retry = async (func, ...settings) => {
|
|
|
59
57
|
var interval = {
|
|
60
58
|
fixed: (delay) => (_retries) => delay,
|
|
61
59
|
linear: (delay) => (retries) => retries * delay,
|
|
62
|
-
exponential: (delay, base = 2) => (retries) =>
|
|
60
|
+
exponential: (delay, base = 2) => (retries) => base ** (retries - 1) * delay
|
|
63
61
|
};
|
|
64
62
|
var clientErrorCodes = [
|
|
65
63
|
400,
|
|
@@ -108,20 +106,17 @@ var serverErrorCodes = [
|
|
|
108
106
|
var retryCondition = {
|
|
109
107
|
always: (_error) => true,
|
|
110
108
|
httpErrors: (...codes) => (error) => {
|
|
111
|
-
var _a, _b;
|
|
112
109
|
const captureCodes = codes.flatMap((code) => {
|
|
113
|
-
if (typeof code === "number")
|
|
114
|
-
|
|
115
|
-
if (code === "
|
|
116
|
-
|
|
117
|
-
if (code === "server")
|
|
118
|
-
return serverErrorCodes;
|
|
119
|
-
return;
|
|
110
|
+
if (typeof code === "number") return code;
|
|
111
|
+
if (code === "client") return clientErrorCodes;
|
|
112
|
+
if (code === "server") return serverErrorCodes;
|
|
113
|
+
return 500;
|
|
120
114
|
}).filter((code) => !!code);
|
|
121
|
-
const status = error.status || error.statusCode ||
|
|
115
|
+
const status = error.status || error.statusCode || error.response?.status || error.response?.statusCode || 0;
|
|
122
116
|
return captureCodes.includes(status);
|
|
123
117
|
},
|
|
124
118
|
serverErrors: (error) => retryCondition.httpErrors("server")(error),
|
|
119
|
+
// biome-ignore lint/suspicious/noExplicitAny: Errors can be anything
|
|
125
120
|
custom: (func) => func
|
|
126
121
|
};
|
|
127
122
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -4,8 +4,7 @@ var defaultSettings = {
|
|
|
4
4
|
};
|
|
5
5
|
var mergeSettings = (...settings) => {
|
|
6
6
|
const s = settings.filter((s2) => s2);
|
|
7
|
-
if (!s.length)
|
|
8
|
-
return void 0;
|
|
7
|
+
if (!s.length) return void 0;
|
|
9
8
|
const merged = Object.assign(defaultSettings, ...s);
|
|
10
9
|
return merged;
|
|
11
10
|
};
|
|
@@ -17,13 +16,12 @@ var retry = async (func, ...settings) => {
|
|
|
17
16
|
const response = await func();
|
|
18
17
|
return response;
|
|
19
18
|
} catch (error) {
|
|
20
|
-
if (mergedSettings &&
|
|
21
|
-
|
|
22
|
-
await wait(mergedSettings.interval(retries));
|
|
23
|
-
return makeCall(retries);
|
|
24
|
-
} else {
|
|
25
|
-
throw error;
|
|
19
|
+
if (mergedSettings && // biome-ignore lint/style/noNonNullAssertion: Will be set by defaultSettings
|
|
20
|
+
mergedSettings.retryCondition(error) && retries < mergedSettings.maxRetries) {
|
|
21
|
+
await wait(mergedSettings.interval(retries + 1));
|
|
22
|
+
return makeCall(retries + 1);
|
|
26
23
|
}
|
|
24
|
+
throw error;
|
|
27
25
|
}
|
|
28
26
|
};
|
|
29
27
|
return makeCall(0);
|
|
@@ -31,7 +29,7 @@ var retry = async (func, ...settings) => {
|
|
|
31
29
|
var interval = {
|
|
32
30
|
fixed: (delay) => (_retries) => delay,
|
|
33
31
|
linear: (delay) => (retries) => retries * delay,
|
|
34
|
-
exponential: (delay, base = 2) => (retries) =>
|
|
32
|
+
exponential: (delay, base = 2) => (retries) => base ** (retries - 1) * delay
|
|
35
33
|
};
|
|
36
34
|
var clientErrorCodes = [
|
|
37
35
|
400,
|
|
@@ -80,20 +78,17 @@ var serverErrorCodes = [
|
|
|
80
78
|
var retryCondition = {
|
|
81
79
|
always: (_error) => true,
|
|
82
80
|
httpErrors: (...codes) => (error) => {
|
|
83
|
-
var _a, _b;
|
|
84
81
|
const captureCodes = codes.flatMap((code) => {
|
|
85
|
-
if (typeof code === "number")
|
|
86
|
-
|
|
87
|
-
if (code === "
|
|
88
|
-
|
|
89
|
-
if (code === "server")
|
|
90
|
-
return serverErrorCodes;
|
|
91
|
-
return;
|
|
82
|
+
if (typeof code === "number") return code;
|
|
83
|
+
if (code === "client") return clientErrorCodes;
|
|
84
|
+
if (code === "server") return serverErrorCodes;
|
|
85
|
+
return 500;
|
|
92
86
|
}).filter((code) => !!code);
|
|
93
|
-
const status = error.status || error.statusCode ||
|
|
87
|
+
const status = error.status || error.statusCode || error.response?.status || error.response?.statusCode || 0;
|
|
94
88
|
return captureCodes.includes(status);
|
|
95
89
|
},
|
|
96
90
|
serverErrors: (error) => retryCondition.httpErrors("server")(error),
|
|
91
|
+
// biome-ignore lint/suspicious/noExplicitAny: Errors can be anything
|
|
97
92
|
custom: (func) => func
|
|
98
93
|
};
|
|
99
94
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/retry",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -11,11 +11,12 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "tsup-node src/index.ts --format esm,cjs --dts",
|
|
13
13
|
"dev": "tsc --watch --noEmit",
|
|
14
|
-
"lint": "
|
|
15
|
-
"test": "vitest --passWithNoTests --coverage",
|
|
16
|
-
"typecheck": "vitest typecheck --passWithNoTests"
|
|
14
|
+
"lint": "biome lint .",
|
|
15
|
+
"test": "vitest run --passWithNoTests --coverage",
|
|
16
|
+
"typecheck": "vitest --typecheck.only --passWithNoTests"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"tsconfig": "*"
|
|
19
|
+
"@sebspark/tsconfig": "*",
|
|
20
|
+
"axios": "1.12.2"
|
|
20
21
|
}
|
|
21
22
|
}
|