wrapper-fns 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Uladzimir Kasacheuski
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.
@@ -0,0 +1,3 @@
1
+ export { withBottleneck } from '../logic/withBottleneck';
2
+ export { withRetry } from '../logic/withRetry';
3
+ export { withTimeout } from '../logic/withTimeout';
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimeout = exports.withRetry = exports.withBottleneck = void 0;
4
+ var withBottleneck_1 = require("../logic/withBottleneck");
5
+ Object.defineProperty(exports, "withBottleneck", { enumerable: true, get: function () { return withBottleneck_1.withBottleneck; } });
6
+ var withRetry_1 = require("../logic/withRetry");
7
+ Object.defineProperty(exports, "withRetry", { enumerable: true, get: function () { return withRetry_1.withRetry; } });
8
+ var withTimeout_1 = require("../logic/withTimeout");
9
+ Object.defineProperty(exports, "withTimeout", { enumerable: true, get: function () { return withTimeout_1.withTimeout; } });
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/contract/index.ts"],"names":[],"mappings":";;;AAAA,0DAAyD;AAAhD,gHAAA,cAAc,OAAA;AACvB,gDAA+C;AAAtC,sGAAA,SAAS,OAAA;AAClB,oDAAmD;AAA1C,0GAAA,WAAW,OAAA"}
File without changes
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import Bottleneck from 'bottleneck';
2
+ export declare const withBottleneck: <I extends any[], O extends Promise<any>, T extends (...args: I) => O>(logic: T, options?: {
3
+ bottleneck: Bottleneck;
4
+ }) => T;
@@ -0,0 +1,14 @@
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
+ exports.withBottleneck = void 0;
7
+ const bottleneck_1 = __importDefault(require("bottleneck"));
8
+ const machineWideDefaultBottleneck = new bottleneck_1.default({ maxConcurrent: 1 });
9
+ const withBottleneck = (logic, options = { bottleneck: machineWideDefaultBottleneck }) => {
10
+ const wrapped = (...args) => options.bottleneck.schedule(() => logic(...args));
11
+ return wrapped;
12
+ };
13
+ exports.withBottleneck = withBottleneck;
14
+ //# sourceMappingURL=withBottleneck.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withBottleneck.js","sourceRoot":"","sources":["../../src/logic/withBottleneck.ts"],"names":[],"mappings":";;;;;;AAAA,4DAAoC;AAEpC,MAAM,4BAA4B,GAAG,IAAI,oBAAU,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,CAAC,CAAC;AAEnE,MAAM,cAAc,GAAG,CAK5B,KAAQ,EACR,UAEI,EAAE,UAAU,EAAE,4BAA4B,EAAE,EAC7C,EAAE;IACL,MAAM,OAAO,GAAG,CAAC,GAAG,IAAO,EAAE,EAAE,CAC7B,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAI,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;IACvD,OAAO,OAAY,CAAC;AACtB,CAAC,CAAC;AAbW,QAAA,cAAc,kBAazB"}
@@ -0,0 +1,4 @@
1
+ import { Procedure, VisualogicContext } from 'visualogic';
2
+ export declare function withRetry<TInput, TContext extends VisualogicContext, TOutput>(logic: () => TOutput): typeof logic;
3
+ export declare function withRetry<TInput, TContext extends VisualogicContext, TOutput>(logic: (input: TInput) => TOutput): typeof logic;
4
+ export declare function withRetry<TInput, TContext extends VisualogicContext, TOutput>(logic: Procedure<TInput, TContext, TOutput>): typeof logic;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withRetry = void 0;
4
+ /**
5
+ * function which calls the wrapped function and runs it again one time if an error is caught
6
+ */
7
+ function withRetry(logic) {
8
+ return (async (input, context) => {
9
+ try {
10
+ return await logic(input, context);
11
+ }
12
+ catch (error) {
13
+ if (!(error instanceof Error))
14
+ throw error;
15
+ context.log.warn('withRetry.progress: caught an error, will retry', {
16
+ error: {
17
+ message: error.message,
18
+ stack: error.stack,
19
+ },
20
+ });
21
+ return await logic(input, context);
22
+ }
23
+ });
24
+ }
25
+ exports.withRetry = withRetry;
26
+ //# sourceMappingURL=withRetry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withRetry.js","sourceRoot":"","sources":["../../src/logic/withRetry.ts"],"names":[],"mappings":";;;AAkBA;;GAEG;AACH,SAAgB,SAAS,CACvB,KAA2C;IAE3C,OAAO,CAAC,KAAK,EACX,KAAmC,EACnC,OAAuC,EACC,EAAE;QAC1C,IAAI,CAAC;YACH,OAAO,MAAM,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC;gBAAE,MAAM,KAAK,CAAC;YAC3C,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,iDAAiD,EAAE;gBAClE,KAAK,EAAE;oBACL,OAAO,EAAE,KAAK,CAAC,OAAO;oBACtB,KAAK,EAAE,KAAK,CAAC,KAAK;iBACnB;aACF,CAAC,CAAC;YACH,OAAO,MAAM,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACrC,CAAC;IACH,CAAC,CAAiB,CAAC;AACrB,CAAC;AApBD,8BAoBC"}
@@ -0,0 +1,9 @@
1
+ import { UniDuration } from '@ehmpathy/uni-time';
2
+ import { Procedure } from 'visualogic';
3
+ export declare function withTimeout<TInput, TContext, TOutput>(logic: () => TOutput, // empty inputs override
4
+ options: {
5
+ threshold: UniDuration;
6
+ }): typeof logic;
7
+ export declare function withTimeout<TInput, TContext, TOutput>(logic: Procedure<TInput, TContext, TOutput>, options: {
8
+ threshold: UniDuration;
9
+ }): typeof logic;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withTimeout = void 0;
4
+ const uni_time_1 = require("@ehmpathy/uni-time");
5
+ /**
6
+ * returns a new function which calls the input function and "races" the result against a promise that throws an error on timeout.
7
+ *
8
+ * the result is:
9
+ * - if your async fn takes longer than timeout ms, then an error will be thrown
10
+ * - if your async fn executes faster than timeout ms, you'll get the normal response of the fn
11
+ *
12
+ * ### usage
13
+ * ```ts
14
+ * const result = await withTimeout(() => doSomethingAsync(...args), 3000);
15
+ * ```
16
+ * or
17
+ * ```ts
18
+ * const result = await withTimeout(doSomethingAsync, 3000)(...args);
19
+ * ```
20
+ * or even
21
+ * ```ts
22
+ * const doSomethingAsyncWithTimeout = withTimeout(doSomethingAsync, 3000);
23
+ * const result = await doSomethingAsyncWithTimeout(...args);
24
+ * ```
25
+ */
26
+ function withTimeout(logic, options) {
27
+ const thresholdMs = (0, uni_time_1.toMilliseconds)(options.threshold);
28
+ return (input, context) => {
29
+ // create a promise that rejects in <ms> milliseconds; https://italonascimento.github.io/applying-a-timeout-to-your-promises/
30
+ const timeout = new Promise((resolve, reject) => {
31
+ const id = setTimeout(() => {
32
+ clearTimeout(id);
33
+ reject(new Error(`promise was timed out in ${thresholdMs} ms, by withTimeout`));
34
+ }, thresholdMs); // tslint:disable-line align
35
+ });
36
+ // returns a "race" between our timeout and the function executed with the input params
37
+ return Promise.race([
38
+ logic(input, context), // the wrapped fn, executed w/ the input params
39
+ timeout, // the timeout
40
+ ]);
41
+ };
42
+ }
43
+ exports.withTimeout = withTimeout;
44
+ //# sourceMappingURL=withTimeout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withTimeout.js","sourceRoot":"","sources":["../../src/logic/withTimeout.ts"],"names":[],"mappings":";;;AAAA,iDAAiE;AAiBjE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,SAAgB,WAAW,CACzB,KAA2C,EAC3C,OAAmC;IAEnC,MAAM,WAAW,GAAG,IAAA,yBAAc,EAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACtD,OAAO,CACL,KAAmC,EACnC,OAAuC,EACR,EAAE;QACjC,6HAA6H;QAC7H,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC9C,MAAM,EAAE,GAAG,UAAU,CAAC,GAAG,EAAE;gBACzB,YAAY,CAAC,EAAE,CAAC,CAAC;gBACjB,MAAM,CACJ,IAAI,KAAK,CACP,4BAA4B,WAAW,qBAAqB,CAC7D,CACF,CAAC;YACJ,CAAC,EAAE,WAAW,CAAC,CAAC,CAAC,4BAA4B;QAC/C,CAAC,CAAC,CAAC;QAEH,uFAAuF;QACvF,OAAO,OAAO,CAAC,IAAI,CAAC;YAClB,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,+CAA+C;YACtE,OAAO,EAAE,cAAc;SACxB,CAAkC,CAAC;IACtC,CAAC,CAAC;AACJ,CAAC;AA3BD,kCA2BC"}
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "wrapper-fns",
3
+ "author": "ehmpathy",
4
+ "description": "Use wrappers for simpler, safer, and more readable code",
5
+ "version": "1.0.0",
6
+ "repository": "ehmpathy/wrapper-fns",
7
+ "homepage": "https://github.com/ehmpathy/wrapper-fns",
8
+ "keywords": [
9
+ "wrapper",
10
+ "retry",
11
+ "bottleneck",
12
+ "timeout"
13
+ ],
14
+ "bugs": "https://github.com/ehmpathy/wrapper-fns/issues",
15
+ "license": "MIT",
16
+ "main": "dist/index.js",
17
+ "engines": {
18
+ "node": ">=8.0.0"
19
+ },
20
+ "files": [
21
+ "/dist"
22
+ ],
23
+ "scripts": {
24
+ "build:ts": "tsc -p ./tsconfig.build.json",
25
+ "commit:with-cli": "npx cz",
26
+ "fix:format:prettier": "prettier --write '**/*.ts' --config ./prettier.config.js",
27
+ "fix:format": "npm run fix:format:prettier",
28
+ "fix:lint": "eslint -c ./.eslintrc.js src/**/*.ts --fix",
29
+ "build:clean": "rm dist/ -rf",
30
+ "build:compile": "tsc -p ./tsconfig.build.json",
31
+ "build": "npm run build:clean && npm run build:compile",
32
+ "test:commits": "LAST_TAG=$(git describe --tags --abbrev=0 @^ 2> /dev/null || git rev-list --max-parents=0 HEAD) && npx commitlint --from $LAST_TAG --to HEAD --verbose",
33
+ "test:types": "tsc -p ./tsconfig.build.json --noEmit",
34
+ "test:format:prettier": "prettier --parser typescript --check 'src/**/*.ts' --config ./prettier.config.js",
35
+ "test:format": "npm run test:format:prettier",
36
+ "test:lint:deps": "npx depcheck -c ./depcheckrc.yml",
37
+ "test:lint:eslint": "eslint -c ./.eslintrc.js src/**/*.ts",
38
+ "test:lint": "npm run test:lint:eslint && npm run test:lint:deps",
39
+ "test:unit": "jest -c ./jest.unit.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main')",
40
+ "test:integration": "jest -c ./jest.integration.config.ts --forceExit --verbose --passWithNoTests $([ -z $THOROUGH ] && echo '--changedSince=main')",
41
+ "test:acceptance:locally": "npm run build && LOCALLY=true jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
42
+ "test": "npm run test:commits && npm run test:types && npm run test:format && npm run test:lint && npm run test:unit && npm run test:integration && npm run test:acceptance:locally",
43
+ "test:acceptance": "npm run build && jest -c ./jest.acceptance.config.ts --forceExit --verbose --runInBand --passWithNoTests",
44
+ "prepush": "npm run test && npm run build",
45
+ "prepublish": "npm run build",
46
+ "preversion": "npm run prepush",
47
+ "postversion": "git push origin HEAD --tags --no-verify",
48
+ "postinstall": "[ -d .git ] && npm run prepare:husky || exit 0",
49
+ "prepare:husky": "npx husky install && chmod ug+x .husky/*"
50
+ },
51
+ "dependencies": {
52
+ "@ehmpathy/uni-time": "1.7.4",
53
+ "bottleneck": "2.19.5",
54
+ "visualogic": "1.3.2"
55
+ },
56
+ "devDependencies": {
57
+ "@commitlint/cli": "19.3.0",
58
+ "@commitlint/config-conventional": "13.1.0",
59
+ "@trivago/prettier-plugin-sort-imports": "4.3.0",
60
+ "@tsconfig/node-lts-strictest": "18.12.1",
61
+ "@types/jest": "29.2.4",
62
+ "@typescript-eslint/eslint-plugin": "7.8.0",
63
+ "@typescript-eslint/parser": "7.8.0",
64
+ "core-js": "3.26.1",
65
+ "cz-conventional-changelog": "3.3.0",
66
+ "declapract": "0.12.0",
67
+ "declapract-typescript-ehmpathy": "0.39.5",
68
+ "depcheck": "1.4.3",
69
+ "eslint": "8.56.0",
70
+ "eslint-config-airbnb-typescript": "18.0.0",
71
+ "eslint-config-prettier": "8.5.0",
72
+ "eslint-plugin-import": "2.26.0",
73
+ "eslint-plugin-prettier": "4.2.1",
74
+ "husky": "8.0.3",
75
+ "jest": "29.3.1",
76
+ "prettier": "2.8.1",
77
+ "ts-jest": "29.1.3",
78
+ "ts-node": "10.9.2",
79
+ "typescript": "5.4.5"
80
+ },
81
+ "config": {
82
+ "commitizen": {
83
+ "path": "./node_modules/cz-conventional-changelog"
84
+ }
85
+ }
86
+ }
package/readme.md ADDED
@@ -0,0 +1,58 @@
1
+ # wrapper-fns
2
+
3
+ ![test](https://github.com/ehmpathy/wrapper-fns/workflows/test/badge.svg)
4
+ ![publish](https://github.com/ehmpathy/wrapper-fns/workflows/publish/badge.svg)
5
+
6
+ Use wrappers for simpler, safer, and more readable code.
7
+
8
+ Wrap your procedures with before and after effects to abstract away common lifecycle tasks. This library makes it simple to use wrappers and includes a standard set of primitive wrappers.
9
+
10
+
11
+ # install
12
+
13
+ ```sh
14
+ npm install wrapper-fns
15
+ ```
16
+
17
+ # use
18
+
19
+ ### `withRetry`
20
+
21
+ retries the wrapped logic execution in case of failure
22
+
23
+ for example
24
+ ```ts
25
+ const result = await withRetry(doSomethingFlakey)()
26
+ ```
27
+
28
+
29
+ ### `withBottleneck`
30
+
31
+ executes the wrapped logic execution within a bottleneck
32
+
33
+ for example
34
+ ```ts
35
+ // use the global bottleneck by default
36
+ const result = await withBottleneck(doSomethingRatelimited)()
37
+ ```
38
+ or
39
+ ```ts
40
+ // use a dedicated bottleneck instead
41
+ const options = { bottleneck: new Bottleneck({ maxConcurrent: 3 }) }
42
+ const result = await withBottleneck(doSomethingRatelimited, options)()
43
+ ```
44
+
45
+ ### `withTimeout`
46
+
47
+ throws a timeout error if the wrapped logic takes longer than the threshold
48
+
49
+ - if your async fn takes longer than timeout ms, then an error will be thrown
50
+ - if your async fn executes faster than timeout ms, you'll get the normal response of the fn
51
+
52
+
53
+ for example
54
+ or
55
+ ```ts
56
+ const options = { threshold: { seconds: 3 } }
57
+ const result = await withTimeout(doSomethingAsync, options)(...args);
58
+ ```