@golevelup/profiguration

中文文档
0.2.1发布于 2020/05/29TypeScriptCommonJS近 30 天下载 1.7万

About

原始使用文档
文档目录6 节
  1. About
  2. Features
  3. Usage
  4. Install
  5. Define an interface to represent your Profiguration
  6. Create a Profiguration instance

@golevelup/profiguration

About

Profiguration is currently built on top of the rock solid Convict configuration library from Mozilla. It helps you acheive 12Factor compliant application configuration by putting a focus on leveraging environment variables in a type safe way while enabling you to load configuration from .env and .json files for convenience during development.

Features

  • Load configuration from .env files or json files automatically with hierarchical overriding
  • Easily control which configuration files get loaded based on your Node Environment
  • Built in support for validation of different types
  • Intellisense and Type Safety
  • Simple testing utilties for when you want to mock your configuration

Usage

Install

npm install ---save @golevelup/profiguration

or

yarn add @golevelup/profiguration

Define an interface to represent your Profiguration

The interface should represent the object stucture for you you would like to access the configuration for your app. It can be a complex object with nested values and different types

For example:

interface Config {
  db: {
    password: string;
    host: {
      url: string;
      port: number;
    };
  };
  appPort: number;
  globalPrefix: string;
  controllerPrefix: string;
  showWelcomeMessage: boolean;
}

Create a Profiguration instance

The createProfiguration factory method takes a schema that indicates how the various properties of your configuration interface should be loaded from the process environment. See the convict documentation for additional options and documentation.

The second argument configures profiguration with options for logging, validation and relative file loading paths as well as a user provided function configureEnv which allows for per environment overriding of the profiguration options as well as files that should be loaded which contain configuration values.

import { createProfiguration } from '@golevelup/profiguration';

export const config = createProfiguration<Config>(
  {
    appPort: {
      default: 3333,
      env: 'APP_PORT'
    },
    db: {
      password: {
        default: null,
        format: String,
        env: 'DB_PASSWORD'
      },
      host: {
        url: {
          default: '',
          env: 'DB_HOST_URL'
        },
        port: {
          default: -1,
          format: 'port',
          env: 'DB_HOST_PORT'
        }
      }
    },
    globalPrefix: {
      default: 'api',
      env: 'GLOBAL_PREFIX'
    },
    controllerPrefix: {
      default: '',
      env: 'CONTROLLER_PREFIX'
    },
    showWelcomeMessage: {
      default: true,
      env: 'WELCOME_MESSAGE'
    }
  },
  {
    strict: true,
    verbose: true,
    loadRelativeTo: 'parent-module',
    configureEnv: env => ({
      files: `${env}.env`
    })
  }
);

版本与发布记录

@golevelup/profiguration 共发布 8 个版本,首次发布于 2020/05/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.1latest2020/05/296 年前
0.2.02020/05/296 年前
0.1.3-rc.32020/05/296 年前
0.1.3-rc.22020/05/296 年前
0.1.22020/05/196 年前
0.1.12020/05/196 年前

关于 @golevelup/profiguration 的常见问题

@golevelup/profiguration 是什么?

About

如何安装 @golevelup/profiguration?

在项目目录运行 npm install @golevelup/profiguration,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@types/dotenv(^8.2.0)、@types/convict(^5.2.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@golevelup/profiguration 支持 TypeScript 吗?

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

@golevelup/profiguration 支持 ESM 还是 CommonJS?

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

@golevelup/profiguration 可以商用吗?

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

@golevelup/profiguration 的下载量有多少?

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

@golevelup/profiguration 还在维护吗?

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