### Complete Configuration Example with Multiple Decorators and Dotenv Source: https://context7.com/petrgrishin/config-decorator/llms.txt A comprehensive example showcasing the integration of various config-decorator features, including multiple configuration classes, different data types, default values, optional properties, custom transformations, and dotenv support. This illustrates a full application configuration setup. ```typescript import 'reflect-metadata'; import { Config, Nullable, Option } from 'config-decorator'; // Application configuration with 'app' prefix @Config('app') export class AppConfig { @Option({ type: 'string', default: 'development', }) public readonly env!: string; @Option({ type: 'number', default: 3000, }) @Nullable() public readonly port!: number; @Option({ type: 'boolean', default: false, }) @Nullable() public readonly debug!: boolean; } // Module-specific configuration with 'module' prefix @Config('module') export class ModuleConfig { @Option({ type: 'string', }) public readonly nameTest!: string; // MODULE_NAME_TEST (camelCase -> SNAKE_CASE) } // Global configuration without prefix @Config() export class DefaultConfig { @Option({ type: 'string', }) public readonly name!: string; @Option({ type: 'string', transform: JSON.parse, }) public readonly allowedOrigins!: string[]; @Option({ type: 'string', transform: JSON.parse, }) public readonly featureFlags!: Record; } // Environment setup (or use .env file) process.env.APP_ENV = 'production'; process.env.APP_PORT = '8080'; process.env.APP_DEBUG = 'true'; process.env.MODULE_NAME_TEST = 'my-module'; process.env.NAME = 'my-application'; process.env.ALLOWED_ORIGINS = '["https://example.com", "https://api.example.com"]'; process.env.FEATURE_FLAGS = '{"newUI": true, "betaFeatures": false}'; // Instantiate configurations const appConfig = new AppConfig(); const moduleConfig = new ModuleConfig(); const defaultConfig = new DefaultConfig(); // Access configuration values console.log(appConfig.env); // Output: 'production' console.log(appConfig.port); // Output: 8080 console.log(appConfig.debug); // Output: true console.log(moduleConfig.nameTest); // Output: 'my-module' console.log(defaultConfig.name); // Output: 'my-application' console.log(defaultConfig.allowedOrigins); // Output: ['https://example.com', 'https://api.example.com'] console.log(defaultConfig.featureFlags.newUI); // Output: true ``` -------------------------------- ### Install config-decorator using npm Source: https://github.com/petrgrishin/config-decorator/blob/master/README.md This command installs the config-decorator package from npm. It is a prerequisite for using the library in your project. ```bash npm i config-decorator ``` -------------------------------- ### Define and Use App Configuration with Decorators Source: https://github.com/petrgrishin/config-decorator/blob/master/README.md Demonstrates how to define application configuration using the Config decorator. It shows how to specify options like type, default values, and nullability, and how to instantiate and access the configuration. The example also illustrates how environment variables can override default values. ```typescript import { Config, Nullable, Option } from 'config-decorator'; @Config('app') export class AppConfig { @Option({ type: 'number', default: 3000, }) @Nullable() public readonly port!: number; } const appConfig = new AppConfig(); console.log(appConfig.port); // 3000 ``` -------------------------------- ### Configure Database Options with @Option and Custom Transforms (TypeScript) Source: https://context7.com/petrgrishin/config-decorator/llms.txt Illustrates defining database configuration properties using the @Option decorator, including types like 'integer' and custom transformations. The example shows how to use `JSON.parse` for complex types like arrays from environment variables. ```typescript import { Config, Option } from 'config-decorator'; @Config('database') export class DatabaseConfig { @Option({ type: 'string', default: 'localhost', }) public readonly host!: string; // DATABASE_HOST @Option({ type: 'integer', default: 5432, }) public readonly port!: number; // DATABASE_PORT @Option({ type: 'string', }) public readonly connectionString!: string; // DATABASE_CONNECTION_STRING // Using transform for complex types (JSON arrays, objects) @Option({ type: 'string', transform: JSON.parse, }) public readonly replicaHosts!: string[]; // DATABASE_REPLICA_HOSTS } // Environment setup process.env.DATABASE_HOST = 'db.example.com'; process.env.DATABASE_PORT = '5433'; process.env.DATABASE_CONNECTION_STRING = 'postgresql://user:pass@db.example.com:5433/mydb'; process.env.DATABASE_REPLICA_HOSTS = '["replica1.example.com", "replica2.example.com"]'; const dbConfig = new DatabaseConfig(); console.log(dbConfig.host); // Output: 'db.example.com' console.log(dbConfig.port); // Output: 5433 console.log(dbConfig.connectionString); // Output: 'postgresql://user:pass@db.example.com:5433/mydb' console.log(dbConfig.replicaHosts); // Output: ['replica1.example.com', 'replica2.example.com'] ``` -------------------------------- ### Set Environment Variable for Port Configuration Source: https://github.com/petrgrishin/config-decorator/blob/master/README.md This example shows how to set an environment variable to configure the application's port. The config-decorator library automatically picks up this value, overriding the default if provided. ```dotenv APP_PORT=3000 ``` -------------------------------- ### Optional Properties with @Nullable Decorator Source: https://context7.com/petrgrishin/config-decorator/llms.txt Demonstrates how to use the @Nullable decorator to mark configuration properties as optional. These properties will not cause validation errors if their corresponding environment variables are not set and no default value is provided. It shows examples with and without default values. ```typescript import { Config, Nullable, Option } from 'config-decorator'; @Config('server') export class ServerConfig { @Option({ type: 'number', default: 3000, }) @Nullable() public readonly port!: number; // Optional with default @Option({ type: 'string', }) @Nullable() public readonly sslCertPath!: string; // Optional, no default @Option({ type: 'string', }) public readonly hostname!: string; // Required - will throw if not set } // Only required variables need to be set process.env.SERVER_HOSTNAME = 'api.example.com'; // SERVER_PORT not set - will use default 3000 // SERVER_SSL_CERT_PATH not set - will be undefined const serverConfig = new ServerConfig(); console.log(serverConfig.port); // Output: 3000 (default value) console.log(serverConfig.hostname); // Output: 'api.example.com' console.log(serverConfig.sslCertPath); // Output: undefined ``` -------------------------------- ### Define Configuration with @Config and @Option Decorators (TypeScript) Source: https://context7.com/petrgrishin/config-decorator/llms.txt Demonstrates how to define application configuration using the @Config decorator to set prefixes and the @Option decorator to declare configuration properties with types and defaults. It shows automatic environment variable binding and type casting. ```typescript import { Config, Option } from 'config-decorator'; // With prefix: environment variables will be prefixed with "APP_" @Config('app') export class AppConfig { @Option({ type: 'string', default: 'development', }) public readonly environment!: string; // Reads from APP_ENVIRONMENT @Option({ type: 'number', default: 8080, }) public readonly port!: number; // Reads from APP_PORT } // Without prefix: environment variables use property names directly @Config() export class GlobalConfig { @Option({ type: 'string', }) public readonly apiKey!: string; // Reads from API_KEY } // Usage process.env.APP_ENVIRONMENT = 'production'; process.env.APP_PORT = '3000'; process.env.API_KEY = 'secret-key-123'; const appConfig = new AppConfig(); console.log(appConfig.environment); // Output: 'production' console.log(appConfig.port); // Output: 3000 (automatically cast to number) const globalConfig = new GlobalConfig(); console.log(globalConfig.apiKey); // Output: 'secret-key-123' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.