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.
 
 
 
 

41 lines
1.5 KiB

import { getCache, setCache, getCurrentUser, getConfigGlobal } from "#server/utils/context";
import { KNOWN_CONFIG_KEYS } from "#server/service/config/registry";
import type { KnownConfigKey, KnownConfigValue } from "#server/service/config/registry";
const PUBLIC_GLOBAL_CONFIG_KEYS = [
"siteName",
"allowRegister",
] as const satisfies readonly KnownConfigKey[];
const SECRET_MASKED_GLOBAL_CONFIG_KEYS = new Set<KnownConfigKey>([]);
export default defineWrappedResponseHandler(async (event) => {
const user = await getCurrentUser(event);
const isAdmin = user?.role === "admin";
const cacheKey = isAdmin ? "config:global:admin" : "config:global:public";
const cached = await getCache<{ config: Record<string, unknown> }>(cacheKey);
if (cached) return R.success(cached);
const keys: readonly KnownConfigKey[] = isAdmin ? KNOWN_CONFIG_KEYS : PUBLIC_GLOBAL_CONFIG_KEYS;
const entries = await Promise.all(
keys.map(async (key) => {
const value = await getConfigGlobal(key);
const safeValue =
SECRET_MASKED_GLOBAL_CONFIG_KEYS.has(key)
? ""
: value;
return [key, safeValue] as const satisfies [KnownConfigKey, KnownConfigValue<KnownConfigKey>];
}),
);
const config = Object.fromEntries(entries);
if (!isAdmin) {
await setCache(cacheKey, { config }, 300);
return R.success({ config });
}
const result = { config: { ...config } };
await setCache(cacheKey, result, 300);
return R.success(result);
});