Skip to content

Commit

Permalink
fix: manually merge runtime config with env (#143)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <pyapar@gmail.com>
  • Loading branch information
danielroe and pi0 authored Apr 22, 2022
1 parent 2be8aaf commit 1be5ac2
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/runtime/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import destr from 'destr'
import { snakeCase } from 'scule'
import { createDefu } from 'defu'

// Bundled runtime config (injected by nitro)
const _runtimeConfig = process.env.RUNTIME_CONFIG as any
Expand All @@ -13,18 +12,27 @@ const getEnv = (key: string) => {
const envKey = snakeCase(key).toUpperCase()
return destr(process.env[ENV_PREFIX + envKey] ?? process.env[ENV_PREFIX_ALT + envKey])
}

const mergeWithEnvVariables = createDefu((obj: Record<string, any>, key: string, _value, namespace) => {
// key: { subKey } can be overridden by KEY_SUB_KEY`
const override = getEnv(namespace ? `${namespace}.${key}` : key)
if (override !== undefined) {
obj[key] = override
return true
function isObject (input: unknown) {
return typeof input === 'object' && !Array.isArray(input)
}
function overrideConfig (obj: object, parentKey: string = '') {
for (const key in obj) {
const subKey = parentKey ? `${parentKey}_${key}` : key
const envValue = getEnv(subKey)
if (isObject(obj[key])) {
if (isObject(envValue)) {
obj[key] = { ...obj[key], ...envValue }
}
overrideConfig(obj[key], subKey)
} else {
obj[key] = envValue ?? obj[key]
}
}
})
}
overrideConfig(_runtimeConfig)

// Named exports
const config = deepFreeze(mergeWithEnvVariables(_runtimeConfig, _runtimeConfig))
const config = deepFreeze(_runtimeConfig)
export const useRuntimeConfig = () => config
export default config

Expand Down

0 comments on commit 1be5ac2

Please sign in to comment.