@huevo/plugin-retry 0.0.0-stage → 0.1.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/README.md CHANGED
@@ -1,3 +1,33 @@
1
- # Temporary Holding Version
1
+ # @huevo/plugin-retry
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Авторетраи на slow-рейт-лимиты для [huevo](https://www.npmjs.com/package/huevo).
4
+
5
+ Любой эндпоинт huevo.su может ответить `{"error":"slow","retry":N}` — плагин
6
+ ждёт `retry` секунд и повторяет запрос сам, незаметно для вашего кода.
7
+
8
+ ```
9
+ npm i huevo @huevo/plugin-retry
10
+ ```
11
+
12
+ ```ts
13
+ import { Huevo } from "huevo";
14
+ import { retry } from "@huevo/plugin-retry";
15
+
16
+ const huevo = Huevo.fromToken(session);
17
+ huevo.use(retry({ maxRetries: 5 }));
18
+
19
+ await huevo.clicker.post(400); // slow? подождёт и повторит — вы этого не заметите
20
+ ```
21
+
22
+ ## опции
23
+
24
+ | Опция | По умолчанию | Описание |
25
+ |---|---|---|
26
+ | `maxRetries` | `3` | максимум повторов подряд на один запрос |
27
+ | `margin` | `1` | запас к `retry`-секундам сервера, сек |
28
+ | `onRetry` | — | колбэк `(waitMs, opts, attempt)` перед каждым повтором |
29
+
30
+ Работает через response-интерцепторы ядра (`huevo.http.interceptors`) — ядро
31
+ не содержит логики ретраев, плагин добавляет её снаружи.
32
+
33
+ [Репозиторий](https://github.com/neverlane/huevo/tree/master/packages/retry) · MIT
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @huevo/plugin-retry — авторетраи на рейт-лимит.
3
+ *
4
+ * huevo.use(retry()) — и `{"error":"slow","retry":N}` от любого эндпоинта
5
+ * больше не ваши проблемы: SDK ждёт и повторяет сам.
6
+ *
7
+ * ```ts
8
+ * huevo.use(retry({ maxRetries: 5 }));
9
+ * ```
10
+ */
11
+ import type { HuevoPlugin, RequestOpts } from 'huevo';
12
+ export interface RetryOptions {
13
+ /** Максимум повторов подряд на один запрос. По умолчанию 3 */
14
+ maxRetries?: number;
15
+ /** Запас к retry-секундам сервера, сек. По умолчанию 1 */
16
+ margin?: number;
17
+ /** Колбэк перед каждым повтором */
18
+ onRetry?: (waitMs: number, opts: RequestOpts, attempt: number) => void;
19
+ }
20
+ export declare function retry(options?: RetryOptions): HuevoPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
2
+ export function retry(options = {}) {
3
+ const maxRetries = options.maxRetries ?? 3;
4
+ const margin = options.margin ?? 1;
5
+ return (huevo) => {
6
+ const attempts = new WeakMap();
7
+ const interceptor = async (data, opts, retryFn) => {
8
+ if (!data || typeof data !== 'object' ||
9
+ data.error !== 'slow') {
10
+ return undefined; // не slow — не трогаем
11
+ }
12
+ const attempt = (attempts.get(opts) ?? 0) + 1;
13
+ attempts.set(opts, attempt);
14
+ if (attempt > maxRetries)
15
+ return undefined;
16
+ const retrySec = Number(data.retry) || 8;
17
+ const waitMs = (retrySec + margin) * 1000;
18
+ options.onRetry?.(waitMs, opts, attempt);
19
+ await sleep(waitMs);
20
+ return retryFn();
21
+ };
22
+ huevo.http.interceptors.push(interceptor);
23
+ return () => {
24
+ const i = huevo.http.interceptors.indexOf(interceptor);
25
+ if (i >= 0)
26
+ huevo.http.interceptors.splice(i, 1);
27
+ };
28
+ };
29
+ }
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@huevo/plugin-retry",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "Авторетраи на slow-рейт-лимиты для huevo SDK",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsc -p tsconfig.json"
19
+ },
20
+ "license": "MIT",
21
+ "peerDependencies": {
22
+ "huevo": "^0.1.0"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "https://github.com/neverlane/huevo.git",
27
+ "directory": "packages/retry"
28
+ },
29
+ "homepage": "https://github.com/neverlane/huevo/tree/master/packages/retry"
30
+ }