forked from RiseUP/riseup_squad_03
modified: src/components/AppShell.jsx modified: src/config/api.js modified: src/config/permissions.js modified: src/data/mockData.js modified: src/hooks/useAgenda.js modified: src/hooks/useAuth.js modified: src/mappers/appointmentMapper.js modified: src/pages/AgendaPage.jsx modified: src/pages/AuthPages.jsx modified: src/pages/HomePage.jsx modified: src/pages/MedicalRecordsPage.jsx modified: src/pages/MessagesPage.jsx modified: src/pages/NotFoundPage.jsx modified: src/pages/PatientsPage.jsx modified: src/pages/ReportsPage.jsx modified: src/pages/TeamPage.jsx modified: src/pages/UsersPage.jsx modified: src/pages/VisitsPage.jsx modified: src/repositories/authRepository.js new file: src/repositories/availabilityRepository.js modified: src/repositories/communicationRepository.js modified: src/repositories/patientRepository.js modified: src/repositories/professionalRepository.js modified: src/repositories/profileRepository.js modified: src/repositories/reportRepository.js modified: src/repositories/repositoryUtils.js modified: src/repositories/settingsRepository.js modified: src/repositories/userRepository.js modified: src/repositories/visitRepository.js
96 lines
2.0 KiB
JavaScript
96 lines
2.0 KiB
JavaScript
export async function fetchJsonWithFallback(requests, fallbackMessage) {
|
|
let lastResponse = null
|
|
let lastError = null
|
|
|
|
for (const request of requests) {
|
|
let response
|
|
|
|
try {
|
|
response = await fetch(request.url, request.options)
|
|
lastResponse = response
|
|
} catch (error) {
|
|
lastError = error
|
|
continue
|
|
}
|
|
|
|
if (response.ok) {
|
|
return parseJsonResponse(response)
|
|
}
|
|
|
|
if (!shouldFallback(response)) {
|
|
throw new Error(await getResponseError(response, fallbackMessage))
|
|
}
|
|
}
|
|
|
|
if (lastError && !lastResponse) {
|
|
throw new Error(lastError.message || fallbackMessage)
|
|
}
|
|
|
|
throw new Error(await getResponseError(lastResponse, fallbackMessage))
|
|
}
|
|
|
|
export function normalizeCollection(data, keys = []) {
|
|
if (Array.isArray(data)) return data
|
|
|
|
for (const key of keys) {
|
|
if (Array.isArray(data?.[key])) return data[key]
|
|
}
|
|
|
|
return []
|
|
}
|
|
|
|
export function normalizeItem(data, keys = []) {
|
|
if (Array.isArray(data)) return data[0] || null
|
|
|
|
for (const key of keys) {
|
|
if (data?.[key]) return data[key]
|
|
}
|
|
|
|
return data || null
|
|
}
|
|
|
|
export async function getResponseError(response, fallbackMessage) {
|
|
if (!response) return fallbackMessage
|
|
|
|
const text = await response.text().catch(() => '')
|
|
const error = parseErrorBody(text)
|
|
const message =
|
|
error.error_description ||
|
|
error.msg ||
|
|
error.message ||
|
|
error.error ||
|
|
error.details ||
|
|
error.hint ||
|
|
text ||
|
|
fallbackMessage
|
|
|
|
return response.status ? `${fallbackMessage} (${response.status}): ${message}` : message
|
|
}
|
|
|
|
function shouldFallback(response) {
|
|
return [404, 405].includes(response.status)
|
|
}
|
|
|
|
async function parseJsonResponse(response) {
|
|
if (response.status === 204) return null
|
|
|
|
const text = await response.text()
|
|
if (!text) return null
|
|
|
|
try {
|
|
return JSON.parse(text)
|
|
} catch {
|
|
return { message: text }
|
|
}
|
|
}
|
|
|
|
function parseErrorBody(text) {
|
|
if (!text) return {}
|
|
|
|
try {
|
|
return JSON.parse(text)
|
|
} catch {
|
|
return { message: text }
|
|
}
|
|
}
|