@orion-js/helpers 3.5.0 → 3.5.3
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/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/retries.d.ts +1 -0
- package/lib/retries.js +23 -0
- package/lib/retries.test.d.ts +1 -0
- package/lib/retries.test.js +47 -0
- package/package.json +5 -3
- package/yarn-error.log +467 -2445
package/lib/index.d.ts
CHANGED
|
@@ -7,4 +7,5 @@ import PermissionsError from './Errors/PermissionsError';
|
|
|
7
7
|
import UserError from './Errors/UserError';
|
|
8
8
|
import createMapArray from './createMapArray';
|
|
9
9
|
export * from './composeMiddlewares';
|
|
10
|
+
export * from './retries';
|
|
10
11
|
export { createMap, createMapArray, generateId, hashObject, sleep, OrionError, PermissionsError, UserError, OrionErrorInformation };
|
package/lib/index.js
CHANGED
|
@@ -31,3 +31,4 @@ exports.UserError = UserError_1.default;
|
|
|
31
31
|
const createMapArray_1 = __importDefault(require("./createMapArray"));
|
|
32
32
|
exports.createMapArray = createMapArray_1.default;
|
|
33
33
|
__exportStar(require("./composeMiddlewares"), exports);
|
|
34
|
+
__exportStar(require("./retries"), exports);
|
package/lib/retries.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function executeWithRetries<TFunc extends () => Promise<any>>(fn: TFunc, retries: number, timeout: number): Promise<ReturnType<TFunc>>;
|
package/lib/retries.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.executeWithRetries = void 0;
|
|
4
|
+
function executeWithRetries(fn, retries, timeout) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const retry = async (retries) => {
|
|
7
|
+
try {
|
|
8
|
+
const result = await fn();
|
|
9
|
+
resolve(result);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (retries > 0) {
|
|
13
|
+
setTimeout(() => retry(retries - 1), timeout);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
reject(error);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
retry(retries);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
exports.executeWithRetries = executeWithRetries;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const retries_1 = require("./retries");
|
|
4
|
+
describe('Retry helpers', () => {
|
|
5
|
+
it('should retry a function', async () => {
|
|
6
|
+
let counter = 0;
|
|
7
|
+
const fn = async () => {
|
|
8
|
+
counter++;
|
|
9
|
+
if (counter < 3) {
|
|
10
|
+
throw new Error('error');
|
|
11
|
+
}
|
|
12
|
+
return 'ok';
|
|
13
|
+
};
|
|
14
|
+
const result = await (0, retries_1.executeWithRetries)(fn, 3, 0);
|
|
15
|
+
expect(result).toEqual('ok');
|
|
16
|
+
});
|
|
17
|
+
it('should the throw the error if the retries are over', async () => {
|
|
18
|
+
let counter = 0;
|
|
19
|
+
const fn = async () => {
|
|
20
|
+
counter++;
|
|
21
|
+
throw new Error('error');
|
|
22
|
+
};
|
|
23
|
+
expect.assertions(2);
|
|
24
|
+
try {
|
|
25
|
+
await (0, retries_1.executeWithRetries)(fn, 3, 0);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
expect(error.message).toEqual('error');
|
|
29
|
+
expect(counter).toEqual(4);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
it('should run only once a function with 0 or undefined retrires', async () => {
|
|
33
|
+
let counter = 0;
|
|
34
|
+
const fn = async () => {
|
|
35
|
+
counter++;
|
|
36
|
+
throw new Error('error');
|
|
37
|
+
};
|
|
38
|
+
expect.assertions(2);
|
|
39
|
+
try {
|
|
40
|
+
await (0, retries_1.executeWithRetries)(fn, null, 0);
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
expect(error.message).toEqual('error');
|
|
44
|
+
expect(counter).toEqual(1);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/helpers",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.3",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"test": "
|
|
8
|
+
"test": "jest",
|
|
9
9
|
"prepare": "yarn run build",
|
|
10
10
|
"clean": "rm -rf ./lib",
|
|
11
11
|
"build": "yarn run clean && tsc",
|
|
@@ -18,10 +18,12 @@
|
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@shelf/jest-mongodb": "^2.1.0",
|
|
20
20
|
"@types/jest": "^27.0.2",
|
|
21
|
+
"jest": "^29.5.0",
|
|
22
|
+
"ts-jest": "^29.0.5",
|
|
21
23
|
"typescript": "^4.4.4"
|
|
22
24
|
},
|
|
23
25
|
"publishConfig": {
|
|
24
26
|
"access": "public"
|
|
25
27
|
},
|
|
26
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "34430096bb765afdc07b6f3c079915d424491b9c"
|
|
27
29
|
}
|