@metriport/shared 0.10.0 → 0.10.1-alpha.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.
Files changed (45) hide show
  1. package/dist/common/file-downloader.d.ts +14 -0
  2. package/dist/common/file-downloader.d.ts.map +1 -0
  3. package/dist/common/file-downloader.js +63 -0
  4. package/dist/common/file-downloader.js.map +1 -0
  5. package/dist/common/http.d.ts +42 -0
  6. package/dist/common/http.d.ts.map +1 -0
  7. package/dist/common/http.js +83 -0
  8. package/dist/common/http.js.map +1 -0
  9. package/dist/common/retry-with-backoff.d.ts +27 -0
  10. package/dist/common/retry-with-backoff.d.ts.map +1 -0
  11. package/dist/common/retry-with-backoff.js +47 -0
  12. package/dist/common/retry-with-backoff.js.map +1 -0
  13. package/dist/common/stream.d.ts +6 -0
  14. package/dist/common/stream.d.ts.map +1 -0
  15. package/dist/common/stream.js +18 -0
  16. package/dist/common/stream.js.map +1 -0
  17. package/dist/common/uuid.d.ts +2 -0
  18. package/dist/common/uuid.d.ts.map +1 -0
  19. package/dist/common/uuid.js +5 -0
  20. package/dist/common/uuid.js.map +1 -0
  21. package/dist/domain/contact/__tests__/demographics.test.d.ts +2 -0
  22. package/dist/domain/contact/__tests__/demographics.test.d.ts.map +1 -0
  23. package/dist/domain/contact/__tests__/demographics.test.js +46 -0
  24. package/dist/domain/contact/__tests__/demographics.test.js.map +1 -0
  25. package/dist/net/__tests__/another.d.ts +1 -0
  26. package/dist/net/__tests__/another.d.ts.map +1 -0
  27. package/dist/net/__tests__/another.js +2 -0
  28. package/dist/net/__tests__/another.js.map +1 -0
  29. package/dist/net/__tests__/http.test.d.ts +2 -0
  30. package/dist/net/__tests__/http.test.d.ts.map +1 -0
  31. package/dist/net/__tests__/http.test.js +102 -0
  32. package/dist/net/__tests__/http.test.js.map +1 -0
  33. package/dist/net/net.d.ts +2 -0
  34. package/dist/net/net.d.ts.map +1 -0
  35. package/dist/net/net.js +16 -0
  36. package/dist/net/net.js.map +1 -0
  37. package/dist/net/retry-with-backoff.d.ts +42 -0
  38. package/dist/net/retry-with-backoff.d.ts.map +1 -0
  39. package/dist/net/retry-with-backoff.js +83 -0
  40. package/dist/net/retry-with-backoff.js.map +1 -0
  41. package/dist/net/stream.d.ts +6 -0
  42. package/dist/net/stream.d.ts.map +1 -0
  43. package/dist/net/stream.js +18 -0
  44. package/dist/net/stream.js.map +1 -0
  45. package/package.json +2 -2
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import * as stream from "stream";
4
+ export type DownloadFileParams = {
5
+ url: string;
6
+ outputStream: stream.Writable;
7
+ headers?: {
8
+ [index: string]: string;
9
+ };
10
+ timeout?: number;
11
+ };
12
+ export declare function downloadFileToMemory(params: Omit<DownloadFileParams, "outputStream">): Promise<Buffer>;
13
+ export declare function downloadFileToStream({ url, outputStream, headers, timeout, }: DownloadFileParams): Promise<boolean>;
14
+ //# sourceMappingURL=file-downloader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-downloader.d.ts","sourceRoot":"","sources":["../../src/common/file-downloader.ts"],"names":[],"mappings":";;AACA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAKjC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,CAAC,QAAQ,CAAC;IAC9B,OAAO,CAAC,EAAE;QACR,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;KACzB,CAAC;IACF,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,IAAI,CAAC,kBAAkB,EAAE,cAAc,CAAC,GAC/C,OAAO,CAAC,MAAM,CAAC,CAajB;AAED,wBAAsB,oBAAoB,CAAC,EACzC,GAAG,EACH,YAAY,EACZ,OAAO,EACP,OAAO,GACR,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC,CAYvC"}
@@ -0,0 +1,63 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.downloadFileToStream = exports.downloadFileToMemory = void 0;
30
+ const axios_1 = __importDefault(require("axios"));
31
+ const stream = __importStar(require("stream"));
32
+ const util = __importStar(require("util"));
33
+ const pipeline = util.promisify(stream.pipeline);
34
+ async function downloadFileToMemory(params) {
35
+ const buffers = Array();
36
+ const writableStream = new stream.Writable();
37
+ const buffer = streamToBuffer(writableStream);
38
+ writableStream.on("data", (chunk) => buffers.push(Buffer.from(chunk)));
39
+ writableStream.on("error", (err) => {
40
+ throw err;
41
+ });
42
+ writableStream.on("end", () => {
43
+ return Buffer.concat(buffers);
44
+ });
45
+ await downloadFileToStream({ ...params, outputStream: writableStream });
46
+ return Buffer.concat(buffers);
47
+ }
48
+ exports.downloadFileToMemory = downloadFileToMemory;
49
+ async function downloadFileToStream({ url, outputStream, headers, timeout, }) {
50
+ const requestConfig = {
51
+ responseType: "stream",
52
+ ...(timeout ? { timeout } : undefined),
53
+ transitional: {
54
+ clarifyTimeoutError: true,
55
+ },
56
+ ...(headers && { headers }),
57
+ };
58
+ const response = await axios_1.default.get(url, requestConfig);
59
+ await pipeline(response.data, outputStream);
60
+ return true;
61
+ }
62
+ exports.downloadFileToStream = downloadFileToStream;
63
+ //# sourceMappingURL=file-downloader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"file-downloader.js","sourceRoot":"","sources":["../../src/common/file-downloader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAkD;AAClD,+CAAiC;AACjC,2CAA6B;AAE7B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAW1C,KAAK,UAAU,oBAAoB,CACxC,MAAgD;IAEhD,MAAM,OAAO,GAAG,KAAK,EAAO,CAAC;IAC7B,MAAM,cAAc,GAAG,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC;IAC7C,MAAM,MAAM,GAAG,cAAc,CAAC,cAAc,CAAC,CAAC;IAC9C,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACvE,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;QACjC,MAAM,GAAG,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;QAC5B,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IACH,MAAM,oBAAoB,CAAC,EAAE,GAAG,MAAM,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC,CAAC;IACxE,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AAChC,CAAC;AAfD,oDAeC;AAEM,KAAK,UAAU,oBAAoB,CAAC,EACzC,GAAG,EACH,YAAY,EACZ,OAAO,EACP,OAAO,GACY;IACnB,MAAM,aAAa,GAAuB;QACxC,YAAY,EAAE,QAAQ;QACtB,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACtC,YAAY,EAAE;YACZ,mBAAmB,EAAE,IAAI;SAC1B;QACD,GAAG,CAAC,OAAO,IAAI,EAAE,OAAO,EAAE,CAAC;KAC5B,CAAC;IACF,MAAM,QAAQ,GAAG,MAAM,eAAK,CAAC,GAAG,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;IACrD,MAAM,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IAC5C,OAAO,IAAI,CAAC;AACd,CAAC;AAjBD,oDAiBC"}
@@ -0,0 +1,42 @@
1
+ export type ExecuteWithRetriesOptions = {
2
+ /** The intitial delay in milliseconds. Defaults to 100ms. */
3
+ initialDelay: number;
4
+ /** The maximum delay in milliseconds, optional. Defaults to Infinity. */
5
+ maxDelay: number;
6
+ /**
7
+ * Determines how the backoff is determined.
8
+ * It's unlikely that you would change that.
9
+ * Defaults to 2 (each retry waits double of what the previous one did).
10
+ */
11
+ backoffMultiplier: number;
12
+ /** The maximum number of retries. Defaults to 10. */
13
+ maxAttempts: number;
14
+ shouldRetry: (error: unknown, attempt: number) => boolean | Promise<boolean>;
15
+ };
16
+ /**
17
+ * TODO WIP
18
+ * TODO WIP
19
+ * TODO WIP
20
+ * TODO WIP
21
+ *
22
+ * @param fn
23
+ * @param options
24
+ * @returns
25
+ */
26
+ export declare function executeWithRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithRetriesOptions>): Promise<T>;
27
+ export type ExecuteWithHttpRetriesOptions = ExecuteWithRetriesOptions & {
28
+ /** The HTTP status codes to retry. See `defaultOptions` for defaults. */
29
+ httpStatusToRetry: number[];
30
+ };
31
+ /**
32
+ * TODO WIP
33
+ * TODO WIP
34
+ * TODO WIP
35
+ * TODO WIP
36
+ *
37
+ * @param fn
38
+ * @param options
39
+ * @returns
40
+ */
41
+ export declare function executeWithHttpRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithHttpRetriesOptions>): Promise<T>;
42
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/common/http.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,yBAAyB,GAAG;IACtC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9E,CAAC;AAUF;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,yBAAyB,CAAkB,GAC3D,OAAO,CAAC,CAAC,CAAC,CA2BZ;AAED,MAAM,MAAM,6BAA6B,GAAG,yBAAyB,GAAG;IACtE,yEAAyE;IACzE,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAC;AAYF;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAAC,CAAC,EAC5C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,6BAA6B,CAAkB,GAC/D,OAAO,CAAC,CAAC,CAAC,CAWZ"}
@@ -0,0 +1,83 @@
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.executeWithHttpRetries = exports.executeWithRetries = void 0;
7
+ const shared_1 = require("@metriport/shared");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ const http_status_1 = __importDefault(require("http-status"));
10
+ const lodash_1 = require("lodash");
11
+ const defaultOptions = {
12
+ initialDelay: 100,
13
+ maxDelay: Infinity,
14
+ backoffMultiplier: 2,
15
+ maxAttempts: 10,
16
+ shouldRetry: (_error, _attempt) => true,
17
+ };
18
+ /**
19
+ * TODO WIP
20
+ * TODO WIP
21
+ * TODO WIP
22
+ * TODO WIP
23
+ *
24
+ * @param fn
25
+ * @param options
26
+ * @returns
27
+ */
28
+ async function executeWithRetries(fn, options = defaultOptions) {
29
+ const actualOptions = { ...defaultOptions, ...options };
30
+ const { initialDelay, maxDelay, backoffMultiplier, maxAttempts: _maxAttempts, shouldRetry, } = actualOptions;
31
+ const maxAttempts = Math.max(_maxAttempts, 1);
32
+ let attempt = 0;
33
+ while (++attempt <= maxAttempts + 1) {
34
+ try {
35
+ return await fn();
36
+ }
37
+ catch (error) {
38
+ if (attempt >= maxAttempts) {
39
+ throw error;
40
+ }
41
+ if (!(await shouldRetry(error, attempt))) {
42
+ throw error;
43
+ }
44
+ const temp = Math.min(initialDelay * backoffMultiplier * attempt, maxDelay);
45
+ const timeToWait = temp / 2 + (0, lodash_1.random)(0, temp / 2);
46
+ await (0, shared_1.sleep)(timeToWait);
47
+ }
48
+ }
49
+ throw new shared_1.MetriportError("Unreachable code", undefined, { attempt, maxRetries: maxAttempts });
50
+ }
51
+ exports.executeWithRetries = executeWithRetries;
52
+ const defaultHttpRetryOptions = {
53
+ ...defaultOptions,
54
+ httpStatusToRetry: [
55
+ http_status_1.default.INTERNAL_SERVER_ERROR,
56
+ http_status_1.default.BAD_GATEWAY,
57
+ http_status_1.default.SERVICE_UNAVAILABLE,
58
+ http_status_1.default.GATEWAY_TIMEOUT,
59
+ ],
60
+ };
61
+ /**
62
+ * TODO WIP
63
+ * TODO WIP
64
+ * TODO WIP
65
+ * TODO WIP
66
+ *
67
+ * @param fn
68
+ * @param options
69
+ * @returns
70
+ */
71
+ async function executeWithHttpRetries(fn, options = defaultOptions) {
72
+ const actualOptions = { ...defaultHttpRetryOptions, ...options };
73
+ const { httpStatusToRetry } = actualOptions;
74
+ return executeWithRetries(fn, {
75
+ ...actualOptions,
76
+ shouldRetry: (error, _attempt) => {
77
+ const responseStatus = axios_1.default.isAxiosError(error) && error.response?.status ? error.response.status : 500;
78
+ return httpStatusToRetry.includes(responseStatus);
79
+ },
80
+ });
81
+ }
82
+ exports.executeWithHttpRetries = executeWithHttpRetries;
83
+ //# sourceMappingURL=http.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/common/http.ts"],"names":[],"mappings":";;;;;;AAAA,8CAA0D;AAC1D,kDAA0B;AAC1B,8DAAqC;AACrC,mCAAgC;AAkBhC,MAAM,cAAc,GAA8B;IAChD,YAAY,EAAE,GAAG;IACjB,QAAQ,EAAE,QAAQ;IAClB,iBAAiB,EAAE,CAAC;IACpB,WAAW,EAAE,EAAE;IACf,WAAW,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI;CACxC,CAAC;AAEF;;;;;;;;;GASG;AACI,KAAK,UAAU,kBAAkB,CACtC,EAAoB,EACpB,UAA8C,cAAc;IAE5D,MAAM,aAAa,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IACxD,MAAM,EACJ,YAAY,EACZ,QAAQ,EACR,iBAAiB,EACjB,WAAW,EAAE,YAAY,EACzB,WAAW,GACZ,GAAG,aAAa,CAAC;IAClB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,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AG,aAAa;QAChB,WAAW,EAAE,CAAC,KAAc,EAAE,QAAgB,EAAE,EAAE;YAChD,MAAM,cAAc,GAClB,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;YACpF,OAAO,iBAAiB,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACpD,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAdD,wDAcC"}
@@ -0,0 +1,27 @@
1
+ export type ExecuteWithRetriesOptions = {
2
+ /** The intitial delay in milliseconds. Defaults to 100ms. */
3
+ initialDelay: number;
4
+ /** The maximum delay in milliseconds, optional. Defaults to Infinity. */
5
+ maxDelay: number;
6
+ /**
7
+ * Determines how the backoff is determined.
8
+ * It's unlikely that you would change that.
9
+ * Defaults to 2 (each retry waits double of what the previous one did).
10
+ */
11
+ backoffMultiplier: number;
12
+ /** The maximum number of retries. Defaults to 10. */
13
+ maxAttempts: number;
14
+ shouldRetry: (error: unknown, attempt: number) => boolean | Promise<boolean>;
15
+ };
16
+ /**
17
+ * TODO WIP
18
+ * TODO WIP
19
+ * TODO WIP
20
+ * TODO WIP
21
+ *
22
+ * @param fn
23
+ * @param options
24
+ * @returns
25
+ */
26
+ export declare function executeWithRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithRetriesOptions>): Promise<T>;
27
+ //# sourceMappingURL=retry-with-backoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-with-backoff.d.ts","sourceRoot":"","sources":["../../src/common/retry-with-backoff.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,yBAAyB,GAAG;IACtC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9E,CAAC;AAUF;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,yBAAyB,CAAkB,GAC3D,OAAO,CAAC,CAAC,CAAC,CA2BZ"}
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.executeWithRetries = void 0;
4
+ const shared_1 = require("@metriport/shared");
5
+ const lodash_1 = require("lodash");
6
+ const defaultOptions = {
7
+ initialDelay: 100,
8
+ maxDelay: Infinity,
9
+ backoffMultiplier: 2,
10
+ maxAttempts: 10,
11
+ shouldRetry: (_error, _attempt) => true,
12
+ };
13
+ /**
14
+ * TODO WIP
15
+ * TODO WIP
16
+ * TODO WIP
17
+ * TODO WIP
18
+ *
19
+ * @param fn
20
+ * @param options
21
+ * @returns
22
+ */
23
+ async function executeWithRetries(fn, options = defaultOptions) {
24
+ const actualOptions = { ...defaultOptions, ...options };
25
+ const { initialDelay, maxDelay, backoffMultiplier, maxAttempts: _maxAttempts, shouldRetry, } = actualOptions;
26
+ const maxAttempts = Math.max(_maxAttempts, 1);
27
+ let attempt = 0;
28
+ while (++attempt <= maxAttempts + 1) {
29
+ try {
30
+ return await fn();
31
+ }
32
+ catch (error) {
33
+ if (attempt >= maxAttempts) {
34
+ throw error;
35
+ }
36
+ if (!(await shouldRetry(error, attempt))) {
37
+ throw error;
38
+ }
39
+ const temp = Math.min(initialDelay * backoffMultiplier * attempt, maxDelay);
40
+ const timeToWait = temp / 2 + (0, lodash_1.random)(0, temp / 2);
41
+ await (0, shared_1.sleep)(timeToWait);
42
+ }
43
+ }
44
+ throw new shared_1.MetriportError("Unreachable code", undefined, { attempt, maxRetries: maxAttempts });
45
+ }
46
+ exports.executeWithRetries = executeWithRetries;
47
+ //# sourceMappingURL=retry-with-backoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-with-backoff.js","sourceRoot":"","sources":["../../src/common/retry-with-backoff.ts"],"names":[],"mappings":";;;AAAA,8CAA0D;AAC1D,mCAAgC;AAkBhC,MAAM,cAAc,GAA8B;IAChD,YAAY,EAAE,GAAG;IACjB,QAAQ,EAAE,QAAQ;IAClB,iBAAiB,EAAE,CAAC;IACpB,WAAW,EAAE,EAAE;IACf,WAAW,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,CAAC,IAAI;CACxC,CAAC;AAEF;;;;;;;;;GASG;AACI,KAAK,UAAU,kBAAkB,CACtC,EAAoB,EACpB,UAA8C,cAAc;IAE5D,MAAM,aAAa,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,OAAO,EAAE,CAAC;IACxD,MAAM,EACJ,YAAY,EACZ,QAAQ,EACR,iBAAiB,EACjB,WAAW,EAAE,YAAY,EACzB,WAAW,GACZ,GAAG,aAAa,CAAC;IAClB,MAAM,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC;IAC9C,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,OAAO,EAAE,OAAO,IAAI,WAAW,GAAG,CAAC,EAAE;QACnC,IAAI;YACF,OAAO,MAAM,EAAE,EAAE,CAAC;SACnB;QAAC,OAAO,KAAU,EAAE;YACnB,IAAI,OAAO,IAAI,WAAW,EAAE;gBAC1B,MAAM,KAAK,CAAC;aACb;YACD,IAAI,CAAC,CAAC,MAAM,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE;gBACxC,MAAM,KAAK,CAAC;aACb;YACD,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,YAAY,GAAG,iBAAiB,GAAG,OAAO,EAAE,QAAQ,CAAC,CAAC;YAC5E,MAAM,UAAU,GAAG,IAAI,GAAG,CAAC,GAAG,IAAA,eAAM,EAAC,CAAC,EAAE,IAAI,GAAG,CAAC,CAAC,CAAC;YAClD,MAAM,IAAA,cAAK,EAAC,UAAU,CAAC,CAAC;SACzB;KACF;IACD,MAAM,IAAI,uBAAc,CAAC,kBAAkB,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC,CAAC;AAChG,CAAC;AA9BD,gDA8BC"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import * as stream from "stream";
4
+ export declare function streamToBuffer(stream: stream.Readable): Promise<Buffer>;
5
+ export declare function streamToString(stream: stream.Readable): Promise<String>;
6
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/common/stream.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,CAO7E;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,CAG7E"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamToString = exports.streamToBuffer = void 0;
4
+ async function streamToBuffer(stream) {
5
+ const buffer = Array();
6
+ return new Promise((resolve, reject) => {
7
+ stream.on("data", chunk => buffer.push(Buffer.from(chunk)));
8
+ stream.on("error", err => reject(err));
9
+ stream.on("end", () => resolve(Buffer.concat(buffer)));
10
+ });
11
+ }
12
+ exports.streamToBuffer = streamToBuffer;
13
+ async function streamToString(stream) {
14
+ const buffer = await streamToBuffer(stream);
15
+ return buffer.toString();
16
+ }
17
+ exports.streamToString = streamToString;
18
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/common/stream.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,cAAc,CAAC,MAAuB;IAC1D,MAAM,MAAM,GAAG,KAAK,EAAO,CAAC;IAC5B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC5D,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACvC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACL,CAAC;AAPD,wCAOC;AAEM,KAAK,UAAU,cAAc,CAAC,MAAuB;IAC1D,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAHD,wCAGC"}
@@ -0,0 +1,2 @@
1
+ export declare const uuidRegex: RegExp;
2
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/common/uuid.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,QAAmB,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uuidRegex = void 0;
4
+ exports.uuidRegex = /^[a-f0-9]{32}$/;
5
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../src/common/uuid.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG,gBAAgB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=demographics.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"demographics.test.d.ts","sourceRoot":"","sources":["../../../../src/domain/contact/__tests__/demographics.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const phone_1 = require("../phone");
4
+ describe("isPhoneValid", () => {
5
+ it("returns true when phone has 10 digits", async () => {
6
+ const res = (0, phone_1.isPhoneValid)("1234567890");
7
+ expect(res).toBeTruthy();
8
+ });
9
+ it("returns false when phone is undefined", async () => {
10
+ const res = (0, phone_1.isPhoneValid)(undefined);
11
+ expect(res).toBeFalsy();
12
+ });
13
+ it("returns false when phone is string", async () => {
14
+ const res = (0, phone_1.isPhoneValid)(null);
15
+ expect(res).toBeFalsy();
16
+ });
17
+ it("returns false when phone contains non-numbers", async () => {
18
+ const res = (0, phone_1.isPhoneValid)("a234567890");
19
+ expect(res).toBeFalsy();
20
+ });
21
+ it("returns false when phone contains non-number prefix", async () => {
22
+ const res = (0, phone_1.isPhoneValid)("a234567890");
23
+ expect(res).toBeFalsy();
24
+ });
25
+ it("returns false when phone contains non-number suffix", async () => {
26
+ const res = (0, phone_1.isPhoneValid)("123456789c");
27
+ expect(res).toBeFalsy();
28
+ });
29
+ it("returns false when phone contains non-number in the middle", async () => {
30
+ const res = (0, phone_1.isPhoneValid)("1234-67890");
31
+ expect(res).toBeFalsy();
32
+ });
33
+ it("returns false when phone contains less than 10 digits", async () => {
34
+ const res = (0, phone_1.isPhoneValid)("123456789");
35
+ expect(res).toBeFalsy();
36
+ });
37
+ it("returns false when phone contains more than 10 digits", async () => {
38
+ const res = (0, phone_1.isPhoneValid)("12345678901");
39
+ expect(res).toBeFalsy();
40
+ });
41
+ it("returns false when phone contains more than 10 digits", async () => {
42
+ const res = (0, phone_1.isPhoneValid)("12345678901");
43
+ expect(res).toBeFalsy();
44
+ });
45
+ });
46
+ //# sourceMappingURL=demographics.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"demographics.test.js","sourceRoot":"","sources":["../../../../src/domain/contact/__tests__/demographics.test.ts"],"names":[],"mappings":";;AAAA,oCAAwC;AAExC,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,uCAAuC,EAAE,KAAK,IAAI,EAAE;QACrD,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,YAAY,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,CAAC,UAAU,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uCAAuC,EAAE,KAAK,IAAI,EAAE;QACrD,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,SAA8B,CAAC,CAAC;QACzD,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,KAAK,IAAI,EAAE;QAClD,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,IAAyB,CAAC,CAAC;QACpD,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,+CAA+C,EAAE,KAAK,IAAI,EAAE;QAC7D,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,YAAY,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qDAAqD,EAAE,KAAK,IAAI,EAAE;QACnE,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,YAAY,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,qDAAqD,EAAE,KAAK,IAAI,EAAE;QACnE,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,YAAY,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,4DAA4D,EAAE,KAAK,IAAI,EAAE;QAC1E,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,YAAY,CAAC,CAAC;QACvC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,WAAW,CAAC,CAAC;QACtC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,aAAa,CAAC,CAAC;QACxC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QACrE,MAAM,GAAG,GAAG,IAAA,oBAAY,EAAC,aAAa,CAAC,CAAC;QACxC,MAAM,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=another.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"another.d.ts","sourceRoot":"","sources":["../../../src/net/__tests__/another.js"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ //# sourceMappingURL=another.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"another.js","sourceRoot":"","sources":["../../../src/net/__tests__/another.js"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=http.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.test.d.ts","sourceRoot":"","sources":["../../../src/net/__tests__/http.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /* eslint-disable @typescript-eslint/no-empty-function */
4
+ const faker_1 = require("@faker-js/faker");
5
+ const axios_1 = require("axios");
6
+ const http_1 = require("../http");
7
+ // describe("executeWithRetries", () => {
8
+ // const fnImpl = async () => {
9
+ // throw new Error("error");
10
+ // };
11
+ // const fn = jest.fn().mockImplementation(fnImpl);
12
+ // it("returns undefined when gets undefined", async () => {
13
+ // await expect(async () =>
14
+ // executeWithRetries(fn, {
15
+ // initialDelay: 10,
16
+ // maxAttempts: 10,
17
+ // })
18
+ // ).rejects.toThrow();
19
+ // expect(fn).toHaveBeenCalledTimes(10);
20
+ // });
21
+ // });
22
+ describe("executeWithHttpRetries", () => {
23
+ const makeFnImplementation = (status) => async () => {
24
+ const error = new axios_1.AxiosError("error");
25
+ const actualStatus = status === undefined ? 500 : status;
26
+ if (actualStatus !== null) {
27
+ error.response = {
28
+ status: actualStatus,
29
+ data: faker_1.faker.lorem.sentence(),
30
+ statusText: faker_1.faker.lorem.sentence(),
31
+ headers: {},
32
+ config: {},
33
+ };
34
+ }
35
+ throw error;
36
+ };
37
+ let fn = jest.fn();
38
+ beforeEach(() => {
39
+ fn.mockImplementation(makeFnImplementation());
40
+ });
41
+ afterEach(() => {
42
+ jest.resetAllMocks();
43
+ });
44
+ // it("returns on first execution when fn does not fail", async () => {
45
+ // const expectedResult = faker.lorem.word();
46
+ // fn.mockImplementationOnce(() => expectedResult);
47
+ // const resp = await executeWithHttpRetries(fn, {
48
+ // initialDelay: 1,
49
+ // maxAttempts: 2,
50
+ // });
51
+ // expect(resp).toEqual(expectedResult);
52
+ // expect(fn).toHaveBeenCalledTimes(1);
53
+ // });
54
+ // it("returns on second execution when fn fails once", async () => {
55
+ // const expectedResult = faker.lorem.word();
56
+ // fn.mockImplementationOnce(makeFnImplementation());
57
+ // fn.mockImplementationOnce(() => expectedResult);
58
+ // const resp = await executeWithHttpRetries(fn, {
59
+ // initialDelay: 1,
60
+ // maxAttempts: 3,
61
+ // });
62
+ // expect(resp).toEqual(expectedResult);
63
+ // expect(fn).toHaveBeenCalledTimes(2);
64
+ // });
65
+ // it("executes until maxAttempts if fn always fails", async () => {
66
+ // await expect(async () =>
67
+ // executeWithHttpRetries(fn, {
68
+ // initialDelay: 1,
69
+ // // maxDelay: 100,
70
+ // // backoffMultiplier: 2,
71
+ // maxAttempts: 5,
72
+ // // httpStatusToRetry: [500],
73
+ // })
74
+ // ).rejects.toThrow();
75
+ // expect(fn).toHaveBeenCalledTimes(5);
76
+ // });
77
+ it("does not retry on 400", async () => {
78
+ fn.mockImplementationOnce(makeFnImplementation(400));
79
+ await expect(async () => (0, http_1.executeWithHttpRetries)(fn, {
80
+ initialDelay: 1,
81
+ maxAttempts: 2,
82
+ })).rejects.toThrow();
83
+ expect(fn).toHaveBeenCalledTimes(1);
84
+ });
85
+ it("does not retry on 500 when gets array of status without 500", async () => {
86
+ await expect(async () => (0, http_1.executeWithHttpRetries)(fn, {
87
+ initialDelay: 1,
88
+ maxAttempts: 2,
89
+ httpStatusToRetry: [404],
90
+ })).rejects.toThrow();
91
+ expect(fn).toHaveBeenCalledTimes(1);
92
+ });
93
+ it("retries when AxiosError does not have a response", async () => {
94
+ fn.mockImplementationOnce(makeFnImplementation(null));
95
+ await expect(async () => (0, http_1.executeWithHttpRetries)(fn, {
96
+ initialDelay: 1,
97
+ maxAttempts: 2,
98
+ })).rejects.toThrow();
99
+ expect(fn).toHaveBeenCalledTimes(2);
100
+ });
101
+ });
102
+ //# sourceMappingURL=http.test.js.map
@@ -0,0 +1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I,EAAE;QAChE,EAAE,CAAC,sBAAsB,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC;QACtD,MAAM,MAAM,CAAC,KAAK,IAAI,EAAE,CACtB,IAAA,6BAAsB,EAAC,EAAE,EAAE;YACzB,YAAY,EAAE,CAAC;YACf,WAAW,EAAE,CAAC;SACf,CAAC,CACH,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,CAAC,EAAE,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function isValidUrl(url: string | undefined): boolean;
2
+ //# sourceMappingURL=net.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"net.d.ts","sourceRoot":"","sources":["../../src/net/net.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,OAAO,CAQ3D"}
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isValidUrl = void 0;
4
+ function isValidUrl(url) {
5
+ if (!url)
6
+ return false;
7
+ try {
8
+ new URL(url);
9
+ return true;
10
+ }
11
+ catch (e) {
12
+ return false;
13
+ }
14
+ }
15
+ exports.isValidUrl = isValidUrl;
16
+ //# sourceMappingURL=net.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"net.js","sourceRoot":"","sources":["../../src/net/net.ts"],"names":[],"mappings":";;;AAAA,SAAgB,UAAU,CAAC,GAAuB;IAChD,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAC;IACvB,IAAI;QACF,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;KACb;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AARD,gCAQC"}
@@ -0,0 +1,42 @@
1
+ export type ExecuteWithRetriesOptions = {
2
+ /** The intitial delay in milliseconds. Defaults to 100ms. */
3
+ initialDelay: number;
4
+ /** The maximum delay in milliseconds, optional. Defaults to Infinity. */
5
+ maxDelay: number;
6
+ /**
7
+ * Determines how the backoff is determined.
8
+ * It's unlikely that you would change that.
9
+ * Defaults to 2 (each retry waits double of what the previous one did).
10
+ */
11
+ backoffMultiplier: number;
12
+ /** The maximum number of retries. Defaults to 10. */
13
+ maxAttempts: number;
14
+ shouldRetry: (error: unknown, attempt: number) => boolean | Promise<boolean>;
15
+ };
16
+ /**
17
+ * TODO WIP
18
+ * TODO WIP
19
+ * TODO WIP
20
+ * TODO WIP
21
+ *
22
+ * @param fn
23
+ * @param options
24
+ * @returns
25
+ */
26
+ export declare function executeWithRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithRetriesOptions>): Promise<T>;
27
+ export type ExecuteWithHttpRetriesOptions = ExecuteWithRetriesOptions & {
28
+ /** The HTTP status codes to retry. See `defaultOptions` for defaults. */
29
+ httpStatusToRetry: number[];
30
+ };
31
+ /**
32
+ * TODO WIP
33
+ * TODO WIP
34
+ * TODO WIP
35
+ * TODO WIP
36
+ *
37
+ * @param fn
38
+ * @param options
39
+ * @returns
40
+ */
41
+ export declare function executeWithHttpRetries<T>(fn: () => Promise<T>, options?: Partial<ExecuteWithHttpRetriesOptions>): Promise<T>;
42
+ //# sourceMappingURL=retry-with-backoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-with-backoff.d.ts","sourceRoot":"","sources":["../../src/net/retry-with-backoff.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,yBAAyB,GAAG;IACtC,6DAA6D;IAC7D,YAAY,EAAE,MAAM,CAAC;IACrB,yEAAyE;IACzE,QAAQ,EAAE,MAAM,CAAC;IACjB;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,CAAC;IAC1B,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC9E,CAAC;AAUF;;;;;;;;;GASG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,yBAAyB,CAAkB,GAC3D,OAAO,CAAC,CAAC,CAAC,CA2BZ;AAED,MAAM,MAAM,6BAA6B,GAAG,yBAAyB,GAAG;IACtE,yEAAyE;IACzE,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAC;AAYF;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAAC,CAAC,EAC5C,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EACpB,OAAO,GAAE,OAAO,CAAC,6BAA6B,CAAkB,GAC/D,OAAO,CAAC,CAAC,CAAC,CAWZ"}
@@ -0,0 +1,83 @@
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.executeWithHttpRetries = exports.executeWithRetries = void 0;
7
+ const shared_1 = require("@metriport/shared");
8
+ const axios_1 = __importDefault(require("axios"));
9
+ const http_status_1 = __importDefault(require("http-status"));
10
+ const lodash_1 = require("lodash");
11
+ const defaultOptions = {
12
+ initialDelay: 100,
13
+ maxDelay: Infinity,
14
+ backoffMultiplier: 2,
15
+ maxAttempts: 10,
16
+ shouldRetry: (_error, _attempt) => true,
17
+ };
18
+ /**
19
+ * TODO WIP
20
+ * TODO WIP
21
+ * TODO WIP
22
+ * TODO WIP
23
+ *
24
+ * @param fn
25
+ * @param options
26
+ * @returns
27
+ */
28
+ async function executeWithRetries(fn, options = defaultOptions) {
29
+ const actualOptions = { ...defaultOptions, ...options };
30
+ const { initialDelay, maxDelay, backoffMultiplier, maxAttempts: _maxAttempts, shouldRetry, } = actualOptions;
31
+ const maxAttempts = Math.max(_maxAttempts, 1);
32
+ let attempt = 0;
33
+ while (++attempt <= maxAttempts + 1) {
34
+ try {
35
+ return await fn();
36
+ }
37
+ catch (error) {
38
+ if (attempt >= maxAttempts) {
39
+ throw error;
40
+ }
41
+ if (!(await shouldRetry(error, attempt))) {
42
+ throw error;
43
+ }
44
+ const temp = Math.min(initialDelay * backoffMultiplier * attempt, maxDelay);
45
+ const timeToWait = temp / 2 + (0, lodash_1.random)(0, temp / 2);
46
+ await (0, shared_1.sleep)(timeToWait);
47
+ }
48
+ }
49
+ throw new shared_1.MetriportError("Unreachable code", undefined, { attempt, maxRetries: maxAttempts });
50
+ }
51
+ exports.executeWithRetries = executeWithRetries;
52
+ const defaultHttpRetryOptions = {
53
+ ...defaultOptions,
54
+ httpStatusToRetry: [
55
+ http_status_1.default.INTERNAL_SERVER_ERROR,
56
+ http_status_1.default.BAD_GATEWAY,
57
+ http_status_1.default.SERVICE_UNAVAILABLE,
58
+ http_status_1.default.GATEWAY_TIMEOUT,
59
+ ],
60
+ };
61
+ /**
62
+ * TODO WIP
63
+ * TODO WIP
64
+ * TODO WIP
65
+ * TODO WIP
66
+ *
67
+ * @param fn
68
+ * @param options
69
+ * @returns
70
+ */
71
+ async function executeWithHttpRetries(fn, options = defaultOptions) {
72
+ const actualOptions = { ...defaultHttpRetryOptions, ...options };
73
+ const { httpStatusToRetry } = actualOptions;
74
+ return executeWithRetries(fn, {
75
+ ...actualOptions,
76
+ shouldRetry: (error, _attempt) => {
77
+ const responseStatus = axios_1.default.isAxiosError(error) && error.response?.status ? error.response.status : 500;
78
+ return httpStatusToRetry.includes(responseStatus);
79
+ },
80
+ });
81
+ }
82
+ exports.executeWithHttpRetries = executeWithHttpRetries;
83
+ //# sourceMappingURL=retry-with-backoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry-with-backoff.js","sourceRoot":"","sources":["../../src/net/retry-with-backoff.ts"],"names":[],"mapp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;IACD,MAAM,IAAI,uBAAc,CAAC,kBAAkB,EAAE,SAAS,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,CAAC,CAAC;AAChG,CAAC;AA9BD,gDA8BC;AAOD,MAAM,uBAAuB,GAAkC;IAC7D,GAAG,cAAc;IACjB,iBAAiB,EAAE;QACjB,qBAAU,CAAC,qBAAqB;QAChC,qBAAU,CAAC,WAAW;QACtB,qBAAU,CAAC,mBAAmB;QAC9B,qBAAU,CAAC,eAAe;KAC3B;CACF,CAAC;AAEF;;;;;;;;;GASG;AACI,KAAK,UAAU,sBAAsB,CAC1C,EAAoB,EACpB,UAAkD,cAAc;IAEhE,MAAM,aAAa,GAAG,EAAE,GAAG,uBAAuB,EAAE,GAAG,OAAO,EAAE,CAAC;IACjE,MAAM,EAAE,iBAAiB,EAAE,GAAG,aAAa,CAAC;IAC5C,OAAO,kBAAkB,CAAC,EAAE,EAAE;QAC5B,GAAG,aAAa;QAChB,WAAW,EAAE,CAAC,KAAc,EAAE,QAAgB,EAAE,EAAE;YAChD,MAAM,cAAc,GAClB,eAAK,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC;YACpF,OAAO,iBAAiB,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAC;QACpD,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAdD,wDAcC"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import * as stream from "stream";
4
+ export declare function streamToBuffer(stream: stream.Stream): Promise<Buffer>;
5
+ export declare function streamToString(stream: stream.Stream): Promise<String>;
6
+ //# sourceMappingURL=stream.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/net/stream.ts"],"names":[],"mappings":";;AAAA,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AAEjC,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAO3E;AAED,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3E"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.streamToString = exports.streamToBuffer = void 0;
4
+ async function streamToBuffer(stream) {
5
+ const buffers = Array();
6
+ return new Promise((resolve, reject) => {
7
+ stream.on("data", chunk => buffers.push(Buffer.from(chunk)));
8
+ stream.on("error", err => reject(err));
9
+ stream.on("end", () => resolve(Buffer.concat(buffers)));
10
+ });
11
+ }
12
+ exports.streamToBuffer = streamToBuffer;
13
+ async function streamToString(stream) {
14
+ const buffer = await streamToBuffer(stream);
15
+ return buffer.toString();
16
+ }
17
+ exports.streamToString = streamToString;
18
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/net/stream.ts"],"names":[],"mappings":";;;AAEO,KAAK,UAAU,cAAc,CAAC,MAAqB;IACxD,MAAM,OAAO,GAAG,KAAK,EAAO,CAAC;IAC7B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7D,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;QACvC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAC1D,CAAC,CAAC,CAAC;AACL,CAAC;AAPD,wCAOC;AAEM,KAAK,UAAU,cAAc,CAAC,MAAqB;IACxD,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC;AAHD,wCAGC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metriport/shared",
3
- "version": "0.10.0",
3
+ "version": "0.10.1-alpha.1",
4
4
  "description": "Common code shared across packages - by Metriport Inc.",
5
5
  "author": "Metriport Inc. <contact@metriport.com>",
6
6
  "homepage": "https://metriport.com/",
@@ -58,5 +58,5 @@
58
58
  "devDependencies": {
59
59
  "@faker-js/faker": "^8.0.2"
60
60
  },
61
- "gitHead": "264de9aeada5d3d96d9ca097c2949f92206358f9"
61
+ "gitHead": "b6a4d75f2184aeca67572cbeb1a6d45f22d5ac4b"
62
62
  }