You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.8 KiB

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<T>(key: string): Promise<T | null> {
return _cache.get<T>(key);
}
export async function setCache<T>(key: string, value: T, ttl?: number): Promise<void> {
return _cache.set(key, value, ttl);
}
export async function delCache(key: string): Promise<void> {
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<K extends KnownConfigKey>(key: K): Promise<KnownConfigValue<K>> {
return getGlobalConfigValue(key);
}
export async function getConfigUser<K extends KnownConfigKey>(event: H3Event, key: K): Promise<KnownConfigValue<K> | undefined> {
const auth = getAuth(event);
const user = await auth.getCurrent();
return getUserConfigValue(user?.id ?? undefined, key);
}
export async function getConfig<K extends KnownConfigKey>(event: H3Event, key: K): Promise<KnownConfigValue<K>> {
const auth = getAuth(event);
const user = await auth.getCurrent();
return getMergedConfigValue(user?.id ?? undefined, key);
}