fix(rates): fallback на прямой fetch если прокси недоступен
Made-with: Cursor
This commit is contained in:
+13
-11
@@ -56,18 +56,20 @@ function App() {
|
||||
if (!settings?.ratesUrl) {
|
||||
return
|
||||
}
|
||||
const proxyUrl = `/api/rates-proxy?url=${encodeURIComponent(settings.ratesUrl)}`
|
||||
fetch(proxyUrl)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error('Не удалось получить курсы валют')
|
||||
}
|
||||
return response.json()
|
||||
})
|
||||
.then((payload) => {
|
||||
setRatesData(normalizeRatesPayload(payload) ?? payload)
|
||||
setRatesError('')
|
||||
const directUrl = settings.ratesUrl
|
||||
const proxyUrl = `/api/rates-proxy?url=${encodeURIComponent(directUrl)}`
|
||||
const handlePayload = (payload) => {
|
||||
setRatesData(normalizeRatesPayload(payload) ?? payload)
|
||||
setRatesError('')
|
||||
}
|
||||
const fetchJson = (url) =>
|
||||
fetch(url).then((r) => {
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`)
|
||||
return r.json()
|
||||
})
|
||||
fetchJson(proxyUrl)
|
||||
.then(handlePayload)
|
||||
.catch(() => fetchJson(directUrl).then(handlePayload))
|
||||
.catch((error) => {
|
||||
setRatesError(error.message || 'Ошибка загрузки курсов')
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user