Add explicit runtime checks to prevent runtime config functions from being called on the client. This guards against accidental client-side invocation by throwing errors if executed outside the server environment.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { ensureAdminSeed } from '@/lib/server/admin/seed';
|
|
import {
|
|
getRuntimeConfig,
|
|
getRuntimeConfigWithSources,
|
|
type RuntimeConfig,
|
|
type RuntimeConfigKey,
|
|
type RuntimeConfigSource,
|
|
} from '@/lib/server/admin/settings';
|
|
|
|
export type ResolvedRuntimeConfig = RuntimeConfig;
|
|
|
|
function assertServerRuntime(caller: string): void {
|
|
if (typeof window !== 'undefined') {
|
|
throw new Error(`${caller} must be called on the server`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the resolved site-wide runtime config in the shape consumed by
|
|
* the client via SSR injection. Triggers the boot-time seed on first call
|
|
* so env values land in the DB before the first read.
|
|
*/
|
|
export async function getResolvedRuntimeConfig(): Promise<ResolvedRuntimeConfig> {
|
|
assertServerRuntime('getResolvedRuntimeConfig');
|
|
await ensureAdminSeed();
|
|
return getRuntimeConfig();
|
|
}
|
|
|
|
export async function getResolvedRuntimeConfigWithSources(): Promise<{
|
|
values: RuntimeConfig;
|
|
sources: Record<RuntimeConfigKey, RuntimeConfigSource | 'default'>;
|
|
}> {
|
|
assertServerRuntime('getResolvedRuntimeConfigWithSources');
|
|
await ensureAdminSeed();
|
|
return getRuntimeConfigWithSources();
|
|
}
|
|
|
|
export type { RuntimeConfig, RuntimeConfigKey };
|