@thorprovider/medusa-extended 1.1.1 → 1.2.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.
- package/AGENTS.md +1 -1
- package/CHANGELOG.md +14 -0
- package/README.md +10 -3
- package/dist/index.d.mts +30 -4
- package/dist/index.d.ts +30 -4
- package/dist/index.js +122 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +121 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/admin.ts +2 -1
- package/src/dropshipper.test.ts +50 -3
- package/src/dropshipper.ts +75 -9
- package/src/request-resilience.ts +81 -0
- package/dist/.tsbuildinfo +0 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ProviderAPIError, backendCircuitBreaker } from '@thorprovider/adapters'
|
|
2
|
+
|
|
3
|
+
type RetryConfig = {
|
|
4
|
+
maxRetries?: number
|
|
5
|
+
initialDelayMs?: number
|
|
6
|
+
backoffMultiplier?: number
|
|
7
|
+
maxDelayMs?: number
|
|
8
|
+
timeoutMs?: number
|
|
9
|
+
retryableStatusCodes?: number[]
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const DEFAULT_RETRY_CONFIG: Required<RetryConfig> = {
|
|
13
|
+
maxRetries: 3,
|
|
14
|
+
initialDelayMs: 300,
|
|
15
|
+
backoffMultiplier: 2,
|
|
16
|
+
maxDelayMs: 5000,
|
|
17
|
+
timeoutMs: 10000,
|
|
18
|
+
retryableStatusCodes: [408, 429, 500, 502, 503, 504],
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sleep(ms: number): Promise<void> {
|
|
22
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function calculateDelay(attempt: number, config: Required<RetryConfig>): number {
|
|
26
|
+
const delay = config.initialDelayMs * Math.pow(config.backoffMultiplier, attempt)
|
|
27
|
+
return Math.min(delay, config.maxDelayMs)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function isRetryableError(error: unknown, config: Required<RetryConfig>): boolean {
|
|
31
|
+
const err = error as { name?: string; message?: string; status?: number }
|
|
32
|
+
|
|
33
|
+
if (err.name === 'TypeError' || err.message?.includes('fetch failed')) return true
|
|
34
|
+
if (err.name === 'AbortError' || err.message?.includes('timeout')) return true
|
|
35
|
+
if (typeof err.status === 'number' && config.retryableStatusCodes.includes(err.status)) return true
|
|
36
|
+
return false
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function fetchWithRetry(url: string, init: RequestInit, retryConfig?: RetryConfig): Promise<Response> {
|
|
40
|
+
const config = { ...DEFAULT_RETRY_CONFIG, ...retryConfig }
|
|
41
|
+
let lastError: unknown
|
|
42
|
+
|
|
43
|
+
for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
|
|
44
|
+
try {
|
|
45
|
+
const controller = new AbortController()
|
|
46
|
+
const timeoutId = setTimeout(() => controller.abort(), config.timeoutMs)
|
|
47
|
+
|
|
48
|
+
const response = await fetch(url, { ...init, signal: controller.signal })
|
|
49
|
+
clearTimeout(timeoutId)
|
|
50
|
+
|
|
51
|
+
if (!response.ok && config.retryableStatusCodes.includes(response.status)) {
|
|
52
|
+
const error = new Error(`HTTP ${response.status}: ${response.statusText}`) as Error & { status?: number }
|
|
53
|
+
error.status = response.status
|
|
54
|
+
throw error
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return response
|
|
58
|
+
} catch (error) {
|
|
59
|
+
lastError = error
|
|
60
|
+
const shouldRetry = attempt < config.maxRetries && isRetryableError(error, config)
|
|
61
|
+
|
|
62
|
+
if (shouldRetry) {
|
|
63
|
+
await sleep(calculateDelay(attempt, config))
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
break
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
throw lastError instanceof Error ? lastError : new Error('Backend request failed')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function fetchBackendWithResilience(url: string, init: RequestInit, retryConfig?: RetryConfig): Promise<Response> {
|
|
75
|
+
try {
|
|
76
|
+
return await backendCircuitBreaker.execute(() => fetchWithRetry(url, init, retryConfig))
|
|
77
|
+
} catch (error) {
|
|
78
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
79
|
+
throw new ProviderAPIError(`Backend request failed: ${message}`, 'backend', 503)
|
|
80
|
+
}
|
|
81
|
+
}
|