import type { H3Event } from "h3"; import { createCache } from "cache"; import { createAuthContext } from "../service/auth/context"; import { getGlobalConfigValue, getMergedConfigValue, getUserConfigValue } from "../service/config"; import type { KnownConfigKey, KnownConfigValue } from "../service/config/registry"; // ============= Cache ============= const _cache = createCache({ memory: true, }); export const cache = _cache; export async function getCache(key: string): Promise { return _cache.get(key); } export async function setCache(key: string, value: T, ttl?: number): Promise { return _cache.set(key, value, ttl); } export async function delCache(key: string): Promise { return _cache.del(key); } // ============= Auth ============= export function getAuth(event: H3Event) { return createAuthContext(event); } export async function getCurrentUser(event: H3Event) { const auth = getAuth(event); return auth.getCurrent(); } export async function requireUser(event: H3Event) { const auth = getAuth(event); return auth.requireUser(); } // ============= Config ============= export async function getConfigGlobal(key: K): Promise> { return getGlobalConfigValue(key); } export async function getConfigUser(event: H3Event, key: K): Promise | undefined> { const auth = getAuth(event); const user = await auth.getCurrent(); return getUserConfigValue(user?.id ?? undefined, key); } export async function getConfig(event: H3Event, key: K): Promise> { const auth = getAuth(event); const user = await auth.getCurrent(); return getMergedConfigValue(user?.id ?? undefined, key); }