import apiService from './api.service' import { applicationConfigurationUrl } from './abpConfig.service' export interface SetupStatusDto { dbExists: boolean error?: string } export const getSetupStatus = () => apiService.fetchData({ method: 'GET', url: '/api/setup/status', }) export const getSetupMigrate = () => apiService.fetchData({ method: 'POST', url: '/api/setup/migrate', }) export const getMigrateUrl = (): string => { const base = import.meta.env.VITE_API_URL ?? '' return `${base}/api/setup/migrate` } /** * Sunucu yeniden başlayana kadar ABP config endpoint'ini poll eder. * Arka arkaya 2 başarılı yanıt alındığında onReady çağrılır. * @param onReady Sunucu hazır olduğunda çağrılacak callback * @param onAttempt Her denemede kaçıncı deneme olduğunu bildiren opsiyonel callback */ export const pollUntilServerReady = (onReady: () => void, onAttempt?: (attempt: number) => void): (() => void) => { const REQUIRED_SUCCESS = 2 let attempt = 0 let successCount = 0 let timerId: ReturnType | null = null let cancelled = false const tick = async () => { if (cancelled) return attempt++ onAttempt?.(attempt) try { const res = await fetch( `${import.meta.env.VITE_API_URL ?? ''}${applicationConfigurationUrl(false)}`, { method: 'GET', headers: { Accept: 'application/json' }, cache: 'no-store' }, ) if (res.status === 200) { try { const json = await res.json() if (json && typeof json.currentUser === 'object') { successCount++ if (successCount >= REQUIRED_SUCCESS) { onReady() return } timerId = setTimeout(tick, 1000) return } } catch { /* parse hatası */ } } successCount = 0 } catch { successCount = 0 } timerId = setTimeout(tick, 2000) } // İlk denemeden önce kısa bekleme (sunucunun kapanma süresi) timerId = setTimeout(tick, 3000) // İptal fonksiyonu döner return () => { cancelled = true if (timerId) clearTimeout(timerId) } }