@freighthub/typed-env

中文文档
1.3.4发布于 2019/10/23TypeScriptCommonJS近 30 天下载 2734

Enforceable environment variable contracts at runtime

此软件包已被标记为弃用

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

原始使用文档
文档目录3 节
  1. Usage
  2. Types
  3. Example

typed-env

Enforceable environment variable contracts at runtime.

CircleCI

Use this library to make sure all the environment variables used in a project are valid.

Usage

To use the library, first declare a schema that consists of groups. A group is a set of related environment variables that typically share a common prefix. Groups are created using envGroup function that accepts a dictionary which contains variable names and their types, e.g.:

import * as typedEnv from '@freighthub/typed-env'

const statsd = typedEnv.envGroup({
    HOST: NonEmptyString,
    PORT: PortNumber,
    PREFIX: NonEmptyString,
}, 'STATSD')

const schema = typedEnv.envSchema({
    statsd: statsd,
})

In the above example statsd group will be used to load variables

  • STATSD_HOST
  • STATSD_PORT
  • STATSD_PREFIX

The optional STATSD prefix is passed as the second parameter to envGroup. The schema is then created using envSchema function that accepts a dictionary of groups.

To validate environment variables, make sure they're set (use dotenv.config() to load .env if required). Then use loadFromEnv as follows:

const env = typedEnv.loadFromEnv(schema)

If any variables are missing or don't pass type checking, an exception will occur.

Types

typed-env provides the following schema types:

  • Number - any number
  • Integer - integer number
  • PortNumber - integer number between 1 and 65535
  • NonEmptyString - non-empty string of any length
  • URI - URI as checked by valid-url
  • Boolean - true or false
  • Union - a collection of string literals to resctrict a variable's possible value

Example

import * as typedEnv from '@freighthub/typed-env'

const schema = typedEnv.envSchema({
    elasticSearch: typedEnv.envGroup({
        URL: typedEnv.types.URI
    }, 'ELASTICSEARCH'),
})

const env = typedEnv.loadFromEnv(schema)
console.log(env.elasticSearch.URL)

版本与发布记录

@freighthub/typed-env 共发布 16 个版本,首次发布于 2018/04/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.3.4latest2019/10/237 年前
1.3.32019/04/087 年前
1.3.22019/03/077 年前
1.3.12018/12/137 年前
1.3.02018/08/208 年前
1.2.02018/08/038 年前

关于 @freighthub/typed-env 的常见问题

@freighthub/typed-env 是什么?

Enforceable environment variable contracts at runtime注意:该包已被作者标记为弃用——Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

如何安装 @freighthub/typed-env?

在项目目录运行 npm install @freighthub/typed-env,或使用 pnpm add / yarn add / bun add。

@freighthub/typed-env 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freighthub/typed-env 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@freighthub/typed-env 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=10.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@freighthub/typed-env 可以商用吗?

@freighthub/typed-env 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@freighthub/typed-env 的下载量有多少?

根据 npm 官方下载统计,@freighthub/typed-env 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,734 次,日均约 91 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freighthub/typed-env 还在维护吗?

最新版本 1.3.4 发布于 2019/10/23(7 年前);至今共发布 16 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/04/11。作者已将其标记为弃用,新项目建议寻找替代方案。