@dot/config

中文文档
2.0.1发布于 2024/08/30MPL-2.0TypeScriptCommonJS近 30 天下载 2430

A configuration contract for Node + AWS projects

原始使用文档
文档目录9 节
  1. Requirements
  2. Install
  3. Usage
  4. Exports
  5. DotEnv
  6. env
  7. envPrefix
  8. init
  9. Meta

@dot/config

A configuration contract for Node + AWS projects.

The contract that this package implements covers the following:

  • Error handling and assertion of configuration values. Empty or missing values stop execution.
  • Ease of access to environment variables
  • Ease of access to AWS SSM Parameters
  • Ease of access to AWS Secrets Manager secrets
  • No need to use or access process.env
  • Specify default values for configuration
  • Provide canonical environment names, centalize environment parsing

For TypeScript users, known configuration values are provided in intellisense based on the keys of configuration options passed to init.

Requirements

This package requires an Active LTS Node version (v18+).

Install

Using pnpm:

pnpm add @dot/config

Usage

import { env, init } from '@dot/config';

const defaultConfig = { ... };
const secretConfig = {
  BATCAVE_CODE: `/${env}/batcave/code`
};
const ssmConfig = { ... };
const { get } = init({ defaultConfig, pkg, secretConfig, ssmConfig });

const value = await get('BATCAVE_CODE');

Exports

DotEnv

Type: Enum

An enum representing common Node, deployment, or run environments.

enum DotEnv {
  DEV = 'dev',
  PROD = 'prod',
  STAGE = 'stage',
  TEST = 'test'
}

env

Type: String

Represents the current env value, corresponding to DotEnv.

envPrefix

Type: String

Represents the current env value in a prefix useful for deployment resource identifiers and other identifiers or prefixes. e.g. prod-

init

Type: Function
Returns: { get, put }

Initializes a configuration getter and putter.

Options

defaultConfig

Type: Object

An object whereby keys identify configuration values.

secretConfig

Type: Object

An object whereby keys match AWS SecretsManager names.

ssmConfig

Type: Object

An object whereby keys match AWS SSM Parameter Store paths.

get

Type: Function
Parameters: (key: String)
Returns: Promise<String | Number>

Get a value for the specified key. The key may represent a key present on:

  • process.env
  • The default configuration object
  • The secrets configuration object
  • The SSM configuration object

If the key does not exist, or the value returned from any of the possible sources is undefined, a RangeError is thrown.

const name = await get('BATMAN');

put

Type: Function
Parameters: (key: String, value: any)
Returns: Promise<AWS Response>

Saves a value to the specified key, which corresponds to an AWS SSM Parameter Path. This function is limited to AWS SSM Parameter Store.

await put('DB_NAME', 'batman');

Meta

CONTRIBUTING

LICENSE (Mozilla Public License)

版本与发布记录

@dot/config 共发布 15 个版本,首次发布于 2023/01/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2024/08/302 年前
2.0.02024/07/292 年前
1.3.02024/06/102 年前
1.2.12024/05/162 年前
1.2.02024/03/192 年前
1.1.22023/05/313 年前

关于 @dot/config 的常见问题

@dot/config 是什么?

A configuration contract for Node + AWS projects

如何安装 @dot/config?

在项目目录运行 npm install @dot/config,或使用 pnpm add / yarn add / bun add。

@dot/config 支持 TypeScript 吗?

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

@dot/config 支持 ESM 还是 CommonJS?

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

@dot/config 需要什么版本的 Node.js?

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

@dot/config 可以商用吗?

@dot/config 使用 MPL-2.0 许可。弱 Copyleft:可以商用,也可以和闭源代码一起使用;但如果分发时修改了 MPL 授权的文件,这些文件的修改需以 MPL 开源。以上为概括,不构成法律意见,请以许可原文为准。

@dot/config 的下载量有多少?

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

@dot/config 还在维护吗?

最新版本 2.0.1 发布于 2024/08/30(2 年前);至今共发布 15 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/01/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。