@yaebal/again 0.0.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/LICENSE +21 -0
- package/README.md +13 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +34 -0
- package/lib/index.js.map +1 -0
- package/lib/index.test.d.ts +2 -0
- package/lib/index.test.d.ts.map +1 -0
- package/lib/index.test.js +29 -0
- package/lib/index.test.js.map +1 -0
- package/package.json +48 -0
- package/src/index.test.ts +44 -0
- package/src/index.ts +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 neverlane
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @yaebal/again
|
|
2
|
+
|
|
3
|
+
max retries after the first attempt. defaults to 3.
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @yaebal/again
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
part of [**yaebal**](https://github.com/neverlane/yaebal) — a type-safe, runtime-agnostic Telegram Bot API framework. MIT.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Api, type ErrorAction } from "@yaebal/core";
|
|
2
|
+
export interface AutoRetryOptions {
|
|
3
|
+
/** max retries after the first attempt. defaults to 3. */
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
/** cap on a single wait, in ms. defaults to 30_000. */
|
|
6
|
+
maxDelayMs?: number;
|
|
7
|
+
/** also retry transient 5xx server errors. defaults to true. */
|
|
8
|
+
retryOnInternal?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* decide whether a failed call should be retried, and after how long.
|
|
12
|
+
* pure (no I/O) — exported so the policy is unit-testable on its own.
|
|
13
|
+
*/
|
|
14
|
+
export declare function decideRetry(error: unknown, attempt: number, options?: AutoRetryOptions): ErrorAction | undefined;
|
|
15
|
+
/** install auto-retry on a bot's API: `autoRetry(bot.api)`. */
|
|
16
|
+
export declare function autoRetry(api: Api, options?: AutoRetryOptions): void;
|
|
17
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,KAAK,WAAW,EAAiB,MAAM,cAAc,CAAC;AAEzE,MAAM,WAAW,gBAAgB;IAChC,0DAA0D;IAC1D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gEAAgE;IAChE,eAAe,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,gBAAqB,GAC5B,WAAW,GAAG,SAAS,CAsBzB;AAED,+DAA+D;AAC/D,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,GAAE,gBAAqB,GAAG,IAAI,CAExE"}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { TelegramError } from "@yaebal/core";
|
|
2
|
+
/**
|
|
3
|
+
* decide whether a failed call should be retried, and after how long.
|
|
4
|
+
* pure (no I/O) — exported so the policy is unit-testable on its own.
|
|
5
|
+
*/
|
|
6
|
+
export function decideRetry(error, attempt, options = {}) {
|
|
7
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
8
|
+
const maxDelayMs = options.maxDelayMs ?? 30_000;
|
|
9
|
+
const retryOnInternal = options.retryOnInternal ?? true;
|
|
10
|
+
if (attempt > maxRetries)
|
|
11
|
+
return undefined;
|
|
12
|
+
if (!(error instanceof TelegramError))
|
|
13
|
+
return undefined;
|
|
14
|
+
// 429: respect telegram's "retry after N", else exponential backoff.
|
|
15
|
+
if (error.code === 429) {
|
|
16
|
+
const retryAfter = parseRetryAfter(error.message);
|
|
17
|
+
const seconds = retryAfter ?? 2 ** attempt;
|
|
18
|
+
return { retry: true, delayMs: Math.min(seconds * 1000, maxDelayMs) };
|
|
19
|
+
}
|
|
20
|
+
// 5xx: transient server-side failure.
|
|
21
|
+
if (retryOnInternal && error.code >= 500) {
|
|
22
|
+
return { retry: true, delayMs: Math.min(2 ** attempt * 1000, maxDelayMs) };
|
|
23
|
+
}
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
/** install auto-retry on a bot's API: `autoRetry(bot.api)`. */
|
|
27
|
+
export function autoRetry(api, options = {}) {
|
|
28
|
+
api.onError((_method, error, attempt) => decideRetry(error, attempt, options));
|
|
29
|
+
}
|
|
30
|
+
function parseRetryAfter(message) {
|
|
31
|
+
const m = message.match(/retry after (\d+)/i);
|
|
32
|
+
return m ? Number(m[1]) : undefined;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAA8B,aAAa,EAAE,MAAM,cAAc,CAAC;AAWzE;;;GAGG;AACH,MAAM,UAAU,WAAW,CAC1B,KAAc,EACd,OAAe,EACf,UAA4B,EAAE;IAE9B,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC;IAC3C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,IAAI,MAAM,CAAC;IAChD,MAAM,eAAe,GAAG,OAAO,CAAC,eAAe,IAAI,IAAI,CAAC;IAExD,IAAI,OAAO,GAAG,UAAU;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,CAAC,CAAC,KAAK,YAAY,aAAa,CAAC;QAAE,OAAO,SAAS,CAAC;IAExD,qEAAqE;IACrE,IAAI,KAAK,CAAC,IAAI,KAAK,GAAG,EAAE,CAAC;QACxB,MAAM,UAAU,GAAG,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAClD,MAAM,OAAO,GAAG,UAAU,IAAI,CAAC,IAAI,OAAO,CAAC;QAE3C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,EAAE,UAAU,CAAC,EAAE,CAAC;IACvE,CAAC;IAED,sCAAsC;IACtC,IAAI,eAAe,IAAI,KAAK,CAAC,IAAI,IAAI,GAAG,EAAE,CAAC;QAC1C,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,OAAO,GAAG,IAAI,EAAE,UAAU,CAAC,EAAE,CAAC;IAC5E,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,SAAS,CAAC,GAAQ,EAAE,UAA4B,EAAE;IACjE,GAAG,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;AAChF,CAAC;AAED,SAAS,eAAe,CAAC,OAAe;IACvC,MAAM,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;IAE9C,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { TelegramError } from "@yaebal/core";
|
|
4
|
+
import { decideRetry } from "./index.js";
|
|
5
|
+
test("429 with retry_after waits exactly that long", () => {
|
|
6
|
+
assert.deepEqual(decideRetry(new TelegramError("sendMessage", 429, "Too Many Requests: retry after 7"), 1), { retry: true, delayMs: 7000 });
|
|
7
|
+
});
|
|
8
|
+
test("5xx retries with exponential backoff", () => {
|
|
9
|
+
assert.deepEqual(decideRetry(new TelegramError("sendMessage", 502, "Bad Gateway"), 1), {
|
|
10
|
+
retry: true,
|
|
11
|
+
delayMs: 2000,
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
test("stops once attempts exceed maxRetries", () => {
|
|
15
|
+
assert.equal(decideRetry(new TelegramError("sendMessage", 429, "retry after 1"), 4), undefined);
|
|
16
|
+
});
|
|
17
|
+
test("4xx client errors are not retried", () => {
|
|
18
|
+
assert.equal(decideRetry(new TelegramError("sendMessage", 400, "Bad Request"), 1), undefined);
|
|
19
|
+
});
|
|
20
|
+
test("non-Telegram errors are not retried", () => {
|
|
21
|
+
assert.equal(decideRetry(new Error("socket hang up"), 1), undefined);
|
|
22
|
+
});
|
|
23
|
+
test("maxDelayMs caps the wait", () => {
|
|
24
|
+
assert.deepEqual(decideRetry(new TelegramError("x", 429, "retry after 999"), 1, { maxDelayMs: 5000 }), { retry: true, delayMs: 5000 });
|
|
25
|
+
});
|
|
26
|
+
test("retryOnInternal:false skips 5xx", () => {
|
|
27
|
+
assert.equal(decideRetry(new TelegramError("x", 503, "Service Unavailable"), 1, { retryOnInternal: false }), undefined);
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC,IAAI,CAAC,8CAA8C,EAAE,GAAG,EAAE;IACzD,MAAM,CAAC,SAAS,CACf,WAAW,CAAC,IAAI,aAAa,CAAC,aAAa,EAAE,GAAG,EAAE,kCAAkC,CAAC,EAAE,CAAC,CAAC,EACzF,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAC9B,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,sCAAsC,EAAE,GAAG,EAAE;IACjD,MAAM,CAAC,SAAS,CAAC,WAAW,CAAC,IAAI,aAAa,CAAC,aAAa,EAAE,GAAG,EAAE,aAAa,CAAC,EAAE,CAAC,CAAC,EAAE;QACtF,KAAK,EAAE,IAAI;QACX,OAAO,EAAE,IAAI;KACb,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,uCAAuC,EAAE,GAAG,EAAE;IAClD,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,aAAa,CAAC,aAAa,EAAE,GAAG,EAAE,eAAe,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AACjG,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC9C,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,aAAa,CAAC,aAAa,EAAE,GAAG,EAAE,aAAa,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AAC/F,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,qCAAqC,EAAE,GAAG,EAAE;IAChD,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;AACtE,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACrC,MAAM,CAAC,SAAS,CACf,WAAW,CAAC,IAAI,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,iBAAiB,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,EACpF,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAC9B,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,iCAAiC,EAAE,GAAG,EAAE;IAC5C,MAAM,CAAC,KAAK,CACX,WAAW,CAAC,IAAI,aAAa,CAAC,GAAG,EAAE,GAAG,EAAE,qBAAqB,CAAC,EAAE,CAAC,EAAE,EAAE,eAAe,EAAE,KAAK,EAAE,CAAC,EAC9F,SAAS,CACT,CAAC;AACH,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yaebal/again",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "yaebal auto-retry plugin — retries on 429/flood-wait and transient 5xx.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./lib/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@yaebal/core": "0.0.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "latest"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"telegram",
|
|
29
|
+
"telegram-bot",
|
|
30
|
+
"yaebal",
|
|
31
|
+
"retry",
|
|
32
|
+
"flood-wait"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/neverlane/yaebal",
|
|
38
|
+
"directory": "packages/again"
|
|
39
|
+
},
|
|
40
|
+
"publishConfig": {
|
|
41
|
+
"access": "public"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.json",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
46
|
+
"test": "node --test lib"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
import { TelegramError } from "@yaebal/core";
|
|
4
|
+
import { decideRetry } from "./index.js";
|
|
5
|
+
|
|
6
|
+
test("429 with retry_after waits exactly that long", () => {
|
|
7
|
+
assert.deepEqual(
|
|
8
|
+
decideRetry(new TelegramError("sendMessage", 429, "Too Many Requests: retry after 7"), 1),
|
|
9
|
+
{ retry: true, delayMs: 7000 },
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("5xx retries with exponential backoff", () => {
|
|
14
|
+
assert.deepEqual(decideRetry(new TelegramError("sendMessage", 502, "Bad Gateway"), 1), {
|
|
15
|
+
retry: true,
|
|
16
|
+
delayMs: 2000,
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("stops once attempts exceed maxRetries", () => {
|
|
21
|
+
assert.equal(decideRetry(new TelegramError("sendMessage", 429, "retry after 1"), 4), undefined);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("4xx client errors are not retried", () => {
|
|
25
|
+
assert.equal(decideRetry(new TelegramError("sendMessage", 400, "Bad Request"), 1), undefined);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("non-Telegram errors are not retried", () => {
|
|
29
|
+
assert.equal(decideRetry(new Error("socket hang up"), 1), undefined);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("maxDelayMs caps the wait", () => {
|
|
33
|
+
assert.deepEqual(
|
|
34
|
+
decideRetry(new TelegramError("x", 429, "retry after 999"), 1, { maxDelayMs: 5000 }),
|
|
35
|
+
{ retry: true, delayMs: 5000 },
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test("retryOnInternal:false skips 5xx", () => {
|
|
40
|
+
assert.equal(
|
|
41
|
+
decideRetry(new TelegramError("x", 503, "Service Unavailable"), 1, { retryOnInternal: false }),
|
|
42
|
+
undefined,
|
|
43
|
+
);
|
|
44
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type Api, type ErrorAction, TelegramError } from "@yaebal/core";
|
|
2
|
+
|
|
3
|
+
export interface AutoRetryOptions {
|
|
4
|
+
/** max retries after the first attempt. defaults to 3. */
|
|
5
|
+
maxRetries?: number;
|
|
6
|
+
/** cap on a single wait, in ms. defaults to 30_000. */
|
|
7
|
+
maxDelayMs?: number;
|
|
8
|
+
/** also retry transient 5xx server errors. defaults to true. */
|
|
9
|
+
retryOnInternal?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* decide whether a failed call should be retried, and after how long.
|
|
14
|
+
* pure (no I/O) — exported so the policy is unit-testable on its own.
|
|
15
|
+
*/
|
|
16
|
+
export function decideRetry(
|
|
17
|
+
error: unknown,
|
|
18
|
+
attempt: number,
|
|
19
|
+
options: AutoRetryOptions = {},
|
|
20
|
+
): ErrorAction | undefined {
|
|
21
|
+
const maxRetries = options.maxRetries ?? 3;
|
|
22
|
+
const maxDelayMs = options.maxDelayMs ?? 30_000;
|
|
23
|
+
const retryOnInternal = options.retryOnInternal ?? true;
|
|
24
|
+
|
|
25
|
+
if (attempt > maxRetries) return undefined;
|
|
26
|
+
if (!(error instanceof TelegramError)) return undefined;
|
|
27
|
+
|
|
28
|
+
// 429: respect telegram's "retry after N", else exponential backoff.
|
|
29
|
+
if (error.code === 429) {
|
|
30
|
+
const retryAfter = parseRetryAfter(error.message);
|
|
31
|
+
const seconds = retryAfter ?? 2 ** attempt;
|
|
32
|
+
|
|
33
|
+
return { retry: true, delayMs: Math.min(seconds * 1000, maxDelayMs) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 5xx: transient server-side failure.
|
|
37
|
+
if (retryOnInternal && error.code >= 500) {
|
|
38
|
+
return { retry: true, delayMs: Math.min(2 ** attempt * 1000, maxDelayMs) };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** install auto-retry on a bot's API: `autoRetry(bot.api)`. */
|
|
45
|
+
export function autoRetry(api: Api, options: AutoRetryOptions = {}): void {
|
|
46
|
+
api.onError((_method, error, attempt) => decideRetry(error, attempt, options));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function parseRetryAfter(message: string): number | undefined {
|
|
50
|
+
const m = message.match(/retry after (\d+)/i);
|
|
51
|
+
|
|
52
|
+
return m ? Number(m[1]) : undefined;
|
|
53
|
+
}
|