fix(qenv): Improve documentation, update dependencies, and refine project configuration

This commit is contained in:
2025-08-14 07:20:05 +00:00
parent 7b34ef8453
commit 88bed685bc
11 changed files with 3818 additions and 3208 deletions

384
readme.md
View File

@@ -1,109 +1,347 @@
# @push.rocks/qenv
easy promised environments
# @push.rocks/qenv 🔐
**Smart Environment Variable Management for Node.js**
## Install
To install `@push.rocks/qenv`, you need to have Node.js installed on your system. Once Node.js is installed, you can add `@push.rocks/qenv` to your project by running the following command in your project's root directory:
> Never hardcode secrets again. Load environment variables from multiple sources with ease and confidence.
## 🚀 Features
**Multi-source Loading** - Automatically loads from environment variables, config files, and Docker secrets
**Type-Safe** - Full TypeScript support with comprehensive type definitions
**Flexible Formats** - Supports `.yml`, `.yaml`, and `.json` configuration files
**Docker Ready** - Built-in support for Docker secrets and secret.json files
**Async & Sync** - Both synchronous and asynchronous variable retrieval
**Strict Mode** - Optional strict mode that throws errors for missing variables
**Base64 Objects** - Handle complex configuration objects with automatic encoding/decoding
**Dynamic Resolution** - Support for async functions as environment variable sources
## 📦 Installation
```bash
# Using npm
npm install @push.rocks/qenv --save
# Using pnpm (recommended)
pnpm add @push.rocks/qenv
# Using yarn
yarn add @push.rocks/qenv
```
This command will add `@push.rocks/qenv` as a dependency to your project, and you will be ready to use it in your application.
## Usage
`@push.rocks/qenv` provides a convenient way to manage and access environment variables in your Node.js projects, especially when dealing with different environments like development, testing, and production. Its primary use is to load environment-specific variables in an easy and organized manner. Below is an extensive guide on how to use this module effectively in various scenarios, ensuring you can handle environment variables efficiently in your projects.
### Getting Started
First, ensure you have TypeScript configured in your project. `@push.rocks/qenv` is fully typed, providing excellent IntelliSense support when working in editors that support TypeScript, such as Visual Studio Code.
#### Importing Qenv
To get started, import the `Qenv` class from `@push.rocks/qenv`:
## 🎯 Quick Start
```typescript
import { Qenv } from '@push.rocks/qenv';
// Create a new Qenv instance
const qenv = new Qenv('./', './', true);
// Access environment variables
const dbHost = await qenv.getEnvVarOnDemand('DB_HOST');
const apiKey = await qenv.getEnvVarOnDemand('API_KEY');
// Use strict mode to ensure variables exist
const criticalVar = await qenv.getEnvVarOnDemandStrict('CRITICAL_CONFIG');
// Throws error if CRITICAL_CONFIG is not set!
```
#### Basic Configuration
`@push.rocks/qenv` works with two main files: `qenv.yml` for specifying required environment variables, and `env.yml` for specifying values for these variables. These files should be placed in your project directory.
## 📖 Configuration
##### qenv.yml
This file specifies the environment variables that are required by your application. An example `qenv.yml` might look like this:
### Setting Up Your Environment Files
#### 1. Define Required Variables (`qenv.yml`)
Create a `qenv.yml` file to specify which environment variables your application needs:
```yaml
required:
- DB_HOST
- DB_USER
- DB_PASS
- DB_PASSWORD
- API_KEY
- LOG_LEVEL
```
##### env.yml
This file contains the actual values for the environment variables in a development or testing environment. An example `env.yml` could be:
#### 2. Provide Values (`env.yml` or `env.json`)
For local development, create an `env.yml` or `env.json` file:
**env.yml:**
```yaml
DB_HOST: localhost
DB_USER: user
DB_PASS: pass
DB_USER: developer
DB_PASSWORD: supersecret123
API_KEY: dev-key-12345
LOG_LEVEL: debug
```
#### Instantiating Qenv
Create an instance of `Qenv` by providing paths to the directories containing the `qenv.yml` and `env.yml` files, respectively:
```typescript
const myQenv = new Qenv('./path/to/dir/with/qenv', './path/to/dir/with/env');
**env.json:**
```json
{
"DB_HOST": "localhost",
"DB_USER": "developer",
"DB_PASSWORD": "supersecret123",
"API_KEY": "dev-key-12345",
"LOG_LEVEL": "debug"
}
```
If the `env.yml` file is in the same directory as `qenv.yml`, you can omit the second argument:
> 💡 **Pro Tip:** Add `env.yml` and `env.json` to your `.gitignore` to keep secrets out of version control!
## 🔥 Advanced Usage
### Loading Priority
Qenv loads variables in this order (first found wins):
1. **Process environment variables** - Already set in `process.env`
2. **Configuration files** - From `env.yml` or `env.json`
3. **Docker secrets** - From `/run/secrets/`
4. **Docker secret JSON** - From `/run/secrets/secret.json`
### Handling Complex Objects
Store and retrieve complex configuration objects:
```typescript
const myQenv = new Qenv('./path/to/dir/with/both');
```
#### Accessing Environment Variables
After instantiating `Qenv`, you can access the loaded environment variables directly from `process.env` in Node.js or through the `myQenv` instance for more complex scenarios like asynchronous variable resolution:
```typescript
// Accessing directly via process.env
console.log(process.env.DB_HOST); // 'localhost' in development environment
// Accessing via Qenv instance for more advanced scenarios
(async () => {
const dbHost = await myQenv.getEnvVarOnDemand('DB_HOST');
console.log(dbHost); // 'localhost'
})();
```
### Advanced Usage
#### Handling Missing Variables
By default, `Qenv` will throw an error and exit if any of the required environment variables specified in `qenv.yml` are missing. You can disable this behavior by passing `false` as the third argument to the constructor, which allows your application to handle missing variables gracefully:
```typescript
const myQenv = new Qenv('./path/to/dir/with/qenv', './path/to/dir/with/env', false);
```
#### Dynamic Environment Variables
For dynamic or computed environment variables, you can define functions that resolve these variables asynchronously. This is particularly useful for variables that require fetching from an external source:
```typescript
// Define a function to fetch a variable
const fetchDbHost = async () => {
// Logic to fetch DB_HOST from an external service
return 'dynamic.host';
// In env.yml
const config = {
database: {
host: 'localhost',
port: 5432,
options: {
ssl: true,
poolSize: 10
}
}
};
// Use the function with getEnvVarOnDemand
(async () => {
const dbHost = await myQenv.getEnvVarOnDemand(fetchDbHost);
console.log(dbHost); // 'dynamic.host'
})();
// Qenv automatically handles base64 encoding
const dbConfig = await qenv.getEnvVarOnDemandAsObject('DATABASE_CONFIG');
console.log(dbConfig.database.options.poolSize); // 10
```
#### Reading Variables from Docker Secrets or Other Sources
Internally, `@push.rocks/qenv` supports reading from Docker secrets, providing flexibility for applications deployed in Docker environments. The module attempts to read each required variable from the process environment, a provided `env.yml` file, Docker secrets, or any custom source you integrate.
### Dynamic Environment Variables
### Conclusion
`@push.rocks/qenv` simplifies handling environment variables across different environments, making your application's configuration more manageable and secure. By separating variable definitions from their values and providing support for dynamic resolution, `@push.rocks/qenv` offers a robust solution for managing configuration in Node.js projects. Whether you're working in a local development environment, CI/CD pipelines, or production, `@push.rocks/qenv` ensures that you have the correct configuration for the task at hand.
Load variables from external sources dynamically:
Note: Due to the complexity and depth of `@push.rocks/qenv`, this documentation aims to cover general and advanced usage comprehensively. Please refer to the module's official documentation and typed definitions for further details on specific features or configuration options.
```typescript
const qenv = new Qenv();
// Define an async function to fetch configuration
const fetchFromVault = async () => {
const response = await fetch('https://vault.example.com/api/secret');
const data = await response.json();
return data.secret;
};
// Use the function as an environment variable source
const secret = await qenv.getEnvVarOnDemand(fetchFromVault);
```
### Working with Docker
Qenv seamlessly integrates with Docker secrets:
```dockerfile
# docker-compose.yml
version: '3.7'
services:
app:
image: your-app
secrets:
- db_password
- api_key
secrets:
db_password:
external: true
api_key:
external: true
```
Your application automatically reads from `/run/secrets/`:
```typescript
const qenv = new Qenv();
// Automatically loads from /run/secrets/db_password
const dbPassword = await qenv.getEnvVarOnDemand('db_password');
```
### Handling Missing Variables
Control how your application handles missing environment variables:
```typescript
// Fail fast (default behavior)
const qenvStrict = new Qenv('./', './', true);
// Application exits if required variables are missing
// Graceful handling
const qenvRelaxed = new Qenv('./', './', false);
// Application continues, you handle missing variables
// Check what's missing
if (qenvRelaxed.missingEnvVars.length > 0) {
console.warn('Missing variables:', qenvRelaxed.missingEnvVars);
// Implement fallback logic
}
```
### Strict Mode for Critical Variables
Use the new strict getter when you absolutely need a variable:
```typescript
try {
// This will throw if TOKEN is not set
const token = await qenv.getEnvVarOnDemandStrict('TOKEN');
// You can also check multiple fallback names
const db = await qenv.getEnvVarOnDemandStrict(['DATABASE_URL', 'DB_CONNECTION']);
} catch (error) {
console.error('Critical configuration missing:', error.message);
process.exit(1);
}
```
## 🏗️ CI/CD Integration
### GitHub Actions
```yaml
name: Deploy
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy with secrets
env:
API_KEY: ${{ secrets.API_KEY }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
run: |
npm install
npm run deploy
```
### GitLab CI
```yaml
deploy:
stage: deploy
script:
- npm install
- npm run deploy
variables:
API_KEY: $CI_API_KEY
DB_PASSWORD: $CI_DB_PASSWORD
```
## 🎭 Testing
For testing, create a separate `test/assets/env.yml`:
```typescript
import { Qenv } from '@push.rocks/qenv';
describe('MyApp', () => {
let qenv: Qenv;
beforeEach(() => {
qenv = new Qenv('./test/assets', './test/assets', false);
});
it('should load test configuration', async () => {
const testVar = await qenv.getEnvVarOnDemand('TEST_VAR');
expect(testVar).toBe('test-value');
});
});
```
## 🔍 Debugging
Enable detailed logging to troubleshoot environment variable loading:
```typescript
const qenv = new Qenv();
// Check what's loaded
console.log('Required vars:', qenv.requiredEnvVars);
console.log('Available vars:', qenv.availableEnvVars);
console.log('Missing vars:', qenv.missingEnvVars);
// Access the logger
qenv.logger.log('info', 'Custom log message');
```
## 📊 Real-World Example
Here's how you might use qenv in a production Node.js application:
```typescript
import { Qenv } from '@push.rocks/qenv';
import { createServer } from './server';
import { connectDatabase } from './database';
async function bootstrap() {
// Initialize environment
const qenv = new Qenv();
// Load critical configuration
const config = {
port: await qenv.getEnvVarOnDemand('PORT') || '3000',
dbUrl: await qenv.getEnvVarOnDemandStrict('DATABASE_URL'),
apiKey: await qenv.getEnvVarOnDemandStrict('API_KEY'),
logLevel: await qenv.getEnvVarOnDemand('LOG_LEVEL') || 'info',
features: await qenv.getEnvVarOnDemandAsObject('FEATURE_FLAGS')
};
// Connect to database
await connectDatabase(config.dbUrl);
// Start server
const server = createServer(config);
server.listen(config.port, () => {
console.log(`🚀 Server running on port ${config.port}`);
});
}
bootstrap().catch(error => {
console.error('Failed to start application:', error);
process.exit(1);
});
```
## 🤝 API Reference
### Class: `Qenv`
#### Constructor
```typescript
new Qenv(
qenvFileBasePathArg?: string, // Path to qenv.yml (default: process.cwd())
envFileBasePathArg?: string, // Path to env.yml/json (default: same as qenv)
failOnMissing?: boolean // Exit on missing vars (default: true)
)
```
#### Methods
| Method | Description | Returns |
|--------|-------------|---------|
| `getEnvVarOnDemand(name)` | Get environment variable value | `Promise<string \| undefined>` |
| `getEnvVarOnDemandStrict(name)` | Get variable or throw error | `Promise<string>` |
| `getEnvVarOnDemandSync(name)` | Synchronously get variable | `string \| undefined` |
| `getEnvVarOnDemandAsObject(name)` | Get variable as decoded object | `Promise<any>` |
#### Properties
| Property | Type | Description |
|----------|------|-------------|
| `requiredEnvVars` | `string[]` | List of required variable names |
| `availableEnvVars` | `string[]` | List of found variable names |
| `missingEnvVars` | `string[]` | List of missing variable names |
| `keyValueObject` | `object` | All loaded variables as key-value pairs |
## License and Legal Information
@@ -122,4 +360,4 @@ Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.