@beesley/ts-env (英语) 出口
TypeScript 的环境可变读取器. 简单而最小
这基本上是由 @lpgera 创建的软件包, 但是由于软件包在使用 es 模块时, 与打字机完全不兼容, 我把它加固并重建, 是一个通用的软件包, 可以使用 es 模块或普通的 js .
安装
此软件包需要 typescript 将安装.
# with npm
npm install @beesley/ts-env
# or with Yarn
yarn add @beesley/ts-env
示例
不读取不同类型的环境变量 ts-env数字 :
if (!process.env.PORT) {
throw new Error('process.env.PORT is missing')
}
if (!process.env.DATABASE) {
throw new Error('process.env.DATABASE is missing')
}
const port: number = parseInt(process.env.PORT)
const database: string = process.env.DATABASE
const debug: boolean | undefined = process.env.DEBUG === 'true'
做同样的事情 ts-env数字 :
import tsEnv from '@beesley/ts-env'
const port: number = tsEnv.numberOrThrow('PORT')
const database: string = tsEnv.stringOrThrow('DATABASE')
const debug: boolean | undefined = tsEnv.boolean('DEBUG')
API 密码
.string(键:字符串):字符串qq未定义
从 process.env[key]。 。 。 .
.stringOrthrow(键:字符串):字符串
从 process.env[key]。 。 。 .
如果发生错误, 则丢出错误 undefined。 。 。 .
.boolean(键:字符串):布尔qq未定义
读取布尔值 process.env[key]。 。 。 .
"0"和"假"被认为是 false,"1"和"真实"被认为是 true。 。 。 .
其他一切将读为 undefined。 。 。 .
.boolean Orthrow( 键: 字符串):布尔
读取布尔值 process.env[key]。 。 。 .
"0"和"假"被认为是 false,"1"和"真实"被认为是 true。 。 。 .
其他的都会造成一个错误.
数字( 键: 字符串): 未定义的数字 qq
从 process.env[key]。 。 。 .
如果一个数字无法解析, 将会返回 undefined。 。 。 .
数字 Orthrow( 键: 字符串): 数字
从 process.env[key]。 。 。 .
如果一个数字无法解析, 则会出现出错 .
.stringArray(键:字符串,分隔符:字符串=','):字符串[]QQ未定义
从 process.env[key]。 。 。 .
.stringArray Orthrow(键:字符串,分隔符:字符串=','):字符串[]
从 process.env[key]。 。 。 .
如果发生错误, 则丢出错误 undefined。 。 。 .
. 数字Array(键:字符串,分隔符:字符串=','):数字[] QQ 未定义
读取数字数组 process.env[key]。 。 。 .
如果无法解析任何项目, 将返回 undefined。 。 。 .
数字Array Orthrow(键:字符串,分隔符:字符串=','):数字[]
读取数字数组 process.env[key]。 。 。 .
如果无法解析任何项目, 就会出现错误 .