@huevo/plugin-retry 0.0.0-stage → 0.1.0

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.
@@ -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,22 @@
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.0",
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": ["dist"],
15
+ "scripts": {
16
+ "build": "tsc -p tsconfig.json"
17
+ },
18
+ "license": "MIT",
19
+ "peerDependencies": {
20
+ "huevo": "^0.1.0"
21
+ }
22
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
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.