@47ng/check-env 中文文档教程

发布于 3年前 浏览 28 项目主页 更新于 3年前

check-env

检查是否为您的应用设置了关键环境变量, 并且您没有在生产中留下危险的开发覆盖。

NPM 麻省理工学院许可证 持续集成 覆盖状态 Dependabot 状态

Installation

yarn add @47ng/check-env

Usage

import checkEnv from '@47ng/check-env'

checkEnv({
  // Will log an error and throw if any of these are missing:
  required: [
    'SOME_API_SECRET',
    'PRIVATE_TOKEN',
    'SOME_OTHER_IMPORTANT_THING'
    // ...
  ],

  // Will log an error and throw if any of these are set in production:
  unsafe: [
    'LOCAL_OVERRIDE_DISABLE_HTTPS',
    'INSECURE_COOKIES'
    // ...
  ]
})

如果没有设置某些必需的环境变量,它会告诉你并抛出 最后一个错误:

Error handling

您可以使用 noThrow 选项选择跳过抛出错误:

checkEnv({
  noThrow: true,
  ...
})

Conditional Checks

如果您只想在生产中需要一些变量,你可以添加一个条件 在变量名之前,任何虚假值都将被忽略:

const __PROD__ = process.env.NODE_ENV === 'production'

checkEnv({
  required: [
    'ALWAYS_REQUIRED',
    __PROD__ && 'ONLY_REQUIRED_IN_PRODUCTION',
    !__PROD__ && 'YOU_GET_THE_IDEA'
  ]
})

Logging

默认情况下,check-env 使用带有表情符号的 console.err

您可以使用 logMissinglogUnsafe 覆盖默认的日志记录方法。

使用 Pino 的示例:

const logger = require('pino')()

checkEnv({
  logMissing: name => logger.error(`Missing required environment variable ${name}`),
  logUnsafe: name => logger.warn(`Unsafe environment variable ${name} set in production`),
  ...
})

License

MIT - François Best 用 ❤️ 制作

在工作中使用这个包? 赞助我以帮助支持和维护。

check-env

Check that the critical environment variables are set for your app, and that you did not leave dangerous development overrides in production.

NPM MIT License Continuous Integration Coverage Status Dependabot Status

Installation

yarn add @47ng/check-env

Usage

import checkEnv from '@47ng/check-env'

checkEnv({
  // Will log an error and throw if any of these are missing:
  required: [
    'SOME_API_SECRET',
    'PRIVATE_TOKEN',
    'SOME_OTHER_IMPORTANT_THING'
    // ...
  ],

  // Will log an error and throw if any of these are set in production:
  unsafe: [
    'LOCAL_OVERRIDE_DISABLE_HTTPS',
    'INSECURE_COOKIES'
    // ...
  ]
})

If some required environment variable are not set, it will tell you and throw an error at the end: "CLI output"

Error handling

You can choose to skip throwing an error with the noThrow option:

checkEnv({
  noThrow: true,
  ...
})

Conditional Checks

If you want to require some variables only in production, you can add a condition before the variable name, any falsy value will be ignored:

const __PROD__ = process.env.NODE_ENV === 'production'

checkEnv({
  required: [
    'ALWAYS_REQUIRED',
    __PROD__ && 'ONLY_REQUIRED_IN_PRODUCTION',
    !__PROD__ && 'YOU_GET_THE_IDEA'
  ]
})

Logging

By default, check-env uses console.err with emoji.

You can override the default logging methods with logMissing and logUnsafe.

Example using Pino:

const logger = require('pino')()

checkEnv({
  logMissing: name => logger.error(`Missing required environment variable ${name}`),
  logUnsafe: name => logger.warn(`Unsafe environment variable ${name} set in production`),
  ...
})

License

MIT - Made with ❤️ by François Best

Using this package at work ? Sponsor me to help with support and maintenance.

更多

友情链接

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文