@orion-js/helpers 3.11.8 → 4.0.0-alpha.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.cjs +1018 -0
- package/dist/index.d.ts +362 -0
- package/dist/index.js +992 -0
- package/package.json +22 -15
- package/LICENSE +0 -21
- package/jest.config.js +0 -8
- package/lib/Errors/OrionError.d.ts +0 -13
- package/lib/Errors/OrionError.js +0 -10
- package/lib/Errors/PermissionsError.d.ts +0 -4
- package/lib/Errors/PermissionsError.js +0 -24
- package/lib/Errors/UserError.d.ts +0 -4
- package/lib/Errors/UserError.js +0 -25
- package/lib/composeMiddlewares.d.ts +0 -6
- package/lib/composeMiddlewares.js +0 -44
- package/lib/createMap.d.ts +0 -1
- package/lib/createMap.js +0 -10
- package/lib/createMapArray.d.ts +0 -1
- package/lib/createMapArray.js +0 -11
- package/lib/generateId.d.ts +0 -6
- package/lib/generateId.js +0 -54
- package/lib/generateId.test.d.ts +0 -1
- package/lib/generateId.test.js +0 -11
- package/lib/generateUUID.d.ts +0 -1
- package/lib/generateUUID.js +0 -8
- package/lib/generateUUID.test.d.ts +0 -1
- package/lib/generateUUID.test.js +0 -11
- package/lib/hashObject.d.ts +0 -1
- package/lib/hashObject.js +0 -10
- package/lib/hashObject.test.d.ts +0 -1
- package/lib/hashObject.test.js +0 -30
- package/lib/index.d.ts +0 -12
- package/lib/index.js +0 -35
- package/lib/retries.d.ts +0 -1
- package/lib/retries.js +0 -23
- package/lib/retries.test.d.ts +0 -1
- package/lib/retries.test.js +0 -47
- package/lib/sleep.d.ts +0 -5
- package/lib/sleep.js +0 -8
- package/tsconfig.json +0 -16
- package/yarn-error.log +0 -710
package/package.json
CHANGED
|
@@ -1,29 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/helpers",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "
|
|
3
|
+
"version": "4.0.0-alpha.2",
|
|
4
|
+
"main": "./dist/index.cjs",
|
|
5
|
+
"module": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"require": "./dist/index.cjs"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
5
15
|
"author": "nicolaslopezj",
|
|
6
16
|
"license": "MIT",
|
|
7
17
|
"scripts": {
|
|
8
|
-
"test": "
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"watch": "tsc -w",
|
|
13
|
-
"upgrade-interactive": "yarn upgrade-interactive"
|
|
18
|
+
"test": "bun test",
|
|
19
|
+
"build": "bun run build.ts",
|
|
20
|
+
"dev": "bun --watch src/index.ts",
|
|
21
|
+
"clean": "rm -rf ./dist"
|
|
14
22
|
},
|
|
15
23
|
"dependencies": {
|
|
16
24
|
"object-hash": "^2.2.0"
|
|
17
25
|
},
|
|
18
26
|
"devDependencies": {
|
|
19
|
-
"@
|
|
20
|
-
"@types/
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"typescript": "^4.4.4"
|
|
27
|
+
"@types/bun": "^1.2.4",
|
|
28
|
+
"@types/node": "^20.9.0",
|
|
29
|
+
"typescript": "^4.4.4",
|
|
30
|
+
"vitest": "^0.34.6"
|
|
24
31
|
},
|
|
25
32
|
"publishConfig": {
|
|
26
33
|
"access": "public"
|
|
27
34
|
},
|
|
28
|
-
"gitHead": "
|
|
29
|
-
}
|
|
35
|
+
"gitHead": "a485b1fe6a1840ee6cb58fd69d6de62585f1ed10"
|
|
36
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 Orionjs Team
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/jest.config.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export interface OrionErrorInformation {
|
|
2
|
-
error: string;
|
|
3
|
-
message: string;
|
|
4
|
-
extra: any;
|
|
5
|
-
}
|
|
6
|
-
export declare class OrionError extends Error {
|
|
7
|
-
isOrionError: boolean;
|
|
8
|
-
isUserError: boolean;
|
|
9
|
-
isPermissionsError: boolean;
|
|
10
|
-
code: string;
|
|
11
|
-
extra: any;
|
|
12
|
-
getInfo: () => OrionErrorInformation;
|
|
13
|
-
}
|
package/lib/Errors/OrionError.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const OrionError_1 = require("./OrionError");
|
|
4
|
-
class PermissionsError extends OrionError_1.OrionError {
|
|
5
|
-
constructor(permissionErrorType, extra = {}) {
|
|
6
|
-
// Calling parent constructor of base Error class.
|
|
7
|
-
const message = extra.message || `Client is not allowed to perform this action [${permissionErrorType}]`;
|
|
8
|
-
super(message);
|
|
9
|
-
Error.captureStackTrace(this, this.constructor);
|
|
10
|
-
this.isOrionError = true;
|
|
11
|
-
this.isPermissionsError = true;
|
|
12
|
-
this.code = 'PermissionsError';
|
|
13
|
-
this.extra = extra;
|
|
14
|
-
this.getInfo = () => {
|
|
15
|
-
return {
|
|
16
|
-
...extra,
|
|
17
|
-
error: 'PermissionsError',
|
|
18
|
-
message,
|
|
19
|
-
type: permissionErrorType
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
exports.default = PermissionsError;
|
package/lib/Errors/UserError.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const OrionError_1 = require("./OrionError");
|
|
4
|
-
class UserError extends OrionError_1.OrionError {
|
|
5
|
-
constructor(code, message, extra) {
|
|
6
|
-
if (!message && code) {
|
|
7
|
-
message = code;
|
|
8
|
-
code = 'error';
|
|
9
|
-
}
|
|
10
|
-
super(message);
|
|
11
|
-
Error.captureStackTrace(this, this.constructor);
|
|
12
|
-
this.isOrionError = true;
|
|
13
|
-
this.isUserError = true;
|
|
14
|
-
this.code = code;
|
|
15
|
-
this.extra = extra;
|
|
16
|
-
this.getInfo = () => {
|
|
17
|
-
return {
|
|
18
|
-
error: code,
|
|
19
|
-
message,
|
|
20
|
-
extra
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.default = UserError;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// from https://github.com/koajs/compose/blob/master/index.js
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.composeMiddlewares = void 0;
|
|
5
|
-
/**
|
|
6
|
-
* Compose `middleware` returning
|
|
7
|
-
* a fully valid middleware comprised
|
|
8
|
-
* of all those which are passed.
|
|
9
|
-
*/
|
|
10
|
-
function composeMiddlewares(middleware) {
|
|
11
|
-
if (!Array.isArray(middleware))
|
|
12
|
-
throw new TypeError('Middleware stack must be an array!');
|
|
13
|
-
for (const fn of middleware) {
|
|
14
|
-
if (typeof fn !== 'function')
|
|
15
|
-
throw new TypeError('Middleware must be composed of functions!');
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* @param {Object} context
|
|
19
|
-
* @return {Promise}
|
|
20
|
-
* @api public
|
|
21
|
-
*/
|
|
22
|
-
return function (context, next) {
|
|
23
|
-
// last called middleware #
|
|
24
|
-
let index = -1;
|
|
25
|
-
return dispatch(0);
|
|
26
|
-
function dispatch(i) {
|
|
27
|
-
if (i <= index)
|
|
28
|
-
return Promise.reject(new Error('next() called multiple times'));
|
|
29
|
-
index = i;
|
|
30
|
-
let fn = middleware[i];
|
|
31
|
-
if (i === middleware.length)
|
|
32
|
-
fn = next;
|
|
33
|
-
if (!fn)
|
|
34
|
-
return Promise.resolve();
|
|
35
|
-
try {
|
|
36
|
-
return Promise.resolve(fn(context, dispatch.bind(null, i + 1)));
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
return Promise.reject(err);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
exports.composeMiddlewares = composeMiddlewares;
|
package/lib/createMap.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function createMap<T>(array: Array<T>, key?: string): Record<string, T>;
|
package/lib/createMap.js
DELETED
package/lib/createMapArray.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function createMapArray<T>(array: Array<T>, key?: string): Record<string, Array<T>>;
|
package/lib/createMapArray.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
function createMapArray(array, key = '_id') {
|
|
4
|
-
const map = {};
|
|
5
|
-
for (const item of array) {
|
|
6
|
-
map[item[key]] = map[item[key]] || [];
|
|
7
|
-
map[item[key]].push(item);
|
|
8
|
-
}
|
|
9
|
-
return map;
|
|
10
|
-
}
|
|
11
|
-
exports.default = createMapArray;
|
package/lib/generateId.d.ts
DELETED
package/lib/generateId.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const crypto_1 = __importDefault(require("crypto"));
|
|
7
|
-
const UNMISTAKABLE_CHARS = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghjkmnopqrstuvwxyz';
|
|
8
|
-
const hexString = function (digits) {
|
|
9
|
-
var numBytes = Math.ceil(digits / 2);
|
|
10
|
-
var bytes;
|
|
11
|
-
// Try to get cryptographically strong randomness. Fall back to
|
|
12
|
-
// non-cryptographically strong if not available.
|
|
13
|
-
try {
|
|
14
|
-
bytes = crypto_1.default.randomBytes(numBytes);
|
|
15
|
-
}
|
|
16
|
-
catch (e) {
|
|
17
|
-
// XXX should re-throw any error except insufficient entropy
|
|
18
|
-
bytes = crypto_1.default.pseudoRandomBytes(numBytes);
|
|
19
|
-
}
|
|
20
|
-
var result = bytes.toString('hex');
|
|
21
|
-
// If the number of digits is odd, we'll have generated an extra 4 bits
|
|
22
|
-
// of randomness, so we need to trim the last digit.
|
|
23
|
-
return result.substring(0, digits);
|
|
24
|
-
};
|
|
25
|
-
const fraction = function () {
|
|
26
|
-
var numerator = parseInt(hexString(8), 16);
|
|
27
|
-
return numerator * 2.3283064365386963e-10; // 2^-32
|
|
28
|
-
};
|
|
29
|
-
const choice = function (arrayOrString) {
|
|
30
|
-
var index = Math.floor(fraction() * arrayOrString.length);
|
|
31
|
-
if (typeof arrayOrString === 'string')
|
|
32
|
-
return arrayOrString.substr(index, 1);
|
|
33
|
-
else
|
|
34
|
-
return arrayOrString[index];
|
|
35
|
-
};
|
|
36
|
-
const randomString = function (charsCount, alphabet) {
|
|
37
|
-
var digits = [];
|
|
38
|
-
for (var i = 0; i < charsCount; i++) {
|
|
39
|
-
digits[i] = choice(alphabet);
|
|
40
|
-
}
|
|
41
|
-
return digits.join('');
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Returns a random ID
|
|
45
|
-
* @param charsCount length of the ID
|
|
46
|
-
* @param chars characters used to generate the ID
|
|
47
|
-
*/
|
|
48
|
-
function generateId(charsCount, chars = UNMISTAKABLE_CHARS) {
|
|
49
|
-
if (!charsCount) {
|
|
50
|
-
charsCount = 17;
|
|
51
|
-
}
|
|
52
|
-
return randomString(charsCount, chars);
|
|
53
|
-
}
|
|
54
|
-
exports.default = generateId;
|
package/lib/generateId.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/generateId.test.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const generateId_1 = __importDefault(require("./generateId"));
|
|
7
|
-
const isString_1 = __importDefault(require("lodash/isString"));
|
|
8
|
-
it('should generate random ids', async () => {
|
|
9
|
-
expect((0, generateId_1.default)()).not.toBe((0, generateId_1.default)());
|
|
10
|
-
expect((0, isString_1.default)((0, generateId_1.default)())).toBe(true);
|
|
11
|
-
});
|
package/lib/generateUUID.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function generateUUID(): any;
|
package/lib/generateUUID.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/generateUUID.test.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const isString_1 = __importDefault(require("lodash/isString"));
|
|
7
|
-
const generateUUID_1 = require("./generateUUID");
|
|
8
|
-
it('should generate random uuid v4', async () => {
|
|
9
|
-
expect((0, generateUUID_1.generateUUID)()).not.toBe((0, generateUUID_1.generateUUID)());
|
|
10
|
-
expect((0, isString_1.default)((0, generateUUID_1.generateUUID)())).toBe(true);
|
|
11
|
-
});
|
package/lib/hashObject.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function (object: any): string;
|
package/lib/hashObject.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const object_hash_1 = __importDefault(require("object-hash"));
|
|
7
|
-
function default_1(object) {
|
|
8
|
-
return (0, object_hash_1.default)(object);
|
|
9
|
-
}
|
|
10
|
-
exports.default = default_1;
|
package/lib/hashObject.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/hashObject.test.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const hashObject_1 = __importDefault(require("./hashObject"));
|
|
7
|
-
it('should create a string hash of an object', async () => {
|
|
8
|
-
const object = {
|
|
9
|
-
hello: 'world',
|
|
10
|
-
date: new Date(),
|
|
11
|
-
subObject: {
|
|
12
|
-
name: 'Nicolás'
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
const hash = (0, hashObject_1.default)(object);
|
|
16
|
-
expect(typeof hash).toBe('string');
|
|
17
|
-
});
|
|
18
|
-
it('object with different key orders should return same hash', async () => {
|
|
19
|
-
const object1 = {
|
|
20
|
-
one: '1',
|
|
21
|
-
two: '2'
|
|
22
|
-
};
|
|
23
|
-
const object2 = {
|
|
24
|
-
two: '2',
|
|
25
|
-
one: '1'
|
|
26
|
-
};
|
|
27
|
-
const hash1 = (0, hashObject_1.default)(object1);
|
|
28
|
-
const hash2 = (0, hashObject_1.default)(object2);
|
|
29
|
-
expect(hash1).toBe(hash2);
|
|
30
|
-
});
|
package/lib/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import sleep from './sleep';
|
|
2
|
-
import hashObject from './hashObject';
|
|
3
|
-
import generateId from './generateId';
|
|
4
|
-
import createMap from './createMap';
|
|
5
|
-
import { OrionError, OrionErrorInformation } from './Errors/OrionError';
|
|
6
|
-
import PermissionsError from './Errors/PermissionsError';
|
|
7
|
-
import UserError from './Errors/UserError';
|
|
8
|
-
import createMapArray from './createMapArray';
|
|
9
|
-
export * from './composeMiddlewares';
|
|
10
|
-
export * from './retries';
|
|
11
|
-
export * from './generateUUID';
|
|
12
|
-
export { createMap, createMapArray, generateId, hashObject, sleep, OrionError, PermissionsError, UserError, OrionErrorInformation };
|
package/lib/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
-
}) : (function(o, m, k, k2) {
|
|
6
|
-
if (k2 === undefined) k2 = k;
|
|
7
|
-
o[k2] = m[k];
|
|
8
|
-
}));
|
|
9
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.UserError = exports.PermissionsError = exports.OrionError = exports.sleep = exports.hashObject = exports.generateId = exports.createMapArray = exports.createMap = void 0;
|
|
17
|
-
const sleep_1 = __importDefault(require("./sleep"));
|
|
18
|
-
exports.sleep = sleep_1.default;
|
|
19
|
-
const hashObject_1 = __importDefault(require("./hashObject"));
|
|
20
|
-
exports.hashObject = hashObject_1.default;
|
|
21
|
-
const generateId_1 = __importDefault(require("./generateId"));
|
|
22
|
-
exports.generateId = generateId_1.default;
|
|
23
|
-
const createMap_1 = __importDefault(require("./createMap"));
|
|
24
|
-
exports.createMap = createMap_1.default;
|
|
25
|
-
const OrionError_1 = require("./Errors/OrionError");
|
|
26
|
-
Object.defineProperty(exports, "OrionError", { enumerable: true, get: function () { return OrionError_1.OrionError; } });
|
|
27
|
-
const PermissionsError_1 = __importDefault(require("./Errors/PermissionsError"));
|
|
28
|
-
exports.PermissionsError = PermissionsError_1.default;
|
|
29
|
-
const UserError_1 = __importDefault(require("./Errors/UserError"));
|
|
30
|
-
exports.UserError = UserError_1.default;
|
|
31
|
-
const createMapArray_1 = __importDefault(require("./createMapArray"));
|
|
32
|
-
exports.createMapArray = createMapArray_1.default;
|
|
33
|
-
__exportStar(require("./composeMiddlewares"), exports);
|
|
34
|
-
__exportStar(require("./retries"), exports);
|
|
35
|
-
__exportStar(require("./generateUUID"), exports);
|
package/lib/retries.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function executeWithRetries<TFunc extends () => Promise<any>>(fn: TFunc, retries: number, timeout: number): Promise<ReturnType<TFunc>>;
|
package/lib/retries.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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;
|
package/lib/retries.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/retries.test.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
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/lib/sleep.d.ts
DELETED
package/lib/sleep.js
DELETED
package/tsconfig.json
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"strict": false,
|
|
4
|
-
"outDir": "./lib",
|
|
5
|
-
"rootDir": "./src",
|
|
6
|
-
"module": "commonjs",
|
|
7
|
-
"target": "es2020",
|
|
8
|
-
"moduleResolution": "node",
|
|
9
|
-
"lib": ["es2020"],
|
|
10
|
-
"declaration": true,
|
|
11
|
-
|
|
12
|
-
"esModuleInterop": true,
|
|
13
|
-
"skipLibCheck": true,
|
|
14
|
-
"forceConsistentCasingInFileNames": true
|
|
15
|
-
}
|
|
16
|
-
}
|