diff --git a/lib/config.ts b/lib/config.ts index 7e44038..839a62e 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -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') // ---------------------------------------------------------------------------- diff --git a/lib/get-config-value.ts b/lib/get-config-value.ts index 4ba17f8..2faf917 100644 --- a/lib/get-config-value.ts +++ b/lib/get-config-value.ts @@ -49,15 +49,15 @@ export function getRequiredSiteConfig(key: string): T { export const isServer = typeof window === 'undefined' -export function getEnv( +export function getEnv( 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 }