univapay-node 4.0.90 → 4.0.91
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.
|
@@ -2,5 +2,6 @@
|
|
|
2
2
|
* @internal
|
|
3
3
|
* @module Utils
|
|
4
4
|
*/
|
|
5
|
+
export declare const rateLimit: <R>(exec: (...args: any[]) => Promise<R>) => Promise<R>;
|
|
5
6
|
export declare const parseJSON: <FormattedBody>(response: Response, ignoreKeys?: string[]) => Promise<FormattedBody>;
|
|
6
7
|
export declare const checkStatus: (response: Response) => Promise<Response>;
|
package/dist/cjs/utils/fetch.js
CHANGED
|
@@ -4,10 +4,47 @@
|
|
|
4
4
|
* @module Utils
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.checkStatus = exports.parseJSON = void 0;
|
|
7
|
+
exports.checkStatus = exports.parseJSON = exports.rateLimit = void 0;
|
|
8
8
|
const json_bigint_1 = require("@apimatic/json-bigint");
|
|
9
9
|
const APIError_js_1 = require("../errors/APIError.js");
|
|
10
|
+
const RequestResponseError_js_1 = require("../errors/RequestResponseError.js");
|
|
10
11
|
const object_js_1 = require("./object.js");
|
|
12
|
+
const COOLDOWN_ADJUST = 1.2;
|
|
13
|
+
const COOLDOWN_WAIT = 50; // Cooldown wait period after first fail (50ms)
|
|
14
|
+
const FAIL_TRESHOLD = 10; // Fail processing after X concurrent fails. If 0, then will try forever.
|
|
15
|
+
const rateLimit = (exec) => {
|
|
16
|
+
let cooldown = 0;
|
|
17
|
+
let tries = 0;
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const executor = async () => {
|
|
20
|
+
try {
|
|
21
|
+
tries++;
|
|
22
|
+
const result = await exec();
|
|
23
|
+
cooldown = 0;
|
|
24
|
+
tries = 0;
|
|
25
|
+
resolve(result);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error instanceof RequestResponseError_js_1.ResponseError &&
|
|
29
|
+
(error.errorResponse.code === APIError_js_1.ResponseErrorCode.TooManyRequests ||
|
|
30
|
+
error.errorResponse.httpCode === 429)) {
|
|
31
|
+
if (FAIL_TRESHOLD > 0 && tries > FAIL_TRESHOLD) {
|
|
32
|
+
reject(error);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
cooldown = cooldown === 0 ? COOLDOWN_WAIT : cooldown * COOLDOWN_ADJUST;
|
|
36
|
+
setTimeout(executor, cooldown);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
reject(error);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
return executor();
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
exports.rateLimit = rateLimit;
|
|
11
48
|
const parseJSON = async (response, ignoreKeys = ["metadata"]) => {
|
|
12
49
|
const text = await response.text();
|
|
13
50
|
return text ? (0, object_js_1.transformKeys)((0, json_bigint_1.parse)(text), object_js_1.toCamelCase, ignoreKeys) : {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../../src/utils/fetch.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,uDAA8C;AAE9C,
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../../src/utils/fetch.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,uDAA8C;AAE9C,uDAAoE;AACpE,+EAAkE;AAElE,2CAAyD;AAEzD,MAAM,eAAe,GAAG,GAAG,CAAC;AAC5B,MAAM,aAAa,GAAG,EAAE,CAAC,CAAC,+CAA+C;AACzE,MAAM,aAAa,GAAG,EAAE,CAAC,CAAC,yEAAyE;AAE5F,MAAM,SAAS,GAAG,CAAI,IAAoC,EAAc,EAAE;IAC7E,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,IAAI,KAAK,GAAG,CAAC,CAAC;IAEd,OAAO,IAAI,OAAO,CAAC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uBAAW,EAAE,UAAU,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAC3E,CAAC,CAAC;AANW,QAAA,SAAS,aAMpB;AAEK,MAAM,WAAW,GAAG,KAAK,EAAE,QAAkB,EAAqB,EAAE;IACvE,IAAI,QAAQ,CAAC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QAClD,OAAO,QAAQ,CAAC;IACpB,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,IAAA,iBAAS,EAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IACrD,MAAM,IAAI,sBAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAC9C,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB"}
|
|
@@ -2,5 +2,6 @@
|
|
|
2
2
|
* @internal
|
|
3
3
|
* @module Utils
|
|
4
4
|
*/
|
|
5
|
+
export declare const rateLimit: <R>(exec: (...args: any[]) => Promise<R>) => Promise<R>;
|
|
5
6
|
export declare const parseJSON: <FormattedBody>(response: Response, ignoreKeys?: string[]) => Promise<FormattedBody>;
|
|
6
7
|
export declare const checkStatus: (response: Response) => Promise<Response>;
|
package/dist/esm/utils/fetch.js
CHANGED
|
@@ -3,8 +3,44 @@
|
|
|
3
3
|
* @module Utils
|
|
4
4
|
*/
|
|
5
5
|
import { parse } from "@apimatic/json-bigint";
|
|
6
|
-
import { APIError } from "../errors/APIError.js";
|
|
6
|
+
import { APIError, ResponseErrorCode } from "../errors/APIError.js";
|
|
7
|
+
import { ResponseError } from "../errors/RequestResponseError.js";
|
|
7
8
|
import { toCamelCase, transformKeys } from "./object.js";
|
|
9
|
+
const COOLDOWN_ADJUST = 1.2;
|
|
10
|
+
const COOLDOWN_WAIT = 50; // Cooldown wait period after first fail (50ms)
|
|
11
|
+
const FAIL_TRESHOLD = 10; // Fail processing after X concurrent fails. If 0, then will try forever.
|
|
12
|
+
export const rateLimit = (exec) => {
|
|
13
|
+
let cooldown = 0;
|
|
14
|
+
let tries = 0;
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
const executor = async () => {
|
|
17
|
+
try {
|
|
18
|
+
tries++;
|
|
19
|
+
const result = await exec();
|
|
20
|
+
cooldown = 0;
|
|
21
|
+
tries = 0;
|
|
22
|
+
resolve(result);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof ResponseError &&
|
|
26
|
+
(error.errorResponse.code === ResponseErrorCode.TooManyRequests ||
|
|
27
|
+
error.errorResponse.httpCode === 429)) {
|
|
28
|
+
if (FAIL_TRESHOLD > 0 && tries > FAIL_TRESHOLD) {
|
|
29
|
+
reject(error);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
cooldown = cooldown === 0 ? COOLDOWN_WAIT : cooldown * COOLDOWN_ADJUST;
|
|
33
|
+
setTimeout(executor, cooldown);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
reject(error);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return executor();
|
|
42
|
+
});
|
|
43
|
+
};
|
|
8
44
|
export const parseJSON = async (response, ignoreKeys = ["metadata"]) => {
|
|
9
45
|
const text = await response.text();
|
|
10
46
|
return text ? transformKeys(parse(text), toCamelCase, ignoreKeys) : {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"fetch.js","sourceRoot":"","sources":["../../../src/utils/fetch.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,OAAO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