SPB Git forge

spb/fetcha

Public
11commits 1branches 0releases
1.5 MBsize
maindefault branch
16 days agolast push
TypeScript 97.5% SQL 1.4% Python 0.8%
1.8 KB · 56 lines typescript
Raw Blame History
1import "server-only";2import { getDb, apiKeys, fetchRequests, eq, and, isNull, desc } from "@fetcha/db";34/** Keys shown in the Playground code generator. Never includes the hash or plaintext. */5export interface PlaygroundKeyRow {6  id: string;7  name: string;8  keyPrefix: string;9  last4: string;10  mode: string;11}1213export async function listPlaygroundKeys(projectId: string, organizationId: string): Promise<PlaygroundKeyRow[]> {14  const db = getDb();15  return db16    .select({ id: apiKeys.id, name: apiKeys.name, keyPrefix: apiKeys.keyPrefix, last4: apiKeys.last4, mode: apiKeys.mode })17    .from(apiKeys)18    .where(and(eq(apiKeys.projectId, projectId), eq(apiKeys.organizationId, organizationId), isNull(apiKeys.revokedAt)))19    .orderBy(desc(apiKeys.createdAt))20    .limit(50);21}2223export interface ReplayRequestRow {24  id: string;25  url: string;26  method: string;27  requestedNetwork: string;28  country: string | null;29  region: string | null;30  city: string | null;31  format: string;32  sessionId: string | null;33}3435/** Load a past request (scoped to the current project) so the Playground can replay it. */36export async function getReplayRequest(projectId: string, requestId: string): Promise<ReplayRequestRow | null> {37  if (!/^req_[A-Za-z0-9_-]{4,64}$/.test(requestId)) return null;38  const db = getDb();39  const [row] = await db40    .select({41      id: fetchRequests.id,42      url: fetchRequests.url,43      method: fetchRequests.method,44      requestedNetwork: fetchRequests.requestedNetwork,45      country: fetchRequests.country,46      region: fetchRequests.region,47      city: fetchRequests.city,48      format: fetchRequests.format,49      sessionId: fetchRequests.sessionId,50    })51    .from(fetchRequests)52    .where(and(eq(fetchRequests.id, requestId), eq(fetchRequests.projectId, projectId)))53    .limit(1);54  return row ?? null;55}56