Skip to content

Commit

Permalink
feat: 环境变量读取
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 5, 2023
1 parent 30db9dc commit 887b432
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/utils/envUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

/**
* 获取环境变量
* 获取环境变量,未定义返回空
*
* @param key key
* @author terwer
Expand All @@ -33,16 +33,27 @@
const getEnv = (key: string): string => {
let env = ""
try {
if (import.meta.env[key]) {
env = import.meta.env[key]
}
} catch (e: any) {
throw new Error(e)
}

env = import.meta.env[key] ?? ""
} catch (e: any) {}
return env
}

/**
* 获取环境变量,如果未定义或者为空值,用指定的默认值代替
*
* @param key key
* @param defaultValue 默认值
* @author terwer
* @since 0.1.0
*/
export const getEnvOrDefault = (key: string, defaultValue: string) => {
const value = getEnv(key)
if (value.trim().length == 0) {
return defaultValue
}
return value
}

/**
* 获取Boolean类型的环境变量
*
Expand All @@ -51,10 +62,9 @@ const getEnv = (key: string): string => {
* @since 1.0.0
*/
const getBooleanEnv = (key: string): boolean => {
let env = false
if (getEnv(key)) {
env = getEnv(key).toLowerCase() === "true"
}
let env: boolean
const defVal = getEnvOrDefault(key, "false")
env = defVal.toLowerCase() === "true"
return env
}

Expand Down

0 comments on commit 887b432

Please sign in to comment.