@lokalise/node-core 6.1.0 → 6.2.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/README.md CHANGED
@@ -201,3 +201,22 @@ It's up to the caller of the function to handle the received error or throw an e
201
201
  Read [this article](https://antman-does-software.com/stop-catching-errors-in-typescript-use-the-either-type-to-make-your-code-predictable) for more information on how `Either` works and its benefits.
202
202
 
203
203
  Additionally, `DefineEither` is also provided. It is a variation of the aforementioned `Either`, which may or may not have `error` set, but always has `result`.
204
+
205
+ ### waitAndRetry
206
+
207
+ There is helper function available for writing event-driven assertions in automated tests, which rely on something eventually happening:
208
+
209
+ ```ts
210
+ import {waitAndRetry} from "@lokalise/node-core";
211
+
212
+ const result = await waitAndRetry(
213
+ () => {
214
+ return someEventEmitter.emittedEvents.length > 0
215
+ },
216
+ 20, // sleepTime between attempts
217
+ 30, // maxRetryCount before timeout
218
+ )
219
+
220
+ expect(result).toBe(false) // resolves to what the last attempt has returned
221
+ expect(someEventEmitter.emittedEvents.length).toBe(1)
222
+ ```
package/dist/index.d.ts CHANGED
@@ -19,3 +19,4 @@ export type { AppLoggerConfig } from './src/logging/loggerConfigResolver';
19
19
  export type { ErrorReport, ErrorReporter, ErrorResolver } from './src/errors/errorReporterTypes';
20
20
  export { executeAsyncAndHandleGlobalErrors, executeAndHandleGlobalErrors, globalLogger, resolveGlobalErrorLogObject, } from './src/errors/globalErrorHandler';
21
21
  export type { MayOmit } from './src/common/may-omit';
22
+ export { waitAndRetry } from './src/utils/waitUtils';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveGlobalErrorLogObject = exports.globalLogger = exports.executeAndHandleGlobalErrors = exports.executeAsyncAndHandleGlobalErrors = exports.resolveLoggerConfiguration = exports.hasMessage = exports.isPublicNonRecoverableError = exports.isObject = exports.isStandardizedError = exports.isInternalError = exports.isEmptyObject = exports.copyWithoutUndefined = exports.pickWithoutUndefined = exports.pick = exports.groupBy = exports.callChunked = exports.chunk = exports.createRangeValidator = exports.ensureClosingSlashTransformer = exports.ConfigScope = exports.ResponseStatusError = exports.InternalError = exports.PublicNonRecoverableError = exports.buildClient = exports.httpClient = exports.sendPost = exports.sendGet = exports.sendPatch = exports.sendDelete = exports.sendPutBinary = exports.sendPut = void 0;
3
+ exports.waitAndRetry = exports.resolveGlobalErrorLogObject = exports.globalLogger = exports.executeAndHandleGlobalErrors = exports.executeAsyncAndHandleGlobalErrors = exports.resolveLoggerConfiguration = exports.hasMessage = exports.isPublicNonRecoverableError = exports.isObject = exports.isStandardizedError = exports.isInternalError = exports.isEmptyObject = exports.copyWithoutUndefined = exports.pickWithoutUndefined = exports.pick = exports.groupBy = exports.callChunked = exports.chunk = exports.createRangeValidator = exports.ensureClosingSlashTransformer = exports.ConfigScope = exports.ResponseStatusError = exports.InternalError = exports.PublicNonRecoverableError = exports.buildClient = exports.httpClient = exports.sendPost = exports.sendGet = exports.sendPatch = exports.sendDelete = exports.sendPutBinary = exports.sendPut = void 0;
4
4
  var httpClient_1 = require("./src/http/httpClient");
5
5
  Object.defineProperty(exports, "sendPut", { enumerable: true, get: function () { return httpClient_1.sendPut; } });
6
6
  Object.defineProperty(exports, "sendPutBinary", { enumerable: true, get: function () { return httpClient_1.sendPutBinary; } });
@@ -44,4 +44,6 @@ Object.defineProperty(exports, "executeAsyncAndHandleGlobalErrors", { enumerable
44
44
  Object.defineProperty(exports, "executeAndHandleGlobalErrors", { enumerable: true, get: function () { return globalErrorHandler_1.executeAndHandleGlobalErrors; } });
45
45
  Object.defineProperty(exports, "globalLogger", { enumerable: true, get: function () { return globalErrorHandler_1.globalLogger; } });
46
46
  Object.defineProperty(exports, "resolveGlobalErrorLogObject", { enumerable: true, get: function () { return globalErrorHandler_1.resolveGlobalErrorLogObject; } });
47
+ var waitUtils_1 = require("./src/utils/waitUtils");
48
+ Object.defineProperty(exports, "waitAndRetry", { enumerable: true, get: function () { return waitUtils_1.waitAndRetry; } });
47
49
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,oDAS8B;AAR5B,qGAAA,OAAO,OAAA;AACP,2GAAA,aAAa,OAAA;AACb,wGAAA,UAAU,OAAA;AACV,uGAAA,SAAS,OAAA;AACT,qGAAA,OAAO,OAAA;AACP,sGAAA,QAAQ,OAAA;AACR,wGAAA,UAAU,OAAA;AACV,yGAAA,WAAW,OAAA;AASb,oFAAkF;AAAzE,sIAAA,yBAAyB,OAAA;AAGlC,4DAA0D;AAAjD,8GAAA,aAAa,OAAA;AACtB,wEAAsE;AAA7D,0HAAA,mBAAmB,OAAA;AAG5B,wDAAsD;AAA7C,0GAAA,WAAW,OAAA;AACpB,sEAA+E;AAAtE,mIAAA,6BAA6B,OAAA;AACtC,kEAAoE;AAA3D,wHAAA,oBAAoB,OAAA;AAU7B,qDAA2D;AAAlD,mGAAA,KAAK,OAAA;AAAE,yGAAA,WAAW,OAAA;AAC3B,uDAMgC;AAL9B,sGAAA,OAAO,OAAA;AACP,mGAAA,IAAI,OAAA;AACJ,mHAAA,oBAAoB,OAAA;AACpB,mHAAA,oBAAoB,OAAA;AACpB,4GAAA,aAAa,OAAA;AAGf,mDAM8B;AAL5B,4GAAA,eAAe,OAAA;AACf,gHAAA,mBAAmB,OAAA;AACnB,qGAAA,QAAQ,OAAA;AACR,wHAAA,2BAA2B,OAAA;AAC3B,uGAAA,UAAU,OAAA;AAIZ,2EAA+E;AAAtE,kIAAA,0BAA0B,OAAA;AAInC,sEAKwC;AAJtC,uIAAA,iCAAiC,OAAA;AACjC,kIAAA,4BAA4B,OAAA;AAC5B,kHAAA,YAAY,OAAA;AACZ,iIAAA,2BAA2B,OAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,oDAS8B;AAR5B,qGAAA,OAAO,OAAA;AACP,2GAAA,aAAa,OAAA;AACb,wGAAA,UAAU,OAAA;AACV,uGAAA,SAAS,OAAA;AACT,qGAAA,OAAO,OAAA;AACP,sGAAA,QAAQ,OAAA;AACR,wGAAA,UAAU,OAAA;AACV,yGAAA,WAAW,OAAA;AASb,oFAAkF;AAAzE,sIAAA,yBAAyB,OAAA;AAGlC,4DAA0D;AAAjD,8GAAA,aAAa,OAAA;AACtB,wEAAsE;AAA7D,0HAAA,mBAAmB,OAAA;AAG5B,wDAAsD;AAA7C,0GAAA,WAAW,OAAA;AACpB,sEAA+E;AAAtE,mIAAA,6BAA6B,OAAA;AACtC,kEAAoE;AAA3D,wHAAA,oBAAoB,OAAA;AAU7B,qDAA2D;AAAlD,mGAAA,KAAK,OAAA;AAAE,yGAAA,WAAW,OAAA;AAC3B,uDAMgC;AAL9B,sGAAA,OAAO,OAAA;AACP,mGAAA,IAAI,OAAA;AACJ,mHAAA,oBAAoB,OAAA;AACpB,mHAAA,oBAAoB,OAAA;AACpB,4GAAA,aAAa,OAAA;AAGf,mDAM8B;AAL5B,4GAAA,eAAe,OAAA;AACf,gHAAA,mBAAmB,OAAA;AACnB,qGAAA,QAAQ,OAAA;AACR,wHAAA,2BAA2B,OAAA;AAC3B,uGAAA,UAAU,OAAA;AAIZ,2EAA+E;AAAtE,kIAAA,0BAA0B,OAAA;AAInC,sEAKwC;AAJtC,uIAAA,iCAAiC,OAAA;AACjC,kIAAA,4BAA4B,OAAA;AAC5B,kHAAA,YAAY,OAAA;AACZ,iIAAA,2BAA2B,OAAA;AAK7B,mDAAoD;AAA3C,yGAAA,YAAY,OAAA"}
@@ -0,0 +1 @@
1
+ export declare const waitAndRetry: <T>(predicateFn: () => T, sleepTime?: number, maxRetryCount?: number) => Promise<T>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.waitAndRetry = void 0;
4
+ const waitAndRetry = async (predicateFn, sleepTime = 20, maxRetryCount = 15) => {
5
+ return new Promise((resolve, reject) => {
6
+ let retryCount = 0;
7
+ function performCheck() {
8
+ // amount of retries exceeded
9
+ if (maxRetryCount !== 0 && retryCount > maxRetryCount) {
10
+ resolve(predicateFn());
11
+ }
12
+ // Try executing predicateFn
13
+ Promise.resolve()
14
+ .then(() => {
15
+ return predicateFn();
16
+ })
17
+ .then((result) => {
18
+ if (result) {
19
+ resolve(result);
20
+ }
21
+ else {
22
+ retryCount++;
23
+ setTimeout(performCheck, sleepTime);
24
+ }
25
+ })
26
+ .catch((err) => {
27
+ reject(err);
28
+ });
29
+ }
30
+ performCheck();
31
+ });
32
+ };
33
+ exports.waitAndRetry = waitAndRetry;
34
+ //# sourceMappingURL=waitUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitUtils.js","sourceRoot":"","sources":["../../../src/utils/waitUtils.ts"],"names":[],"mappings":";;;AAAO,MAAM,YAAY,GAAG,KAAK,EAC/B,WAAoB,EACpB,SAAS,GAAG,EAAE,EACd,aAAa,GAAG,EAAE,EACN,EAAE;IACd,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,UAAU,GAAG,CAAC,CAAA;QAClB,SAAS,YAAY;YACnB,6BAA6B;YAC7B,IAAI,aAAa,KAAK,CAAC,IAAI,UAAU,GAAG,aAAa,EAAE;gBACrD,OAAO,CAAC,WAAW,EAAE,CAAC,CAAA;aACvB;YAED,4BAA4B;YAC5B,OAAO,CAAC,OAAO,EAAE;iBACd,IAAI,CAAC,GAAG,EAAE;gBACT,OAAO,WAAW,EAAE,CAAA;YACtB,CAAC,CAAC;iBACD,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;gBACf,IAAI,MAAM,EAAE;oBACV,OAAO,CAAC,MAAM,CAAC,CAAA;iBAChB;qBAAM;oBACL,UAAU,EAAE,CAAA;oBACZ,UAAU,CAAC,YAAY,EAAE,SAAS,CAAC,CAAA;iBACpC;YACH,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;gBACb,MAAM,CAAC,GAAG,CAAC,CAAA;YACb,CAAC,CAAC,CAAA;QACN,CAAC;QAED,YAAY,EAAE,CAAA;IAChB,CAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAjCY,QAAA,YAAY,gBAiCxB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const waitUtils_1 = require("./waitUtils");
5
+ class Counter {
6
+ timeOfSucces;
7
+ executionCounter;
8
+ constructor(msecsTillSuccess) {
9
+ this.timeOfSucces = Date.now() + msecsTillSuccess;
10
+ this.executionCounter = 0;
11
+ }
12
+ process() {
13
+ this.executionCounter++;
14
+ return Date.now() >= this.timeOfSucces;
15
+ }
16
+ }
17
+ (0, vitest_1.describe)('waitUtils', () => {
18
+ (0, vitest_1.describe)('waitAndRetry', () => {
19
+ it('executes once if there is an instant condition match', async () => {
20
+ const counter = new Counter(0);
21
+ const result = await (0, waitUtils_1.waitAndRetry)(() => {
22
+ return counter.process();
23
+ });
24
+ (0, vitest_1.expect)(result).toBe(true);
25
+ (0, vitest_1.expect)(counter.executionCounter).toBe(1);
26
+ });
27
+ it('executes until there is a condition match', async () => {
28
+ const counter = new Counter(1000);
29
+ const result = await (0, waitUtils_1.waitAndRetry)(() => {
30
+ return counter.process();
31
+ }, 50, 30);
32
+ (0, vitest_1.expect)(result).toBe(true);
33
+ (0, vitest_1.expect)(counter.executionCounter > 0).toBe(true);
34
+ });
35
+ it('times out of there is never a condition match', async () => {
36
+ const counter = new Counter(1000);
37
+ const result = await (0, waitUtils_1.waitAndRetry)(() => {
38
+ return counter.process();
39
+ }, 20, 30);
40
+ (0, vitest_1.expect)(result).toBe(false);
41
+ (0, vitest_1.expect)(counter.executionCounter > 0).toBe(true);
42
+ });
43
+ it('handles an error', async () => {
44
+ await (0, vitest_1.expect)((0, waitUtils_1.waitAndRetry)(() => {
45
+ throw new Error('it broke');
46
+ })).rejects.toThrowError('it broke');
47
+ });
48
+ });
49
+ });
50
+ //# sourceMappingURL=waitUtils.spec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"waitUtils.spec.js","sourceRoot":"","sources":["../../../src/utils/waitUtils.spec.ts"],"names":[],"mappings":";;AAAA,mCAAyC;AAEzC,2CAA0C;AAE1C,MAAM,OAAO;IACM,YAAY,CAAQ;IAC9B,gBAAgB,CAAQ;IAC/B,YAAY,gBAAwB;QAClC,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,gBAAgB,CAAA;QACjD,IAAI,CAAC,gBAAgB,GAAG,CAAC,CAAA;IAC3B,CAAC;IAED,OAAO;QACL,IAAI,CAAC,gBAAgB,EAAE,CAAA;QACvB,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,YAAY,CAAA;IACxC,CAAC;CACF;AAED,IAAA,iBAAQ,EAAC,WAAW,EAAE,GAAG,EAAE;IACzB,IAAA,iBAAQ,EAAC,cAAc,EAAE,GAAG,EAAE;QAC5B,EAAE,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;YACpE,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,CAAC,CAAA;YAE9B,MAAM,MAAM,GAAG,MAAM,IAAA,wBAAY,EAAC,GAAG,EAAE;gBACrC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAA;YAC1B,CAAC,CAAC,CAAA;YAEF,IAAA,eAAM,EAAC,MAAM,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACzB,IAAA,eAAM,EAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QAC1C,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,2CAA2C,EAAE,KAAK,IAAI,EAAE;YACzD,MAAM,OAAO,GAAG,IAAI,O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.2.0",
4
4
  "author": {
5
5
  "name": "Lokalise",
6
6
  "url": "https://lokalise.com/"