fix and update

This commit is contained in:
lendry
2026-06-29 21:22:20 +03:00
parent 7233e8b70a
commit 8369abb023
7 changed files with 98 additions and 23 deletions

View File

@@ -434,8 +434,13 @@ export function isPinRequiredError(error: unknown): error is ApiError {
return error instanceof ApiError && error.code === 'PIN_REQUIRED';
}
export function isGatewayUnavailableError(error: unknown): boolean {
return error instanceof ApiError && [502, 503, 504].includes(error.status);
}
export function getApiErrorMessage(error: unknown, fallback: string): string | null {
if (isPinRequiredError(error)) return null;
if (isGatewayUnavailableError(error)) return null;
if (error instanceof ApiError && error.status === 403 && error.message.toLowerCase().includes('pin')) {
return null;
}
@@ -522,6 +527,7 @@ export async function refreshAuthSession(): Promise<RefreshSessionResponse> {
export async function syncFedcmSession(accessToken: string) {
if (typeof window === 'undefined' || !accessToken.trim()) return;
try {
await ensureApiGatewayReady();
await fetchWithGatewayRetry(`${getApiUrl()}/fedcm/session/sync`, {
method: 'POST',
headers: { Authorization: `Bearer ${accessToken.trim()}` },
@@ -625,12 +631,58 @@ export function buildSystemSettingPayload(setting: Pick<SystemSetting, 'key' | '
}
const GATEWAY_RETRY_STATUS = new Set([502, 503, 504]);
const GATEWAY_RETRY_ATTEMPTS = 4;
const GATEWAY_RETRY_ATTEMPTS = 6;
const GATEWAY_WARMUP_MAX_ATTEMPTS = 24;
const GATEWAY_WARMUP_DELAY_MS = 300;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
let gatewayReadyResolved = false;
let gatewayReadyPromise: Promise<void> | null = null;
export function resetApiGatewayWarmup() {
gatewayReadyResolved = false;
gatewayReadyPromise = null;
}
export function markApiGatewayReady() {
gatewayReadyResolved = true;
}
async function probeGatewayHealth(): Promise<boolean> {
try {
const response = await fetch(`${getApiUrl()}/health`, {
method: 'GET',
cache: 'no-store'
});
return response.ok;
} catch {
return false;
}
}
/** Ждёт готовности api-gateway (кратковременные 502 после перезапуска nginx/upstream). */
export async function ensureApiGatewayReady(force = false): Promise<void> {
if (typeof window === 'undefined') return;
if (gatewayReadyResolved && !force) return;
if (gatewayReadyPromise && !force) return gatewayReadyPromise;
gatewayReadyPromise = (async () => {
for (let attempt = 0; attempt < GATEWAY_WARMUP_MAX_ATTEMPTS; attempt++) {
if (await probeGatewayHealth()) {
gatewayReadyResolved = true;
return;
}
await sleep(GATEWAY_WARMUP_DELAY_MS);
}
gatewayReadyResolved = true;
})();
return gatewayReadyPromise;
}
/** Повтор при кратковременном 502/503/504 (nginx не достучался до api-gateway после перезапуска). */
async function fetchWithGatewayRetry(url: string, init: RequestInit): Promise<Response> {
let lastNetworkError: unknown;
@@ -655,6 +707,10 @@ async function fetchWithGatewayRetry(url: string, init: RequestInit): Promise<Re
}
export async function apiFetch<T>(path: string, options: RequestInit = {}, token?: string | null, allowRetry = true): Promise<T> {
if (typeof window !== 'undefined' && path !== '/health' && !path.startsWith('/health?')) {
await ensureApiGatewayReady();
}
const resolvedToken = token ?? (typeof window !== 'undefined' ? window.localStorage.getItem(AUTH_TOKEN_KEY) : null);
const method = (options.method ?? 'GET').toUpperCase();
const hasBody = options.body !== undefined && options.body !== null;