@xavierchow/retry 1.0.4
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 +33 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/package.json +28 -0
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Retry
|
|
2
|
+
|
|
3
|
+
This is a tiny library to handle the retry mechanism, it provides
|
|
4
|
+
|
|
5
|
+
- max attempts, setting to 1 means `do it once and retry it once`
|
|
6
|
+
- criteria control for retry or not
|
|
7
|
+
- back off for a certain delay
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Install
|
|
11
|
+
|
|
12
|
+
T.B.D
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# Development
|
|
17
|
+
|
|
18
|
+
## Install Dependence
|
|
19
|
+
``` sh
|
|
20
|
+
pnpm install
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Build
|
|
24
|
+
|
|
25
|
+
``` sh
|
|
26
|
+
pnpm build
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Test
|
|
30
|
+
|
|
31
|
+
``` sh
|
|
32
|
+
pnpm test
|
|
33
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type RetryConfig = {
|
|
2
|
+
maxAttempts: number;
|
|
3
|
+
delayInMs?: number;
|
|
4
|
+
shouldRetry?: (err: unknown) => boolean;
|
|
5
|
+
mode?: "linear" | "exp";
|
|
6
|
+
};
|
|
7
|
+
export declare function withRetry(config: RetryConfig, fn: () => Promise<unknown> | unknown): Promise<unknown>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,KAAK,WAAW,GAAG;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC;IACxC,IAAI,CAAC,EAAE,QAAQ,GAAG,KAAK,CAAC;CACzB,CAAC;AACF,wBAAsB,SAAS,CAC7B,MAAM,EAAE,WAAW,EACnB,EAAE,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,oBAsBrC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withRetry = withRetry;
|
|
4
|
+
async function withRetry(config, fn) {
|
|
5
|
+
const shouldRetry = config.shouldRetry || (() => true);
|
|
6
|
+
let attempts = 0;
|
|
7
|
+
const exec = async () => {
|
|
8
|
+
try {
|
|
9
|
+
attempts++;
|
|
10
|
+
const r = await fn();
|
|
11
|
+
return r;
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
const toBeRetried = shouldRetry(err);
|
|
15
|
+
if (toBeRetried && attempts <= config.maxAttempts) {
|
|
16
|
+
const timeout = computeTimeout(config, attempts);
|
|
17
|
+
await new Promise((resolve) => setTimeout(resolve, timeout));
|
|
18
|
+
return exec();
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
return exec();
|
|
26
|
+
}
|
|
27
|
+
function computeTimeout(config, attempts) {
|
|
28
|
+
config.delayInMs = config.delayInMs || 1000;
|
|
29
|
+
const factor = 2;
|
|
30
|
+
if (config.mode && config.mode === "exp") {
|
|
31
|
+
const timeout = config.delayInMs * Math.pow(factor, attempts);
|
|
32
|
+
return timeout;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
return config.delayInMs;
|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AC;QACzC,MAAM,OAAO,GAAG,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAC9D,OAAO,OAAO,CAAC;IACjB,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC,SAAS,CAAC;IAC1B,CAAC;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xavierchow/retry",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prebuild": "rimraf dist",
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@types/jest": "^30.0.0",
|
|
21
|
+
"@types/node": "^25.0.3",
|
|
22
|
+
"jest": "^30.2.0",
|
|
23
|
+
"prettier": "3.7.4",
|
|
24
|
+
"rimraf": "^6.1.2",
|
|
25
|
+
"ts-jest": "^29.4.6",
|
|
26
|
+
"typescript": "^5.9.3"
|
|
27
|
+
}
|
|
28
|
+
}
|