wrapper-fns 1.0.0 → 1.1.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.
@@ -1,3 +1,5 @@
1
1
  export { withBottleneck } from '../logic/withBottleneck';
2
2
  export { withRetry } from '../logic/withRetry';
3
3
  export { withTimeout } from '../logic/withTimeout';
4
+ export * from '../domain/objects/Wrapper';
5
+ export * from '../logic/withWrappers';
@@ -1,4 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
17
  exports.withTimeout = exports.withRetry = exports.withBottleneck = void 0;
4
18
  var withBottleneck_1 = require("../logic/withBottleneck");
@@ -7,4 +21,6 @@ var withRetry_1 = require("../logic/withRetry");
7
21
  Object.defineProperty(exports, "withRetry", { enumerable: true, get: function () { return withRetry_1.withRetry; } });
8
22
  var withTimeout_1 = require("../logic/withTimeout");
9
23
  Object.defineProperty(exports, "withTimeout", { enumerable: true, get: function () { return withTimeout_1.withTimeout; } });
24
+ __exportStar(require("../domain/objects/Wrapper"), exports);
25
+ __exportStar(require("../logic/withWrappers"), exports);
10
26
  //# sourceMappingURL=index.js.map
@@ -1 +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"}
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;AAEpB,4DAA0C;AAC1C,wDAAsC"}
@@ -0,0 +1,6 @@
1
+ import { Procedure } from 'domain-glossary-procedure';
2
+ export type Wrapper<TProcedure extends Procedure, TOptions> = (logic: TProcedure, options: TOptions) => TProcedure;
3
+ export type WrapperChoice<TProcedure extends Procedure, TOptions> = {
4
+ wrapper: Wrapper<TProcedure, TOptions>;
5
+ options: TOptions;
6
+ };
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Wrapper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Wrapper.js","sourceRoot":"","sources":["../../../src/domain/objects/Wrapper.ts"],"names":[],"mappings":""}
package/dist/index.d.ts CHANGED
@@ -0,0 +1 @@
1
+ export * from './contract/index';
package/dist/index.js CHANGED
@@ -1,2 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./contract/index"), exports);
2
18
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,mDAAiC"}
@@ -0,0 +1,16 @@
1
+ import { Procedure } from 'domain-glossary-procedure';
2
+ import { WrapperChoice } from '../domain/objects/Wrapper';
3
+ /**
4
+ * .what = a helper procedure to ensure that the choice.options matches the wrapper.options
5
+ */
6
+ export declare const setWrapper: <TProc extends Procedure, TOptions>(input: WrapperChoice<TProc, TOptions>) => WrapperChoice<TProc, TOptions> & {
7
+ _safe: true;
8
+ };
9
+ /**
10
+ * .what = a wrapper of wrappers
11
+ * .why =
12
+ * - eliminates nested indentation when multiple wrappers are used
13
+ */
14
+ export declare function withWrappers<TProcedure extends Procedure, TChoices extends ReadonlyArray<WrapperChoice<TProcedure, any> & {
15
+ _safe: true;
16
+ }>>(logic: TProcedure, wrappers: TChoices): TProcedure;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withWrappers = exports.setWrapper = void 0;
4
+ /**
5
+ * .what = a helper procedure to ensure that the choice.options matches the wrapper.options
6
+ */
7
+ const setWrapper = (input) => {
8
+ return Object.assign(input, { _safe: true }); // add the safe flag to compile-time warn if setter was not used; // todo: use a symbol instead
9
+ };
10
+ exports.setWrapper = setWrapper;
11
+ /**
12
+ * .what = a wrapper of wrappers
13
+ * .why =
14
+ * - eliminates nested indentation when multiple wrappers are used
15
+ */
16
+ function withWrappers(logic, wrappers) {
17
+ return wrappers.reduce((wrapped, { wrapper, options }) => wrapper(wrapped, options), logic);
18
+ }
19
+ exports.withWrappers = withWrappers;
20
+ //# sourceMappingURL=withWrappers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withWrappers.js","sourceRoot":"","sources":["../../src/logic/withWrappers.ts"],"names":[],"mappings":";;;AAIA;;GAEG;AACI,MAAM,UAAU,GAAG,CACxB,KAAqC,EACa,EAAE;IACpD,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,IAAa,EAAE,CAAC,CAAC,CAAC,+FAA+F;AACxJ,CAAC,CAAC;AAJW,QAAA,UAAU,cAIrB;AAEF;;;;GAIG;AACH,SAAgB,YAAY,CAK1B,KAAiB,EAAE,QAAkB;IACrC,OAAO,QAAQ,CAAC,MAAM,CACpB,CAAC,OAAO,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,EAC5D,KAAK,CACN,CAAC;AACJ,CAAC;AAVD,oCAUC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const test_fns_1 = require("test-fns");
4
+ const withWrappers_1 = require("./withWrappers");
5
+ describe('withWrappers', () => {
6
+ (0, test_fns_1.given)('a simple procedure to wrap', () => {
7
+ const withTimeoutCallInputs = [];
8
+ const withTimeout = (logic, { threshold }) => {
9
+ return async (input, context) => {
10
+ withTimeoutCallInputs.push(input);
11
+ const start = Date.now();
12
+ const result = await logic(input, context);
13
+ const duration = Date.now() - start;
14
+ if (duration > threshold)
15
+ console.warn(`Took too long: ${duration}ms`);
16
+ return result;
17
+ };
18
+ };
19
+ const withLogTrailCallInputs = [];
20
+ const withLogTrail = (logic, { name }) => {
21
+ return async (input, context) => {
22
+ withLogTrailCallInputs.push(input);
23
+ console.log(`[${name}] Input:`, input);
24
+ const result = await logic(input, context);
25
+ console.log(`[${name}] Output:`, result);
26
+ return result;
27
+ };
28
+ };
29
+ const logic = async (input, context) => {
30
+ return `Processed ${input.payload} for ${context.userUuid}`;
31
+ };
32
+ (0, test_fns_1.then)('it should successfully execute the wrappers', async () => {
33
+ const wrapped = (0, withWrappers_1.withWrappers)(logic, [
34
+ (0, withWrappers_1.setWrapper)({
35
+ wrapper: withLogTrail,
36
+ options: { name: 'wrapped-procedure' },
37
+ }),
38
+ (0, withWrappers_1.setWrapper)({
39
+ wrapper: withTimeout,
40
+ options: { threshold: 100 },
41
+ }),
42
+ ]);
43
+ await wrapped({ payload: 'yellow' }, { userUuid: 'beefbeef...' });
44
+ expect(withLogTrailCallInputs.length).toEqual(1);
45
+ expect(withTimeoutCallInputs.length).toEqual(1);
46
+ await wrapped({ payload: 'blue' }, { userUuid: 'beefbeef...' });
47
+ expect(withLogTrailCallInputs.length).toEqual(2);
48
+ expect(withTimeoutCallInputs.length).toEqual(2);
49
+ });
50
+ (0, test_fns_1.then)('it should typethrow if setWrapper was not used', {
51
+ because: 'this guarantees the options are typechecked',
52
+ }, () => {
53
+ (0, withWrappers_1.withWrappers)(logic, [
54
+ // @ts-expect-error: Property '_safe' is missing in type '{ wrapper: Wrapper<Procedure, { name: string; }>; options: { name: string; }; }' but required in type '{ _safe: true; }'.
55
+ {
56
+ wrapper: withLogTrail,
57
+ options: { name: 'wrapped-procedure' },
58
+ },
59
+ ]);
60
+ });
61
+ });
62
+ });
63
+ //# sourceMappingURL=withWrappers.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withWrappers.test.js","sourceRoot":"","sources":["../../src/logic/withWrappers.test.ts"],"names":[],"mappings":";;AAAA,uCAAuC;AAIvC,iDAA0D;AAE1D,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,IAAA,gBAAK,EAAC,4BAA4B,EAAE,GAAG,EAAE;QACvC,MAAM,qBAAqB,GAAU,EAAE,CAAC;QACxC,MAAM,WAAW,GAA8C,CAC7D,KAAK,EACL,EAAE,SAAS,EAAE,EACb,E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K,EAAE;gBAClC,IAAA,yBAAU,EAAC;oBACT,OAAO,EAAE,YAAY;oBACrB,OAAO,EAAE,EAAE,IAAI,EAAE,mBAAmB,EAAE;iBACvC,CAAC;gBACF,IAAA,yBAAU,EAAC;oBACT,OAAO,EAAE,WAAW;oBACpB,OAAO,EAAE,EAAE,SAAS,EAAE,GAAG,EAAE;iBAC5B,CAAC;aACH,CAAC,CAAC;YAEH,MAAM,OAAO,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAC;YAClE,MAAM,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACjD,MAAM,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAEhD,MAAM,OAAO,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,QAAQ,EAAE,aAAa,EAAE,CAAC,CAAC;YAChE,MAAM,CAAC,sBAAsB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YACjD,MAAM,CAAC,qBAAqB,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAClD,CAAC,CAAC,CAAC;QAEH,IAAA,eAAI,EACF,gDAAgD,EAChD;YACE,OAAO,EAAE,6CAA6C;SACvD,EACD,GAAG,EAAE;YACH,IAAA,2BAAY,EAAC,KAAK,EAAE;gBAClB,mLAAmL;gBACnL;oBACE,OAAO,EAAE,YAAY;oBACrB,OAAO,EAAE,EAAE,IAAI,EAAE,mBAAmB,EAAE;iBACvC;aACF,CAAC,CAAC;QACL,CAAC,CACF,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrapper-fns",
3
3
  "author": "ehmpathy",
