@wandzai/utils 1.0.72 → 1.0.73-5162-1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wandzai/utils",
3
- "version": "1.0.72",
3
+ "version": "1.0.73-5162-1",
4
4
  "description": "Common used utilities library",
5
5
  "main": "src/index",
6
6
  "scripts": {
package/src/http.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { Request } from 'express';
2
+ import { Logger } from '@nestjs/common';
2
3
  export declare function getIpFromHttpRequest(req: Request): string;
3
4
  export declare function buildUriBodyString(queryParams: Record<string, any>): string;
4
5
  export declare function getEncodedQueryParams(fullUrl: string): object;
5
6
  export declare function buildQueryString(params: Record<string, any>): string;
7
+ export declare function retryWithBackoff<T>(fn: () => Promise<T>, logger: Logger, options?: {
8
+ retries: number;
9
+ baseDelayMs: number;
10
+ }): Promise<T>;
package/src/http.js CHANGED
@@ -4,6 +4,7 @@ exports.getIpFromHttpRequest = getIpFromHttpRequest;
4
4
  exports.buildUriBodyString = buildUriBodyString;
5
5
  exports.getEncodedQueryParams = getEncodedQueryParams;
6
6
  exports.buildQueryString = buildQueryString;
7
+ exports.retryWithBackoff = retryWithBackoff;
7
8
  const querystring = require("querystring");
8
9
  function getIpFromHttpRequest(req) {
9
10
  return req.headers['x-forwarded-for']
@@ -44,4 +45,31 @@ function buildQueryString(params) {
44
45
  });
45
46
  return queryParams.toString();
46
47
  }
48
+ async function retryWithBackoff(fn, logger, options = {
49
+ retries: 3,
50
+ baseDelayMs: 500,
51
+ }) {
52
+ const { retries, baseDelayMs } = options;
53
+ for (let attempt = 1; attempt <= retries; attempt++) {
54
+ try {
55
+ logger.log(`Attempt #${attempt} of ${retries}`);
56
+ return await fn();
57
+ }
58
+ catch (error) {
59
+ const status = error?.response?.status;
60
+ const isServerError = status >= 500;
61
+ const isLastAttempt = attempt === retries;
62
+ if (!isServerError || isLastAttempt) {
63
+ if (isLastAttempt) {
64
+ logger.error(`All ${retries} retries failed: ${error?.message || error}`);
65
+ }
66
+ throw error;
67
+ }
68
+ logger.warn(`Retry #${attempt} failed (HTTP ${status}): ${error?.message || error}`);
69
+ const delayMs = baseDelayMs * 2 ** (attempt - 1);
70
+ await new Promise((res) => setTimeout(res, delayMs));
71
+ }
72
+ }
73
+ throw new Error('Unexpected retry loop failure');
74
+ }
47
75
  //# sourceMappingURL=http.js.map
package/src/http.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"http.js","sourceRoot":"","sources":["../../../../libs/utils/src/http.ts"],"names":[],"mappings":";;AAOA,oDAIC;AAED,gDAIC;AAQD,sDAkBC;AAQD,4CAYC;AA9DD,2CAA2C;AAM3C,SAAgB,oBAAoB,CAAC,GAAY;IAC/C,OAAO,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAC;QACnC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;QAC7D,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC;AAC/B,CAAC;AAED,SAAgB,kBAAkB,CAAC,WAAgC;IACjE,OAAO,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC;SAC5B,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,kBAAkB,CAAC,GAAG,CAAC,IAAI,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;SAClG,IAAI,CAAC,GAAG,CAAC,CAAC;AACf,CAAC;AAQD,SAAgB,qBAAqB,CAAC,OAAe;IACnD,MAAM,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACnD,IAAI,qBAAqB,KAAK,CAAC,CAAC,EAAE,CAAC;QACjC,OAAO,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GAAG,OAAO,KAAK,OAAO,CAAC;YAG1C,IAAI,CAAC,aAAa,IAAI,aAAa,EAAE,CAAC;gBACpC,IAAI,aAAa,EAAE,CAAC;oBAClB,MAAM,CAAC,KAAK,CAAC,OAAO,OAAO,oBAAoB,KAAK,EAAE,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC;gBAC5E,CAAC;gBACD,MAAM,KAAK,CAAC;YACd,CAAC;YAGD,MAAM,CAAC,IAAI,CAAC,UAAU,OAAO,iBAAiB,MAAM,MAAM,KAAK,EAAE,OAAO,IAAI,KAAK,EAAE,CAAC,CAAC;YAGrF,MAAM,OAAO,GAAG,WAAW,GAAG,CAAC,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;YACjD,MAAM,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IAED,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;AACnD,CAAC"}
package/src/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export * from './schedule';
12
12
  export * from './cache';
13
13
  export * from './interactions';
14
14
  export * from './slack';
15
+ export * from './sentry';
package/src/index.js CHANGED
@@ -28,4 +28,5 @@ __exportStar(require("./schedule"), exports);
28
28
  __exportStar(require("./cache"), exports);
29
29
  __exportStar(require("./interactions"), exports);
30
30
  __exportStar(require("./slack"), exports);
31
+ __exportStar(require("./sentry"), exports);
31
32
  //# sourceMappingURL=index.js.map
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/utils/src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB;AACvB,4CAA0B;AAC1B,2CAAyB;AACzB,0CAAwB;AACxB,oDAAkC;AAClC,0CAAwB;AACxB,8CAA4B;AAC5B,6CAA2B;AAC3B,0CAAwB;AACxB,iDAA+B;AAC/B,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/utils/src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB;AACvB,4CAA0B;AAC1B,2CAAyB;AACzB,0CAAwB;AACxB,oDAAkC;AAClC,0CAAwB;AACxB,8CAA4B;AAC5B,6CAA2B;AAC3B,0CAAwB;AACxB,iDAA+B;AAC/B,0CAAwB;AACxB,2CAAwB"}
@@ -0,0 +1,2 @@
1
+ import { ISentrySkippedErrors, ISentrySkippedErrorsInput } from '@wandzai/wandz-interfaces';
2
+ export declare const shouldSkipSentryError: (err: unknown, sentrySkipErrors: ISentrySkippedErrors[], sentrySkippedErrorsInput: ISentrySkippedErrorsInput) => boolean;
package/src/sentry.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shouldSkipSentryError = void 0;
4
+ const shouldSkipSentryError = (err, sentrySkipErrors, sentrySkippedErrorsInput) => {
5
+ const { currentRepo, currentRoute, componentName, info } = sentrySkippedErrorsInput;
6
+ if (!Array.isArray(sentrySkipErrors) || !(err instanceof Error))
7
+ return false;
8
+ const message = err.message;
9
+ return sentrySkipErrors.some((skippedError) => {
10
+ const originMatch = skippedError.origin === currentRepo;
11
+ const textExactMatch = skippedError.errorExactText ? message === skippedError.errorExactText : false;
12
+ const textContainedMatch = skippedError.errorContainedText
13
+ ? message.includes(skippedError.errorContainedText)
14
+ : false;
15
+ const componentMatch = skippedError.componentName ? skippedError.componentName === componentName : true;
16
+ const urlMatch = skippedError.url ? skippedError.url === currentRoute : true;
17
+ const infoMatch = skippedError.info ? skippedError.info === info : true;
18
+ return originMatch && (textExactMatch || textContainedMatch) && componentMatch && urlMatch && infoMatch;
19
+ });
20
+ };
21
+ exports.shouldSkipSentryError = shouldSkipSentry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