Skip to content

Commit

Permalink
feat: allow overriding runtime config with NITRO_ or an alternative…
Browse files Browse the repository at this point in the history
… speficied with `NITRO_ENV_PREFIX_ALT`
  • Loading branch information
pi0 committed Apr 6, 2022
1 parent 8d77b3d commit a27d529
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
}
Expand Down

0 comments on commit a27d529

Please sign in to comment.