UNPKG

kawkab-frontend

Version:

Kawkab frontend is a frontend library for the Kawkab framework

40 lines (39 loc) 1.2 kB
let envValues = {}; export function setEnv(values) { envValues = values; } export function getEnvValue(key) { return envValues[key]; } export const env = { get(key, fallback) { const fullKey = 'KAWKAB_' + key.trim().replace(/[\s\-]+/g, '_').toUpperCase(); const value = getEnvValue(fullKey); if (value == null) { if (fallback !== undefined) return fallback; throw new Error(`Missing environment variable: ${fullKey}`); } return value; }, number(key, fallback) { const val = this.get(key, fallback?.toString()); const num = Number(val); if (isNaN(num)) throw new Error(`Invalid number for ${key}`); return num; }, boolean(key, fallback) { const val = this.get(key, fallback ? 'true' : 'false'); return ['1', 'true', 'yes', 'on'].includes(val.toLowerCase()); }, json(key, fallback) { const val = this.get(key, fallback ? JSON.stringify(fallback) : undefined); try { return JSON.parse(val); } catch { throw new Error(`Invalid JSON in env: ${key}`); } } };