4
4
  "description": "Use wrappers for simpler, safer, and more readable code",
5
- "version": "1.0.0",
5
+ "version": "1.1.0",
6
6
  "repository": "ehmpathy/wrapper-fns",
7
7
  "homepage": "https://github.com/ehmpathy/wrapper-fns",
8
8
  "keywords": [
@@ -51,6 +51,7 @@
51
51
  "dependencies": {
52
52
  "@ehmpathy/uni-time": "1.7.4",
53
53
  "bottleneck": "2.19.5",
54
+ "domain-glossary-procedure": "1.0.0",
54
55
  "visualogic": "1.3.2"
55
56
  },
56
57
  "devDependencies": {
@@ -74,6 +75,7 @@
74
75
  "husky": "8.0.3",
75
76
  "jest": "29.3.1",
76
77
  "prettier": "2.8.1",
78
+ "test-fns": "1.5.0",
77
79
  "ts-jest": "29.1.3",
78
80
  "ts-node": "10.9.2",
79
81
  "typescript": "5.4.5"
package/readme.md CHANGED
@@ -16,6 +16,26 @@ npm install wrapper-fns
16
16
 
17
17
  # use
18
18
 
19
+ ### `withWrappers`
20
+
21
+ applies wrappers chosen in a list, to avoid nested indentations
22
+
23
+ ```ts
24
+ const wrapped = withWrappers(doGreatThing, [
25
+ setWrapper({
26
+ wrapper: withLogTrail,
27
+ options: { name: 'doGreatThing' },
28
+ }),
29
+ setWrapper({
30
+ wrapper: withTimeout,
31
+ options: { threshold: 100 },
32
+ }),
33
+ ]);
34
+ ```
35
+
36
+ *note, we use `setWrapper` to allow typescript to enforce the relationship between `options` and the `wrapper`*
37
+
38
+
19
39
  ### `withRetry`
20
40
 
21
41
  retries the wrapped logic execution in case of failure