Skip to content

Commit 55caa35

Browse files
feat: improve env parsing
1 parent 1d9a659 commit 55caa35

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

lib/config.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,22 @@ export const isSearchEnabled: boolean = getSiteConfig('isSearchEnabled', true)
113113
// ----------------------------------------------------------------------------
114114

115115
// Optional redis instance for persisting preview images
116-
export const isRedisEnabled: boolean = getSiteConfig('isRedisEnabled', false)
116+
export const isRedisEnabled: boolean =
117+
getSiteConfig('isRedisEnabled', false) || !!getEnv('REDIS_ENABLED')
117118

118119
// (if you want to enable redis, only REDIS_HOST and REDIS_PASSWORD are required)
119120
// we recommend that you store these in a local `.env` file
120-
export const redisHost: string | undefined = getEnv('REDIS_HOST')
121-
export const redisPassword: string | undefined = getEnv('REDIS_PASSWORD')
122-
export const redisUser: string | undefined = getEnv('REDIS_USER', 'default')
121+
export const redisHost = getEnv('REDIS_HOST', isRedisEnabled ? undefined : null)
122+
export const redisPassword = getEnv(
123+
'REDIS_PASSWORD',
124+
isRedisEnabled ? undefined : null
125+
)
126+
export const redisUser: string = getEnv('REDIS_USER', 'default')
123127
export const redisUrl = getEnv(
124128
'REDIS_URL',
125-
`redis://${redisUser}:${redisPassword}@${redisHost}`
126-
)
127-
export const redisNamespace: string | undefined = getEnv(
128-
'REDIS_NAMESPACE',
129-
'preview-images'
129+
isRedisEnabled ? `redis://${redisUser}:${redisPassword}@${redisHost}` : null
130130
)
131+
export const redisNamespace = getEnv('REDIS_NAMESPACE', 'preview-images')
131132

132133
// ----------------------------------------------------------------------------
133134

lib/get-config-value.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ export function getRequiredSiteConfig<T>(key: string): T {
4949

5050
export const isServer = typeof window === 'undefined'
5151

52-
export function getEnv(
52+
export function getEnv<T>(
5353
key: string,
54-
defaultValue?: string,
54+
defaultValue?: string | T,
5555
env = process.env
56-
): string | undefined {
56+
): string | T {
5757
const value = env[key]
5858

5959
if (value !== undefined) {
60-
return value
60+
return value as string
6161
}
6262

6363
if (defaultValue !== undefined) {
@@ -67,4 +67,6 @@ export function getEnv(
6767
if (isServer) {
6868
throw new Error(`Config error: missing required env variable "${key}"`)
6969
}
70+
71+
return null as unknown as T
7072
}

0 commit comments

Comments
 (0)