diff --git a/src/runtime/config.ts b/src/runtime/config.ts index aef219b23b..7012a202ae 100644 --- a/src/runtime/config.ts +++ b/src/runtime/config.ts @@ -4,16 +4,21 @@ import { snakeCase } from 'scule' // Bundled runtime config (injected by nitro) const _runtimeConfig = process.env.RUNTIME_CONFIG as any +const ENV_PREFIX = 'NITRO_' +const ENV_PREFIX_ALT = _runtimeConfig.NITRO_ENV_PREFIX_ALT || process.env.NITRO_ENV_PREFIX_ALT || '_' + // Allow override from process.env and deserialize -for (const key in _runtimeConfig) { - // baseURL can be overridden by BASE_URL +const getEnv = (key) => { const envKey = snakeCase(key).toUpperCase() - _runtimeConfig[key] = destr(process.env[envKey] || _runtimeConfig[key]) + return destr(process.env[ENV_PREFIX + envKey] ?? (ENV_PREFIX_ALT && process.env[ENV_PREFIX_ALT + envKey])) +} + +for (const key in _runtimeConfig) { + _runtimeConfig[key] = getEnv(key) ?? _runtimeConfig[key] if (_runtimeConfig[key] && typeof _runtimeConfig[key] === 'object') { for (const subkey in _runtimeConfig[key]) { // key: { subKey } can be overridden by KEY_SUB_KEY` - const envKeyName = `${envKey}_${snakeCase(subkey).toUpperCase()}` - _runtimeConfig[key][subkey] = destr(process.env[envKeyName]) || _runtimeConfig[key][subkey] + _runtimeConfig[key][subkey] = getEnv(`${key}_${subkey}`) ?? _runtimeConfig[key][subkey] } } }