import 'server-only' import { asAdmin } from '@/lib/db/with-user' import { appSettings } from '@/lib/db/schema' import { eq } from 'drizzle-orm' export async function getApiKey(key: string): Promise { try { const rows = await asAdmin(db => db.select({ value: appSettings.value }) .from(appSettings) .where(eq(appSettings.key, key)) .limit(1) ) if (rows[0]?.value) return rows[0].value } catch { // fall through to env } const env = process.env[key] if (env) return env throw new Error(`${key} not configured. Add it in Settings (/hse/settings) or as an environment variable.`) }