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