mirror of
https://github.com/d0zingcat/nextjs-notion-starter-kit.git
synced 2026-05-13 15:09:47 +00:00
feat: improve env parsing
This commit is contained in:
@@ -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')
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user