feat: improve env parsing

This commit is contained in:
Travis Fischer
2025-06-07 00:06:04 +07:00
parent 1d9a659c96
commit 55caa3533e
2 changed files with 16 additions and 13 deletions

View File

@@ -113,21 +113,22 @@ export const isSearchEnabled: boolean = getSiteConfig('isSearchEnabled', true)
// ----------------------------------------------------------------------------
// Optional redis instance for persisting preview images
export const isRedisEnabled: boolean = getSiteConfig('isRedisEnabled', false)
export const isRedisEnabled: boolean =
getSiteConfig('isRedisEnabled', false) || !!getEnv('REDIS_ENABLED')
// (if you want to enable redis, only REDIS_HOST and REDIS_PASSWORD are required)
// we recommend that you store these in a local `.env` file
export const redisHost: string | undefined = getEnv('REDIS_HOST')
export const redisPassword: string | undefined = getEnv('REDIS_PASSWORD')
export const redisUser: string | undefined = getEnv('REDIS_USER', 'default')
export const redisHost = getEnv('REDIS_HOST', isRedisEnabled ? undefined : null)
export const redisPassword = getEnv(
'REDIS_PASSWORD',
isRedisEnabled ? undefined : null
)
export const redisUser: string = getEnv('REDIS_USER', 'default')
export const redisUrl = getEnv(
'REDIS_URL',
`redis://${redisUser}:${redisPassword}@${redisHost}`
)
export const redisNamespace: string | undefined = getEnv(
'REDIS_NAMESPACE',
'preview-images'
isRedisEnabled ? `redis://${redisUser}:${redisPassword}@${redisHost}` : null
)
export const redisNamespace = getEnv('REDIS_NAMESPACE', 'preview-images')
// ----------------------------------------------------------------------------

View File

@@ -49,15 +49,15 @@ export function getRequiredSiteConfig<T>(key: string): T {
export const isServer = typeof window === 'undefined'
export function getEnv(
export function getEnv<T>(
key: string,
defaultValue?: string,
defaultValue?: string | T,
env = process.env
): string | undefined {
): string | T {
const value = env[key]
if (value !== undefined) {
return value
return value as string
}
if (defaultValue !== undefined) {
@@ -67,4 +67,6 @@ export function getEnv(
if (isServer) {
throw new Error(`Config error: missing required env variable "${key}"`)
}
return null as unknown as T
}