import "server-only"; import { getDb, apiKeys, fetchRequests, eq, and, isNull, desc } from "@fetcha/db"; /** Keys shown in the Playground code generator. Never includes the hash or plaintext. */ export interface PlaygroundKeyRow { id: string; name: string; keyPrefix: string; last4: string; mode: string; } export async function listPlaygroundKeys(projectId: string, organizationId: string): Promise { const db = getDb(); return db .select({ id: apiKeys.id, name: apiKeys.name, keyPrefix: apiKeys.keyPrefix, last4: apiKeys.last4, mode: apiKeys.mode }) .from(apiKeys) .where(and(eq(apiKeys.projectId, projectId), eq(apiKeys.organizationId, organizationId), isNull(apiKeys.revokedAt))) .orderBy(desc(apiKeys.createdAt)) .limit(50); } export interface ReplayRequestRow { id: string; url: string; method: string; requestedNetwork: string; country: string | null; region: string | null; city: string | null; format: string; sessionId: string | null; } /** Load a past request (scoped to the current project) so the Playground can replay it. */ export async function getReplayRequest(projectId: string, requestId: string): Promise { if (!/^req_[A-Za-z0-9_-]{4,64}$/.test(requestId)) return null; const db = getDb(); const [row] = await db .select({ id: fetchRequests.id, url: fetchRequests.url, method: fetchRequests.method, requestedNetwork: fetchRequests.requestedNetwork, country: fetchRequests.country, region: fetchRequests.region, city: fetchRequests.city, format: fetchRequests.format, sessionId: fetchRequests.sessionId, }) .from(fetchRequests) .where(and(eq(fetchRequests.id, requestId), eq(fetchRequests.projectId, projectId))) .limit(1); return row ?? null; }