Fix API key error states and verify revocation lifecycle
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { apiKeyRequest } from './apiKeys.js';
|
||||
import { sessionGuard } from './sessionGuard.js';
|
||||
/**
|
||||
* Sunucu tarafı hesap servisleri: plan/kota, kullanıcı verisi, API anahtarları, belgeler.
|
||||
@@ -37,9 +38,9 @@ export const setUserData = (anahtar, deger) =>
|
||||
iste('/api/me/data', { method: 'PUT', body: JSON.stringify({ anahtar, deger }) });
|
||||
|
||||
// --- API anahtarları ---
|
||||
export const listApiKeys = () => iste('/api/me/apikeys');
|
||||
export const createApiKey = (ad) => iste('/api/me/apikeys', { method: 'POST', body: JSON.stringify({ ad }) });
|
||||
export const revokeApiKey = (id) => iste(`/api/me/apikeys?id=${encodeURIComponent(id)}`, { method: 'DELETE' });
|
||||
export const listApiKeys = () => apiKeyRequest();
|
||||
export const createApiKey = (ad) => apiKeyRequest('POST', { ad });
|
||||
export const revokeApiKey = (id) => apiKeyRequest('DELETE', { id });
|
||||
|
||||
// --- Belgeler ---
|
||||
export const listDocuments = () => iste('/api/me/documents');
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { getToken } from './auth.js';
|
||||
|
||||
// A lost mutation response is uncertain: never automatically repeat key creation.
|
||||
export async function apiKeyRequest(method = 'GET', { id, ad, timeoutMs = 30000 } = {}) {
|
||||
const token = getToken();
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
||||
try {
|
||||
const response = await fetch(`/api/me/apikeys${id ? `?id=${encodeURIComponent(id)}` : ''}`, {
|
||||
method, signal: controller.signal,
|
||||
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
||||
...(method === 'POST' ? { body: JSON.stringify({ ad }) } : {}),
|
||||
});
|
||||
const data = await response.json();
|
||||
if (getToken() !== token) throw new Error('Oturum değişti; anahtar ekranını yeniden açın.');
|
||||
if (!response.ok || data?.success !== true) throw new Error(data?.error || 'API anahtarı işlemi tamamlanamadı.');
|
||||
const valid = method === 'POST'
|
||||
? typeof data.anahtar?.id === 'string' && typeof data.anahtar?.anahtar === 'string' && data.anahtar.anahtar.startsWith('ih_live_')
|
||||
: Array.isArray(data.anahtarlar) && data.anahtarlar.every(k => k && typeof k.id === 'string' && typeof k.ad === 'string') && (data.limit === null || (Number.isFinite(data.limit) && data.limit >= 0));
|
||||
if (!valid) throw new Error('Anahtar bilgileri eksik veya geçersiz. Listeyi yeniden yükleyin.');
|
||||
return data;
|
||||
} catch (error) {
|
||||
if (controller.signal.aborted) throw new Error(method === 'GET'
|
||||
? 'Anahtar listesi zamanında alınamadı. Yeniden deneyin.'
|
||||
: 'İşlem sonucu alınamadı. Tekrar işlem yapmadan önce listeyi yenileyin; işlem sunucuda gerçekleşmiş olabilir.');
|
||||
if (error instanceof SyntaxError) throw new Error('Sunucu geçerli anahtar bilgisi döndürmedi. Listeyi yeniden yükleyin.');
|
||||
throw error;
|
||||
} finally { clearTimeout(timer); }
|
||||
}
|
||||
Reference in New Issue
Block a user