Configuration
The Config module manages application configuration from environment variables, .env files, and programmatic overrides.
Usage
import { FluxKit } from "@fluxkitdev/core";
const app = new FluxKit({
database: { uri: process.env.MONGODB_URI },
});
// Read a config value
const port = app.config.get("PORT", 3000);
// Read a required value (throws if missing)
const dbUri = app.config.require("MONGODB_URI");
// Check if a value exists
if (app.config.has("SMTP_HOST")) {
// messaging is configured
}
// Get all config as an object
const all = app.config.toObject();
API Reference
config.get(key: string, defaultValue?: T): T
Returns the value for key. If the key is not found, returns defaultValue. If no default is provided, returns undefined.
const port = app.config.get("PORT", 3000); // number
const name = app.config.get("APP_NAME", "my-app"); // string
config.require(key: string): string
Returns the value for key. Throws an error if the key is not set.
const secret = app.config.require("JWT_SECRET");
// Throws: "Missing required config: JWT_SECRET"
config.has(key: string): boolean
Returns true if the key exists in the configuration.
if (app.config.has("REDIS_URL")) {
// use Redis cache
}
config.set(key: string, value: string): void
Programmatically sets a configuration value. This does not modify .env files.
app.config.set("CUSTOM_FLAG", "true");
config.toObject(): Record<string, string>
Returns all configuration as a plain object.
Priority Order
Configuration values are resolved in this order (highest priority first):
- Programmatic overrides via
config.set() - Constructor options passed to
FluxKit() - Environment variables (
process.env) .envfile in the project root- Default values