Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 35x 35x 35x 35x 58x 58x 812x 812x 812x 1x 1x 58x 8469x 1x 1x 1x 1x 1x 1x 1x 42x 1x 1x 1x 1x 1x 1x 1x | /**
* EnvOverrideLayer - Environment Variable Configuration Overrides
*
* Processes QUIQR_* environment variables and applies them as config overrides.
* Environment variables take precedence over file-based configuration.
*/
import {
type EnvVarMapping,
standardEnvMappings,
} from '@quiqr/types';
/**
* Result of parsing an environment variable
*/
export interface EnvOverride {
configPath: string;
value: unknown;
envVar: string;
}
/**
* EnvOverrideLayer handles environment variable configuration
*/
export class EnvOverrideLayer {
private prefix: string;
private mappings: EnvVarMapping[];
private overrides: Map<string, EnvOverride>;
constructor(prefix: string = 'QUIQR_', customMappings?: EnvVarMapping[]) {
this.prefix = prefix;
this.mappings = customMappings || standardEnvMappings;
this.overrides = new Map();
this.loadFromEnvironment();
}
/**
* Load overrides from current environment
*/
loadFromEnvironment(): void {
this.overrides.clear();
// Process standard mappings
for (const mapping of this.mappings) {
const envVarName = `${this.prefix}${mapping.envVar}`;
const rawValue = process.env[envVarName];
if (rawValue !== undefined) {
const value = this.transformValue(rawValue, mapping.transform);
this.overrides.set(mapping.configPath, {
configPath: mapping.configPath,
value,
envVar: envVarName,
});
}
}
// Also process any QUIQR_* vars that follow the convention
// Format: QUIQR_SECTION_KEY or QUIQR_SECTION_SUBSECTION_KEY
for (const [key, value] of Object.entries(process.env)) {
if (key.startsWith(this.prefix) && value !== undefined) {
const pathParts = key.slice(this.prefix.length).toLowerCase().split('_');
Eif (pathParts.length >= 2) {
const configPath = pathParts.join('.');
// Skip if already handled by explicit mapping
Eif (!this.overrides.has(configPath)) {
// Auto-detect type
const transformedValue = this.autoTransform(value);
this.overrides.set(configPath, {
configPath,
value: transformedValue,
envVar: key,
});
}
}
}
}
}
/**
* Get all current overrides
*/
getOverrides(): EnvOverride[] {
return Array.from(this.overrides.values());
}
/**
* Get override for a specific config path
*/
getOverride(configPath: string): EnvOverride | undefined {
return this.overrides.get(configPath);
}
/**
* Check if a config path has an env override
*/
hasOverride(configPath: string): boolean {
return this.overrides.has(configPath);
}
/**
* Get the override value for a config path
*/
getValue(configPath: string): unknown | undefined {
return this.overrides.get(configPath)?.value;
}
/**
* Apply overrides to a config object
* Modifies the object in place and returns paths that were overridden
*/
applyOverrides(config: Record<string, unknown>): string[] {
const appliedPaths: string[] = [];
for (const override of this.overrides.values()) {
this.setNestedValue(config, override.configPath, override.value);
appliedPaths.push(override.configPath);
}
return appliedPaths;
}
/**
* Transform a string value based on the specified type
*/
private transformValue(
value: string,
transform: 'string' | 'number' | 'boolean' | 'json'
): unknown {
switch (transform) {
case 'string':
return value;
case 'number':
const num = Number(value);
return isNaN(num) ? value : num;
case 'boolean':
return value.toLowerCase() === 'true' || value === '1';
case 'json':
try {
return JSON.parse(value);
} catch {
return value;
}
default:
return value;
}
}
/**
* Auto-detect and transform a value
*/
private autoTransform(value: string): unknown {
// Check for boolean
Iif (value.toLowerCase() === 'true' || value.toLowerCase() === 'false') {
return value.toLowerCase() === 'true';
}
// Check for number
const num = Number(value);
Iif (!isNaN(num) && value.trim() !== '') {
return num;
}
// Check for JSON
Iif ((value.startsWith('{') && value.endsWith('}')) ||
(value.startsWith('[') && value.endsWith(']'))) {
try {
return JSON.parse(value);
} catch {
// Not valid JSON, return as string
}
}
return value;
}
/**
* Set a nested value in an object using dot notation
*/
private setNestedValue(
obj: Record<string, unknown>,
path: string,
value: unknown
): void {
const parts = path.split('.');
let current: Record<string, unknown> = obj;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!(part in current) || typeof current[part] !== 'object') {
current[part] = {};
}
current = current[part] as Record<string, unknown>;
}
current[parts[parts.length - 1]] = value;
}
}
|