@astroway/sdk 0.1.0-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.
- package/CHANGELOG.md +24 -0
- package/LICENSE +21 -0
- package/README.md +222 -0
- package/dist/errors.d.ts +78 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +98 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +122 -0
- package/dist/index.js.map +1 -0
- package/dist/retry.d.ts +21 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +53 -0
- package/dist/retry.js.map +1 -0
- package/dist/types.generated.d.ts +76788 -0
- package/dist/types.generated.d.ts.map +1 -0
- package/dist/types.generated.js +6 -0
- package/dist/types.generated.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/package.json +75 -0
package/dist/retry.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry helper. Default — 2 retries, exponential backoff with full jitter,
|
|
3
|
+
* on connection errors / 408 / 409 / 429 / 5xx. Honors `Retry-After`
|
|
4
|
+
* (seconds or HTTP-date) when present on 429.
|
|
5
|
+
*
|
|
6
|
+
* openapi-fetch middleware can't trigger a re-fetch internally, so the SDK
|
|
7
|
+
* wraps each call with `fetchWithRetry()` from inside its custom `fetch`
|
|
8
|
+
* adapter — see src/index.ts.
|
|
9
|
+
*/
|
|
10
|
+
export interface RetryOptions {
|
|
11
|
+
/** Number of retries on top of the initial request. Default 2 — total 3 attempts. */
|
|
12
|
+
maxRetries?: number;
|
|
13
|
+
/** Base delay in ms. Default 250. Actual: random(0, base * 2^attempt). */
|
|
14
|
+
baseDelayMs?: number;
|
|
15
|
+
/** Max delay cap per attempt. Default 30_000. */
|
|
16
|
+
maxDelayMs?: number;
|
|
17
|
+
/** Status codes considered retryable. Default 408/409/429/5xx. */
|
|
18
|
+
retryableStatuses?: ReadonlySet<number>;
|
|
19
|
+
}
|
|
20
|
+
export declare function fetchWithRetry(doFetch: () => Promise<Response>, opts?: RetryOptions): Promise<Response>;
|
|
21
|
+
//# sourceMappingURL=retry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../src/retry.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,MAAM,WAAW,YAAY;IAC3B,qFAAqF;IACrF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0EAA0E;IAC1E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iDAAiD;IACjD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kEAAkE;IAClE,iBAAiB,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CACzC;AAmBD,wBAAsB,cAAc,CAClC,OAAO,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,EAChC,IAAI,GAAE,YAAiB,GACtB,OAAO,CAAC,QAAQ,CAAC,CAsBnB"}
|
package/dist/retry.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry helper. Default — 2 retries, exponential backoff with full jitter,
|
|
3
|
+
* on connection errors / 408 / 409 / 429 / 5xx. Honors `Retry-After`
|
|
4
|
+
* (seconds or HTTP-date) when present on 429.
|
|
5
|
+
*
|
|
6
|
+
* openapi-fetch middleware can't trigger a re-fetch internally, so the SDK
|
|
7
|
+
* wraps each call with `fetchWithRetry()` from inside its custom `fetch`
|
|
8
|
+
* adapter — see src/index.ts.
|
|
9
|
+
*/
|
|
10
|
+
const DEFAULT_RETRYABLE = new Set([408, 409, 429, 500, 502, 503, 504]);
|
|
11
|
+
function parseRetryAfter(header) {
|
|
12
|
+
if (!header)
|
|
13
|
+
return undefined;
|
|
14
|
+
const seconds = Number(header);
|
|
15
|
+
if (Number.isFinite(seconds) && seconds >= 0)
|
|
16
|
+
return seconds * 1000;
|
|
17
|
+
const parsed = Date.parse(header);
|
|
18
|
+
if (!Number.isNaN(parsed)) {
|
|
19
|
+
const wait = parsed - Date.now();
|
|
20
|
+
return wait > 0 ? wait : 0;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
function jitterDelay(attempt, baseDelayMs, maxDelayMs) {
|
|
25
|
+
const upper = Math.min(maxDelayMs, baseDelayMs * 2 ** attempt);
|
|
26
|
+
return Math.floor(Math.random() * upper);
|
|
27
|
+
}
|
|
28
|
+
export async function fetchWithRetry(doFetch, opts = {}) {
|
|
29
|
+
const maxRetries = opts.maxRetries ?? 2;
|
|
30
|
+
const baseDelayMs = opts.baseDelayMs ?? 250;
|
|
31
|
+
const maxDelayMs = opts.maxDelayMs ?? 30_000;
|
|
32
|
+
const retryable = opts.retryableStatuses ?? DEFAULT_RETRYABLE;
|
|
33
|
+
let lastError;
|
|
34
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
35
|
+
try {
|
|
36
|
+
const res = await doFetch();
|
|
37
|
+
if (!retryable.has(res.status) || attempt === maxRetries)
|
|
38
|
+
return res;
|
|
39
|
+
const retryAfterMs = parseRetryAfter(res.headers.get('retry-after'));
|
|
40
|
+
const delay = retryAfterMs ?? jitterDelay(attempt, baseDelayMs, maxDelayMs);
|
|
41
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
lastError = e;
|
|
45
|
+
if (attempt === maxRetries)
|
|
46
|
+
throw e;
|
|
47
|
+
const delay = jitterDelay(attempt, baseDelayMs, maxDelayMs);
|
|
48
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
throw lastError ?? new Error('retry loop exhausted without response');
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0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