@bitxx/config-loader
v1.0.0-beta.1
Published
Bitxx Config Loader Library
Readme
@bitxx/config-loader
Type-safe configuration loading and validation for Node.js and TypeScript.
@bitxx/config-loader loads environment variables, discovers application configuration files, validates configuration values using @bitxx/validator, generates TypeScript configuration definitions, and provides type-safe access to nested configuration properties.
Installation
npm install @bitxx/config-loader @bitxx/validatorUsing pnpm:
pnpm add @bitxx/config-loader @bitxx/validatorUsing yarn:
yarn add @bitxx/config-loader add @bitxx/validatorFeatures
- Environment variable loading from
.env. - Automatic discovery of
.jsand.tsconfiguration files. - Type-safe configuration access through dot notation.
- TypeScript configuration type generation.
- Optional configuration validation.
- Deep-frozen configuration objects.
- Duplicate configuration name detection.
- Worker-friendly configuration loading.
Quick Start
Project structure
my-app/
├── .env
├── configs/
│ ├── app.ts
│ └── database.ts
├── src/
│ └── index.ts
└── package.jsonEnvironment variables
Create a .env file:
NODE_ENV=development
APP_PORT=3000
DB_HOST=localhost
DB_PORT=5432Configuration file
Create configs/database.ts:
import { defineConfig } from "@bitxx/config-loader";
defineConfig({
name: "database",
values: {
host: process.env.DB_HOST ?? "",
port: Number(process.env.DB_PORT ?? 5432),
},
rules: {
host: {
required: true,
},
port: {
required: true,
},
},
});Initialize configuration
import { Load } from "@bitxx/config-loader";
Load();Read configuration
import Config from "@bitxx/config-loader";
const appName = Config("app.name");
const port = Config("app.port");
console.log(appName, port);API
Load()initializes the environment and loads configuration files.LoadConfig()loads configuration files whenAPP_DIRis already initialized.Config(path)retrieves a configuration value through a type-safe dot path.defineConfig()registers named configuration values and optionally validates them.
Validation uses rules supported by @bitxx/validator. Failed validation throws an error.
License
MIT. See LICENSE.
